function P=findpeaksLSS(x,y,SlopeThreshold,AmpThreshold,smoothwidth,peakgroup,smoothtype) % function P=findpeaks(x,y,SlopeThreshold,AmpThreshold,smoothwidth,peakgroup,smoothtype) % Function to locate the positive peaks in a noisy x-y time series data % set. Detects peaks by looking for downward zero-crossings in the first % derivative that exceed SlopeThreshold. Returns list (P) containing peak % number and position, height, width, area and start and stop positions of % each peak, assuming a Lorentzian peak shape. Arguments "slopeThreshold", % "ampThreshold" and "smoothwidth" control peak sensitivity. Higher values % will neglect smaller features. "Smoothwidth" is the width of the smooth % applied before peak detection; larger values ignore narrow peaks. If % smoothwidth=0, no smoothing is performed. "Peakgroup" is the number % points around the top part of the peak that are taken for measurement. If % Peakgroup=0 the local maximum is takes as the peak height and position. % The argument "smoothtype" determines the smooth algorithm: % If smoothtype=1, rectangular (sliding-average or boxcar) If % smoothtype=2, triangular (2 passes of sliding-average) If smoothtype=3, % pseudo-Gaussian (3 passes of sliding-average) % Skip peaks if peak measurement results in NaN values % See http://terpconnect.umd.edu/~toh/spectrum/Smoothing.html and % http://terpconnect.umd.edu/~toh/spectrum/PeakFindingandMeasurement.htm % T. C. O'Haver, Version 1.1, Last revised December, 2013 % % Example: Three noisy Lorentzian peaks at x=20,50,80; % all heights=1.0; all widths=5. % x=1:.2:100; % y=lorentzian(x,20,5)+lorentzian(x,50,5)+lorentzian(x,80,5)+.1.*randn(size(x)); % findpeaksLSS(x,y,0.0004,0.3,17,21,3) % Peak Position Height Width Area Start End % 1 19.755 1.0217 4.5289 7.2648 -2.7757 42.287 % 2 50.205 0.98515 4.6306 7.1621 27.168 73.242 % 3 80.019 1.0427 4.2611 6.9754 58.82 101.22 % Same signal without noise: % 1 20.003 1.0084 5.0342 7.9698 -5.0422 45.048 % 2 50 1.0135 5.0513 8.0374 24.871 75.13 % 3 79.997 1.0085 5.031 7.9656 54.968 105.0 % % Related functions: % findvalleys.m, findpeaksL.m, findpeaksb.m, findpeaksplot.m, peakstats.m, % findpeaksnr.m, findpeaksGSS.m, findpeaks.m, findpeaksfit.m. if nargin~=7;smoothtype=1;end % smoothtype=1 if not specified in argument if smoothtype>3;smoothtype=3;end if smoothtype<1;smoothtype=1;end smoothwidth=round(smoothwidth); peakgroup=round(peakgroup); if smoothwidth>1, d=fastsmooth(deriv(y),smoothwidth,smoothtype); else d=y; end n=round(peakgroup/2+1); P=[0 0 0 0 0 0 0]; vectorlength=length(y); peak=1; AmpTest=AmpThreshold; for j=2*round(smoothwidth/2)-1:length(y)-smoothwidth, if sign(d(j)) > sign (d(j+1)), % Detects zero-crossing if d(j)-d(j+1) > SlopeThreshold, % if slope of derivative is larger than SlopeThreshold if y(j) > AmpTest, % if height of peak is larger than AmpThreshold xx=zeros(size(peakgroup));yy=zeros(size(peakgroup)); for k=1:peakgroup, % Create sub-group of points near peak groupindex=j+k-n+1; if groupindex<1, groupindex=1;end if groupindex>vectorlength, groupindex=vectorlength;end xx(k)=x(groupindex);yy(k)=y(groupindex); end if peakgroup>3, z=rmnan(ones(size(xx))./yy); coef=polyfit(xx,z,2); PeakY=4*coef(1)./((4*coef(1)*coef(3))-coef(2)^2); PeakX=-coef(2)/(2*coef(1)); MeasuredWidth=sqrt(((4*coef(1)*coef(3))-coef(2)^2)./coef(1))./sqrt(coef(1)); % if the peak is too narrow for least-squares technique to work % well, just use the max value of y in the sub-group of points near peak. else PeakY=max(yy); pindex=val2ind(yy,PeakY); PeakX=xx(pindex(1)); MeasuredWidth=0; end % Construct matrix P. One row for each peak % detected, containing the peak number, peak % position (x-value) and peak height (y-value). % If peak measurements fails and results in NaN, skip this % peak if isnan(PeakX) || isnan(PeakY) || PeakY> v=[1 2 3 4 Inf 6 7 Inf 9]; % >> rmnan(v) % ans = % 1 2 3 4 4 6 7 7 9 la=length(a); if isnan(a(1)) || isinf(a(1)),a(1)=0;end for point=1:la, if isnan(a(point)) || isinf(a(point)), a(point)=a(point-1); end end