# Three Fast Methods to Compute the DCT-2

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.

# Derivation of fast DCT-4 algorithm based on DFT

It’s well known that an point DCT-4 can be computed using an point complex FFT. Although the algorithm is widespread, the texts which I have read on the subject have not provided the details as to how it works. I’ve been trying to understand this for a while (I tend not to use algorithms until I understand them) and finally figured it out and thought I would share (please provide comments/links if you can find a better or shorter explanation – this is as good as I could get).

Trig functions are hard to manipulate in expressions (for me anyway), so knowing that is real, we change the into a complex exponential and obtain the following:

It is worth noting that the sign of the exponent is irrelevant.

We then split the expression up to operate over two half-length sequences composed of and . The second sequence is reversed and decimated because when the is replaced by in the term of the exponential, the expression is negated rather than the offset being modified. We move the term into another exponential which becomes trivial as cancels the denominator. i.e.:

Or:

The exponentials still contain no term which resembles an DFT (over length anyway). To get one, we break up into the terms and . Again we choose to reverse the second sequence because it will keep the exponential terms in a similar but negated form.

Now because we are only interested in the real terms, we can ignore or conjugate anything which contributes to the imaginary term of the above expressions. This means that we can conjugate the exponentials when they are only modulating a real term. Doing this, we obtain:

Almost there, but to obtain the full benefit we need the inner terms to be the same. If we now multiply the term by , we get:

Done. If we expand out the and terms completely we get:

Which give us the steps for a DFT based DCT-4 algorithm:

• Transform the point real sequence into the point complex sequence .
• Multiply each element of the sequence by .
• Find , the DFT of the sequence .
• Multiply each element of by .
• The DCT-4 outputs are given by: