Find peaks (maxima and minima) of a function
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
Platform Compatibility
Windows macOS LinuxCategories
- Signal Processing > Signal Processing Toolbox > Measurements and Feature Extraction > Descriptive Statistics >
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.