[iahadamard] [Up] [iahwt] Image Transformation

iahadamardmatrix
Kernel matrix for the Hadamard Transform.

Synopsis

A = iahadamardmatrix( N )

Implemented in Python.

Input

N Double. Non-negative integer.

Output

A Image. Gray-scale (uint8 or uint16) or binary image (logical).

Examples

Visualization of the Hadamard matrix
>>> A = iahadamardmatrix(128)

              
>>> iashow(A)
(128, 128) Min= -0.0883883476483 Max= 0.0883883476483 Mean=0.001 Std=0.09
A
Hadamard matrix is a unitary matrix
>>> import Numeric

              
>>> A = iahadamardmatrix(4)

              
>>> print 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]]
>>> print Numeric.matrixmultiply(A, Numeric.transpose(A))
[[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]

Source Code

def iahadamardmatrix(N):
    from Numeric import floor, log, sqrt
    def bitsum(x):
        s = 0
        while x:
            s += x & 1
            x >>= 1
        return s
    n = floor(log(N)/log(2))
    if 2**n != N:
       iaerror('error: size '+str(N)+' is not multiple of power of 2')
       return -1
    u, x = iameshgrid(range(N), range(N))
    A = ((-1)**(bitsum(x & u)))/sqrt(N)
    return A
    
[iahadamard] [Up] [iahwt] http://www.python.org