## Window Functions

In the FFT example I carefully chose a cosine wave that will have a full number of cycles in 1 second, which is basically any integer number for the frequency value. The reason for this was had I chosen a noninteger value, I would've ended up with a signal that does not have full wave cycles. The problem with this signal is when you perform the FFT of the signal, you'll start seeing other frequencies and not just the frequency of your original signal. The reason for this is, in essence, FFT assumes the signal is repetitive; that is, it's not just from 0 to 1 second, it's from minus infinity to infinity. And so it treats the signal as if it's copied left and right an infinite number of times. If the signal has an integer number of cycles, it will nicely fit when copied left and right. But in reality, you can't guarantee an integer number of waves in your sampled signal, so you'll start seeing these sampling effects. To minimize the effect, we can use a window function.

Several window functions such as hamming(), hanning(), bartlett(), and kaiser() help minimize this effect but with a cost: the signal itself is also distorted. To use a window, multiply it by the time-domain vector, as shown in Listing 8-6.

Listing 8-6. Hamming Window

F = 25.5 # wave frequency t = arange(N)/float(N) # sampled over 1 second f = t*N # frequency domain x = cos(2*pi*t*F) # the signal xh = x*hamming(512) # multiply with a hamming window plot(f, abs(fft(x)), 's-', label='original')

legend()

title('Signal with Hamming window') xlabel('Frequency [Hz]') ylabel('Amplitude []')

I've plot the FFT of two vectors: the original and the one with a Hamming window. In Figure 8-13, you can see I've zoomed on the 25.5 Hz frequency to show the effects of the window function.

Signal with Hamming window

Figure 8-13. Signal with Hamming window

Figure 8-13. Signal with Hamming window

The scipy.signal module provides additional window functions. To access these, issue

>>> from scipy import signal >>> help(signal)

and scroll down to the window functions section.