Find peaks for a given 2D curve.

The code is very simple and needs only two lines of code.
53 Downloads
Updated 21 Jul 2022

View License

Full call:
PeakValues=FindPeaksSimple(x,y,PeakType,Interpolate)
INPUT:
1 x: x-values. Vector with numbers.
2 y: y-values. Vector with numbers.
Optional values:
You may omit all off them or the trailing value.
3 PeakType: -1: only minima
0: minima and maxima (default)
1: only maxima
4 Interpolate: true or 1: parabolic interpolation
false or 0: no interpolation (default)
If the curve is noisy, you might consider to smooth it beforehand,
OUTPUT:
PeakValues: Array with peak x-positions and peak y-values
Empty PeakValues for no peak at all.
Processing time on my PC is:
No interpolation: 40us overhead + 17ns per sample.
Is for 10^6 samples: 17 ms.
With interpolation: 40us overhead + 17ns per sample + 34ns per peak.
Is for 10^6 samples with 3.4*10^5 peaks=28 ms
If you process always in the same manner, you can delete many code lines.
E.g. you want only nearest maxima:
PeakValues=FindPeaksSimpleMaxima(x,y)
PeakIndx = find(diff(sign(diff(y)))<0)+1;
PeakValues=[x(PeakIndx)',y(PeakIndx)'];
Or you want only maxima interpolated:
PeakValues=FindPeaksSimpleMaximaInterpolated(x,y)
PeakIndx = find(diff(sign(diff(y)))<0)+1;
y1=y(PeakIndx-1);%Left of peak
y2=y(PeakIndx);%At peak
y3=y(PeakIndx+1);%Right of peak
PeakPos=(x(2)-x(1))*(y3-y1)./(4*y2-2*y1-2*y3)+x(PeakIndx);
PeakMinMax=y2-(y3-y1).^2./(8*(y1-2*y2+y3));
PeakValues=[PeakPos(:),PeakMinMax(:)];

Cite As

Peter Seibold (2024). Find peaks for a given 2D curve. (https://www.mathworks.com/matlabcentral/fileexchange/114145-find-peaks-for-a-given-2d-curve), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2020a
Compatible with R2016a and later releases
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.0.3

Minor speed improvement.
Some changes in demo.

1.0.2

Minor changes in demo.

1.0.1

Corrected description

1.0.0