12.07.2015 Views

Lode's Computer Graphics Tutorial Fourier Transform

Lode's Computer Graphics Tutorial Fourier Transform

Lode's Computer Graphics Tutorial Fourier Transform

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

FFT, which uses only O(n*logn) operations. This makes a big difference for very large n: if n would be1024, the DFT function would take 1048576 (about 1 million) loops, while the FFT would use only10240.The FFT works by splitting the signal into two halves: one halve contains all the values with evenindex, the other all the ones with odd index. Then, it splits up these two halves again, etc..., recursivily.Due to this, a requirement is that the size of the signal, n, must be a power of 2. There exist other FFTversions that can work on signals that can have length n*m where both n and m are primes, but thatgoes beyond the scope of this tutorial.Here's a new version of the DFT function, called FFT, which uses the Fast <strong>Fourier</strong> <strong>Transform</strong> instead.The FFT algorithm used here is called the Radix-2 algorithm, by Cooley-Tukey, developed 1965. Youcan add it to the previous program and change the function call of it to FFT to test it out.First, this function will calculate the logarithm of n, needed for the algorithm.void FFT(int n, bool inverse, double *gRe, double *gIm, double *GRe, double *GIm){//Calculate m=log_2(n)int m = 0;int p = 1;while(p < n){p *= 2;m++;}Next, Bit Reversal is performed. Because of the way the FFT works, by splitting the signal in it's evenand odd components every time, the algorithm requires that the input signal is already in the formwhere all odd components come first, then the even ones, and so on in each of the halves. This is thesame as reversing the bits of the index of each value (for example the second value, with index 0001will now get index 1000 and this go to the center), hence the name.//Bit reversalGRe[n - 1] = gRe[n - 1];GIm[n - 1] = gIm[n - 1];int j = 0;for(int i = 0; i < n - 1; i++){GRe[i] = gRe[j];GIm[i] = gIm[j];int k = n / 2;while(k

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!