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