Calculates the amplitude of *gRe and *gIm and puts the result in *gAmpvoid calculateAmp(int n, double *gAmp, double *gRe, double *gIm){for(int x = 0; x < n; x++){gAmp[x] = sqrt(gRe[x] * gRe[x] + gIm[x] * gIm[x]);}}The output of this program is as follows:The red function is the input function, which is a sine, and the black funtion at the bottom is theamplitude of it's spectrum, which is calculated by the DFT function. The green curves are theimaginary parts.The Fast <strong>Fourier</strong> <strong>Transform</strong>The above DFT function correctly calculates the Discrete <strong>Fourier</strong> <strong>Transform</strong>, but uses two for loops ofn times, so it takes O(n²) arithmetical operations. A faster algorithm is the Fast <strong>Fourier</strong> <strong>Transform</strong> or
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
- Page 1 and 2: Lode's Computer Graphics TutorialFo
- Page 3 and 4: would have a single peak. A spectru
- Page 5 and 6: The spectrum is positive everywhere
- Page 7 and 8: This looks already much more progra
- Page 9 and 10: that remains at fixed position, whi
- Page 11: It takes 4 arrays as parameters: *g
- Page 15 and 16: FiltersIn the Fourier Domain, it's
- Page 17 and 18: Now the keys all change the spectru
- Page 19 and 20: A HP FilterHere, the same is done w
- Page 21 and 22: The main function first loads an im
- Page 23 and 24: And here's the Fast Fourier Transfo
- Page 25 and 26: left corner at xpos,yPos. The shift
- Page 27 and 28: would have only 2 color channels, o
- Page 29 and 30: The sloped lines in the spectrum he
- Page 31 and 32: shown here, the code for all keys i
- Page 33 and 34: }calculateAmp(N, M, fAmp[0][0], fRe
- Page 35 and 36: component back.This is the original
- Page 37 and 38: In the above images, a circular sec
- Page 39 and 40: It looks pretty ugly, because there
- Page 41 and 42: And here's the result of a much thi