% Tests the Walsh Hadamard transform.
%
% Course in Wavelets, TU Chemnitz, 2012

% Simple Test
%y=[322.5;2.0;1.5;-33.0;-44.5;-15.0;-16.5;13.0;87.0;-22.5;-18.0;28.5;32.0;12.5;0.0;-17.5];
y=[429.25; -50.75; -13.75; 45.25; 46.75; -26.25; -73.25; 12.75; -1.75; -47.75; -20.75; 46.25; 57.75; -33.25; -60.25; 21.75];
disp(sprintf(char(round(walsh(y,'matrix')))'));

% Timings and Accuracy
lmax=15;

t=inf*ones(lmax,3);
e=inf*ones(lmax,3);

for l=1:lmax
  fprintf('l=%d, ',l);
  x=rand(2^l,1);

  if(l<=12)
    tic
    y=walsh(x,'matrix');
    t(l,1)=toc;
    x2=walsh(y,'matrix');
    e(l,1)=norm(x-x2)/norm(x);
  end;

  tic
  y=walsh(x,'sparseProd');
  t(l,2)=toc;
  x2=walsh(y,'sparseProd');
  e(l,2)=norm(x-x2)/norm(x);

  tic
  y=walsh(x,'recursive');
  t(l,3)=toc;
  x2=walsh(y,'recursive');
  e(l,3)=norm(x-x2)/norm(x);
end;

fprintf('\n\n');

l=(1:lmax)';
n=2.^l;

figure(1);
tmin=min(t(:));
loglog(n,t(:,1),n,t(:,2),n,t(:,3),n,tmin*n.^2,n,tmin*n.*l,n,tmin*n);
legend('matrix','sparseProd','recursive','n^2','nlogn','n','Location','NorthWest')

print hausaufgabe_1.pdf -dpdf

figure(2);
loglog(n,e(:,1),n,e(:,2),n,e(:,3));
legend('matrix','sparseProd','recursive','Location','NorthWest');

print hausaufgabe1_2.pdf -dpdf
