MATLAB: Question of FFFT in Matlab

fftMATLAB

WIth the code below, I am taking the DFT of the sinewave at multiple samples in frequency domain. Why am I not seeing in DFT spectrum (second part, N=100) clear two impulses at 100 Hz?
clc;
clear all;
f = 128;
Fs = 1024;
t = (0:2047)/Fs;
x = sin(2*pi*f*t);
% calculate full sample point DFT
N = 128;
X = (1/sqrt(N))*fft(x, N);
XdB1 = 20*log10(abs(fftshift(X)));
f1 = -(Fs/2):Fs/N:(Fs/2)-1;
% plot magnitude spectrum

plot(f1, XdB1); title('DFT of sine wave at 100 Hz');
xlabel('Frequency --> f'); ylabel('Amplitude in dB'); axis([-512 511 -30 20]); hold on;
% calculate only 128 point DFT
N = 100;
deltaF = Fs/N;
X = (1/sqrt(N))*fft(x, N);
XdB2 = 20*log10(abs(fftshift(X)));
f2 = -(Fs/2):deltaF:(Fs/2)-1;
% plot magnitude spectrum
plot(f2, XdB2,'r');
xlabel('Frequency --> f'); ylabel('Amplitude in dB');

Best Answer

The frequency of the sine wave is defined as 128 Hz (f = 128) in the above code. Hence there will be two impulses (ideally, but here since x is just an approximation of the sine wave you can observe peaks at that frequency) at 128 Hz and -128Hz in the FFT output.
If you take FFT with N = 128 or N = 100, x is truncated to match the length of N and FFT is computed on the resultant x.
If the frequency f is changed to 100, you could observe a peak at 100Hz.
Related Question