- Text
- Spectrum,
- Fourier,
- Imaginary,
- Filter,
- Component,
- Frequency,
- Amplitude,
- Functions,
- Inverse,
- Transform,
- Graphics,
- Tutorial

Lode's Computer Graphics Tutorial Fourier Transform

function every time. ca and sa still represent the cosine and sine, but are calculated with the half-angleformulas and initual values -1.0 and 0.0 (cos and sin of pi): each loop, ca and sa represent the cosineand sine of half the angle as the previous loop.//Calculate the FFTdouble ca = -1.0;double sa = 0.0;int l1 = 1, l2 = 1;for(int l = 0; l < m; l++){l1 = l2;l2 *= 2;double u1 = 1.0;double u2 = 0.0;for(int j = 0; j < l1; j++){for(int i = j; i < n; i += l2){int i1 = i + l1;double t1 = u1 * GRe[i1] - u2 * GIm[i1];double t2 = u1 * GIm[i1] + u2 * GRe[i1];GRe[i1] = GRe[i] - t1;GIm[i1] = GIm[i] - t2;GRe[i] += t1;GIm[i] += t2;}double z = u1 * ca - u2 * sa;u2 = u1 * sa + u2 * ca;u1 = z;}sa = sqrt((1.0 - ca) / 2.0);if(!inverse) sa =- sa;ca = sqrt((1.0 + ca) / 2.0);}Finally, the values are divided through n if it isn't the inverse DFT.}//Divide through n if it isn't the IDFTif(!inverse)for(int i = 0; i < n; i++){GRe[i] /= n;GIm[i] /= n;}The function reads from *gRe and *gIm again and stores the result in *GRe and *GIm.There are 3 nested loops now in the FFT part, but only the first one goes from 0 to n-1, the other twotogether only loop log_2(n) times. For this small signal of only 128 values you probably won't noticeany increase in speed because both DFT and FFT are calculated instantly (unless you're working onsome computer from the '40s), but on bigger signals, on 2D signals, and in applications where the FThas be be calculated over and over in real time, it'll make an important difference.

FiltersIn the **Fourier** Domain, it's easy to apply some filters. All you have to do is multiply the spectrum withthe transfer function of the filter.Low pass filters only let through components with low frequencies (for example the bass from music),while High Pass filters let only components with high frequencies through. You can also make BandPass and Band Stop filters, which let through or block components with a certain frequency. Amplifiersmultiply the whole spectrum with a constant value, and you can of course make filters that amplifycertain frequencies, or triangular filters that weaken higher frequencies more and more, etc...So to make for example a Low Pass filter, multiply the spectrum with a rectangular function with therectangle around the origin. All the low frequency components will then be kept, while the high onesare multiplied by zero and are thus filtered away.Here's a part of code for a program that'll first allow you to select different signals and watch their FT,and after pressing the space key, will allow you to choose different filters on the spectrum, and it'llcalculate the inverse FT to see how the original signal is affected by the filter.The full code can be downloaded here: fourier1d.cpp.First all variables and functions are declared.const int N = 128; //yeah ok, we use old fashioned fixed size arrays heredouble fRe[N]; //the function's real part, imaginary part, and amplitudedouble fIm[N];double fAmp[N];double FRe[N]; //the FT's real part, imaginary part and amplitudedouble FIm[N];double FAmp[N];const double pi = 3.1415926535897932384626433832795;void FFT(int n, bool inverse, double *gRe, double *gIm, double *GRe, double *GIm); //Calculates the DFTvoid plot(int yPos, double *g, double scale, bool trans, ColorRGB color); //plots a functionvoid calculateAmp(int n, double *ga, double *gRe, double *gIm); //calculates the amplitude of a complex functionThe main function starts here. It enters the first loop, this loop will allow you to choose a function. Theparameters p1 and p2 can be changed to modify the amplitude and/or width of some of the functions.int main(int /*argc*/, char */*argv*/[]){screen(640, 480, 0, "**Fourier** **Transform** and Filters");for(int x = 0; x < N; x++) fRe[x] = fIm[x] = 0;bool endloop = 0, changed = 1;int n = N;double p1 = 25.0, p2 = 2.0;while(!endloop){if(done()) end();readKeys();

- 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 and 12: It takes 4 arrays as parameters: *g
- Page 13: FFT, which uses only O(n*logn) oper
- 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