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 10:05:57 浏览: 7
可以在代码中增加以下内容来计算波峰点和波谷点的平均值以及所有数据平均值:
```
% 计算波峰点和波谷点的平均值
meanIndMin = mean(data_smooth(IndMin));
meanIndMax = mean(data_smooth(IndMax));
% 计算所有数据的平均值
meanData = mean(data(:, 1));
```
然后在第一个subplot中的legend中增加以下内容:
```
legend('曲线','波谷点','波峰点', '波谷平均值', '波峰平均值', '所有数据平均值');
```
并在plot中增加以下内容:
```
plot([1, N], [meanIndMin, meanIndMin], 'g--');
plot([1, N], [meanIndMax, meanIndMax], 'b--');
plot([1, N], [meanData, meanData], 'm--');
```
最终的代码如下:
```
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);
% 计算波峰点和波谷点的平均值
meanIndMin = mean(data_smooth(IndMin));
meanIndMax = mean(data_smooth(IndMax));
% 计算所有数据的平均值
meanData = mean(data(:, 1));
figure;
subplot(2,1,1);
plot(data_smooth);
hold on;
plot(IndMin, data_smooth(IndMin), 'r^');
plot(IndMax, data_smooth(IndMax), 'k*');
plot([1, N], [meanIndMin, meanIndMin], 'g--');
plot([1, N], [meanIndMax, meanIndMax], 'b--');
plot([1, N], [meanData, meanData], 'm--');
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
```
阅读全文