[PYTHON] Discrete cosine transform using chainer.links.Linear

I made a note because it didn't come out when I googled it. I created a Discrete Cosine Transform (DCT) using the chainer.links.Linear class.

The Discrete Cosine Transform is a relative of the Fast Fourier Transform (FFT) and has the same meaning as extracting the real part of the FFT. (Detailed explanation depends on other people's explanation)

I have confirmed that using the following function DCT (wave) returns the same result as scipy.fftpack.dct (wave). Modules are imported according to Chainer Tutorial.

DCT.py


def DCT(wave):
    num_base = np.size(wave.data[0,:])
    lDCT = L.Linear(num_base,num_base)
    for n in range(num_base):
        for k in range(num_base):
            lDCT.W.data[k,n] = 2*np.cos(np.pi*k*(2*n+1)/(2*num_base))
    lDCT.b.data[:] = 0.0
    return lDCT(wave)

The point I got into is -Variable format to pass to Linear -Pass as two dimensions like np.array ([[input vector]]) That is the point. With this, you can (should) create a Chain or error function using DCT. (Not used yet)

(Addition: February 4, 2016) I also made an inverse discrete cosine transform (Inverse DCT, IDCT). From Scipy documentation, scipy.fftpack.dct (wave) is Type 2 by default. Returns. In this case, Type3 should be used for the inverse conversion, so the implementation is as follows.

IDCT.py


def IDCT(wave):
    num_base = np.size(wave.data[0,:])
    lIDCT = L.Linear(num_base,num_base)
    for n in range(num_base):
        for k in range(num_base):
            if(n==0):
                lIDCT.W.data[k,n] = 0.0
            else:
                lIDCT.W.data[k,n] = 2*np.cos(np.pi*(k+0.5)*n/num_base)
    lIDCT.b.data[:] = wave.data[0,0]
    return lIDCT(wave)

The caveat of this IDCT is that it cannot be used for batch processing (!). I wish there was a better implementation. ..

Recommended Posts

Discrete cosine transform using chainer.links.Linear
Noise removal method using wavelet transform
Properties of the discrete Fourier transform
Circular object recognition using Hough transform