data = load('out3.txt'); N = length(data(:,1)); b = fir1(15, 0.3, chebwin(16, 30)); data_smooth = filtfilt(b, 1, data(:,1)); [IndMin, IndMax] = findExtrema(data_smooth); figure; subplot(2,1,1); plot(data_smooth); hold on; plot(IndMin, data_smooth(IndMin), 'r^'); plot(IndMax, data_smooth(IndMax), 'k*'); box on; legend('曲线','波谷点','波峰点'); title('计算离散节点的波峰波谷信息', 'FontWeight', 'Bold'); subplot(2,1,2); plot(data(:,1), 'k'); hold on; plot(data_smooth, 'r'); box on; legend('原始数据','平滑数据'); title('原始数据和平滑数据', 'FontWeight', 'Bold'); function [IndMin, IndMax] = findExtrema(data_smooth) %获得局部最小值的位置 IndMin = find(diff(sign(diff(data_smooth))) > 0) + 1; %获得局部最大值的位置 IndMax = find(diff(sign(diff(data_smooth))) < 0) + 1; end 加入插值方法,使其和原始数据拟合效果更好
时间: 2023-12-22 07:04:57 浏览: 14
可以使用 MATLAB 中的 interp1 函数进行插值,代码如下:
data = load('out3.txt');
N = length(data(:,1));
b = fir1(15, 0.3, chebwin(16, 30));
data_smooth = filtfilt(b, 1, data(:,1));
% 对平滑后的数据进行插值
x = 1:N;
xq = linspace(1,N,10*N);
data_interp = interp1(x, data_smooth, xq, 'spline');
% 对插值后的数据寻找极值点
[IndMin, IndMax] = findExtrema(data_interp);
figure;
subplot(2,1,1);
plot(data_interp); hold on;
plot(IndMin, data_interp(IndMin), 'r^');
plot(IndMax, data_interp(IndMax), 'k*');
box on;
legend('曲线','波谷点','波峰点');
title('计算离散节点的波峰波谷信息', 'FontWeight', 'Bold');
subplot(2,1,2);
plot(data(:,1), 'k'); hold on;
plot(data_smooth, 'r');
plot(xq, data_interp, 'g');
box on;
legend('原始数据','平滑数据', '插值数据');
title('原始数据和插值数据', 'FontWeight', 'Bold');
function [IndMin, IndMax] = findExtrema(data_interp)
%获得局部最小值的位置
IndMin = find(diff(sign(diff(data_interp))) > 0) + 1;
%获得局部最大值的位置
IndMax = find(diff(sign(diff(data_interp))) < 0) + 1;
end
这样,就可以得到插值后的数据和更好的波峰波谷信息。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)