% Three peaks, noise test % Demonstration script for peakfit.m, with built-in signal generator. % You can change any of the initial values in lines 12-28. % T. C. O'Haver (toh@umd.edu). Version 4: May, 2009. clear close format long g format compact warning off all % Initial settings of users-adjustable parameters n=300; % maximum x-value increment=1; % Difference between adjacent x values x=[1:increment:n]; % Creates x-vector amp=[1 2 3]; % Amplitudes of the peaks (one entry for each peak) pos=[100 130 200]; % Positions of the peaks (one entry for each peak) wid=[30 40 50]; % Widths of the peaks (one entry for each peak) noise=.01; % Amplitude of random noise added to signal NumRepeats=2; % Number of repeat samples with independent noise NumTrials=1; % Number of trail fits per sample PeakShape=1; % Gaussian=1, Lorentzian=2, Logistic=3, Pearson=4 % "extra" determines the shape of the Pearson function. When extra is % 1.0, the shape is Lorentzian. When extra is large (>20), the shape % approaches Gaussian. At small values (<1), the shape is a pointy "cusp". extra=2; center=160; % x-value in center of fitted range window=260; % range of x-values in fitted range startvector=[98 28 129 41 190 49]; % First guess vector [pos1 wid1 pos2 wid2 ...] Must be 2 for each peak % Generate simulated signal peaks % A = matrix containing one of the unit-amplidude functions in each of its rows clear A A=zeros(length(pos),length(x)); for k=1:length(pos), switch PeakShape case 1 A(k,:)=exp(-((x-pos(k))./(0.6006.*wid(k))) .^2); % Gaussian function case 2 A(k,:)=ones(size(x))./(1+((x-pos(k))./(0.5.*wid(k))).^2); % Lorentzian function case 3 L = exp(-((x-pos(k))/(.477.*wid(k))) .^2); A(k,:)= (2.*L)./(1+L); case 4 % pearson function (shape determined by value of extra in line 21 A(k,:)=ones(size(x))./(1+((x-pos(k))./((0.5.^(2/extra)).*4.62.*wid(k))).^2).^extra; end % switch area(k)=amp(k).*trapz(x,A(k,:)); % Computes peak areas via trapaziod method end % for TrueY=amp*A; % Multiplies each row by the corresponding amplitude and adds them up % To test the autozero baseline subtraction, un-comment next line. % TrueY=TrueY+ones(size(x))./(1+((x+100)./(300)).^2); % Adds sloping curved baseline % Arrange the peak parameters of the simulated signal into a matrix ColumnLabels=' Peak# Position Height Width Area'; NumPeaks=length(pos); for m=1:NumPeaks, if m==1, ActualParameters=[m pos(m) amp(m) wid(m) area(m) ];, else ActualParameters=[ActualParameters; [m pos(m) amp(m) wid(m) area(m) ]]; end end % Generate and fit NumRepeats separate samples of the simulated signal with noise. AllFitResults=zeros(NumPeaks,5,NumRepeats); AllFittingErrors=zeros(1,NumRepeats); AllPercentParameterErrors=zeros(NumPeaks,5,NumRepeats); for repeat=1:NumRepeats, % Add random noise to simulated signal y = TrueY + noise .* randn(size(x)); % Execute the peakfit script [FitResults,LowestError]=peakfit([x' y'],center,window,NumPeaks,PeakShape,extra,NumTrials,startvector); AllFitResults(:,:,repeat)=FitResults; AllFittingErrors(repeat)=LowestError; AllPercentParameterErrors(:,:,repeat)=100.*(FitResults-ActualParameters)./ActualParameters; end % Calculate the mean and relative standard deviaiton of parameters for peak=1:NumPeaks, MeanParameters(peak,:)=mean(squeeze(AllFitResults(peak,:,:))'); RSDparameters(peak,:)=100.*std(squeeze(AllFitResults(peak,:,:))')./mean(squeeze(AllFitResults(peak,:,:))'); end disp('-------------------------------------------------------') disp(['Noise amplitude: ' num2str(noise)]) disp(['Number of repeats samples: ' num2str(NumRepeats)]) disp(['Number of trial fits per sample: ' num2str(NumTrials)]) disp('Peak# Position Height Width Area'); disp('Actual parameters');disp(num2str(ActualParameters)) disp('Average measured parameters');disp(num2str(MeanParameters)) RSDparameters(:,1)=ActualParameters(:,1); disp('Percent RSD of measured parameters');disp(num2str(RSDparameters)) meanRSD=mean(RSDparameters); meanRSD=mean(meanRSD(2:4)); disp(['Average %RSD of measured parameters: ' num2str(meanRSD)]) disp(['Average percent fitting error: ' num2str(mean(AllFittingErrors))]) PercentParameterErrors=100.*(MeanParameters-ActualParameters)./ActualParameters; PercentParameterErrors(:,1)=ActualParameters(:,1); disp('Percent error of measured parameters');disp(num2str(PercentParameterErrors)) AverageParameterErrors=mean(abs(PercentParameterErrors(:,2:5))) disp(['Overall Average Parameter Error: ' num2str(mean(AverageParameterErrors)) ])