Find peaks (maxima and minima) of a function

This function detects the transition points (maxima and minima) in a function like y = f(x)
703 Downloads
Updated 28 Jan 2016

View License

function [nmax,maxAt,maxValues,nmin,minAt,minValues] = peak(y,x,threshold,stepsize)
This function detects the transition points (maxima and minima) of a function like y = f(x), where x is indpendent variable and y is dependent variable. If there is no clue about x, then use x = [], which is an empty vector, and if so, it automatically defines x = 1:length(y).

Optional input arguments: threshold is the thresholding applied to y, and stepsize is the step size for computation of slopes. The smaller the stepsize is, the more accurate result is. By default stepsize = abs(x(1)-x(2))/10.
Finally, it returns, nmax: the number of peaks in y; maxAt: the x values at which peaks occur; maxValues: the peak values of y; nmin: number of minima in y; minAt: the x values at which minima occur; and
minValues: the minima of y; (all after thresholding, if any).

Examples 1: no threshodling
x = -pi:pi/100:pi;
y = sin(x)+cos(x.^2);
[nmax,maxAt,maxValues,nmin,minAt,minValues] = peak(y,x)


Example 2: thresholding at 0
x = -pi:pi/100:pi;
y = sin(x)+cos(x.^2);
[nmax,maxAt,maxValues,nmin,minAt,minValues] = peak(y,x,0)

Cite As

Shoaibur Rahman (2024). Find peaks (maxima and minima) of a function (https://www.mathworks.com/matlabcentral/fileexchange/48818-find-peaks-maxima-and-minima-of-a-function), MATLAB Central File Exchange. Retrieved .

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

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.1.0.0

Change in function name as it was conflicting with a built-in function
Title made shorter
Change title

1.0.0.0