采用haar小波实现一维信号的分解与重构:
%% 构造原始信号
f1=30; % 频率1
f2=150; % 频率2
fs=450; % 采样频率
Ts=1/fs;% 采样间隔
T=128; % 采样点数
n=1:T;
y=sin(2*pi*f1*n*Ts)+sin(2*pi*f2*n*Ts); % 正弦波混合
figure(1)
plot(y);
title('原始信号')
%% 信号分解
l1=wfilters('haar','l')*sqrt(2)/2; % 参考低通滤波器
l1_zeros=[l1,zeros(1,T-length(l1))]; % 低通滤波器1
h1=wfilters('haar','h')*sqrt(2)/2; % 参考高通滤波器
h1_zeros=[h1,zeros(1,T-length(h1))]; % 高通滤波器1
low1=ifft(fft(y).*fft(l1_zeros)); % 低频分量1
high1=ifft(fft(y).*fft(h1_zeros)); % 高频分量1
l2=dyadup(l1); % 原滤波器插值
l2_zeros=[l2,zeros(1,T-length(l2))]; % 低通滤波器2
h2=dyadup(h1); % 原滤波器插值
h2_zeros=[h2,zeros(1,T-length(h2))]; % 高通滤波器2
low2=ifft(fft(low1).*fft(l2_zeros)); % 低频分量2
high2=ifft(fft(low1).*fft(h2_zeros)); % 高频分量2
figure(2); % 信号图
subplot(2,1,1)
plot(real(low1));
title('第一层低频分量')
subplot(2,1,2)
plot(real(high1));
title('第一层高频分量')
figure(3); % 信号图
subplot(2,1,1)
plot(real(low2));
title('第二层低频分量')
subplot(2,1,2)
plot(real(high2));
title('第二层高频分量')
%% 信号的重构
lr2=circshift(l2_zeros(end:-1:1).',1).';
hr2=circshift(h2_zeros(end:-1:1).',1).';
lr1=circshift(l1_zeros(end:-1:1).',1).';
hr1=circshift(h1_zeros(end:-1:1).',1).';
lowr=(ifft(fft(low2).*fft(lr2))+ifft(fft(high2).*fft(hr2)));
r_s=(ifft(fft(lowr).*fft(lr1))+ifft(fft(high1).*fft(hr1)));
figure(3);
plot(low1,'r');
hold on;
plot(low2,'b');
legend('第一层低频','第二层低频');
figure(4);
plot(high1,'r');
hold on;
plot(high2,'b');
legend('第一层高频','第二层高频');
figure(5)
plot(real(lowr),'r','linewidth',2);
hold on;
plot(low1);
legend('重构第一层低频','第一层低频');
figure(6)
plot(real(r_s),'r','linewidth',2);
hold on;
plot(y);
legend('重建信号','原始信号');
disp(norm(low1-lowr))
disp(norm(y-r_s))
评论0