Fourier coefficients computed by MATLAB fft function don't match exact ones

473 Views Asked by At

I am trying to find out the Fourier coefficients of function $\cos\left(\frac{\pi x}{7}\right)$ on the interval of $[0\,,\,7]$ by using MATLAB built-in function fft in order to verify my own DFT and FFT functions; the wave number here $k=0\,,1\,,2\,...15$.

However, the results are different from the one I calculated by hand, which is

$$\hat{f}_k =\frac{1}{7}\int_{0}^{7}\cos\left(\frac{\pi x}{7}\right)e^{-i\frac{2\pi}{7}kx}dx\\ =\frac{1}{7}\int_{0}^{7}\cos\left(\frac{\pi x}{7}\right)\left[\cos\left(\frac{2\pi k}{7}x\right)-i\sin\left(\frac{2\pi k}{7}x\right)\right]dx\\ =\frac{1}{14}\int_{0}^{7}\left[\cos\frac{(1+2k)\pi}{7}x+\cos\frac{(1-2k)\pi}{7}x\right]dx - \frac{1}{14}i\int_{0}^{7}\left[\sin\frac{(1+2k)\pi}{7}x-\sin\frac{(1-2k)\pi}{7}x\right]dx\\ =0-\left[\frac{1}{(1-2k)\pi}-\frac{1}{(1+2k)\pi}\right]i\\ =\boxed{-\frac{4k}{(1-4k^2)\pi}i} $$ obviously this one gives a series of pure imaginary numbers (if I didn't make any arithmetic mistake).

However, the coefficients I calculated from MATLAB fft are $1.00$ for the real parts of all the wavenumbers and nonzeros for all the imaginary parts. Apparently, they are not the same.

So I am confused, where goes wrong?

PS: I attached my MATLAB script below, please take a look.

L  = 7;
N  = 16;
dx = L/N;

x = 0 : dx : L;

f = cos(pi * x /L); 

fk = fft(f(1:end-1));
1

There are 1 best solutions below

7
On

The interval $[0,7]$ only contains a half period of $\cos(\pi x/7)$. Perhaps you meant $\cos(2\pi x/7)$.

I don't know nearly enough about Fourier analysis to say exactly what the problem is, but my understanding of the issue is that the continuous Fourier transform of $\cos(\pi x/7)$ on $[0,7]$ has unbounded support, so already the Shannon-Nyquist sampling theorem does not apply (and I believe you'll experience a phenomenon called "foldover" due to aliasing -- though I was unable to actually calculate the phenomenon).

I can confirm (with Mathematica) that $$\hat{f}_k =\frac{1}{7}\int_{0}^{7}\cos\left(\frac{\pi x}{7}\right)e^{-i\frac{2\pi}{7}kx}dx=\frac{2i(1+e^{-2i k\pi})k}{(1-4k^2)\pi}$$ for all $k\in\mathbb{R}$, where at $k=\pm 1/2$ you are supposed to think of this as a limit and cancel out the $0/0$. This coincides with what you got when $k$ is a non-negative integer.

The DFT is calculating inner products against $e^{2\pi i k(x/7)}$ sampled at $N$ evenly spaced $x$ on $[0,7]$, for $k=0,1,\dots,N-1$. A reconstruction of the function from the DFT using $k=-7,-6,\dots,7,8$ (to reduce the frequency content of the result) gives the following graph, where the blue curve is the real part, the red curve is the imaginary part, and the green curve is $\cos(\pi x/7)$.

A plot of the real and imaginary parts of a reconstruction along with the original function

This is a sort of band-limited representation of the original function with enforced periodicity.

Notice also that the sampled function's coordinates sum to $1$ (the DFT's zeroeth term), vs $\hat{f}_0=0$.