Code covered by the BSD License  

Highlights from
Local Peaks

4.75

4.8 | 4 ratings Rate this file 14 Downloads (last 30 days) File Size: 1.43 KB File ID: #25355

Local Peaks

by Christopher Hummersone

 

18 Sep 2009 (Updated 01 Jun 2010)

Find local peaks or troughs in a vector

| Watch this File

File Information
Description

LOCALPEAKS Find local peaks and troughs in a vector
 
SYNTAX
 
    peaks = localpeaks(X)
    peaks = localpeaks(X,MODE)
 
 
DESCRIPTION
 
peaks = localpeaks(X) locates the local peaks in vector X.
 
peaks = localpeaks(X,MODE) locates local features specified by MODE. MODE can be set to 'peaks' (default), 'troughs' in order to identify local troughs or 'both' in order to identify both local peaks and troughs.

Acknowledgements
This submission has inspired the following:
Peaks picking
MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
25 Sep 2009 Herve

Brillant !
You could have added an extra output which gives the values of the local peaks or throughs.

01 Apr 2010 Christopher Hummersone

I didn't really see the point, if you want the value you can just do for example:
y = x(localpeaks(x));

28 Apr 2010 Christopher Hummersone

or rather,

y = zeros(size(x)); peaks = localpeaks(x);
y(peaks) = x(peaks);

25 May 2010 michael scheinfeild  
01 Jun 2010 Christos Saragiotis

Very good and an excellent example of terse programming.

I have two remarks though:
1. when selecting the 'both' option some peaks or troughs may be lost. E.g consider the vector [0, 5 -5, 5, 0]. Clearly -5 is a trough but localpeaks will not pick it.
2. if we consider again the vector [ 0 5 5 5 0], localpeaks will pick the first and last 5 as a peak but not the second one. I'm not sure if this was intentional but the way I see it it should either pick all of them or none.

01 Jun 2010 Christopher Hummersone

Thanks for your comment Christos, I hadn't noticed this behaviour (I had been using it with long signals). I have fixed the function to address your first remark, and the code will now correctly identify peaks and troughs in a vector like [0 5 -5 5 0] with the 'both' option. I have not addressed your second comment since, as you alluded to, this is a plateaux and not a true peak.

19 Apr 2011 Jeremy

I don't like not having a blank line after the last line of code. Just kidding, awesome job on this function. Very simple, intuitive, and fast.

Please login to add a comment or rating.
Updates
27 Apr 2010

added the 'both' option, updated help, acknowledged other submission

29 Apr 2010

Fixed a silly mistake, removed index output to bring the function more in line with MATLAB conventions (find(localpeaks(x)) would do the same), added an error trap

27 May 2010

Updated description

01 Jun 2010

Updated to address Christos Saragiotis' comment

Tag Activity for this File
Tag Applied By Date/Time
local Christopher Hummersone 18 Sep 2009 12:44:05
peaks Christopher Hummersone 18 Sep 2009 12:44:05
troughs Christopher Hummersone 18 Sep 2009 12:44:05
maxima Christopher Hummersone 18 Sep 2009 12:44:05
minima Christopher Hummersone 18 Sep 2009 12:44:05

Contact us at files@mathworks.com