File Exchange

image thumbnail


version (150 KB) by Christopher Hummersone
Find local peaks or troughs in a vector


Updated 05 May 2018

From GitHub

View Version History

View license on GitHub

NOTE: this function is now available from the IoSR Matlab Toolbox as iosr.dsp.localpeaks.
LOCALPEAKS Find local peaks and troughs in a vector


peaks = localpeaks(X)
peaks = localpeaks(X,MODE)


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.

Cite As

Christopher Hummersone (2021). Localpeaks (, GitHub. Retrieved .

Comments and Ratings (10)

Benjamin Paul




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.

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.

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.

michael scheinfeild

or rather,

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

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


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

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Peaks picking

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!