% 
% calculate isotopic distributions of molecules using the FFT
%
% (c) Magnus Palmblad, 1999
%

MAX_ELEMENTS=5; MAX_MASS=2^13;      % fast radix-2 fast-Fourier transform algorithm is used

M=[378 234 65 75 6];                % empirical formula, e.g. bovine insulin

A=zeros(MAX_ELEMENTS,MAX_MASS);                 % isotopic abundancies stored in A
A(1,2:3)=[0.9998443 0.0001557];                 % H 
A(2,13:14)=[0.98889 0.01111];                   % C
A(3,15:16)=[0.99634 0.00366];                   % N
A(4,17:19)=[0.997628 0.000372 0.002000];        % O
A(5,33:37)=[0.95018 0.00750 0.04215 0 0.00017]; % S (extend to other elements as needed)

tA=fft(A,[],2);                     % FFT along each element's isotopic distribution

ptA=ones(1,MAX_MASS);
for i=1:MAX_ELEMENTS,
  ptA=ptA.*(tA(i,:).^M(i));         % multiply transforms (elementwise)
end

riptA=real(ifft(ptA));              % inverse FFT to get convolutions

id=zeros(1,MAX_MASS);
id(1:MAX_MASS-1)=riptA(2:MAX_MASS); % shift to real mass

bar(id);                            % bar plot of isotopic distribution