We want to figure out how to use matlab's fast fourier transform (fft) and inverse fast fourier transform (ifft). define the number of intervals: >> n = 2^3 n = 8 define the length of the interval: >> dx = 2*pi/n; define the mesh-points. _important_ we exclude x=2*pi since that's the same as x=0 >> x = 0:dx:2*pi-dx; take u to be the constant function >> u = ones(size(x)); find the fft of u. we'd expect to see 1 and 0s. We see 8 and 0. This tells us that the fft gives back N*f_k where f_k is a fourier transform we're comfortable with. Almost all ffts do something like this but you have to check each time. Since different fft routines can be set-up differently. >> fft(u) ans = 8 0 0 0 0 0 0 0 now we start testing trig functions >> u = cos(x); >> v = fft(u) v = Columns 1 through 4 -0.0000 4.0000 - 0.0000i 0.0000 0 Columns 5 through 8 0.0000 0 0.0000 4.0000 + 0.0000i these are the coeffs 1/2 & and 1/2 for e^ix and e^{-ix} (multiplied by N) >> u - ifft(v) ans = 1.0e-015 * Columns 1 through 4 0 -0.1110 - 0.0555i 0.0114 0 Columns 5 through 8 0 0 + 0.0555i -0.0114 -0.1110 So the inverse fast fourier transform recovers the function up to round-off error. If I want to knock off the complex parts, then it's just >> u - real(ifft(v)) ans = 1.0e-015 * Columns 1 through 7 0 -0.1110 0.0114 0 0 0 -0.0114 Column 8 -0.1110 now check other trig functions >> u = sin(x); >> fft(u) ans = Columns 1 through 4 0.0000 -0.0000 - 4.0000i 0.0000 - 0.0000i 0 Columns 5 through 8 0.0000 0 0.0000 + 0.0000i -0.0000 + 4.0000i these are the coeffs -i/2 & and i/2 for e^ix and e^{-ix} (multiplied by N) >> u = cos(2*x); >> fft(u) ans = Columns 1 through 4 -0.0000 -0.0000 - 0.0000i 4.0000 - 0.0000i 0.0000 - 0.0000i Columns 5 through 8 0.0000 0.0000 + 0.0000i 4.0000 + 0.0000i -0.0000 + 0.0000i these are the coeffs 1/2 & and 1/2 for e^i2x and e^{-i2x} (multiplied by N) >> u = sin(2*x); >> fft(u) ans = Columns 1 through 4 0.0000 0.0000 + 0.0000i -0.0000 - 4.0000i 0.0000 - 0.0000i Columns 5 through 8 0.0000 0.0000 + 0.0000i -0.0000 + 4.0000i 0.0000 - 0.0000i these are the coeffs -i/2 & and i/2 for e^i2x and e^{-i2x} (multiplied by N) >> u = cos(3*x); >> fft(u) ans = Columns 1 through 4 -0.0000 0.0000 - 0.0000i -0.0000 + 0.0000i 4.0000 - 0.0000i Columns 5 through 8 0.0000 4.0000 + 0.0000i -0.0000 - 0.0000i 0.0000 + 0.0000i these are the coeffs 1/2 & and 1/2 for e^i3x and e^{-i3x} (multiplied by N) >> u = sin(3*x); >> fft(u) ans = Columns 1 through 4 -0.0000 0.0000 - 0.0000i 0.0000 + 0.0000i -0.0000 - 4.0000i Columns 5 through 8 0.0000 -0.0000 + 4.0000i 0.0000 - 0.0000i 0.0000 + 0.0000i these are the coeffs -i/2 & and i/2 for e^i3x and e^{-i3x} (multiplied by N) Okay, so the above gives us an idea of how the coefficients are being distributed in the vector. It also tells us that we're about to run into trouble... there's only one spot left and we've got both cos(4x) and sin(4x) coming up. >> u = cos(4*x); >> fft(u) ans = 0 0 0 0 8 0 0 0 this is not consistent with the coeffs 1/2 & and 1/2 for e^i4x and e^{-i4x} It appears to be the coefficents added together. >> u = sin(4*x); >> fft(u) ans = 1.0e-014 * Columns 1 through 4 0.0490 0.0490 + 0.0203i 0.0490 + 0.0490i 0.0490 + 0.1183i Columns 5 through 8 -0.3429 0.0490 - 0.1183i 0.0490 - 0.0490i 0.0490 - 0.0203i this is not consistent with the coeffs -i/2 & and i/2 for e^i4x and e^{-i4x} It appears to be the coefficents added together. Let's try complex trig functions. >> u = exp(i*x); >> fft(u) ans = Columns 1 through 4 -0.0000 + 0.0000i 8.0000 - 0.0000i 0.0000 + 0.0000i 0 + 0.0000i Columns 5 through 8 0.0000 + 0.0000i 0 + 0.0000i 0.0000 + 0.0000i 0 + 0.0000i >> u = exp(2*i*x); >> fft(u) ans = Columns 1 through 4 -0.0000 + 0.0000i -0.0000 + 0.0000i 8.0000 - 0.0000i 0.0000 + 0.0000i Columns 5 through 8 0.0000 + 0.0000i 0.0000 + 0.0000i 0 + 0.0000i -0.0000 + 0.0000i >> u = exp(3*i*x); >> fft(u) ans = Columns 1 through 4 -0.0000 - 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i 8.0000 - 0.0000i Columns 5 through 8 0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 - 0.0000i 0 + 0.0000i >> u = exp(4*i*x); >> fft(u) ans = Columns 1 through 4 0 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i Columns 5 through 8 8.0000 - 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i >> u = exp(5*i*x); >> fft(u) ans = Columns 1 through 4 -0.0000 + 0.0000i 0 - 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i Columns 5 through 8 -0.0000 + 0.0000i 8.0000 - 0.0000i 0.0000 - 0.0000i 0.0000 + 0.0000i >> u = exp(-i*x); >> fft(u) ans = Columns 1 through 4 -0.0000 - 0.0000i 0 - 0.0000i 0.0000 - 0.0000i 0 - 0.0000i Columns 5 through 8 0.0000 - 0.0000i 0 - 0.0000i 0.0000 - 0.0000i 8.0000 + 0.0000i >> u = exp(-2*i*x); >> fft(u) ans = Columns 1 through 4 -0.0000 - 0.0000i -0.0000 - 0.0000i 0 - 0.0000i 0.0000 - 0.0000i Columns 5 through 8 0.0000 - 0.0000i 0.0000 - 0.0000i 8.0000 + 0.0000i -0.0000 - 0.0000i >> u = exp(-3*i*x); >> fft(u) ans = Columns 1 through 4 -0.0000 + 0.0000i 0 - 0.0000i 0.0000 + 0.0000i -0.0000 - 0.0000i Columns 5 through 8 0.0000 - 0.0000i 8.0000 + 0.0000i -0.0000 - 0.0000i 0.0000 - 0.0000i >> u = exp(-4*i*x); >> fft(u) ans = Columns 1 through 4 0 - 0.0000i 0.0000 - 0.0000i 0.0000 - 0.0000i 0.0000 - 0.0000i Columns 5 through 8 8.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i >> u = exp(-5*i*x); >> fft(u) ans = Columns 1 through 4 -0.0000 - 0.0000i 0.0000 - 0.0000i 0.0000 + 0.0000i 8.0000 + 0.0000i Columns 5 through 8 -0.0000 - 0.0000i -0.0000 - 0.0000i 0.0000 - 0.0000i 0 + 0.0000i Note: exp(-5*i*x) is indistinguishable from exp(i*3*x) as far as the discrete fourier transform based on eight intervals. Note: exp(-4*i*x) is indistinguishable from exp(i*4*x) as far as the discrete fourier transform based on eight intervals. This is why sin(4*x) = -i*exp(4*i*x)/2 + i*exp(-4*i*x)/2 has a zero fft. You get -i/2 and i/2 in the same place, added together. In general, for k_0 between -N/2 and N/2-1 you have exp(i*k_0*x) indistinguishable from exp(i*(k_0+jN)*x) for all integers j. (In this way the integers are folded onto N indices. The discrete fft gives a coefficient f(k_0) which is the sum of all the usual fourier coeffcients where the sum is over k_0 + jN