%% Haar Transform 1D % % *Wavelets Workshop* % % June 4-7, 2008 % % University of St. Thomas % % *Catherine Beneteau*, *Caroline Haddad*, *David Ruch*, *Patrick Van Fleet* %% Objectives % In this m-file, we will explore the one-dimensional discrete Haar % wavelet transform. %% Conventions % This m-file uses the DiscreteWavelets Toolbox. Help is available on all % functions in the toolbox by accessing the Help menu and clicking on the % DiscreteWavelets Toolbox. % % Demos are also available. Click the Demos tab under Help to access all % demos. %% The One-Dimensional Haar Transform % Our first task is to write a function to compute one iteration of the % one-dimensional Haar wavelet transformation. % % Find the DiscreteWavelets folder and look inside it for the folder % MyFiles. In MyFiles, find and open MyHWT1D1.m. Put your code for % computing one iteration of the one-dimensional HWT in this file. Save it % and exit. %% Using the Haar Wavelet Transform % It is easy to use the HaarWT function x=1:8 y=MyHWT1D1(x); y' %% % What should MyHWT1D1 do to a constant vector? x=ones(1,10); y=MyHWT1D1(x); y' %% % Here is an example that introduces a command we can use to plot the % wavelet transform. x=1:100; v=x.^2; y=MyHWT1D1(v); plot(v); figure; WaveletVectorPlot(y,1,'UseColors','True'); %% % Plot the differences only WaveletVectorPlot(y,1,'Region','HighPass','Iteration',1,'UseColors','True'); %% One-Dimensional Inverse Haar Wavelet Transformation % % Find the DiscreteWavelets folder and look inside it for the folder % MyFiles. In MyFiles, find and open MyIHWT1D1.m. Put your code for % computing one iteration of the one-dimensional HWT in this file. Save it % and exit. %% Using MyIHWT1D1 % The cell below contain illustrates the use of IHWT1D. Feel free to % change the input vector x. For something different, I've formed x as a % list of 20 random integers whose values range from 0 to 100. x = round(100*rand(1,20)) y = MyHWT1D1(x)' newx = MyIHWT1D1(y)' %% An Illuminating Example for Students % I use this example every time I introduce the Haar wavelet transform. % The student can create a basic function from Calculus I (some examples % are provided). The function is then sampled 100 times from a to b. % This creates a list x that we plot. Note the x-axis on the plot is in % terms of the list elements while the y-axis is in terms of the list % values. % % Feel free to uncomment other functions or define your own. n=100; t=0:1/n:1-1/n; v=sin(2*pi*t); %v=cos(2*pi*t); %v=t.^2; %v=t; %v=1; %v=exp(t); %v=log(1+t); plot(v); %% % Next compute the HWT of x. The first plot is the entire transform. % The second plot shows only the approximation portion of the transform % while the third plot shows only the differences portion of the transform. % % How are the second and third plots related? Try a couple of other % functions. y = MyHWT1D1(v); figure; WaveletVectorPlot(y,1); title('HWT'); figure; WaveletVectorPlot(y,1,'Region','LowPass'); title('Averages'); figure; WaveletVectorPlot(y,1,'Region','HighPass') title('Differences'); %% Iterating the Process % In many applications, scientists will iterate the wavelet transform. % Suppose you are given n-vector x and you compute the HWT y. The next % iteration of transform is applied to the approximation portion of the y. % Here is some code (bulky) to do the job. Note that to iterate a second % time requires n to be divisible by 4. x = [2 4 6 8 10 12 14 16]; n = length(x); y = MyHWT1D1(x) approx = y(1:n/2); diff = y(n/2+1:end); z = MyHWT1D1(approx); iterated = [z; diff]' %% % In general, if the length of x contains the factor 2^i, we can iterate i % times. There is a routine in DiscreteWavelets for computing the % iterated Haar Wavelet Transform. It is called HWT1D. It needs two % arguments. The first is the input vector. The second input is the % number of iterations. % % Here is an example. Note that the length of the input vector is % divisible by 32 so we can perform as many as 5 iterations. n = 96; x = (0:n-1)/n; y = HWT1D(x,2); close all; WaveletVectorPlot(y,2) %% % Here is three iterations of the transform. Note the approximation % portion contains 96/2^3 = 12 elements. y = HWT1D(x,3); WaveletVectorPlot(y,3); %% % You can look at particular portions of the transform. WaveletVectorPlot(y,3,'Iteration',2,'Region','HighPass'); figure; WaveletVectorPlot(y,3,'Region','LowPass'); %% % The process is invertible using the IHWT1D routine from DiscreteWavelets. % Like WT1D, the routine requires two arguments. The first is the input % vector and the second is the number of iterations. x = round(100*rand(1,32)) y = HWT1D(x,5); orig = IHWT1D(y,5)' %% Orthogonalizing the Transform % In the early development of wavelets, the transforms were designed to be % orthogonal matrices. Consider the Haar Wavelet transform matrix of size % 8 x 8 and its inverse: W=[.5 .5 0 0 0 0 0 0; 0 0 .5 .5 0 0 0 0; 0 0 0 0 .5 .5 0 0; 0 0 0 0 0 0 ... .5 .5; -.5 .5 0 0 0 0 0 0; 0 0 -.5 .5 0 0 0 0; 0 0 0 0 -.5 .5 0 0;... 0 0 0 0 0 0 -.5 .5] inv(W) disp('How can we modify the matrix so that it is orthogonal?'); %% % When we use orthogonal transformations, we can use the same list as the % second arguments for WT1D and IWT1D. There is a command in % DiscreteWavelets for generating this list: Haar() %% close all; displayEndOfDemoMessage(mfilename)