% E03_6.m                  16oct15   29mar21
% "energy compaction" ability of an elementary Haar transform
%
clear all;
xx = randn(1,1000)*10;
figure(1), subplot(121), plot(xcorr(xx));
alpha = .8;           % try with [.1, .9999]
xx = filter(1-alpha,[1  -alpha], xx);
subplot(122), plot(xcorr(xx));
return
%%
x1 = xx(1:2:end)';   % generate 500 realizations of a
x2 = xx(2:2:end)';   % length=2 correlated sequence x = [x1,x2]
average_energy_of_x1_and_of_x2 = mean([x1.^2  x2.^2])
figure(2); plot(x1,x2,'o'); a_xy=axis;
return
%%   Walsh-Hadamard or Haar transform X = [X1,X2]
X1 = (x1+x2)/sqrt(2);
X2 = (x1-x2)/sqrt(2);
average_energy_of_X1_and_of_X2 = mean([X1.^2  X2.^2])
close(1), figure(1), plot(X1,X2,'o'); axis(a_xy);

x1h = (X1+X2)/sqrt(2);  % perfect reconstruction
x2h = (X1-X2)/sqrt(2);
figure(2), plot(x1h,x2h,'o'); axis(a_xy);

x1hL = (X1+0)/sqrt(2);  % lossy reconstruction
x2hL = (X1-0)/sqrt(2);
figure(3), plot(x1hL,x2hL,'o'); axis(a_xy);

mse_perfect = mean(mean(([x1h x2h] - [x1 x2]).^2))
mse_lossy = mean(mean(([x1hL x1hL] - [x1 x2]).^2))
mse_lossy_orig_1 = mean(mean(([x1 zeros(size(x1))] - [x1 x2]).^2))
mse_lossy_orig_2 = mean(mean(([zeros(size(x1)) x2] - [x1 x2]).^2))
mse_lossy_orig_duplicate = mean(mean(([x1 x1] - [x1 x2]).^2))
                % same as: mean(mean(([x2 x2] - [x1 x2]).^2))


