Find peaks for a given 2D curve.
Versione 1.0.3 (32,4 KB) da
Peter Seibold
The code is very simple and needs only two lines of code.
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(:)];
Cita come
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. Recuperato .
Compatibilità della release di MATLAB
Creato con
R2020a
Compatibile con R2016a e release successive
Compatibilità della piattaforma
Windows macOS LinuxTag
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Scopri Live Editor
Crea script con codice, output e testo formattato in un unico documento eseguibile.