peakfinder(x0, sel, thresh, extrema, includeEndpoints, interpolate)

Quickly finds local maxima (peaks) or minima (valleys) in a noisy signal.
Updated 2 Oct 2016

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This function quickly finds local peaks or valleys (local extrema) in a noisy vector using a user defined magnitude threshold to determine if each peak is significantly larger (or smaller) than the data around it. The problem with the strictly derivative based peak finding algorithms is that if the signal is noisy many spurious peaks are found. However, more complex methods often take much longer for large data sets, require a large amount of user interaction, and still give highly variable results.
This function attempts to use the alternating nature of the derivatives along with the user defined threshold to identify local maxima or minima in a vector quickly and robustly. The function is able to correctly identify the major peaks on a 1.5 million data point noisy sum of sinusoids in under a second as is shown in the example in the code comments.
Please don't hesitate to comment or contact me if you have suggestions about improvements that could be made to this function.

Cite As

Nathanael Yoder (2024). peakfinder(x0, sel, thresh, extrema, includeEndpoints, interpolate) (, MATLAB Central File Exchange. Retrieved .

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

Inspired: voigt line shape fit

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

Updated checking for the input and output arguments to account of Matlab's new syntax (thanks @arnold) and removed the use of boolean because Matlab deprecated it (thanks @Carl Witthoft)

Fix a couple small bugs (Tanks Binu!)

Added ability to quickly estimate the location and magnitude of the extrema between data points for regularly spaced data using quadratic interpolation. The function continues to work for irregularly spaced data. Default is no interpolation.

Fix bug related to a missing equality comparison (thanks Nina Merkle!) and fix inconsistency when the endpoints are not treated as extrema (thanks Solomon Grant!).

Added an optional boolean to exclude the endpoints since, as pointed out by Peter Cavanagh, most definitions of local extrema do not include them. By default the endpoints are included to maintain backward compatibility.

Removed redundancy (thanks Tim) and added thresholding option (thanks Femi).

Updated the error checking on the threshold level.

Added support for monotone increase/decreasing functions and empty inputs. Thanks again to Andres for the debugging help.

Fixed problems with repeated initial values, repeated final values, and other directional issues. Thanks to Andres for his help finding and debugging these problems.

Fixed example and error checking code. Thanks to Jiro Doke for catching my mistakes.

Updated algorithm to make it slightly faster.

Updated code to make it slightly faster.