echo on
%
%  Demonstration of discrete Haar transform (DHT)
%
%  Example 1.  The characteristic function of [0,9/16).
%  Define and plot the function.
%
t=0:1/256:255/256;
f1=[ones(1,144),zeros(1,112)];
pause
plot(t,f1);
axis([0 1 -.1 1.1]);
pause 
stairs(t,f1);
axis([0 1 -.1 1.1]);
pause
%
%  Now prepare the function for the discrete Haar transform.
% 
c0=f1/16;
%
%  Note that c0 now contains the scale 8 (note that 256=2^8) 
%  Haar scaling coefficients.  We divide by 16 because p_{8,k} has 
%  height 2^4 and width 2^(-8).  Thus, c0(k)= <f1,p_{8,k}>.  
%
pause
%
%  Now calculate the discrete Haar decomposition of c0, one step at 
%  a time.
%
[c1,d1]=dwt(c0,'haar');
stem(c1);
pause
stem(d1);
pause

[c2,d2]=dwt(c1,'haar');
stem(c2);
pause
stem(d2);
pause

[c3,d3]=dwt(c2,'haar');
stem(c3);
pause
stem(d3);
pause

[c4,d4]=dwt(c3,'haar');
stem(c4);
pause
stem(d4);
pause

[c5,d5]=dwt(c4,'haar');
stem(c5);
pause
stem(d5);
pause

[c6,d6]=dwt(c5,'haar');
stem(c6);
pause
stem(d6);
pause

[c7,d7]=dwt(c6,'haar');
stem(c7);
pause
stem(d7);
pause

[c8,d8]=dwt(c7,'haar');
stem(c8);
pause
stem(d8);
pause

%
%  Example 2.  The characteristic function of [0,2/3).
%
t=0:1/256:255/256;
f2=[ones(1,171),zeros(1,85)];
pause
plot(t,f2);
axis([0 1 -.1 1.1]);
pause 
stairs(t,f2);
axis([0 1 -.1 1.1]);
pause
%
%  Now prepare the function for the discrete Haar transform.
% 
c0=f1/16;
pause
%
%  Now compute wavelet decomposition, all at once.
%
pause
[c,l]=wavedec(f2,8,'haar');
pause
%
%  We can recover portions of the Haar decomposition 
%  using appcoef and detcoef.
%
c2=appcoef(c,l,'haar',2);
stem(c2);
pause
d2=detcoef(c,l,2);
stem(d2)
pause

c6=appcoef(c,l,'haar',6);
stem(c6);
pause
d6=detcoef(c,l,6);
stem(d6)
pause



