Function dctmatrix

Synopse

Compute the Kernel matrix for the DCT Transform.

  • A = dctmatrix(N)

    • A:output: DCT matrix NxN.
    • N:input: matrix size (NxN).

In [1]:
import numpy as np

def dctmatrix(N):
    x = np.resize(range(N), (len(range(N)), len(range(N)))) #matrix with columns index
    u = np.transpose(np.resize(range(N), (len(range(N)), len(range(N))))) #matrix with rows index
    alpha = np.ones((N,N)) * np.sqrt(2./N) # 1/sqrt(2/N)
    alpha[0,:] = np.sqrt(1./N) # alpha(u,x)
    A = alpha * np.cos((2*x+1)*u*np.pi / (2.*N)) # Cn(u,x)
    return A

Examples


In [2]:
testing = (__name__ == "__main__")
if testing:
    ! jupyter nbconvert --to python dctmatrix.ipynb
    import numpy as np
    import sys,os
    ia898path = os.path.abspath('../../')
    if ia898path not in sys.path:
        sys.path.append(ia898path)
    import ia898.src as ia


[NbConvertApp] Converting notebook dctmatrix.ipynb to python
[NbConvertApp] Writing 1630 bytes to dctmatrix.py

Example 1


In [3]:
if testing:
    np.set_printoptions(suppress=True, precision=4)
    A = ia.dctmatrix(4)
    print('Visualiza matriz DCT 4x4:\n',A)
    B = np.dot(A,np.transpose(A))
    
    print("\nVisualiza propriedade A*A'= I:\n", B)


Visualiza matriz DCT 4x4:
 [[ 0.5     0.5     0.5     0.5   ]
 [ 0.6533  0.2706 -0.2706 -0.6533]
 [ 0.5    -0.5    -0.5     0.5   ]
 [ 0.2706 -0.6533  0.6533 -0.2706]]

Visualiza propriedade A*A'= I:
 [[ 1.  0.  0. -0.]
 [ 0.  1.  0.  0.]
 [ 0.  0.  1.  0.]
 [-0.  0.  0.  1.]]

Example 2


In [4]:
if testing:
    %matplotlib inline
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import sys,os
    ia898path = os.path.abspath('/home/lotufo')
    if ia898path not in sys.path:
        sys.path.append(ia898path)
    import ia898.src as ia
    A = ia.dctmatrix(128)
    ia.adshow(ia.normalize(A,[0,255]),'DCT 128x128')


DCT 128x128

In [ ]:


In [ ]: