function H = haar(n)
%
% haar: generate Haar matrix of size n by n (n is power of 2)
% On input:
%     n: order of transform
% On output:
%     H: Haar matrix of order n
% Call:
%     H = haar(n)
%

H = zeros(n,n);
H(1,:) = ones(1,n);
coef = zeros(1,n);
coef(1) = 1;
for i1 = 2:n
  coef(i1) = sqrt(2^(i1-2));
end
%coef
iter = log10(n)/log10(2);
for i1 = 1:iter
  subiter = 2^(i1-1);
  len = n/subiter;
  subm(1:len/2) = coef(i1+1)*ones(1,len/2);
  subm((len/2)+1:len) = -subm(1:len/2);
%  subm
  for i2 = 1:subiter
	H(2^(i1-1)+i2,len*(i2-1)+1:i2*len) = subm(1:len);
  end
end
H = H/sqrt(n);
