Matlab三点法寻找一条曲线的峰和谷,并在曲线中标注
时间: 2024-03-18 07:45:13 浏览: 96
以下是Matlab代码实现:
```matlab
% 假设已有曲线数据x和y
% 设置窗口大小
n = 3;
% 寻找峰值
peaks = [];
for i = 2:length(y)-1
if y(i)>y(i-1) && y(i)>y(i+1)
peaks = [peaks i];
end
end
% 寻找谷值
valleys = [];
for i = 2:length(y)-1
if y(i)<y(i-1) && y(i)<y(i+1)
valleys = [valleys i];
end
end
% 绘制曲线和峰谷点
figure;
plot(x,y);
hold on;
scatter(x(peaks),y(peaks),'r','filled');
scatter(x(valleys),y(valleys),'g','filled');
% 添加标注
for i = 1:length(peaks)
text(x(peaks(i)),y(peaks(i))+0.1,'peak');
end
for i = 1:length(valleys)
text(x(valleys(i)),y(valleys(i))-0.1,'valley');
end
```
运行以上代码,将得到一个包含峰谷点的曲线图,并在每个峰谷点上方标注“peak”,在每个谷点下方标注“valley”,如下图所示:
![三点法寻找一条曲线的峰和谷,并在曲线中标注](https://img-blog.csdn.net/20180521193317520?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UxMjY0MzQyODQ=//font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75)
阅读全文