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) ...
tempfft = abs(fftshift(fft(x)));

Output FFT figure

  • \$\begingroup\$ Which features in the spectrum are you calling "errors"? \$\endgroup\$
    – The Photon
    Commented Dec 24, 2015 at 22:29
  • \$\begingroup\$ Small frequency components near the given signal frequencies \$\endgroup\$
    – rcmalli
    Commented Dec 24, 2015 at 22:31

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.