function y=model(beta,x)
%------------------- LineShapeKin MODEL -----------------------------
%-- version 3.1 
%
% 3/17/08
% Written by Evgenii Kovrigin, Medical College of Wisconsin
%
%  This function calculates spectral intensities for composite X data set at every frequency point.
%  This function is called for least squares fitting.
%
%  3/29/08 - only compute K matrix once for every spectrum
%                - pre-allocates spectra
%               - switcheable scaling by Scale Factor

global YES NO B_vector X_columns

[N,M]=size(x);   

spectra=zeros(N,1);

DataID=-2005; %- impossible index to recognize beginning of the series

for i=1:N
  
  current_DataID=x(i,X_columns.DataID); % Remember current data set
  
  %-- only compute K matrix once for a titration point (1D slice) and use for all
  %    frequencies within this 1D slice
  if DataID~=current_DataID
      %-- new dataset - compute K matrix
  
      [p, K, R2_vector, v_vector]=rate_matrix(beta,x(i,:));
      DataID=current_DataID;
  end

  current_frequency = x(i,X_columns.Xtotal); 

  spectra(i)= spectrum(current_frequency, K, R2_vector, v_vector, p) ;

end


if B_vector.fit_scale_factor==YES
    %-- scale entire array of spectral intensities by adjustable scaling factor
    y=spectra*beta(B_vector.scale_factor);
else
   y=spectra;
end
