%% Example 3.14
% This uses the same signal as Example 3.8. 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*(((1:16)-1)/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)')

%
% I compute the three-scale Daub4 transform, using the command daub4mat from Computer Exercise 3.7.3.  I then apply it to x and use the result to plot the trend and details. I call the Daub4 analysis matrix W3.
% Note: I made no attempt to be efficient in the computations

T6 = daub4mat(64); T5 = [daub4mat(32) zeros(32); zeros(32) eye(32)]; 
T4 = [daub4mat(16) zeros(16,48); zeros(16) eye(16) zeros(16,32); zeros(32) eye(32)];
W3 = T4*T5*T6;
w = W3*x'; s3 = w(1:8); d3 = w(9:16); d2 = w(17:32); d1 = w(33:64);

%
% Here are plots of the trend and detail coefficients of the Daub4 transform of x. They differ somewhat from the plots in the book. This probably comes from slightly different definitions of the signal at the end points and/or at the discontinuity.

subplot(4,1,1)
plot(s3,'o')
hold on
for j = 1:8
    plot([j,j],[0,s3(j)],'g')
end
hold off
title('coarse trend coefficients')
subplot(4,1,2)
plot(d3,'o')
hold on
for j = 1:8  
plot([j,j],[0,d3(j)],'g')
end
hold off
title('coarse detail coefficients')
axis([0 8 -0.5 0.5])
subplot(4,1,3)
plot(d2,'o')
hold on
for j = 1:16
    plot([j,j],[0,d2(j)],'g')
end
hold off
title('medium detail coefficients')
axis([0 16 -0.5 0.5])
xticks(0:2:16)
yticks(-0.5:0.5:0.5)
subplot(4,1,4)
plot(d1,'o')
hold on
for j = 1:32
    plot([j,j],[0,d1(j)],'g')
end
hold off
title('fine detail coefficients')

%
%
%
%
% The three-scale synthesis matrix is the transpose of the analysis matrix. I call it W3s.

W3s = W3';

%
% The coarse trend vector is the result of applying the first 8 columns of the synthesis matrix to s3. I compute and plot it.

figure
xct = W3s(:,1:8)*s3;  
plot(xct)
title('coarse trend projection')
axis([0 64 -0.2 1.2])

%
% The coarse detail vector is obtained by applying the next 8 columns to d3.

xcd = W3s(:,9:16)*d3;  
plot(xcd)
title('coarse detail projection')
axis([0 64 -0.2 0.2])

%
% The medium detail vector is obtained by applying the next 16 columns to d2.

xmd = W3s(:,17:32)*d2;  
plot(xmd)
title('medium detail projection')
axis([0 64 -0.2 0.2])

%
% The fine detail vector is obtained by applying the next 16 columns to d2.

xfd = W3s(:,33:64)*d1;  
plot(xfd)
title('fine detail projection')
axis([0 64 -0.2 0.2])

%
% I check that x is the sum of the four projections.

norm(x' - (xct+xcd+xmd+xfd))

% The norm of the difference is essentially 0.
%
% I compute the norm squared of each component and of x.

xn2 = norm(x)^2

%

xctn2=norm(xct)^2

%

xcdn2=norm(xcd)^2

%

xmdn2=norm(xmd)^2

%

xfdn2=norm(xfd)^2

%
% I compute the sum of the  norm squared of the components and notice that it equals the norm squared of x. This reflects the orthogonality of the Daub4 transform.

xctn2+xcdn2+xmdn2+xfdn2

%
% Notice that most of the norm comes from the coarse trend, which is a rough version of the original signal. Again, my values are slightly different form the ones in the book.
%
%