46
An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000 University of New Haven Instructor: Dr. A. Jafarian Student: Wiwat Tharateeraparb 888-80-6557 Contents Introduction……….….…………………………………………………………………1 Chapter 1: M-files and results fig23.m………………………………………………………………...….2 fig27.m……………………………………………………………………3 fig28.m……………………………………………………………………4 fig29.m……………………………………………………………………5 fig30.m………………………………………………………...………….6 fig31.m……………………………………………………………...…….8 fig32.m…………………………………………………………………....9 fig33.m…………………………………….…………………………….11 fig34.m……………………………………….………………………….12 Threshold.m……………………….……………..……………………..14 denoising.m…………………………………………...………………..15 dc_fig7ab.m……………………………………………….……………16 dc_fig8.m……………………………………………………..………...17 D4.m……………………………………………………………...……..18 phivals.m……………………………………………………………..…19 example2.m………………………………………...…………………..20 Chapter 2: MATLAB Functions and Description……………………………………22 Dwt : single-level discrete 1-D wavelet transform Idwt : single-level inverse discrete 1-D wavelet transform Upcoef : direct reconstruction from 1-D wavelet coefficients Wavedec : multi-level 1-D wavelet decomposition Waverec : multi-level 1-D wavelet reconstruction Wpdencmp : de-noising or compression using wavelet packet Appcoef : extract 1-D approximation coefficients Detcoef : extract 1-D detail coefficients Wrcoef : reconstruct single branch from 1-D wavelet coefficients

An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

  • Upload
    buidat

  • View
    253

  • Download
    0

Embed Size (px)

Citation preview

Page 1: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

An Introduction to Wavelets Toolbox and its Applications

EE670N: Wavelets Spring 2000 University of New Haven

Instructor: Dr. A. Jafarian Student: Wiwat Tharateeraparb 888-80-6557

Contents

Introduction……….….…………………………………………………………………1

Chapter 1: M-files and results

fig23.m………………………………………………………………...….2 fig27.m……………………………………………………………………3 fig28.m……………………………………………………………………4 fig29.m……………………………………………………………………5 fig30.m………………………………………………………...………….6 fig31.m……………………………………………………………...…….8 fig32.m…………………………………………………………………....9 fig33.m…………………………………….…………………………….11 fig34.m……………………………………….………………………….12

Threshold.m……………………….……………..……………………..14 denoising.m…………………………………………...………………..15

dc_fig7ab.m……………………………………………….……………16 dc_fig8.m……………………………………………………..………...17 D4.m……………………………………………………………...……..18

phivals.m……………………………………………………………..…19 example2.m………………………………………...…………………..20

Chapter 2: MATLAB Functions and Description……………………………………22

Dwt : single-level discrete 1-D wavelet transform Idwt : single-level inverse discrete 1-D wavelet transform Upcoef : direct reconstruction from 1-D wavelet coefficients Wavedec : multi-level 1-D wavelet decomposition Waverec : multi-level 1-D wavelet reconstruction Wpdencmp : de-noising or compression using wavelet packet Appcoef : extract 1-D approximation coefficients Detcoef : extract 1-D detail coefficients Wrcoef : reconstruct single branch from 1-D wavelet coefficients

Page 2: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

1

Introduction This material introduces the wavelet toolbox used in the EE670N: Wavelet class at University of New Haven. It includes m-files used to generate figures in the “An Introduction to Wavelets through Linear Algebra” by Michael W. Frazier and a few figures in “Discovering Wavelets” by Edward Aboufadel and Steven Schlicker. This material also provides some of wavelet toolbox document in html files. One may find all of wavelet toolbox document at www.mit.edu and searchs for wavelets. All the figure-generating m-files are written by the author of this material. They are good examples to investigate and understand the wavelet functions via MATLAB software. Moreover, the m-files demonstrate the algorithm for simple compression and de-noising techniques. The file named “phivals.m”, which is used in “D4.m”, is written by Kevin Amaratunga of AMARA group. More of wavelet material can be obtained at www.amara.com .

Page 3: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

2

Chapter 1 M-Files and results % Figure 23 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; z=zeros(1,N); z(1,[129:256])=sin((abs([128:255]-128).^1.7)/128); z(1,[385:448])=sin((abs([384:447]-128).^2)/128); subplot(211);plot(z);axis([0 N -1 1]);%axis tight z_hat=fft(z); subplot(212);plot(abs(z_hat));axis([0 N 0 30]);%axis tight

Page 4: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

3

% Figure 27 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; z=zeros(1,N); z(1,[129:256])=sin((abs([128:255]-128).^1.7)/128); z(1,[385:448])=sin((abs([384:447]-128).^2)/128); subplot(321);plot(z); axis([1 N -1.2 1.2]); title('z : original signal'); [c,l] = wavedec(z,4,'db6'); ca4 = appcoef(c,l,'db6',4); cd4 = detcoef(c,l,4); cd3 = detcoef(c,l,3); cd2 = detcoef(c,l,2); cd1 = detcoef(c,l,1); subplot(323);plot(cd1,'x');title('<z,\psi_{-1,k}>');axis([1 length(cd1) -1.5 1.5]); subplot(325);plot(cd2,'x');title('<z,\psi_{-2,k}>');axis([1 length(cd2) -2.0 2.0]); subplot(322);plot(cd3,'x');title('<z,\psi_{-3,k}>');axis([1 length(cd3) -1.5 1.5]); subplot(324);plot(cd4,'x');title('<z,\psi_{-4,k}>');axis([1 length(cd4) -4.0 4.0]); subplot(326);plot(ca4,'x');title('<z,\phi_{-4,k}>');axis([1 length(ca4) -5.0 5.0]); % the below code yields the same result % actually it is what the appcoeff and detcoeff functions do internally % see the description of wavedec function figure; subplot(321);plot(z); axis([1 N -1.2 1.2]); title('z : original signal'); subplot(326);plot(c(1:l(1)),'x');axis tight; for k=1:4, if k == 1, subplot(324); elseif k == 2, subplot(322); elseif k == 3, subplot(325); else subplot(323); end; x1 = sum(l(1:k)); x2 = sum(l(1:k+1)); plot(c(x1:x2),'x');axis tight; end;

Page 5: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

4

% Figure 28 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; z=zeros(1,N); z(1,[1:64])=1-([0:63]/64); z(1,[257:320])=1-([0:63]/64); subplot(321);plot(z); axis([1 N -0.2 1.2]); title('z : original signal'); [c,l] = wavedec(z,4,'db6'); ca4 = appcoef(c,l,'db6',4); cd4 = detcoef(c,l,4); cd3 = detcoef(c,l,3); cd2 = detcoef(c,l,2); cd1 = detcoef(c,l,1); subplot(323);plot(cd1,'x');title('<z,\psi_{-1,k}>');axis([1 length(cd1) -0.6 0.6]); subplot(325);plot(cd2,'x');title('<z,\psi_{-2,k}>');axis([1 length(cd2) -0.6 0.6]); subplot(322);plot(cd3,'x');title('<z,\psi_{-3,k}>');axis([1 length(cd3) -0.8 0.8]); subplot(324);plot(cd4,'x');title('<z,\psi_{-4,k}>');axis([1 length(cd4) -1.0 1.0]); subplot(326);plot(ca4,'x');title('<z,\phi_{-4,k}>');axis([1 length(ca4) -4.0 4.0]);

Page 6: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

5

% Figure 29 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; err1=[]; err2=[]; z=zeros(1,N); z(1,[1:64])=1-([0:63]/64); z(1,[257:320])=1-([0:63]/64); subplot(221);plot(z);axis([0 512 -0.2 1.2]); title('Original Signal: page248 Frazier'); for K=1:200, % Fourier compression z_hat=fft(z); zz=fliplr(sort(abs(z_hat))); thres=zz(1,K); for k=1:N, if (abs(z_hat(k))<thres) z_hat(k)=0; end; end; w1=ifft(z_hat); % Wavelet compression (db6) [c,l] = wavedec(z,4,'db6'); zz=fliplr(sort(abs(c))); thres=zz(1,K); for k=1:N, if (abs(c(k))<thres) c(k)=0; end; end; w2 = waverec(c,l,'db6'); % Relative error err1=[err1 norm(z-w1)./norm(z)]; err2=[err2 norm(z-w2)./norm(z)]; % Plotting if K==20 subplot(222);plot(real(w1));axis([0 512 -0.2 1.1]); title('Fourier compression,K=20'); subplot(223);plot(w2);axis([0 512 -0.2 1.2]); title('D6 wavelet compression,K=20'); end; end; x=length(err1); subplot(224);plot(1:x,err1,':',1:x,err2); title('Relative error'); legend('Fourier basis','D6 basis');

Page 7: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

6

% Figure 30 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; K=75; z=zeros(1,N); z(1,[1:64])=1-([0:63]/64); z(1,[257:320])=1-([0:63]/64); subplot(221);plot(z);axis([0 512 -0.2 1.2]); title('Original Signal: page248 Frazier'); % Fourier compression % Case K=75 z_hat=fft(z); zz=fliplr(sort(abs(z_hat))); thres=zz(1,K); for k=1:N, if (abs(z_hat(k))<thres) z_hat(k)=0; end; end; w1=ifft(z_hat); % Case K=200 z_hat=fft(z); zz=fliplr(sort(abs(z_hat))); thres=zz(1,200); for k=1:N, if (abs(z_hat(k))<thres) z_hat(k)=0; end; end; w3=ifft(z_hat); % Wavelet compression (db6) [c,l] = wavedec(z,4,'db6'); zz=fliplr(sort(abs(c))); thres=zz(1,K); for k=1:N, if (abs(c(k))<thres) c(k)=0; end; end; w2 = waverec(c,l,'db6'); % Plotting subplot(222);plot(real(w1));axis([0 512 -0.2 1.2]); title('Fourier compression,K=75'); subplot(223);plot(real(w3));axis([0 512 -0.2 1.2]); title('Fourier compression,K=200'); subplot(224);plot(w2);axis([0 512 -0.2 1.2]); title('D6 wavelet compression,K=75');

Page 8: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

7

Figure 29

Figure 30

Page 9: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

8

% Figure 31 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; err1=[]; err2=[]; z=zeros(1,N); z(1,[129:256])=sin((abs([128:255]-128).^1.7)/128); z(1,[385:448])=sin((abs([384:447]-128).^2)/128); subplot(221);plot(z);axis([0 512 -1.1 1.1]); title('Original Signal'); for K=1:400, % Fourier compression z_hat=fft(z); zz=fliplr(sort(abs(z_hat))); thres=zz(1,K); for k=1:N, if (abs(z_hat(k))<thres) z_hat(k)=0; end; end; w1=ifft(z_hat); % Wavelet compression (db6) [c,l] = wavedec(z,4,'db6'); zz=fliplr(sort(abs(c))); thres=zz(1,K); for k=1:N, if (abs(c(k))<thres) c(k)=0; end; end; w2 = waverec(c,l,'db6'); % Relative error err1=[err1 norm(z-w1)./norm(z)]; err2=[err2 norm(z-w2)./norm(z)]; % Plotting if K==75 subplot(222);plot(real(w1));axis([0 512 -1.1 1.1]); title('Fourier compression,K=75'); subplot(223);plot(w2);axis([0 512 -1.1 1.1]); title('D6 wavelet compression,K=75'); end; end; x=length(err1); subplot(224);plot(1:x,err1,':',1:x,err2); title('Relative error'); legend('Fourier basis','D6 basis');

Page 10: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

9

% Figure 32 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; err1=[]; err2=[]; z=sin([0:511].^1.5/64); subplot(221);plot(z);axis([0 512 -1.5 1.5]); title('Original Signal'); for K=1:200, % Fourier compression z_hat=fft(z); zz=fliplr(sort(abs(z_hat))); thres=zz(1,K); for k=1:N, if (abs(z_hat(k))<thres) z_hat(k)=0; end; end; w1=ifft(z_hat); % Wavelet compression (db6) [c,l] = wavedec(z,4,'db6'); zz=fliplr(sort(abs(c))); thres=zz(1,K); for k=1:N, if (abs(c(k))<thres) c(k)=0; end; end; w2 = waverec(c,l,'db6'); % Relative error err1=[err1 norm(z-w1)./norm(z)]; err2=[err2 norm(z-w2)./norm(z)]; % Plotting if K==50 subplot(222);plot(real(w1));axis([0 512 -1.5 1.5]); title('Fourier compression,K=50'); subplot(223);plot(w2);axis([0 512 -1.5 1.5]); title('D6 wavelet compression,K=50'); end; end; x=length(err1); subplot(224);plot(1:x,err1,':',1:x,err2); title('Relative error'); legend('Fourier basis','D6 basis');

Page 11: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

10

Figure 31

Figure 32

Page 12: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

11

% Figure 33 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; err1=[]; err2=[]; n=[0:511]; z=((n-256).*exp(-(n-256).^2/512)); subplot(221);plot(z);axis([0 512 -11 11]); title('Original Signal'); for K=1:100, % Fourier compression z_hat=fft(z); zz=fliplr(sort(abs(z_hat))); thres=zz(1,K); for k=1:N, if (abs(z_hat(k))<thres) z_hat(k)=0; end; end; w1=ifft(z_hat); % Wavelet compression (db6) [c,l] = wavedec(z,4,'db6'); zz=fliplr(sort(abs(c))); thres=zz(1,K); for k=1:N, if (abs(c(k))<thres) c(k)=0; end; end; w2 = waverec(c,l,'db6'); % Relative error err1=[err1 norm(z-w1)./norm(z)]; err2=[err2 norm(z-w2)./norm(z)]; % Plotting if K==8 subplot(222);plot(real(w1));axis([0 512 -11 11]); title('Fourier compression,K=8'); subplot(223);plot(w2);axis([0 512 -11 11]); title('D6 wavelet compression,K=8'); end; end; x=length(err1); subplot(224);plot(1:x,err1,':',1:x,err2); title('Relative error'); legend('Fourier basis','D6 basis');

Page 13: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

12

% Figure 34 of Frazier % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; err1=[]; err2=[]; z=zeros(1,N); z(1,[33:96])=1; z(1,[133:260])=2; z(1,[316:411])=4; subplot(221);plot(z);axis([0 512 -1 5]); title('Original Signal'); for K=1:100, % Fourier compression z_hat=fft(z); zz=fliplr(sort(abs(z_hat))); thres=zz(1,K); for k=1:N, if (abs(z_hat(k))<thres) z_hat(k)=0; end; end; w1=ifft(z_hat); % Wavelet compression (db6) [c,l] = wavedec(z,4,'db6'); zz=fliplr(sort(abs(c))); thres=zz(1,K); for k=1:N, if (abs(c(k))<thres) c(k)=0; end; end; w2 = waverec(c,l,'db6'); % Relative error err1=[err1 norm(z-w1)./norm(z)]; err2=[err2 norm(z-w2)./norm(z)]; % Plotting if K==16 subplot(222);plot(real(w1));axis([0 512 -1 5]); title(['Fourier compression,K='int2str(K)]); subplot(223);plot(w2);axis([0 512 -1 5]); title(['D6 wavelet compression,K='int2str(K)]); end; end; x=length(err1); subplot(224);plot(1:x,err1,':',1:x,err2); title('Relative error'); legend('Fourier basis','D6 basis');

Page 14: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

13

Figure 33

Figure 34

Page 15: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

14

% Experiment 1.6,page 9 of Discovering wavelet % Wiwat Tharateeraparb % no pictures attatched!! clear;close all;clc; x = [(1:32)./32].'; f = sin(20*x).*(log(x).^2); plot(x,f);grid; title('Original Signal'); % Define matrix A3 A3 = [1 1 1 0 1 0 0 0; 1 1 1 0 -1 0 0 0; 1 1 -1 0 0 1 0 0; 1 1 -1 0 0 -1 0 0; 1 -1 0 1 0 0 1 0; 1 -1 0 1 0 0 -1 0; 1 -1 0 -1 0 0 0 1; 1 -1 0 -1 0 0 0 -1]; A3_inv = inv(A3); % Separate the data into 4 vector (8x1 each) ff = reshape(f,8,4); % Wavelet Coefficients c1 = A3_inv*ff(:,1); c2 = A3_inv*ff(:,2); c3 = A3_inv*ff(:,3); c4 = A3_inv*ff(:,4); c = [c1;c2;c3;c4]; figure; subplot(211);plot(c);grid; %axis tight; title('Original wavelet coefficients') % Ignore some data lamda = 0.05; for k=1:length(c), if (abs(c(k)) < lamda) c(k)=0; end; end; subplot(212);plot(c);grid; %axis tight; title(['Coefficients with tolerance: ',num2str(lamda)]); % New data cc = reshape(c,8,4); f_new = [A3*cc(:,1);A3*cc(:,2);A3*cc(:,3);A3*cc(:,4)]; figure;plot(x,f_new);grid; title(['New Siganl with tolerance: ',num2str(lamda)]); % We can use MATLAB wavelet toolbox to do this job [C1,l1] = wavedec(ff(:,1),3,'haar'); [C2,l2] = wavedec(ff(:,2),3,'haar'); [C3,l3] = wavedec(ff(:,3),3,'haar'); [C4,l4] = wavedec(ff(:,4),3,'haar'); figure; subplot(211); plot([C1;C2;C3;C4]./(sqrt(2)^3)); % coeff matters sqroot2

Page 16: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

15

% Denoising of a signal using wavelets % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;clc;close all; N=512; K=50; start=2500; load leleccum; z=leleccum(start:start+N-1); subplot(211);plot(z);axis tight; title('Original Signal'); % Wavelet compression (db6) [c,l] = wavedec(z,4,'db6'); zz=fliplr(sort(abs(c))); thres=zz(1,K); for k=1:N, if (abs(c(k))<thres) c(k)=0; end; end; w2 = waverec(c,l,'db6'); % Plotting subplot(212);plot(w2);axis tight; title(['D6 wavelet denoising,K=',int2str(K)]);

Page 17: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

16

% Fig7a-b chapter2 page 10 of Discovering wavelets % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;close all;clc; x = [(1:32)./32]; f = sin(20*x).*(log(x).^2); subplot(211); plot(x,f,[0 1],[0 0],'k');% in matlab 4.2,change 'k' to 'w' axis([0 1 min(f) max(f)]); title('An element on V_5'); [ca1 cd1] = dwt(f,'haar'); c = (1/sqrt(2)).*[ca1 cd1] %figure; subplot(212); plot(x,c,[x(16) x(16)],[min(c) max(c)],':r',[0 1],[0 0],'k'); axis([0 1 min(c) max(c)]); %grid; title('An image box,containing an element of V_4 and V_4 per');

Page 18: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

17

% Fig8 chapter2 page 11 of Discovering wavelets % % Wiwat Tharateeraparb % University of New Haven % June 2000 % clear;close all;clc; x = [(1:32)./32]; f = sin(20*x).*(log(x).^2); subplot(211); plot(x,f,[0 1],[0 0],'k');% in matlab 4.2,change 'k' to 'w' axis([0 1 min(f) max(f)]); title('An element on V_5'); [c l] = wavedec(f,2,'haar'); c = (1/sqrt(2))^2.*c %figure; subplot(212); plot(x,c,[x(16) x(16)],[min(c) max(c)],':r',[x(8) x(8)],[min(c) max(c)],':r',[0 1],[0 0],'k'); axis([0 1 min(c) max(c)]); %grid; title('An image box,containing an element of V_3,V_3 per and V_4 per');

Page 19: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

18

% D4-function generator by Cascade algorithm % % Wiwat Tharateeraparb % June 2000 % need phivals function % clear all; close all; clc; % refinement coefficients c0 = (1+sqrt(3))/4; c1 = (3+sqrt(3))/4; c2 = (3-sqrt(3))/4; c3 = (1-sqrt(3))/4; i = 7; % number of discretization parameter % the number of points per interger step is 2^i h = [c0 c1 c2 c3].'; % filter coefficients,sum(h)=2 as in criteria [x,phi,psi] = phivals(h,i); t = linspace(0,3,length(x)); plot(x,phi);grid; xlabel('t'); ylabel('\phi(t)'); title('Approximation of \phi for D4 wavelets'); figure; plot(x,psi);grid; xlabel('t'); ylabel('\psi(t)'); title('Approximation of \psi for D4 wavelets');

Page 20: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

19

% function [x,phi,psi] = phivals(h,i) % Generate a scaling function and its associated wavelet % using the given filter coefficients % Kevin Amaratunga % 5 March, 1993 % % h = filter coefficients (sum(h)=2) % i = discretization parameter. The number of points per integer % step is 2^i. Thus, setting i = 0 gives the scaling function % and wavelet values at integer points. % function [x,phi,psi] = phivals(h,i) if i < 0 error('phivals: i must be non-negative') end [m,n] = size(h); g = (-1).^(0:m-1)' .* h(m:-1:1); % The Haar filter produces a singular matrix, but since we know the solution % already we treat this as a special case. if m == 2 & h == [1;1] phi = [ones(2^i,1);0]; if i > 0 psi = [ones(2^(i-1),1);-ones(2^(i-1),1);0]; elseif i == 0 psi = [1;0]; end else ch = [h; zeros(m,1)]; rh = [h(1), zeros(1,m-1)]; tmp = toeplitz(ch,rh); M = zeros(m,m); M(:) = tmp(1:2:2*m*m-1); M = M - eye(m); M(m,:) = ones(1,m); tmp = [zeros(m-1,1); 1]; phi = M \ tmp; % Integer values of phi if i > 0 for k = 0:i-1 p = 2^(k+1) * (m-1) + 1; % No of rows in toeplitz matrix q = 2^k * (m-1) + 1; % No of columns toeplitz matrix if (k == 0) ch0 = [h; zeros(p-1-m,1)]; ch = [ch0; 0]; cg0 = [g; zeros(p-1-m,1)]; else ch = zeros(p-1,1); ch(:) = [1; zeros(2^k-1,1)] * ch0'; ch = [ch; 0]; end rh = [ch(1), zeros(1,q-1)]; Th = toeplitz(ch,rh); if k == i-1 cg = [1; zeros(2^k-1,1)] * cg0'; cg = cg(:); cg = [cg; 0]; rg = [cg(1), zeros(1,q-1)]; Tg = toeplitz(cg,rg); psi = Tg * phi; end phi = Th * phi; end elseif i == 0 cg0 = [g; zeros(m-2,1)]; cg = [cg0; 0]; rg = [cg(1), zeros(1,m-1)]; Tg = toeplitz(cg,rg); psi = Tg * phi; psi = psi(1:2:2*m-1); end end [a,b] = size(phi); x = (0:a-1)' / 2^i;

Page 21: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

20

% Example 2: 2D image analysis. % Load a test image. Matlab test images consist of a matrix, X, % color palette, map, which maps each value of the matrix to a % color. Here, we will apply the Discrete Wavelet Transform to X. load woman2 %load detfingr; X = X(1:200,51:250); clf image(X) colormap(map) axis image; set(gca,'XTick',[],'YTick',[]); title('Original') pause % Now compute a 2-level decomposition of the image using the Daubechies 4-tap % filter. dwtmode('sym') wname = 'bior4.4' [wc,s] = wavedec2(X,2,wname); % Extract the level 1 coefficients. a1 = appcoef2(wc,s,wname,1); h1 = detcoef2('h',wc,s,1); v1 = detcoef2('v',wc,s,1); d1 = detcoef2('d',wc,s,1); % Extract the level 2 coefficients. a2 = appcoef2(wc,s,wname,2); h2 = detcoef2('h',wc,s,2); v2 = detcoef2('v',wc,s,2); d2 = detcoef2('d',wc,s,2); % Display the decomposition up to level 1 only. ncolors = size(map,1); % Number of colors. sz = size(X); cod_a1 = wcodemat(a1,ncolors); cod_a1 = wkeep(cod_a1, sz/2); cod_h1 = wcodemat(h1,ncolors); cod_h1 = wkeep(cod_h1, sz/2); cod_v1 = wcodemat(v1,ncolors); cod_v1 = wkeep(cod_v1, sz/2); cod_d1 = wcodemat(d1,ncolors); cod_d1 = wkeep(cod_d1, sz/2); image([cod_a1,cod_h1;cod_v1,cod_d1]); axis image; set(gca,'XTick',[],'YTick',[]); title('Single stage decomposition') colormap(map) pause % Display the entire decomposition upto level 2. cod_a2 = wcodemat(a2,ncolors); cod_a2 = wkeep(cod_a2, sz/4); cod_h2 = wcodemat(h2,ncolors); cod_h2 = wkeep(cod_h2, sz/4); cod_v2 = wcodemat(v2,ncolors); cod_v2 = wkeep(cod_v2, sz/4); cod_d2 = wcodemat(d2,ncolors); cod_d2 = wkeep(cod_d2, sz/4); image([[cod_a2,cod_h2;cod_v2,cod_d2],cod_h1;cod_v1,cod_d1]); axis image; set(gca,'XTick',[],'YTick',[]); title('Two stage decomposition') colormap(map) pause % Here are the reconstructed branches ra2 = wrcoef2('a',wc,s,wname,2); rh2 = wrcoef2('h',wc,s,wname,2); rv2 = wrcoef2('v',wc,s,wname,2); rd2 = wrcoef2('d',wc,s,wname,2); ra1 = wrcoef2('a',wc,s,wname,1); rh1 = wrcoef2('h',wc,s,wname,1); rv1 = wrcoef2('v',wc,s,wname,1); rd1 = wrcoef2('d',wc,s,wname,1); cod_ra2 = wcodemat(ra2,ncolors); cod_rh2 = wcodemat(rh2,ncolors); cod_rv2 = wcodemat(rv2,ncolors); cod_rd2 = wcodemat(rd2,ncolors); cod_ra1 = wcodemat(ra1,ncolors); cod_rh1 = wcodemat(rh1,ncolors); cod_rv1 = wcodemat(rv1,ncolors); cod_rd1 = wcodemat(rd1,ncolors); subplot(3,4,1); image(X); axis image; set(gca,'XTick',[],'YTick',[]); title('Original') subplot(3,4,5); image(cod_ra1); axis image; set(gca,'XTick',[],'YTick',[]); title('ra1') subplot(3,4,6); image(cod_rh1); axis image; set(gca,'XTick',[],'YTick',[]); title('rh1') subplot(3,4,7); image(cod_rv1); axis image; set(gca,'XTick',[],'YTick',[]); title('rv1') subplot(3,4,8); image(cod_rd1); axis image; set(gca,'XTick',[],'YTick',[]); title('rd1') subplot(3,4,9); image(cod_ra2); axis image; set(gca,'XTick',[],'YTick',[]); title('ra2') subplot(3,4,10); image(cod_rh2); axis image; set(gca,'XTick',[],'YTick',[]); title('rh2') subplot(3,4,11); image(cod_rv2); axis image; set(gca,'XTick',[],'YTick',[]); title('rv2')

Page 22: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

21

subplot(3,4,12); image(cod_rd2); axis image; set(gca,'XTick',[],'YTick',[]); title('rd2') pause % Adding together the reconstructed average at level 2 and all of % the reconstructed details gives the full reconstructed image. Xhat = ra2 + rh2 + rv2 + rd2 + rh1 + rv1 + rd1; sprintf('Reconstruction error (using wrcoef2) = %g', max(max(abs(X-Xhat)))) % Another way to reconstruct the image. XXhat = waverec2(wc,s,wname); sprintf('Reconstruction error (using waverec2) = %g', max(max(abs(X-XXhat)))) % Compression can be accomplished by applying a threshold to the % wavelet coefficients. wdencmp is the function that does this. % 'h' means use hard thresholding. Last argument = 1 means do not % threshold the approximation coefficients. % perfL2 = energy recovery = 100 * ||wc_comp||^2 / ||wc||^2. % ||.|| is the L2 vector norm. % perf0 = compression performance = Percentage of zeros in wc_comp. thr = 20; [X_comp,wc_comp,s_comp,perf0,perfL2] = wdencmp('gbl',wc,s,wname,2,thr,'h',1); clf subplot(1,2,1); image(X); axis image; set(gca,'XTick',[],'YTick',[]); title('Original') cod_X_comp = wcodemat(X_comp,ncolors); subplot(1,2,2); image(cod_X_comp); axis image; set(gca,'XTick',[],'YTick',[]); title('Compressed using global hard threshold') xlabel(sprintf('Energy retained = %2.1f%% \nNull coefficients = %2.1f%%',perfL2,perf0)) pause % Better compression can be often be obtained if different thresholds % are allowed for different subbands. thr_h = [21 17]; % horizontal thresholds. thr_d = [23 19]; % diagonal thresholds. thr_v = [21 17]; % vertical thresholds. thr = [thr_h; thr_d; thr_v]; [X_comp,wc_comp,s_comp,perf0,perfL2] = wdencmp('lvd',X,wname,2,thr,'h'); clf subplot(1,2,1); image(X); axis image; set(gca,'XTick',[],'YTick',[]); title('Original') cod_X_comp = wcodemat(X_comp,ncolors); subplot(1,2,2); image(cod_X_comp); axis image; set(gca,'XTick',[],'YTick',[]); title('Compressed using variable hard thresholds') xlabel(sprintf('Energy retained = %2.1f%% \nNull coefficients = %2.1f%%',perfL2,perf0)) % Return to default settings. dwtmode('zpd')

Page 23: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

22

Chapter 2 MATLAB Functions and Description The wavelet functions used in this material are as follows: Dwt : single-level discrete 1-D wavelet transform. Idwt : single-level inverse discrete 1-D wavelet transform. Upcoef : direct reconstruction from 1-D wavelet coefficients. Wavedec : multi-level 1-D wavelet decomposition. Waverec : multi-level 1-D wavelet reconstruction. Wpdencmp : de-noising or compression using wavelet packet. Appcoef : extract 1-D approximation coefficients. Detcoef : extract 1-D detail coefficients. Wrcoef : reconstruct single branch from 1-D wavelet coefficients. MATLAB data files need in this material are as follows: Woman2.mat Leleccum.mat Detfingr.mat Note : The author used MATLAB version 5.3 release II during developing the codes. Some of the m-files have been tested on version 4.2(available at the computer center). Some might not work on version 4.2 but they can be fixed by void some of these functions. Set(….) Axis tight Axis image Another source to experience wavelet toolbox is typing “wavedemo” in command line of MATLAB. This function demonstrates lots of wavelet packages. To get some information of wavelet function and definition, just type “waveinfo” in the command line.

Page 24: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

23

dwt

Single-level discrete 1-D wavelet transform.

Syntax [cA,cD] = dwt(X,'wname') [cA,cD] = dwt(X,Lo_D,Hi_D)

Description

The dwt command performs a single-level one-dimensional wavelet decomposition with respect to either a particular wavelet ('wname', see wfilters) or particular wavelet decomposition filters (Lo_D and Hi_D) you specify. [cA,cD] = dwt(X,'wname') computes the approximation coefficients vector cA and detail coefficients vector cD, obtained by wavelet decomposition of the vector X. [cA,cD] = dwt(X,Lo_D,Hi_D) computes the wavelet decomposition as above, given these filters as input:

• Lo_D is the decomposition low-pass filter and • Hi_D is the decomposition high-pass filter.

Lo_D and Hi_D must be the same length. If lx is the length of X and lf is the length of the filters Lo_D and Hi_D, then length(cA) = length(cD) = floor((lx+lf-1)/2). For the different signal extension modes, see dwtmode.

Examples

% Construct elementary original one-dimensional signal. randn('seed',531316785) s = 2 + kron(ones(1,8),[1 -1]) + ... ((1:16).^2)/32 + 0.2*randn(1,16); % Perform single-level discrete wavelet transform of s by haar. [ca1,cd1] = dwt(s,'haar'); subplot(311); plot(s); title('Original signal'); subplot(323); plot(ca1); title('Approx. coef. for haar'); subplot(324); plot(cd1); title('Detail coef. for haar'); % For a given wavelet, compute the two associated decomposition % filters and compute approximation and detail coefficients % using directly the filters. [Lo_D,Hi_D] = wfilters('haar','d'); [ca1,cd1] = dwt(s,Lo_D,Hi_D);

Page 25: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

24

% Perform single-level discrete wavelet transform of s by db2 % and observe edge effects for last coefficients. % These extra coefficients are only used to ensure exact % global reconstruction. [ca2,cd2] = dwt(s,'db2'); subplot(325); plot(ca2); title('Approx. coef. for db2'); subplot(326); plot(cd2); title('Detail coef. for db2');

Algorithm Starting from a signal s, two sets of coefficients are computed: approximation coefficients CA1 and detail coefficients CD1. These vectors are obtained by convolving s with the low-pass filter Lo_D for approximation, and with the high-pass filter Hi_D for detail, followed by dyadic decimation. More precisely, the first step is:

Page 26: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

25

The length of each filter is equal to 2N. If n = length(s), the signals F and G are of length n + 2N - 1 and then the coefficients CA1 and CD1 are of length

. Note: In order to deal with signal-end effects involved by convolution based algorithm, a global variable managed by dwtmode is used. The possible options are: zero-padding (used in the previous example, this mode is the default), symmetric extension, and smooth extension. It should be noted that dwt has the same single inverse function idwt for the three extension modes.

Limitations

Periodized wavelet transform is handled separately (see dwtper and idwtper).

See Also

dwtmode, dwtper, idwt, wavedec, waveinfo

References

I. Daubechies (1992), "Ten lectures on wavelets," CBMS-NSF conference series in applied mathematics. SIAM Ed. S. Mallat (1989), "A theory for multiresolution signal decomposition: the wavelet representation," IEEE Pattern Anal. and Machine Intell., vol. 11, no. 7, pp 674-693. Y. Meyer (1990), "Ondelettes et opérateurs," Tome 1, Hermann Ed. (English translation: Wavelets and operators, Cambridge Univ. Press. 1993.)

Page 27: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

26

idwt

Single-level inverse discrete 1-D wavelet transform.

Syntax X = idwt(cA,cD,'wname') X = idwt(cA,cD,Lo_R,Hi_R) X = idwt(cA,cD,'wname',L) X = idwt(cA,cD,Lo_R,Hi_R,L)

Description

The idwt command performs a single-level one-dimensional wavelet reconstruction with respect to either a particular wavelet ('wname', see wfilters) or particular wavelet reconstruction filters (Lo_R and Hi_R) you specify. X = idwt(cA,cD,'wname') returns the single-level reconstructed approximation coefficients vector X based on approximation and detail coefficients vectors cA and cD, and using the wavelet 'wname'. X = idwt(cA,cD,Lo_R,Hi_R)reconstructs as above using filters you specify:

• Lo_R is the reconstruction low-pass filter • Hi_R is the reconstruction high-pass filter

Lo_R and Hi_R must be the same length. If la is the length of cA (which also equals the length of cD) and lf is the length of the filters Lo_R and Hi_R, then length(X) = 2*la-lf+2. X = idwt(cA,cD,'wname',L) or X = idwt(cA,cD,Lo_R,Hi_R,L), returns the length-L central portion of the result obtained using idwt(cA,cD,'wname'). L must be less than 2*la-lf+2.

Examples

idwt is the inverse function of dwt in the sense that the abstract statement idwt(dwt(X,'wname'),'wname') gives back X. Consider this example.

% Construct elementary one-dimensional signal s. randn('seed',531316785) s = 2 + kron(ones(1,8),[1 -1]) + ... ((1:16).^2)/32 + 0.2*randn(1,16); % Perform single-level dwt of s using db2. [ca1,cd1] = dwt(s,'db2'); subplot(221); plot(ca1); title('Approx. coef. for db2'); subplot(222); plot(cd1);

Page 28: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

27

title('Detail coef. for db2'); % Perform single-level inverse discrete wavelet transform, % illustrating that idwt is the inverse function of dwt. ss = idwt(ca1,cd1,'db2'); err = norm(s-ss); % Check reconstruction. subplot(212); plot([s;ss]'); title('Original and reconstructed signals'); xlabel(['Error norm = ',num2str(err)]) % For a given wavelet, compute the two associated % reconstruction filters and inverse transform using % the filters directly. [Lo_R,Hi_R] = wfilters('db2','r'); ss = idwt(ca1,cd1,Lo_R,Hi_R);

Algorithm

Starting from the approximation and detail coefficients at level j, cAj and cDj, the inverse discrete wavelet transform reconstructs cAj-1, inverting the decomposition step by inserting zeros and convolving the results with the reconstruction filters.

Page 29: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

28

See Also

dwt, idwtper, upwlev

References I. Daubechies (1992), "Ten lectures on wavelets," CBMS-NSF conference series in applied mathematics. SIAM Ed. S. Mallat (1989), "A theory for multiresolution signal decomposition: the wavelet representation," IEEE Pattern Anal. and Machine Intell., vol. 11, no. 7, pp 674-693. Y. Meyer (1990), "Ondelettes et opérateurs," Tome 1, Hermann Ed. (English translation: Wavelets and operators, Cambridge Univ. Press. 1993.)

Page 30: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

29

upcoef

Direct reconstruction from 1-D wavelet coefficients.

Syntax Y = upcoef(O,X,'wname',N) Y = upcoef(O,X,'wname',N,L) Y = upcoef(O,X,Lo_R,Hi_R,N) Y = upcoef(O,X,Lo_R,Hi_R,N,L) Y = upcoef(O,X,'wname') Y = upcoef(O,X,Lo_R,Hi_R)

Description

upcoef is a one-dimensional wavelet analysis function. Y = upcoef(O,X,'wname',N) computes the N steps reconstructed coefficients of vector X. 'wname' is a string containing the wavelet name. N must be a strictly positive integer. If O = 'a', approximation coefficients are reconstructed. If O = 'd', detail coefficients are reconstructed. Y = upcoef(O,X,'wname',N,L) computes the N steps reconstructed coefficients of vector X and takes the length-L central portion of the result. Instead of giving the wavelet name, you can give the filters. For Y = upcoef(O,X,Lo_R,Hi_R,N) or Y = upcoef(O,X,Lo_R,Hi_R,N,L), Lo_R is the reconstruction low-pass filter and Hi_R is the reconstruction high-pass filter. Y = upcoef(O,X,'wname') is equivalent to Y = upcoef(O,X,'wname',1). Y = upcoef(O,X,Lo_R,Hi_R) is equivalent to Y = upcoef(O,X,Lo_R,Hi_R,1).

Examples

% Approximation signals, obtained from a single coefficient % at levels 1 to 6. cfs = [1]; % Decomposition reduced a single coefficient. essup = 10; % Essential support of the scaling filter db6. figure(1) for i=1:6 % Reconstruct at the top level an approximation % which is equal to zero except at level i where only % one coefficient is equal to 1. rec = upcoef('a',cfs,'db6',i); % essup is the essential support of the % reconstructed signal. subplot(6,1,i),h = plot(rec(1:essup)); set(get(h,'parent'),'xlim',[1 325]);

Page 31: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

30

essup = essup*2; end subplot(611) title(['Approximation signals, obtained from a single ' ... 'coefficient at levels 1 to 6'])

% The same can be done for details. % Details signals, obtained from a single coefficient % at levels 1 to 6. cfs = [1]; mi = 12; ma = 30; % Essential support of % the wavelet filter db6. rec = upcoef('d',cfs,'db6',1); figure(2) subplot(611), plot(rec(3:12)) for i=2:6 % Reconstruct at top level a single detail % coefficient at level i. rec = upcoef('d',cfs,'db6',i); subplot(6,1,i), plot(rec(mi*2^(i-2):ma*2^(i-2))) end subplot(611) title(['Detail signals obtained from a single ' ... 'coefficient at levels 1 to 6'])

Page 32: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

31

Algorithm

upcoef is equivalent to the N times repeated use of the inverse wavelet transform.

See Also

idwt

Page 33: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

32

wavedec

Multi-level 1-D wavelet decomposition.

Syntax [C,L] = wavedec(X,N,'wname') [C,L] = wavedec(X,N,Lo_D,Hi_D)

Description

wavedec performs a multi-level one-dimensional wavelet analysis using either a specific wavelet ('wname', see wfilters) or specific wavelet decomposition filters (Lo_D and Hi_D). [C,L] = wavedec(X,N,'wname') returns the wavelet decomposition of the signal X at level N, using 'wname'. N must be a strictly positive integer (see wmaxlev). The output decomposition structure contains the wavelet decomposition vector C and bookkeeping vector L. The structure is organized as in this level-3 decomposition example:

[C,L] = wavedec(X,N,Lo_D,Hi_D) returns the decomposition structure as above, given the low- and high-pass decomposition filters you specify.

Examples

% Load original one-dimensional signal. load sumsin; s = sumsin; % Perform decomposition at level 3 of s using db1.

Page 34: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

33

[c,l] = wavedec(s,3,'db1');

Algorithm Given a signal s of length N, the DWT consists of log2 N stages at most. The first step produces, starting from s, two sets of coefficients: approximation coefficients CA1 and detail coefficients CD1. These vectors are obtained by convolving s with the low-pass filter Lo_D for approximation, and with the high-pass filter Hi_D for detail, followed by dyadic decimation (downsampling). More precisely, the first step is:

Page 35: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

34

The length of each filter is equal to 2N. If n = length(s), the signals F and G are of length n + 2N - 1 and the coefficients cA1 and cD1 are of length

. The next step splits the approximation coefficients cA1 in two parts using the same scheme, replacing s by cA1, and producing cA2 and cD2, and so on.

The wavelet decomposition of the signal s analyzed at level j has the following structure: [cAj, cDj, ..., cD1].

This structure contains, for J = 3, the terminal nodes of the following tree:

See Also

dwt, waveinfo, wfilters, wmaxlev

Page 36: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

35

waverec

Multi-level 1-D wavelet reconstruction.

Syntax

X = waverec(C,L,'wname') X = waverec(C,L,Lo_R,Hi_R)

Description

waverec performs a multi-level one-dimensional wavelet reconstruction using either a specific wavelet ('wname', see wfilters) or specific reconstruction filters (Lo_R and Hi_R). waverec is the inverse function of wavedec in the sense that the abstract statement waverec(wavedec(X,N,'wname'),'wname') returns X. X = waverec(C,L,'wname') reconstructs the signal X based on the multi-level wavelet decomposition structure [C,L] and wavelet 'wname'. (For information about the decomposition structure, see wavedec.) X = waverec(C,L,Lo_R,Hi_R) reconstructs the signal X as above, using the reconstruction filters you specify.

Remarks

Note that X = waverec(C,L,'wname') is equivalent to X = appcoef(C,L,'wname',0).

Examples

% Load original one-dimensional signal. load leleccum; s = leleccum(1:3920); ls = length(s); % Perform decomposition of signal at level 3 using db5. [c,l] = wavedec(s,3,'db5'); % Reconstruct s from the wavelet decomposition structure [c,l]. a0 = waverec(c,l,'db5'); % Check for perfect reconstruction. err = norm(s-a0) err = 3.2079e-09

See Also appcoef, idwt, wavedec

Page 37: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

36

wpdencmp

De-noising or compression using wavelet packet.

Syntax

[XD,TREED,DATAD,PERF0,PERFL2] = wpdencmp(X,SORH,N,'wname',CRIT,PAR,KEEPAPP) [XD,TREED,DATAD,PERF0,PERFL2] = wpdencmp(TREE,DATA,SORH,CRIT,PAR,KEEPAPP)

Description

wpdencmp is a one- or two-dimensional de-noising and compression oriented function. wpdencmp performs a de-noising or compression process of a signal or an image, using wavelet packet. The ideas and the procedures for de-noising and compression using wavelet packet are the same as those used in the wavelets framework (see wden and wdencmp). [XD,TREED,DATAD,PERF0,PERFL2] =

wpdencmp(X,SORH,N,'wname',CRIT,PAR,KEEPAPP) returns a de-noised or compressed version XD of input signal X (one- or two-dimensional) obtained by wavelet packet coefficients thresholding. Additional output arguments [TREED,DATAD] are the wavelet packet best decomposition structure (see besttree) of XD. PERFL2 and PERF0 are L2 recovery and compression scores in percentages. PERFL2 = 100 * (vector-norm of WP-cfs of XD / vector-norm of WP-cfs of X)2. If X is a one-dimensional signal and 'wname' an orthogonal wavelet, PERFL2 is reduced to

. SORH ('s' or 'h') is for soft or hard thresholding (see wthresh for more details). Wavelet packet decomposition is performed at level N and 'wname' is a string containing wavelet name. Best decomposition is performed using entropy criterion defined by string CRIT and parameter PAR (see wentropy for details). Threshold parameter is also PAR. If KEEPAPP = 1, approximation coefficients cannot be thresholded, otherwise it is possible. [XD,TREED,DATAD,PERF0,PERFL2] =

wpdencmp(TREE,DATA,SORH,CRIT,PAR,KEEPAPP)has the same output arguments, using the same options as above, but obtained directly from the input wavelet packet decomposition structure [TREE,DATA] (see maketree and wpdec) of the signal to be de-noised or compressed.

Page 38: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

37

In addition if CRIT = 'nobest' no optimization is done and the current decomposition is thresholded.

Examples

% Load original signal. load sumlichr; x = sumlichr; % Use wpdencmp for signal compression. % find default values (see ddencmp). [thr,sorh,keepapp,crit] = ddencmp('cmp','wp',x) thr = 0.5193 sorh = h keepapp = 1 crit = threshold % Denoise signal using global thresholding with % threshold best basis. [xc,treed,datad,perf0,perfl2] = ... wpdencmp(x,sorh,3,'db2',crit,thr,keepapp);

Page 39: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

38

% Load original image. load sinsin % Generate noisy image. init = 2055615866; randn('seed',init); x = X/18 + randn(size(X)); % Use wpdencmp for image de-noising. % find default values (see ddencmp). [thr,sorh,keepapp,crit] = ddencmp('den','wp',x) thr = 4.9685 sorh = h keepapp = 1 crit = sure % Denoise image using global thresholding with % SURE best basis. xd = wpdencmp(x,sorh,3,'sym4',crit,thr,keepapp);

% Generate heavy sine and a noisy version of it. [xref,x] = wnoise(5,11,7,init); % Use wpdencmp for signal de-noising. n = length(x); thr = sqrt(2*log(n*log(n)/log(2)));

Page 40: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

39

xwpd = wpdencmp(x,'s',4,'sym4','sure',thr,1); % Compare with wavelet-based de-noising result. xwd = wden(x,'rigrsure','s','one',4,'sym4');

See Also

ddencmp, wdencmp, wentropy, wpdec, wpdec2

References A. Antoniadis, G. Oppenheim, Eds. (1995), "Wavelets and statistics," Lecture Notes in Statistics, 103, Springer Verlag. R.R. Coifman, M.V. Wickerhauser, (1992), "Entropy-based algorithms for best basis selection," IEEE Trans. on Inf. Theory, vol. 38, 2, pp. 713-718. R.A. DeVore, B. Jawerth, B.J. Lucier (1992), "Image compression through wavelet transform coding," IEEE Trans. on Inf. Theory, vol. 38, No 2, pp. 719-746. D.L. Donoho (1993), "Progress in wavelet analysis and WVD: a ten minute tour," in Progress in wavelet analysis and applications, Y. Meyer, S. Roques, pp. 109-128. Frontières Ed. D.L. Donoho, I.M. Johnstone(1994), "Ideal spatial adaptation by wavelet shrinkage," Biometrika, vol 81, pp. 425-455. D.L. Donoho, I.M. Johnstone, G. Kerkyacharian, D. Picard (1995), "Wavelet shrinkage: asymptopia," Jour. Roy. Stat. Soc., series B, vol. 57 no. 2, pp. 301-369.

Page 41: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

40

appcoef

Extract 1-D approximation coefficients.

Syntax

A = appcoef(C,L,'wname',N) A = appcoef(C,L,'wname') A = appcoef(C,L,Lo_R,Hi_R) A = appcoef(C,L,Lo_R,Hi_R,N)

Description

appcoef is a one-dimensional wavelet analysis function. appcoef computes the approximation coefficients of a one-dimensional signal. A = appcoef(C,L,'wname',N) computes the approximation coefficients at level N using the wavelet decomposition structure [C,L] (see wavedec). 'wname' is a string containing the wavelet name. Level N must be an integer such that 0 <= N <= length(L)-2. A = appcoef(C,L,'wname') extracts the approximation coefficients at the last level length(L)-2. Instead of giving the wavelet name, you can give the filters. For A = appcoef(C,L,Lo_R,Hi_R) or A = appcoef(C,L,Lo_R,Hi_R,N), Lo_R is the reconstruction low-pass filter and Hi_R is the reconstruction high-pass filter.

Examples

% Load original one-dimensional signal. load leleccum; s = leleccum(1:3920); ls = length(s); % Perform decomposition at level 3 of s using db1. [c,l] = wavedec(s,3,'db1'); % Extract approximation coefficients at level 3, from the % wavelet decomposition structure [c,l]. ca3 = appcoef(c,l,'db1',3);

Page 42: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

41

Algorithm

The input vectors C and L contain all the information about the signal decomposition. Let NMAX = length(L)-2, then C = [A(NMAX) D(NMAX) ... D(1)], where A and the D are vectors. If N = NMAX a simple extraction is done, otherwise appcoef computes iteratively the approximation coefficients using the inverse wavelet transform.

See Also

detcoef, wavedec

Page 43: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

42

detcoef

Extract 1-D detail coefficients.

Syntax D = detcoef(C,L,N) D = detcoef(C,L)

Description

detcoef is a one-dimensional wavelet analysis function. D = detcoef(C,L,N) extracts the detail coefficients at level N from the wavelet decomposition structure [C,L] (see wavedec). Level N must be an integer such that 1 <= N <= length(L)-2. D = detcoef(C,L) extracts the detail coefficients at last level n=length(L)-2.

Examples

% Load original one-dimensional signal. load leleccum; s = leleccum(1:3920); ls = length(s); % Perform decomposition at level 3 of s using db1. [c,l] = wavedec(s,3,'db1'); % Extract detail coefficients at levels % 1, 2 and 3, from wavelet decomposition % structure [c,l]. cd3 = detcoef(c,l,3); cd2 = detcoef(c,l,2); cd1 = detcoef(c,l,1);

Page 44: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

43

See Also

appcoef, wavedec

Page 45: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

44

wrcoef

Reconstruct single branch from 1-D wavelet coefficients.

Syntax X = wrcoef('type',C,L,'wname',N) X = wrcoef('type',C,L,Lo_R,Hi_R,N) X = wrcoef('type',C,L,'wname') X = wrcoef('type',C,L,Lo_R,Hi_R)

Description

wrcoef reconstructs the coefficients of a one-dimensional signal, given a wavelet decomposition structure (C and L) and either a specified wavelet ('wname', see wfilters) or specified reconstruction filters (Lo_R and Hi_R). X = wrcoef('type',C,L,'wname',N) computes the vector of reconstructed coefficients, based on the wavelet decomposition structure [C,L] (see wavedec), at level N. Argument 'type' determines whether approximation ('type' = 'a') or detail ('type' = 'd') coefficients are reconstructed. When 'type' = 'a', N is allowed to be 0, otherwise strictly positive N is required. Level N must be an integer such that N <= length(L)-2. X = wrcoef('type',C,L,Lo_R,Hi_R,N)computes coefficients as above, given the reconstruction filters you specify. X = wrcoef('type',C,L,'wname') and X = wrcoef('type',C,L,Lo_R,Hi_R) reconstruct coefficients of maximum level N = length(L)-2.

Examples

% Load original one-dimensional signal. load sumsin; s = sumsin; % Perform decomposition at level 5 of s using sym4. [c,l] = wavedec(s,5,'sym4'); % Reconstruct approximation at level 5, % from the wavelet decomposition structure [c,l]. a5 = wrcoef('a',c,l,'sym4',5);

Page 46: An Introduction to Wavelets Toolbox and its Applicationsohhohe.tripod.com/files/Project.pdf · An Introduction to Wavelets Toolbox and its Applications EE670N: Wavelets Spring 2000

45

See Also

appcoef, detcoef, wavedec