data = load('out3.txt'); data_smooth = smoothdata(data(:,1), 'movmean', 5); [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); box on; 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 用小波变换替换代码中的smooth方法
时间: 2023-12-27 19:04:36 浏览: 48
可以使用以下代码将小波变换替换掉原来的smooth方法:
```matlab
% 定义小波变换参数
wname = 'db4';
level = 5;
% 对数据进行小波变换
[c, l] = wavedec(data(:,1), level, wname);
approx = wrcoef('a', c, l, wname, level);
data_smooth = zeros(size(data, 1), 1);
data_smooth(1:length(approx)) = approx;
% 获得波峰波谷位置
[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);
box on;
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
```
需要说明的是,小波变换的具体参数需要根据实际数据进行调整,这里的参数只是一个示例。
阅读全文