%% Example 3.8 Comparison of Haar, CDF(2,2) and Daub transforms
% I sample the analog signal f(t) at 64 points where
% f\left(t\right)=16t^2 \text{  }for\text{ }0\text{ }\le t\le \frac{1}{4}, 1 for \frac{1}{4\text{ }}\le t\text{ }\le \frac{1}{2},\text{ }\frac{1}{2}for\text{ }\frac{1}{2}\le t\le \frac{3}{4},and\text{ }2-2t\text{ }for\text{ }\frac{3}{4}\le t\le 1

x = [16*((0:15)/64).^2 ones(1,16) ones(1,16)/2 (2*ones(1,16)-2*(49:64)/64)];
plot(x)
axis([0 64 -0.2 1.2])
xticks([0:16:64])
title('Signal of length 64 (quadratic, step, linear)')

%
% Next I compute the one-scale Haar transform using the command dwthaar from Computer Exercise 3.7.4 and plot it.

y = dwthaar(x);
plot(y)
axis([0 64 -0.2 1.2])
xticks([0:8:64])
annotation('doublearrow','x',[.13,.51],'y',[.3,.3])
text(12,.2,'trend')
annotation('doublearrow','x',[.51,.9],'y',[.3,.3])
text(45,.2,'detail')
title('One-scale Haar transform')

%
% The trend vector is from 0 to 31 and the detail is from 32 to 63. Notice that the trend looks quite similar to the original signal.
%
% Next I compute the CDF(2,2) transform using the command cdfamat from Computer Exercise 3.7.2.

z = cdfamat(64)*x';
plot(z')
axis([0 64 -0.2 1.6])
xticks([0:8:64])
annotation('doublearrow','x',[.13,.51],'y',[.3,.3])
text(12,.3,'trend')
annotation('doublearrow','x',[.51,.9],'y',[.3,.3])
text(45,.3,'detail')
title('One-scale CDF(2,2) transform')

%
% Notice that the trend vector is distorted around n=16. This reflects the jump discontinuity of f at t=\frac{1}{2}. The derivative of f has a jump at\text{ }t=\frac{3}{4} so n=48. This causes the blip you see in the detail vector at 48. You don't see this in the Haar transform.
%
% Finally, I compute the Daub4 transform, using the command daub4mat from Computer Exercise 3.7.3.

w = daub4mat(64)*x';
plot(w')
axis([0 64 -0.2 1.6])
xticks([0:8:64])
annotation('doublearrow','x',[.13,.51],'y',[.3,.3])
text(12,.3,'trend')
annotation('doublearrow','x',[.51,.9],'y',[.3,.3])
text(45,.3,'detail')
title('One-scale Daub4 transform')

%
% Again there's some distortion in the trend vector around n=16. This jump discontinuity also appears in the detail vector of the Daub4 transform, at about n=40. Again, the slope discontinuity appears at in the detail vector at n=48.
%
% Notice that all three transforms give information about the location in time of the singullaities. The CDF(2,2) transform has more information than the Haar transform and the Daub4 transform has the most information.