pythonscipydct

Use Python's Scipy DCT-II to do 2D or ND DCT


I would like to use scipy's DCT-II since it is already coded and fast. Looking at the doc, it seems it is the 1D implementation. Is it possible to use it in such a way to use it as a 3D implementation? I'm not sure about the math. Are the 2D and 3D implementations the equivalent of multiplying 2 or 3 times the 1D using different dimensions in the calculation?


Solution

  • Basically, the following does the trick:

    import numpy as np
    from scipy.fftpack import dct, idct
    
    # Lets create a 3D array and fill it with some values
    a = np.random.rand(3,3,3)
    
    b = dct(dct(dct(a).transpose(0,2,1)).transpose(1,2,0)).transpose(1,2,0).transpose(0,2,1)