Find peaks for a given 2D curve.

版本 1.0.3 (32.4 KB) 作者: Peter Seibold
The code is very simple and needs only two lines of code.
53.0 次下载
更新时间 2022/7/21

查看许可证

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(:)];

引用格式

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. 检索来源 .

MATLAB 版本兼容性
创建方式 R2020a
与 R2016a 及更高版本兼容
平台兼容性
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
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