Sampling frequency and FFT

fftMATLABsamplingsignal processing

I have tried this example code for sampling and FFT in MATLAB, but I have noticed that when the signal frequency becomes closer to nyquist frequency , some errors ( small frequency components near the given signal frequencies ) in the frequency domain begin to show up. Is there any explanation for this situation?

close all;clear all; clc;
freq = 100;
freq1 = 1000;  
freq2 = 2000;  
freq3 = 3000;  
freq4 = 4000;  
Fs    = 10000;
t = 0:1/Fs:1;

x = cos(2*pi*freq*t) + cos(2*pi*freq1*t)+cos(2*pi*freq2*t) ...
    +cos(2*pi*freq3*t)+cos(2*pi*freq4*t);
tempfft = abs(fftshift(fft(x)));
figure;
stem(0:floor(Fs/2),tempfft(floor(Fs/2):Fs));

Output FFT figure

Best Answer

In comments you said that the "errors" you're interested in are the non-zero FFT response at frequencies near the signal frequencies.

Those are caused by windowing.

Your time vector, generated by the expression 0:1/Fs:1, has 10,001 elements, and your sampling window is 1.0001 seconds. So it does not contain an integer number of cycles of any of your input signals. Therefore the truncated time domain sequence does contain components at the frequencies slightly off the intended frequencies.

If you used

t = 1/Fs:1/Fs:1

to generate the time vector, you would have full cycles of your signal waveforms, and you wouldn't see this effect.