加权平均、EMD、小波等方法去噪效果对比
代码
整体代码如下
%%
clear all;
clc;
load('data_filter120Hz.mat'); %可自己生成随机噪声
fs=1000;%采样频率是1000Hz
%%
%生成正弦波信号
t=linspace(0, length(data)/fs-1/fs, length(data));
y1 =15*sin(2*pi* 2.8 *t);%生成频率为2.8Hz,幅值为15的正弦波
y2 =10*sin(2*pi* 10.5 *t);%生成频率为10.5Hz,幅值为10的正弦波
y3 =3*sin(2*pi* 27 *t);%生成频率为27Hz,幅值为3的正弦波
y4 =0.5*sin(2*pi* 43 *t);%生成频率为43Hz,幅值为0.5的正弦波
y_Sin =y1+y2+y3+y4;
% y_Sin =y1+y2+y3;
y = y_Sin'+data;
%%
saveTime =1;
signal = y(1:saveTime*fs);
dlmwrite('MultiSinWaveWithNoise_1s.txt',signal,'delimiter',' ');
saveTime =10;
signal = y(1:saveTime*fs);
dlmwrite('MultiSinWaveWithNoise_10s.txt',signal,'delimiter',' ');
%%
figure;
subplot(3,1,1);
plot(data);
title('noise');
subplot(3,1,2);
plot(y_Sin);
title('sin wave');
subplot(3,1,3);
plot(y);
title('sin wave + noise');
figure;
plot(y_Sin);
hold on;
plot(y);
legend({'raw','with noise'});
%%
p=0.9;
preTemp =0;
for i=1:length(y)
y_winAve(i) = preTemp*(1-p)+ y(i)*p;
preTemp = y_winAve(i);
end
error_noise = sum(abs(data));
error_winave = sum(abs(y_winAve -y_Sin));
%%
figure;
plot(y_Sin);
hold on;
plot(y_winAve);
legend({'raw','win ave'});
figure;
plot(y);
hold on;
plot(y_winAve);
legend({'raw+noise','win ave'});
%% emd method
emd_num = 2;
imf = emd(y);
y_emd =sum(imf(:,emd_num:end),2);
figure;
plot(y);
hold on;
plot(y_emd);
title('emd denoise');
error_emd = sum(abs(y_emd' -y_Sin));
%% wpdencmp
wpden_num =3;
[thr,sorh,deepapp,crit]=ddencmp('den','wp',y);
[y_wpden,~,~,~]=wpdencmp(y,sorh,wpden_num,'sym6',crit,thr,deepapp);
figure;
plot(y);
hold on;
plot(y_wpden);
title('wpdencmp');
error_wpden = sum(abs(y_wpden' -y_Sin));
%% winave 2nd method
p2 =0.85;
winLen =10;
preTemp =0;
for i=1:length(y)
if(i<length(y)-winLen)
if(winLen>=i)
preTemp =mean(y(1:i+winLen));
else
preTemp =mean(y(i-winLen:i+winLen));
end
else
preTemp =mean(y(i-winLen:length(y)));
end
y_winAve02(i) = preTemp*(1-p2)+ y(i)*p2;
preTemp = y_winAve02(i);
end
error_winave02 = sum(abs(y_winAve02 -y_Sin));
figure;
plot(y);
hold on;
plot(y_winAve02);
legend({'raw+noise','win ave 02'});
%%
close all;
效果
从结果上去看,上述参数中,去噪效果:
小波 >EMD >加权平均 >移动平均(具体设置看上方代码)
Matlab转c++
emd和小波去噪的C++代码效果和matlab自带的效果不太一致(可能是我设置的问题),但都能达到去噪的效果,此时emd效果最好,小波的效果需要调整软阈值的值来优化(0.5->1);
EMD代码
小波代码