what is difference between fft(x) & fft(x,n)

9 views (last 30 days)
nag
nag on 25 Feb 2014
Commented: Kiki on 24 Mar 2016
clc; close all; clear all;
fc=1000; fs=15000; f1=1/fs; t=[0:f1:1-f1]; x=sin(2*pi*fc*t); figure; plot(t(1:100),x(1:100)); xlabel('time'); ylabel('amplitude'); title('Sine wave of Frequency 1 KHz');
fc=2000; f1=1/fs; t=[0:f1:1-f1]; y=sin(2*pi*fc*t); figure; plot(t(1:100),y(1:100)); xlabel('time'); ylabel('amplitude'); title('Sine wave of Frequency 2 KHz');
z=x+y; figure; plot(t(1:100),z(1:100)); xlabel('time'); ylabel('amplitude'); title('Mixed signal');
z1=fft(z,4096); len=length(z1); zdft = (1/len)*z1; freq=0:(fs/len):fs/2-(fs/len); figure; plot(freq,abs(zdft(1:len/2))) xlabel('Frequency'); ylabel('amplitude'); title('Single sided Frequency Spectrum');
In this code at z1 if we use fft(x) i am getting 2Hz width at 2K frequency in frequency spectrum,But if i use fft(x,4096) i am getting width of 20-30 Hz at 2K frequency....Why is this so , Can any one explain the reason.

Answers (2)

Dishant Arora
Dishant Arora on 25 Feb 2014
fft(x) computes discrete fourier transform having as many samples as the original signal x whereas fft(x,n) will have n points in dft. Usually n is taken as integral power of 2 to fasten the computation. In order to recover original signal from fft, number of samples in fft must be greater than or equal to number of samples in original signal . As n increases resolution in frequency domain increases i.e the distance between two consecutive frequency bins decreases .

Rick Rosson
Rick Rosson on 25 Feb 2014
Compare:
>> dF_1 = fs/length(z)
with:
>> dF_2 = fs/length(z1)
  1 Comment
Kiki
Kiki on 24 Mar 2016
Hi
I had a similar question. My codes are as follows.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
n=L;
Y2 = fft(S,n);
P22 = abs(Y2/n);
P11 = P22(1:n/2+1);
P11(2:end-1) = 2*P11(2:end-1);
f2 = Fs*(0:(n/2))/n;
figure(4)
plot(f2,P11)
title('1000 Point DFT Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P11(f)|')
n=4096;
Y2 = fft(S,n);
P22 = abs(Y2/n);
P11 = P22(1:n/2+1);
P11(2:end-1) = 2*P11(2:end-1);
f2 = Fs*(0:(n/2))/n;
figure(5)
plot(f2,P11)
title('1024 Point DFT Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P11(f)|')
n=1000 gives me a perfect spectrum with narrowest main lobe and 0 side lobes, while n=1024 gives me a wider main lobe and some side lobes. I thought bigger N would give us better frequency solution, but why would it add some "noise" to it?
Thank you very much! Kiki

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!