10
$\begingroup$

Let's consider this example:

Fs=1000; 
Ns=500;
t=0:1/Fs:(Ns-1)*1/Fs;
f1=10;
f2=400;
x=5+5*sin(2*pi*f1*t)+2*sin(2*pi*f2*t);
X=fft(x);

In this scenario, frequency resolution is 2, and all frequency components are captured correctly. However, if I do this:

  X=fft(x,1000);

frequency resolution is 1, but there is a spectral leakage. Similar effect is seen here. It seems to me, that Fourier transforms of both windows (one with length 500, and one with length 1000) have zeros at the frequencies that are presented in the signal, so I don't see why leakage will happen?

$\endgroup$
  • $\begingroup$ zero-padding won't reduce the apparent spectral leakage, but will only make the bumps of spectral leakage appear more smooth. $\endgroup$ – robert bristow-johnson Jan 11 '18 at 4:15
17
$\begingroup$

This phenomenon has nothing to do with spectral leakage. What you are observing is the effect of zero padding. Given a number of samples $N$, there is a maximum possible frequency resolution $\Delta f$ that can be achieved:

$$\Delta f=\frac{f_s}{N} $$

In your case $\Delta f$ is exactly $2\;\mathrm{Hz}$. If you zero-pad your signal, there is no extra information to retrieve - you will only decrease the frequency spacing.

In the example above, when you increase $N$ to $1000$, you get a frequency spacing of $1\;\mathrm{Hz}$. All extra observed samples are merely an interpolation, done by the window function ($\mathrm{sinc}$ in your case). You will start observing side-lobes of the window spectrum. Since you implicitly multiplied your signal by a rectangular window, this will result in the convolution of the spectrum of your signal (two Dirac's + DC) with the $\mathrm{sinc}$ function.


Another way to look at it is to imagine that DFT is basically a filter bank, consisting of shifted $\mathrm{sinc}$ functions. Those are aligned in such a way, that peak of one is where zeroes of all the remaining ones are present. If you start looking in between those zeros, you will start taking those samples. Here is an example plot of such $\mathrm{sinc}$ filter bank.

enter image description here

Let's imagine that frequency corresponding to the blue filter is present. That will yield the amplitude in a corresponding bin. All the remaining frequencies are not present (orange and yellow), thus you multiply those $\mathrm{sinc}$'s by $0$ and get nothing in the bins. In the case of zero padding, that will be no longer the case. Samples of that blue $\mathrm{sinc}$ will fall in the intermediate bins and will be sinc-interpolated.


Here is what happens for $N=1000$ and $N=10000$:

enter image description here

And a zoomed part:

enter image description here

Things to notice:

  • For $N=500$, there is no leakage whatsoever. There are perfect spikes, representing each of your frequencies and DC offset.

  • We can also observe the FFT noise at the very bottom.

  • For $N=10000$ the shape of $\mathrm{sinc}$ function is clearly visible.


And obviously the code to reproduce the results:

Fs=1000; 
Ns=500;
Ns2=1000;
Ns3=10000;
t=0:1/Fs:(Ns-1)*1/Fs;
f1=10;
f2=400;
x=5+5*sin(2*pi*f1*t)+2*sin(2*pi*f2*t);

X1 = abs(fft(x))/length(x);
X2 = abs(fft(x, Ns2))/Ns;
X3 = abs(fft(x, Ns3))/Ns;

F1 = 0:Fs/Ns:Fs-Fs/Ns;
F2 = 0:Fs/Ns2:Fs-Fs/Ns2;
F3 = 0:Fs/Ns3:Fs-Fs/Ns3;

plot(F1, 20*log10(X1))
hold on
plot(F2, 20*log10(X2))
plot(F3, 20*log10(X3))
xlim([0, Fs/2])
grid on
legend({'N=500', 'N=1000', 'N=10000'})
$\endgroup$
  • 1
    $\begingroup$ Very complete answer +1. "[...] you will only increase the frequency spacing." It should be decrease I suppose. $\endgroup$ – Matt L. Jul 15 '16 at 12:20
2
$\begingroup$

Spectral leakage is usually another name for the Sinc convolution effect or artifact of rectangular windowing in the other domain (t or time in your case). And zero padding is done by adding a rectangular window (which is your original non-zero-pad data) to an longer FFT.

Your hypothesis that the FT should be zero at all but one frequency is false in general. Any finite length (and non-zero) signal will have an infinite extent of non-zero spectrum. That infinite extent of spectrum (Sinc shaped, or the transform of other Windows) will happen to be invisible in a DFT/FFT result only for pure sinusoids than span the entire FFT width with exact integer periodicity in that width. Zero padding does not allow that.

$\endgroup$
1
$\begingroup$

Leakage arises notably with finite length windows, which you always have in practice. However, if you have exactly an integer number of periods of your sine components, FFT inherent periodization acts as if the sines were "infinite", and its frequencies exactly fall on discretized bins. And thus leakage is somehow cancelled, out of pure luck: if you knew in advance the period of your signal, you would not need to analyse it with Fourier tools.

With zero-padding you don't have a pure sine anymore. Neither with non integer period multiple window. You are concatenating chunks of sines that have abrupt changes at the window boundaries. So the whole periodized signal is not a "infinite sine" anymore. Hence you can get what you assimilate with leakage, but which is a zero-padding effect, as perfectly explained by @jojek.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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