%含噪声的1到10倍基频的谐波信号
%利用db40小波对信号进行7层分解
clear;clc;clf;
%产生正弦信号
f1=50;%信号频率Hz
f2=100;%信号频率Hz
f3=150;%信号频率Hz
f4=200;%信号频率Hz
f5=250;%信号频率Hz
f6=300;%信号频率Hz
f7=350;%信号频率Hz
f8=400;%信号频率Hz
f9=450;%信号频率Hz
f10=500;%信号频率Hz
fs=2048;%采样频率Hz
L=2048;%采样点数
t=(0:L-1)/fs;%采样时间s
x1=20*sin(2*pi*f1*t+0.1*pi);%信号采样值
x2=13*sin(2*pi*f2*t+0.3*pi);%信号采样值
x3=16*sin(2*pi*f3*t+0.5*pi);%信号采样值
x4=10*sin(2*pi*f4*t+0.3*pi);%信号采样值
x5=17*sin(2*pi*f5*t+0.8*pi);%信号采样值
x6=8.5*sin(2*pi*f6*t+0.5*pi);%信号采样值
x7=14.5*sin(2*pi*f7*t+0.1*pi);%信号采样值
x8=15*sin(2*pi*f8*t+0.2*pi);%信号采样值
x9=16*sin(2*pi*f9*t+0.4*pi);%信号采样值
x10=9*sin(2*pi*f10*t+0.2*pi);%信号采样值
x=x1+x2+x3+x4+x5+x6+x7+x8+x9+x10;
figure(1);
subplot(211);
plot(t,x,'linewidth',2);
xlabel('时间t(s)');
ylabel('幅值A');
%叠加高斯噪声信号
X=2*randn(size(t));
subplot(212);
plot(t,X,'linewidth',2);
xlabel('时间t(s)');
ylabel('幅值A');
%叠加信号
y=x+X;
figure(2);
plot(t,y,'linewidth',2);
xlabel('时间t(s)');
ylabel('幅值A');
save dwt_1_10hz.mat y;
%多尺度一维离散小波分解
[c,l]=wavedec(x,7,'db20');
%重构第1-7层逼近系数
a7=wrcoef('a',c,l,'db40',7);
a6=wrcoef('a',c,l,'db40',6);
a5=wrcoef('a',c,l,'db40',5);
a4=wrcoef('a',c,l,'db40',4);
a3=wrcoef('a',c,l,'db40',3);
a2=wrcoef('a',c,l,'db40',2);
a1=wrcoef('a',c,l,'db40',1);
%显示逼近系数
figure(3)
subplot(717)
plot(t,a7,'linewidth',2);
ylabel('a7');
subplot(716)
plot(t,a6,'linewidth',2);
ylabel('a6');
subplot(715)
plot(t,a5,'linewidth',2);
ylabel('a5');
subplot(714)
plot(t,a4,'linewidth',2);
% axis([0 1 -20 20]);
ylabel('a4');
subplot(713)
plot(t,a3,'linewidth',2);
ylabel('a3');
% axis([0 1 -40 40]);
subplot(712)
plot(t,a2,'linewidth',2);
ylabel('a2');
% axis([0 1 -40 40]);
subplot(711)
plot(t,a1,'linewidth',2);
ylabel('a1');
xlabel('样本序号 N');
% axis([0 1 -40 40]);
%重构第1-7层细节系数
d7=wrcoef('d',c,l,'db40',7);
d6=wrcoef('d',c,l,'db40',6);
d5=wrcoef('d',c,l,'db40',5);
d4=wrcoef('d',c,l,'db40',4);
d3=wrcoef('d',c,l,'db40',3);
d2=wrcoef('d',c,l,'db40',2);
d1=wrcoef('d',c,l,'db40',1);
%显示细节系数
figure(4)
subplot(717)
plot(t,d7,'linewidth',2);
ylabel('d7');
subplot(716)
plot(t,d6,'linewidth',2);
ylabel('d6');
subplot(715)
plot(t,d5,'linewidth',2);
ylabel('d5');
subplot(714)
plot(t,d4,'linewidth',2);
ylabel('d4');
% axis([0 1 -40 40]);
subplot(713)
plot(t,d3,'linewidth',2);
ylabel('d3');
axis([0 1 -40 40]);
subplot(712)
plot(t,d2,'linewidth',2);
ylabel('d2');
% axis([0 1 -40 40]);
subplot(711)
plot(t,d1,'linewidth',2);
ylabel('d1');
xlabel('样本序号 N');
% axis([0 1 -40 40]);