Function iahadamardmatrix

Synopse

Kernel matrix for the Hadamard Transform.

  • A = iahadamardmatrix(N)
    • A: Image.
    • N: Double.
01. from numpy import *
02. 
03. def iahadamardmatrix(N):
04.     from iameshgrid import iameshgrid
05. 
06.     def bitsum(x):
07.         s = 0 * x
08.         while x.any():
09.             s += x & 1
10.             x >>= 1
11.         return s
12. 
13.     n = floor(log(N)/log(2))
14. 
15.     if 2**n != N:
16.        raise Exception, 'error: size '+str(N)+' is not multiple of power of 2'
17. 
18.     u, x = iameshgrid(range(N), range(N))
19. 
20.     A = ((-1)**(bitsum(x & u)))/sqrt(N)
21.     return A

Examples

Example 1

1. from ia636 import iahadamardmatrix, ianormalize
2. 
3. A = iahadamardmatrix(128)
4. adshow(ianormalize(A,[0,255]))

Example 2

1. A = iahadamardmatrix(4)
2. print A
3. print dot(A, transpose(A))
[[ 0.5  0.5  0.5  0.5]
 [ 0.5 -0.5  0.5 -0.5]
 [ 0.5  0.5 -0.5 -0.5]
 [ 0.5 -0.5 -0.5  0.5]]
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]