clear
clc
close all
fs=20;
width=16;
height=16;
t=(1/fs:1/fs:200)';
signal=sin(2*pi*0.1)+rand(length(t),1)+3/100*t;
figure(1)
set(gcf,'units','centimeters','Position',[1,2+height,width,height])
plot(t,signal)
smoothed_avg_values = smooth(signal, 20); % 这里的10是平滑窗口的长度
figure(2)
set(gcf,'units','centimeters','Position',[1+width,2+height,width,height])
plot(t,signal)
hold on
plot(t,smoothed_avg_values)
hold off
overlap=5; %每次往前推进的数值。
window_length=200; %保证其是个偶数
num_windows = floor((length(signal) - window_length) / overlap) ;
rms_values = zeros(num_windows, 1);
for II=1:num_windows
start_idx=(II - 1) * overlap + 1;
end_idx=start_idx+ window_length;
rms_values(II,1)=rms(signal(start_idx:end_idx));
rms_time(II,1)=start_idx+window_length/2;
end
figure(3)
set(gcf,'units','centimeters','Position',[1+2*width,2+height,width,height])
plot(rms_time/fs,rms_values)
figure(4)
set(gcf,'Units','centimeters','Position',[1+3*width,2+height,width,height])
plot(t,signal,'b')
hold on
plot(t,smoothed_avg_values,'r')
plot(rms_time/fs,rms_values,'m','LineWidth',2)
hold off