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

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));


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)$.

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$.

• Thanks for the reply! But I am confused, the integral you gave will generate pure imaginary numbers for k being integer. Then where does your real curve come from? This is where I stuck. I can't get any real part in my hand calculation, and yet MATLAB fft function gives coefficients with real part. – TurbJet Sep 10 '18 at 0:22
• @SJH The way I understand it, the DFT of a sampled function computes the continuous-time Fourier transform of a low-pass filter of a periodic version of that function. To create that graph, I did an inverse DFT where I left the exponentials as functions of $x$ (instead of sampling them). Notice that at every multiple of $1/2$ the red curve is $0$ and the blue curve matches the green curve, so the DFT does reconstruct the sampled function. Basically: you can't just do the continuous-time Fourier transform and expect it to correspond to a DFT without some additional properties on the function. – Kyle Miller Sep 10 '18 at 0:46
• I used FFT, basically the same as DFT. I can reconstruct the original function. What baffles me is that why the coefficients are different with my calculation. – TurbJet Sep 10 '18 at 0:59
• @SJH Reconstruct what exactly? The sampled function or the continuous function itself? (And if the latter, how exactly?) – Kyle Miller Sep 10 '18 at 1:08
• The sampling data from the original function actually. Um, I didn't pay too much attention to this. How stupid I am. So in this way, it's natural that my calculation doesn't match the one from FFT function? Now I wonder how should I verify the results of the FFT function? – TurbJet Sep 10 '18 at 1:22