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

Quickly finds local maxima (peaks) or minima (valleys) in a noisy signal.

37.3K Downloads

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 (2023). peakfinder(x0, sel, thresh, extrema, includeEndpoints, interpolate) (https://www.mathworks.com/matlabcentral/fileexchange/25500-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
Acknowledgements

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
2.0.2.0

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)

2.0.1.0

Fix a couple small bugs (Tanks Binu!)

2.0.0.0

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.

1.9.0.0

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!).

1.8.0.0

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.

1.7.0.0

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

1.6.0.0

Updated the error checking on the threshold level.

1.5.0.0

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

1.4.0.0

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

1.3.0.0

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

1.2.0.0

Updated algorithm to make it slightly faster.

1.1.0.0

Updated code to make it slightly faster.

1.0.0.0