After doing my “proof thing” of the fast DFT based implementation of the DCT-4 (which does actually come in useful in my line of work), I thought I would have a fiddle with some of the other DCT variants. The DCT-2 is a transform which I doubt I will ever use, but I managed to find three ways (excluding the slow and obvious one) to build it. Here is the definition:

Like in the DCT-4 post, I hate trig functions so the first thing to do is transform into a complex exponential. I’ll use this as the starting point for the methods.

## Method 1: Recursion and the DCT-4

All of the basis functions of the DCT-2 have a symmetry about ; the symmetry is even for even and odd for odd . This is a big hint that we can benefit from splitting the input sequence into two half-length sequences where one sequence is reversed:

As on the DCT-4 page, we can conjugate any terms we like in the summation as we are only interested in the real part of the result. This allows us to rewrite the above as:

Split into even and odd terms:

The end. It can be seen that the terms can be obtained by recursively calling the function again. The terms can be obtained via a DCT-4. Writing out the steps explicitly:

- Create a sequence of length
- Create a sequence of length
- The resulting terms are given by the DCT-4 of the sequence
- The resulting terms are given by performing this same process on the sequence

## Method 2: A real DFT and a DCT-4

This method is probably not particularly useful given how the next method works – but I thought it was interesting so I’m listing it. Starting from the beginning again:

Rearrange the above into two halves by interleaving the input:

Again, we conjugate terms where we know it won’t impact the result:

For the case, we can write this as:

So for the even output indices, we can use a DFT on some rearranged data followed by a twiddle. For the outputs, this particular form gets ugly – but recalling that in the first method, the outputs were not recursive and simply based on a DCT-4, we just steal that result:

I won’t bother explicitly listing the steps for this one. They are simple to figure out.

## Method 3: A single real DFT

This is almost certainly the method you should use to compute the DCT-2. Recall the third equation in the second method:

I didn’t spot this immediately, but the summation term also happens to be the first stage of a decimation in frequency transform. Recall the DIF FFT stage:

So we create a new vector :

Then substitute back into :

This actually becomes an length DFT if you use a real transform.