## Computing a DCT using an FFT

#####
Reply #3 –

I agree that there is often not enough documentation on how to do a simple DCT using an FFT. Though it is not the best, computational-wise, it is however interesting to see the relationship between the two, esp. why the DCT is used more often than the FFT in compression (the even-symmetry creates smoothness that results in less high frequency energy).

Anyway, I was interested in this problem a while ago as well and found this link to be the best I've found.

Not sure if this is any help or not, but here is some MATLAB code I wrote during that time, which calculates the DCT using FFT.

clear all;

close all;

x=[1 2 3 4];

N=length(x);

d = dct(x);

x2=[4 3 2 1 1 2 3 4];

f=fft(x2);

n = 0:2*N-1;

shift=exp(-j*2*pi*(N-0.5)*n/(2*N));

f2 = real(f./shift);

d2 = f2(1:N)/sqrt(2*N);

d2(1)=d2(1)/sqrt(2);

disp(d);

disp(d2);

Notice how I don't do any zero-setting (no idea why you need to anyway). I just take my original signal, mirror it and then take the fft. Now the thing to note is that the Fourier transform of an even symmetry signal is real but we can't make true even symmetry signals on a computer, so we have to compensate for the phase shift caused by the time-shift of 3.5 samples, hence the division by the FFT of the variable 'shift'. Then I scale the coefficients appropriately (to make them orthonormal), and voila.

Hope that helps in a little way

EDIT: fixed a spelling mistake