function P=findpeaksG(x,y,SlopeThreshold,AmpThreshold,smoothwidth,peakgroup,smoothtype) % function P=findpeaksG(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, and area of each peak. 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) % See http://terpconnect.umd.edu/~toh/spectrum/Smoothing.html and % http://terpconnect.umd.edu/~toh/spectrum/PeakFindingandMeasurement.htm % (c) T.C. O'Haver, 1995, 2014. Version 6, Last revised May, 2016 % Simplified main loop % % Examples: % findpeaksG(0:.01:2,humps(0:.01:2),0,-1,5,5) % x=[0:.01:50];y=(1+cos(x)).^2;P=findpeaksG(x,y,0,-1,5,5) % x=[0:.01:5]';y=x.*sin(x.^2).^2;findpeaksG(x,y,0,-1,5,5) % x=[-10:.1:10];y=exp(-(x).^2);findpeaksG(x,y,0.005,0.3,3,5,3); % % Find, measure, and plot noisy peaks with unknown positions % x=-50:.2:50; % y=exp(-(x).^2)+exp(-(x+50*rand()).^2)+.02.*randn(size(x)); % plot(x,y,'m.') % P=findpeaksG(x,y,0.001,0.2,5,5,3); % text(P(:,2),P(:,3),num2str(P(:,1))) % disp(' peak # Position Height') % disp(P) % % Related functions: % findvalleys.m, findpeaksL.m, findpeaksb.m, findpeaksb3.m, % findpeaksplot.m, peakstats.m, findpeaksnr.m, findpeaksGSS.m, % findpeaksLSS.m, findpeaksfit.m, findsteps.m, findsquarepulse.m, idpeaks.m % Copyright (c) 2013, 2014 Thomas C. O'Haver % % Permission is hereby granted, free of charge, to any person obtaining a copy % of this software and associated documentation files (the "Software"), to deal % in the Software without restriction, including without limitation the rights % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell % copies of the Software, and to permit persons to whom the Software is % furnished to do so, subject to the following conditions: % % The above copyright notice and this permission notice shall be included in % all copies or substantial portions of the Software. % % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR % IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, % FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE % AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN % THE SOFTWARE. % 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 if smoothwidth<1;smoothwidth=1;end smoothwidth=round(smoothwidth); peakgroup=round(peakgroup); if smoothwidth>1, d=fastsmooth(deriv(y),smoothwidth,smoothtype); else d=deriv(y); end n=round(peakgroup/2+1); P=[0 0 0 0 0]; vectorlength=length(y); peak=1; for j=2*round(smoothwidth/2)-1:length(y)-smoothwidth-1, 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) > AmpThreshold, % 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+2; if groupindex<1, groupindex=1;end if groupindex>vectorlength, groupindex=vectorlength;end xx(k)=x(groupindex);yy(k)=y(groupindex); end if peakgroup>2, [Height, Position, Width]=gaussfit(xx,yy); PeakX=real(Position); % Compute peak position and height of fitted parabola PeakY=real(Height); MeasuredWidth=real(Width); % 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 measurement fails and % results in NaN, or if the measured peak height is less % than AmpThreshold, 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 function [Height, Position, Width]=gaussfit(x,y) % Converts y-axis to a log scale, fits a parabola % (quadratic) to the (x,ln(y)) data, then calculates % the position, width, and height of the % Gaussian from the three coefficients of the % quadratic fit. This is accurate only if the data have % no baseline offset (that is, trends to zero far off the % peak) and if there are no zeros or negative values in y. % % Example 1: Simplest Gaussian data set % [Height, Position, Width]=gaussfit([1 2 3],[1 2 1]) % returns Height = 2, Position = 2, Width = 2 % % Example 2: best fit to synthetic noisy Gaussian % x=50:150;y=100.*gaussian(x,100,100)+10.*randn(size(x)); % [Height,Position,Width]=gaussfit(x,y) % returns [Height,Position,Width] clustered around 100,100,100. % % Example 3: plots data set as points and best-fit Gaussian as line % x=[1 2 3 4 5];y=[1 2 2.5 2 1]; % [Height,Position,Width]=gaussfit(x,y); % plot(x,y,'o',linspace(0,8),Height.*gaussian(linspace(0,8),Position,Width)) % Copyright (c) 2012, Thomas C. O'Haver % To prevent problems from taking the log of zero or negative values, % make the lowest value of y equal to 1% of the maximum value. maxy=max(y); for p=1:length(y), if y(p)<(maxy/100),y(p)=maxy/100;end end % for p=1:length(y), logyyy=log(abs(y)); [coef,S,MU]=polyfit(x,logyyy,2); c1=coef(3);c2=coef(2);c3=coef(1); % Compute peak position and height or fitted parabola Position=-((MU(2).*c2/(2*c3))-MU(1)); Height=exp(c1-c3*(c2/(2*c3))^2); Width=norm(MU(2).*2.35703/(sqrt(2)*sqrt(-1*c3)));