Function hadamardmatrix

Synopse

Kernel matrix for the Hadamard Transform.

  • A = hadamardmatrix(N)
    • Output:
      • A: Image.
    • Input:
      • N: Double.

Function code


In [5]:
import numpy as np
 
def hadamardmatrix(N):
 
    def bitsum(x):
        s = 0 * x
        while x.any():
            s += x & 1
            x >>= 1
        return s
 
    n = np.floor(np.log(N)/np.log(2))
 
    if 2**n != N:
        raise Exception('error: size {0} is not multiple of power of 2'.format(N))
 
    u, x = np.meshgrid(range(N), range(N))
 
    A = ((-1)**(bitsum(x & u)))/np.sqrt(N)
    return A

In [1]:
testing = (__name__ == "__main__")

if testing:
    ! jupyter nbconvert --to python hadamardmatrix.ipynb
    import numpy as np
    import sys,os
    import matplotlib.image as mpimg
    ia898path = os.path.abspath('../../')
    if ia898path not in sys.path:
        sys.path.append(ia898path)
    import ia898.src as ia


[NbConvertApp] Converting notebook hadamardmatrix.ipynb to python
[NbConvertApp] Writing 1434 bytes to hadamardmatrix.py

Examples

Example 1


In [2]:
if testing:
    A = ia.hadamardmatrix(128)
    ia.adshow(ia.normalize(A, [0, 255]))


Example 2


In [4]:
if testing:
    A = ia.hadamardmatrix(4)
    print(A)
    print(np.dot(A, np.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.]]

Measuring time:


In [5]:
if testing:
    print('Computational time is:')
    %timeit ia.hadamardmatrix(128)


Computational time is:
1000 loops, best of 3: 802 µs per loop

In [ ]: