Peakdet [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local maxima and minima ("peaks") in the vector V. MAXTAB and MINTAB consists of two columns. Column 1 contains indices in V, and column 2 the found values. With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices in MAXTAB and MINTAB are replaced with the corresponding X-values. A point is considered a maximum peak if it has the maximal value, and was preceded (to the left) by a value lower by DELTA.
0001 function [ maxtab,mintab ] = peakdet( v, delta,x ) 0002 %Peakdet 0003 % [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local 0004 % maxima and minima ("peaks") in the vector V. 0005 % MAXTAB and MINTAB consists of two columns. Column 1 0006 % contains indices in V, and column 2 the found values. 0007 % 0008 % With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices 0009 % in MAXTAB and MINTAB are replaced with the corresponding 0010 % X-values. 0011 % 0012 % A point is considered a maximum peak if it has the maximal 0013 % value, and was preceded (to the left) by a value lower by 0014 % DELTA. 0015 0016 % Eli Billauer, 3.4.05 (Explicitly not copyrighted). 0017 % This function is released to the public domain; Any use is allowe 0018 0019 maxtab = []; 0020 mintab = []; 0021 0022 v = v(:); % Just in case this wasn't a proper vector 0023 0024 if nargin < 3 0025 x = (1:length(v))'; 0026 else 0027 x = x(:); 0028 if length(v)~= length(x) 0029 error('Input vectors v and x must have same length'); 0030 end 0031 end 0032 0033 if (length(delta(:)))>1 0034 error('Input argument DELTA must be a scalar'); 0035 end 0036 0037 if delta <= 0 0038 error('Input argument DELTA must be positive'); 0039 end 0040 0041 mn = Inf; mx = -Inf; 0042 mnpos = NaN; mxpos = NaN; 0043 0044 lookformax = 1; 0045 0046 for i=1:length(v) 0047 this = v(i); 0048 if this > mx, mx = this; mxpos = x(i); end 0049 if this < mn, mn = this; mnpos = x(i); end 0050 0051 if lookformax 0052 if this < mx-delta 0053 maxtab = [maxtab ; mxpos mx]; 0054 mn = this; mnpos = x(i); 0055 lookformax = 0; 0056 end 0057 else 0058 if this > mn+delta 0059 mintab = [mintab ; mnpos mn]; 0060 mx = this; mxpos = x(i); 0061 lookformax = 1; 0062 end 0063 end 0064 end