编写的小波分解合成函数
function [S] = waves(signal,num,wavename)
% load D105.mat
% signal = data(:,1);
% num = 10;
% wavename = 'coif4';
% S = waves(signal,num,wavename)
% figure
% SS = sum(S.d(3:7,:));
% subplot(2,1,1)
% plot(SS)
% subplot(2,1,2)
% plot(signal)
%% 分解及合成
[d,a]= wavedec(signal,num,wavename);
for i=1:num
S.d(i,:) = wrcoef('d',d,a,wavename,i);
end
S.a=wrcoef('a',d,a,wavename,num);
%% 画图
figure;
subplot(num+2,1,1);
plot(signal);ylabel('s');hold on;
subplot(num+2,1,2);
plot(S.a);ylabel(['a', num2str(num)]);
for i=1:num
subplot(num+2,1,i+2);
plot(S.d(i,:));ylabel(['d', num2str(i)]);
end
end
调用它的例子
clear;clc
load D105.mat
%是一个一维信号
signal = data(:,1);
num = 10;
wavename = 'coif4';
S = waves(signal,num,wavename);
SS = sum(S.d(3:7,:));
figure;
subplot(2,1,1)
plot(SS)
subplot(2,1,2)
plot(signal)
smooth()%用于平滑曲线
wavemenu%打开小波工具箱
图像去噪的实验
clear all
QQ = imread('QQ.bmp');
qq = double(QQ);
Q = qq(:,:,1)+qq(:,:,2)+qq(:,:,3);
Q=Q./3;
load tire;
X = Q;
figure;
subplot(2,2,1);
image(X); colormap(map);
title('原始图像');
axis square;
% h=20;
h=180;
x = X + h*randn(size(X));
subplot(2,2,2);
image(x);colormap(map);
title(['加入强度为',num2str(h),'的高斯噪声后']);
axis square;
[c, s] = wavedec2(x,2,'sym4');
a1 = wrcoef2('a',c,s,'sym4',1);
subplot(2,2,3);image(a1);
title('第一次消噪后的图像');
axis square;
a2 = wrcoef2('a',c,s,'sym4',2);
subplot(2,2,4);
image(a2);
axis square;
title('第二次降噪后的图像');
小波去噪
t = 0:pi/100:4*pi;
s = sin(t+pi/4);
subplot(5,1,1);plot(s);
title('原始信号');
[c,l] = wavedec(s,3,'db1');
grid;
ca3 = appcoef(c,l,'db1',3);%提取低频
cd3 = detcoef(c,l,'db1',3);%提取第三层高频系数
cd2 = detcoef(c,l,'db1',2);%提取第二层高频系数
cd1 = detcoef(c,l,'db1',1);%提取第一层高频系数
s1 = waverec(c,l,'db1');%重构小波信号
subplot(5,1,2);plot(s1);grid;title('重构信号');