# Question of FFFT in Matlab

11 views (last 30 days)
Nathan Jaqua on 22 Oct 2019
Answered: Ajay Pattassery on 31 Oct 2019
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');
##### 2 CommentsShowHide 1 older comment
Daniel M on 22 Oct 2019
Not able to test it right now, but seems odd for you to only take a 128 point fft. Just take the whole thing, should be 2047. You will probably get better resolution. By the way, your code and your comments do not match up with each other. It makes it difficult to assess what level of understanding you have of this process.

Ajay Pattassery on 31 Oct 2019
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.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!