home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / SPECMAT.DI$ / HADAMARD.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.2 KB  |  39 lines

  1. function H = hadamard(n)
  2. %HADAMARD  HADAMARD(N) is a Hadamard matrix of order N, that is,
  3. %    a matrix H with elements 1 or -1 such that H'*H = N*EYE(N).
  4. %    An N-by-N Hadamard matrix with N > 2 exists only if REM(N,4)=0.
  5. %    This function handles only the cases where N, N/12 or N/20
  6. %    is a power of 2.
  7.  
  8. %    Author: N.J. Higham 11-14-91.  Revised by CBM, 6/24/92.
  9. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  10.  
  11. %   Reference: S.W. Golomb and L.D. Baumert, The search for Hadamard
  12. %   matrices, Amer. Math. Monthly, 70 (1963) pp. 12-17.
  13.  
  14. [f,e] = log2([n n/12 n/20]);
  15. k = find(f==1/2 & e>0);
  16. if isempty(k)
  17.    error(['N, N/12 or N/20 must be a power of 2.']);
  18. end
  19. e = e(k)-1;
  20.  
  21. if k == 1        % N = 1 * 2^e;
  22.    H = [1];
  23.  
  24. elseif k == 2    % N = 12 * 2^e;
  25.    H = [ones(1,12); ones(11,1) ...
  26.         toeplitz([-1 -1 1 -1 -1 -1 1 1 1 -1 1],[-1 1 -1 1 1 1 -1 -1 -1 1 -1])];
  27.  
  28. elseif k == 3    % N = 20 * 2^e;
  29.    H = [ones(1,20); ones(19,1)   ...
  30.         hankel([-1 -1 1 1 -1 -1 -1 -1 1 -1 1 -1 1 1 1 1 -1 -1 1], ...
  31.                [1 -1 -1 1 1 -1 -1 -1 -1 1 -1 1 -1 1 1 1 1 -1 -1])];
  32. end
  33.  
  34. %  Kronecker product construction.
  35. for i = 1:e
  36.     H = [H  H
  37.          H -H];
  38. end
  39.