Monday, 28 December 2015

frequency spectrum - Why does spectral leakage arise in an FFT?


I've tried googling and wikipedia-ing it, but I haven't gotten any answers beyond 'it's because the frequency of the input signal is sitting between two bins'.


I understand that this is the reason, but what I can't understand is why the leakage seems to extend to several adjacent bins rather than only one adjacent bin.



To illustrate what I'm talking about, here's some simulated data (code at the end of the post) : Freq_10


Above is the FFT spectrum (plotted on a log scale) of a sine wave of frequency 10. The sampling rate is one, and the number of samples is 100. The graph has been FFT-shifted. There's clearly only a peak at bin 10, and the rest is on the order of numerical error, or there about.


Freq_10_1


This is the frequency spectrum at a generated frequency of 10.1. Clearly there is 'leakage' into more bins than just the immediately adjacent bin.


freq_10_5


This is the plot for a frequency of 10.5.


Question: Why is there this leakage, and why does it extend to all the other bins, rather than the immediate adjacent bin?




Code, for anyone who's interested (Python code)


import numpy as np

import matplotlib.pyplot as plt

xFreq = 10.5
xSize = 100.0
xPeriod = xSize/xFreq
x = np.linspace(1,xSize,xSize)

data = np.sin(2*np.pi*x/xPeriod)
fft = np.fft.fft(data)
fft = np.fft.fftshift(fft)


fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(abs(fft), "o")
ax.set_yscale('log')
plt.show()

I changed the xFreq value from 10.0 to 10.5, etc.



Answer



An FFT has finite length, and thus constitutes a default rectangular window on a data stream. A window in the time domain results in a convolution in the frequency domain with the transform of the window. Note that the transform of a rectangular window is a Sinc function (sin(x)/x), which has infinite width. It's not just 2 bins in width. Thus the ripples of the Sinc function will show up as "leakage" far from any spectral peak that is not perfectly periodic in the FFT's length.



The picture below shows part of the frequency response of the sinc function. When the tone is centered on one of the bins all of the other points are lined up with the nulls in the frequency response. If it isn't centered on a bin then it's like shifting the whole frequency response, which causes the other bins to fall on non-null portions of the frequency respoonse.


enter image description here


Another way to look at it is that an FFT is just a filter bank, where each filter stop-band floor has lots of ripples, and is certainly not infinite in attenuation more than 1 bin away from center frequency. Some windows (von Hann,etc.) other than rectangular have lower stop bands, which is one reason for their popular use.


No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...