Search code examples
matlabfftsamplingdft

correct sampling points for FFT


I want to calculate the Fourier series of a signal using FFT on matlab. And I came across the following unexpected issue. Mere example

If I define a grid and then compute the fft as:

M=59;           
x= deal(1*(0:M-1)/M);
y=3*cos(2*pi*x);
Yk=fftshift(fft2(y)/(M));

which gives me the exact analytic values expected: Yk(29)=1.5; Yk(31)=1.5; zeros anything else enter image description here

but if I define the grid as, and repeat the fft calculation:

x=0:1/(M-1):1;
y=3*cos(2*pi*x);
Yk=fftshift(fft2(y)/(M));

got the Yk's values completely screwed up enter image description here

This is an annoying issue since I have to analyse many signals data that was sampled as in the second method so the Yk's values will be wrong. Is there a way to workaround this? an option to tell something to the fft function about the way the signal was sampled. Have no way to resample the data in the correct way.

The main reason to avoid have spectral leaking, is that I do further operations with these Fourier terms individually Real and Imag parts. And the spectral leaking is messing the final results.


Solution

  • The second form of sampling includes one sample too many in the period of the cosine. This causes some spectral leaking, and adds a small shift to your signal (which leads to non-zero imaginary values). If you drop the last point, you'll cosine will again be sampled correctly, and you'll get rid of both of these effects. Your FFT will have one value less, I don't know if this will affect your analyses in any way.

    x = 0:1/(M-1):1;
    y = 3*cos(2*pi*x);
    Yk = fftshift(fft2(y(1:end-1))/(M-1));
    
    >> max(abs(imag(Yk)))
    ans =
         1.837610523517500e-16