Understanding spectral leakage and window functions
Anyone who has worked with Fourier analysis of signals will know that spectral leakage can be a big problem when attempting to detect frequency peaks. Spectral leakage occurs during the conversion process from a time domain signal into frequency domain. It is important to know that this leakage is not an artifact but in fact a perfect sinusoidal representation of what the Fourier Transform has analyzed. The Fourier Transform expect truly periodic measured signal intervals in order to build the frequency domain representation of a perceived repetitive never ending signal. When the signal is not periodic in nature the Fourier Transform will introduce sharp discontinuities into the signal which are then processed into sinusoids. The spectral energy from these sinusoids spread across FFT bins from the fundamental frequency peak.
View the interactive demo (Processing applet).
Part 1: Spectral Leakage
The spectral leakage spreads out from the main primary peak (A) 440Hz and the secondary peak (A#) 466Hz is buried making it very hard to detect. Because the measured signal interval is out of sync from the phase of the signal leakage will occur even with a perfect sine wave.

Part 2: Discontinuities
A perfect sine wave should not have any leakage at all when the measured signal interval is in phase with the signal, ie, the interval length is equal to a multiple of the phase length of the signal, the end points of the signal interval will match up perfectly. When the end points of the measured signal interval do not match up perfectly, slight to severe discontinuities are introduced.
Part 3: Window function
Windowing is the process of shaping the measured signal to a curve that tapers to zero (or near zero) at the end points. In this way the measured signal is attenuated at the end points to ensure that the sharpness of discontinuities is dampened. In the following example the measured signal is multiplied by a Hamming window function. You can see that the buried secondary peak from Part 1 is now clearly visible.

On a side note, Minim, the included Processing sound library has just been added to Github. Some of the new window functions I have added have already been merged into the master branch if you want to try them out in your own projects.

