Fanning Software Consulting

Discrete Cosine Transform

Facebook Twitter RSS Google+

QUESTION: I have need of a discrete cosine transform (DCT) in IDL. What do you know about this?

ANSWER: Unfortunately, this is a subject of which I am completely and utterly ignornant. When this is the case, I usually rely on other experts in the IDL programming community. In this case, Tom Grydeland posted IDL code on the IDL newsgroup to implement type-II and type-III discrete cosine transforms. Here is what he had to say about them.

I had a need for discrete cosine transforms, and since these are not provided in IDL, I have rolled my own, based on the answers I found in this discussion and the references therein. The code I wrote is 1D only.

I benchmarked the different approaches and found that for shorter inputs, the choice of equivalence (length-N, length-2N pad/mirror, length 4N) didn't make much difference, but for longer inputs (> 1000) the shorter FFT would win big.

The DCT program implements a type-II DCT, while the IDCT programs implements a type-III DCT (the common nomenclature). Scaling is the same as used in scipy.fftpack (which is two times the scaling used in the Wikipedia article on DCT). When keyword /ORTHO is set, orthogonal weighting is used, making these routines equivalent to the (i)dct routines from That Other Vectorized Language[tm].

The function ExpiDouble (used in IDCT), if you don't have it already, simply returns DComplex(Cos(arg), Sin(arg)), which is faster than calling the complex exponential but works only for real inputs.

Tom's code is offered without restrictions, but as with all community-supplied code, some acknowledgement of the author, if you use it, is greatly appreciated and goes a long way toward ensuring community code is provided in the future.

Version of IDL used to prepare this article: IDL 8.2.3.

Written: 14 February 2014