% ShowHaarBasis
close all
% Display the Haar basis functions (n = 8):
H8 = [1 1 1 0 1 0 0 0;...
1 1 1 0 -1 0 0 0;...
1 1 -1 0 0 1 0 0;...
1 1 -1 0 0 -1 0 0;...
1 -1 0 1 0 0 1 0;...
1 -1 0 1 0 0 -1 0;...
1 -1 0 -1 0 0 0 1;...
1 -1 0 -1 0 0 0 -1];

for k=1:8
figure
v = H8(:,k)';
x = [ 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 ];
y = [ 0 v(1) v(1) v(2) v(2) v(3) v(3) v(4) v(4) v(5) v(5) v(6) v(6) v(7) v(7) v(8) v(8) 0 ];
plot([-1 9],[0 0])
hold on
plot(x,y,'m','linewidth',3)
axis([-1 9 -1.5 1.5])
hold off
title(sprintf('Haar Basis Function h_{%1d}(x)',k),'fontsize',16)
shg
pause
end
% Suppose f(x) is defined on [0,8] and that we want to interpolate its value
% at x = [0.5 1.5 2.5 3.5 4.5 6.5 7.5] with the following function:
% p(z) = a(1)*h_{1}(x) + ...
% a(2)*h_{2}(x) + ...
% a(3)*h_{3}(x) + ...
% a(4)*h_{4}(x) + ...
% a(5)*h_{5}(x) + ...
% a(6)*h_{6}(x) + ...
% a(7)*h_{7}(x) + ...
% a(8)*h_{8}(x)
% If y(i) = f(x(i)), then the unknowns a(1),...,a(8) solve a linear system
% H8*a = y where H8(i,j) = h_{j}(x(i)).

% Here is H8...

H8 = [1 1 1 0 1 0 0 0;...
1 1 1 0 -1 0 0 0;...
1 1 -1 0 0 1 0 0;...
1 1 -1 0 0 -1 0 0;...
1 -1 0 1 0 0 1 0;...
1 -1 0 1 0 0 -1 0;...
1 -1 0 -1 0 0 0 1;...
1 -1 0 -1 0 0 0 -1];

% Let's revisit the Haar basis functions and indicate where they are
% evaluated in this interpolation problem..
for k=1:8
figure(k)
xlabel(sprintf('Function values reported in H8(:,%1d)',k),'fontsize',16)
hold on
plot(linspace(.5,7.5,8),H8(:,k)','.r','markersize',25)
hold off
shg
pause
end