100% found this document useful (1 vote)
460 views

Peakdet - Peak Detection Using MATLAB (Non-Derivative Local Extremum, Maximum, Minimum)

The document describes an algorithm for detecting peaks and valleys in noisy data without using derivatives. It explains that the algorithm looks for the highest or lowest point surrounded by lower points within a given threshold, rather than finding mathematical extrema. An example using MATLAB demonstrates detecting peaks and valleys in noisy sinusoidal data.

Uploaded by

rnjlmv83
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
460 views

Peakdet - Peak Detection Using MATLAB (Non-Derivative Local Extremum, Maximum, Minimum)

The document describes an algorithm for detecting peaks and valleys in noisy data without using derivatives. It explains that the algorithm looks for the highest or lowest point surrounded by lower points within a given threshold, rather than finding mathematical extrema. An example using MATLAB demonstrates detecting peaks and valleys in noisy sinusoidal data.

Uploaded by

rnjlmv83
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

09/10/2015

peakdet:PeakdetectionusingMATLAB(nonderivativelocalextremum,maximum,minimum)

Home
CV
CV
(hebrew)

peakdet:PeakdetectionusingMATLAB
Here'saproblemIencounterinseveralfields:Findthelocalmaximaand
minimainsomenoisysignal,whichtypicallylookslikethefollowinggraph:

FPGA
Editor
Free
software
Lectures
Perlilog
Eobj
frandom
cdepend
hitec
(LaTeX)
easyspec
peakdet
FIFOtricks
CDCE906
Optical
simulator
HTML
highlighting
Hobbies
Techblog

Thelocalmaximaandminimaareplottedasredandgreenstarsonthe
graph.Totheeyeit'ssoobviouswheretheyare,butmakingacomputer
findthemcanturnouttricky.
Let'sstartwithwhatnottodo:Usingthewellknownzeroderivate
method.Duetothenoise,whichisalwaysthereinreallifesignals,
accidentalzerocrossingsofthefirstderivateoccur,yieldingfalse
detections.Thetypicalsolutionistosmooththecurvewithsomelowpass
filter,usuallykillingtheoriginalsignalatthesametime.Theresultis
usuallythatthealgorithmgoeshorriblywrongwhereit'ssoobvioustothe
eye.
Inmanycases,wedon'treallycareaboutmaximaandminimainthe
mathematicalsense.Wecanseethepeaksandvalleys,andwewantthe
computertofindthem.Thisiswhat"peakdet"does.
Thetrickhereistorealize,thatapeakisthehighestpointbetweem

http://www.billauer.co.il/peakdet.html

1/3

09/10/2015

peakdet:PeakdetectionusingMATLAB(nonderivativelocalextremum,maximum,minimum)

"valleys".Whatmakesapeakisthefactthattherearelowerpointsaround
it.Thisstrategyisadoptedby"peakdet":Lookforthehighestpoint,
aroundwhichtherearepointslowerbyXonbothsides.
Let'sseeanexample:First,let'screatethegraphshowninthefigure
above:
>>t=0:0.001:10;
>>x=0.3*sin(t)+sin(1.3*t)+0.9*sin(4.2*t)+0.02*randn(1,10001);
>>figure;plot(x);
Nowwe'llfindthepeaksandvalleys:(you'llneedtocopythe"peakdet"
functionfromthebottomofthispageandputitinyourworkingdirectory
oradirectoryintheMATLABsearchpath):
>>[maxtab,mintab]=peakdet(x,0.5);
>>holdon;plot(mintab(:,1),mintab(:,2),'g*');
>>plot(maxtab(:,1),maxtab(:,2),'r*');
Notethecalltopeakdet():Thefirstargumentisthevectortoexamine,
andthesecondisthepeakthreshold:Werequireadifferenceofatleast
0.5betweenapeakanditssurroundinginordertodeclareitasapeak.
Samegoeswithvalleys.
Thereturnedvectors"maxtab"and"mintab"containthepeakandvalley
points,asevidentbytheirplots(notethecolors).
Thevector'sXaxisvaluescanbepassedasathirdargument(thanksto
SvenBillietforhiscontributiononthis),inwhichcasepeakdet()returns
thesevaluesinsteadofindices,asshowninthefollowingexample:
>>figure;plot(t,x);
>>[maxtab,mintab]=peakdet(x,0.5,t);
Andfromherewecontinuelikebefore,butnotethattheXaxisrepresents
"t"andnotindices.
>>holdon;plot(mintab(:,1),mintab(:,2),'g*');
>>plot(maxtab(:,1),maxtab(:,2),'r*');
Asfortheimplementationofthisfunction:Theworkisdonewithafor
loop,whichisconsideredlousypracticeinMATLAB.SinceI'venever
neededthisfunctionforanythingelsethanprettyshortvectors(<100000
points),Ialsoneverbotheredtotryspeedingitup.CompilingtoMEXisa
directsolution.I'mnotsureifit'spossibletovectorizethisalgorithmin
MATLAB.I'llbegladtohearsuggestions.
Afinalnote:IfyouhappentopreferPython,youcouldtrythis(someone
hasbeenkindenoughtoconvertthisfunction).Therearealsoaversionin
CbyHongXuandaversioninFORTRAN90byBrianMcNoldy.Ihaven't
verifiedanyofthese.
Andhereisthefunction.Copyandsaveitas'peakdet.m'.It'sreleasedto
thepublicdomain:
function[maxtab,mintab]=peakdet(v,delta,x)
%PEAKDETDetectpeaksinavector
%[MAXTAB,MINTAB]=PEAKDET(V,DELTA)findsthelocal
%maximaandminima("peaks")inthevectorV.
%MAXTABandMINTABconsistsoftwocolumns.Column1
%containsindicesinV,andcolumn2thefoundvalues.
%
%With[MAXTAB,MINTAB]=PEAKDET(V,DELTA,X)theindices
%inMAXTABandMINTABarereplacedwiththecorresponding
http://www.billauer.co.il/peakdet.html

2/3

09/10/2015

peakdet:PeakdetectionusingMATLAB(nonderivativelocalextremum,maximum,minimum)

%Xvalues.
%
%Apointisconsideredamaximumpeakifithasthemaximal
%value,andwaspreceded(totheleft)byavaluelowerby
%DELTA.
%EliBillauer,3.4.05(Explicitlynotcopyrighted).
%Thisfunctionisreleasedtothepublicdomain;Anyuseisallowed.
maxtab=[];
mintab=[];
v=v(:);%Justincasethiswasn'tapropervector
ifnargin<3
x=(1:length(v))';
else
x=x(:);
iflength(v)~=length(x)
error('Inputvectorsvandxmusthavesamelength');
end
end

if(length(delta(:)))>1
error('InputargumentDELTAmustbeascalar');
end
ifdelta<=0
error('InputargumentDELTAmustbepositive');
end
mn=Inf;mx=Inf;
mnpos=NaN;mxpos=NaN;
lookformax=1;
fori=1:length(v)
this=v(i);
ifthis>mx,mx=this;mxpos=x(i);end
ifthis<mn,mn=this;mnpos=x(i);end

iflookformax
ifthis<mxdelta
maxtab=[maxtab;mxposmx];
mn=this;mnpos=x(i);
lookformax=0;
end
else
ifthis>mn+delta
mintab=[mintab;mnposmn];
mx=this;mxpos=x(i);
lookformax=1;
end
end
end
LastmodifiedonFriJul2021:16:582012.Email:
[email protected]

http://www.billauer.co.il/peakdet.html

3/3

You might also like