# Sampling frequency and FFT

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


• Which features in the spectrum are you calling "errors"? Commented Dec 24, 2015 at 22:29
• Small frequency components near the given signal frequencies Commented Dec 24, 2015 at 22:31

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.
t = 1/Fs:1/Fs:1