# Studying spectral leakage due to DFT length and frequency resolution

I guess this is a pretty basic question, but I am kinda stuck... So if I am remembering the DSP theory correctly, spectral leakage can occur when we take "inappropriate" combinations of the following:

1) FFT length

2) sampling frequency

3) fundamental frequency of input signal

Here is a MATLAB script for demonstrating this, in a case of a sinusoid input:

F0=10; %Frequency of the sinusoid
Fs=100; %Sampling Frequency
observationTime = 1; %observation time in seconds
t=0:1/Fs:observationTime-1/Fs; %time base

x=sin(2*pi*F0*t);%sampled sine wave

N1=100; %DFT length same as signal length
X1 = 1/N1*fftshift(fft(x,N1));%N-point complex DFT of x
f1=(-N1/2:1:N1/2-1)*Fs/N1; %frequencies on x-axis
stem(f1,abs(X1));


MY thought was that if the fundamental frequency is a multiple of the frequency resolution, then there will be no spectral leakage, since there is a bin that corresponds to the exact value of the fundamental frequency F0. However, it seems like I am wrong here...

Can someone please explain to me what's the flaw in my rationale? Why is there leakage in the 3rd plot? Thanks in advance

• what's the input to the FFT of the 3rd plot? is it the same 100 samples that you had at the beginning? did they get zero-padded to 200 samples? or did you extend it with another 10 cycles of the sinusoid? – robert bristow-johnson Dec 10 '15 at 18:28

It looks like what you are doing is only changing the DFT size (using N1 variable), this is equivalent to padding your signals with zeros up to a given length. Your signal (for N1=200) looks like:

It can be immediately seen that the periodic extension is not really a continuous waveform. This discontinuity is introducing the leakage effect that you are observing.

Below a quick and dirty code that demonstrates that. I did both padding of input signal in time domain, as well as calling the fft function with twice the signal size. Plots are in dB scale.

F0=10; %Frequency of the sinusoid
Fs=100; %Sampling Frequency
observationTime = 1; %observation time in seconds
t=0:1/Fs:observationTime-1/Fs; %time base

%% Ideal case
x=sin(2*pi*F0*t);%sampled sine wave

N1=100; %DFT length same as signal length
X1 = abs(1/N1*fftshift(fft(x,N1)));%N-point complex DFT of x
f1=(-N1/2:1:N1/2-1)*Fs/N1; %frequencies on x-axis
X1 = 10*log10(X1/max(X1));
subplot(2,1,1)
plot(f1,X1, 'o-');
title('Ideal case')
grid on

x2 = [x zeros(1, length(x))];

N2=200; %DFT length same as signal length
X2 = abs(1/N2*fftshift(fft(x, N2)));%N-point complex DFT of x
f2=(-N2/2:1:N2/2-1)*Fs/N2; %frequencies on x-axis
X2 = 10*log10(X2/max(X2));
subplot(2,1,2)
plot(f2,X2, 'o-');
hold on
grid on

N2=200; %DFT length same as signal length
X3 = abs(1/N2*fftshift(fft(x, N2)));%N-point complex DFT of x
f3=(-N2/2:1:N2/2-1)*Fs/N2; %frequencies on x-axis
X3 = 10*log10(X3/max(X3));
plot(f3,X3, 'r-');