%% Example 3.13 - CDF(2,2) transform, N=8
% We put the normalization in the synthesis matrix.

format rational
Ta = 4*sqrt(2)*cdfamat(8);
Ts = cdfsmat(8)/(4*sqrt(2));

%
% I plot the basis for the trend subspace.

u0t = Ts(:,1);
S = shift(8);
plot(u0t), hold on
plot(S^2*u0t)
plot(S^4*u0t)
plot(S^6*u0t)
hold off
title('Basis for CDF(2,2) trend subspace, N=8')
xticklabels({'0','1','2','3','4','5','6','7'})

%
% I plot the basis for the detail subspace.

v0t = Ts(:,5);
S = shift(8);
plot(v0t), hold on
plot(S^2*v0t)
plot(S^4*v0t)
plot(S^6*v0t)
hold off
title('Basis for CDF(2,2) detail subspace, N=8')
xticklabels({'0','1','2','3','4','5','6','7'})

%
%
% I decompose the signal x={\left\lbrack 0\text{ }1\text{ }2\text{ }3\text{ }4\text{ }5\text{ }6\text{ }7\right\rbrack }^T into its projections onto the trend and detail subspaces. First we look at the basis vectors for the trend and detail subspaces.

x = [0 1 2 3 4 5 6 7]';
y = Ta*x

%
% The trend is

s = y(1:4)

% and the detail is

d = y(5:8)

%
% The projection of x  onto the trend subspace is

xs = Ts*[s;zeros(4,1)]

%
% The projection onto the detail subspace is

xd = Ts*[zeros(4,1);d]

%
% You see that x=xs+xd.
%
% I plot the signal as a solid line and its trend projection as a dashed line.

plot(x), hold on
plot(xs,'--')
xticklabels({'0','1','2','3','4','5','6','7'})

%
% The relative compression error in approximating x by xs is

format short
norm(x-xs)^2/norm(x)^2