Spectral leakage and windowing#


Windowing is an important part of almost any signal processing system, that helps remove/ reduce spectral leakage when processing a non-periodic signal. This blog post provides a small overview of what is spectral usage, when does it occur and how to use windowing to suppress it.

* keywords: * Windowing, Spectral leakage

What is spectral leakage ?#

As Allen B. Downey mentions in his book Think DSP, in order to understand windowing, we must first understand spectral leakage and for that we have first to understand the Discrete Fourier Transform (DFT) and its related assumptions. When using DFT to compute spectograms, we assume signals to be periodic. In practice, this assumption is often not fulfilled, resulting in inaccurate transform computations containing extra residual frequency components 1. This phenomenon is known as spectral leakage, which is a smearing of power across a frequency spectrum that occurs when the processed signal is not periodic in the sample interval. It occurs because of the discrete sampling in the effective Fourier series computations of a waveform having discontinuities results in additional frequency components 2. Leakage is a common error encountered in digital signal processing, that cannot be entirely eliminated but may be reduced using windowing.

What is windowing ?#

Windowing is a counter-measure against spectral leakage that reduces the amplitudes of the samples at the start and end of a signal window 3. This eliminates the discontinuities, helps with the periodicity of the signal and consequently reduces any leakage 1. Therefore, windowing is none other than a multiplication of the input signal with a predefined function.

\begin{eqnarray} S = \sum_{i=0}^{\#F-1} F[i] . w \end{eqnarray}

where:

  • \(S\) : Signal array.

  • \(\#F\) : Number of frames.

  • \(w\) : window function.

How to implement it?#

This is depending on the type of the windowing function to use as there are many. However, since it is just a multiplication, the window samples are simply generated using a predefined function and multiplied with the signal samples. In python this can be done easily using numpy which offers a wide variety of windowing_function

Code#

Here is a small code snippet implementing windowing that I wrote as part of a python audio features extraction library named spafe

Windowing#
 1 import numpy as np
 2
 3
 4 def windowing(frames, frame_len, win_type="hamming", beta=14):
 5     """
 6     generate and apply a window function to avoid spectral leakage.
 7
 8     Args:
 9       frames  (array) : array including the overlapping frames.
10       frame_len (int) : frame length.
11       win_type  (str) : type of window to use.
12                         Default is "hamming"
13
14     Returns:
15       windowed frames.
16     """
17     if   win_type == "hamming" : windows = np.hamming(frame_len)
18     elif win_type == "hanning" : windows = np.hanning(frame_len)
19     elif win_type == "bartlet" : windows = np.bartlett(frame_len)
20     elif win_type == "kaiser"  : windows = np.kaiser(frame_len, beta)
21     elif win_type == "blackman": windows = np.blackman(frame_len)
22     windowed_frames = frames * windows
23     return windowed_frames

Conclusion#

This blog presented windowing, which is a fundamental signal processing technique that helps eliminate discontinuities in a the frames and consequently avoid spectral leakage. This method is often used before any spectrum computations. There are various types of windowing, each having its own pros and cons. In the python code introduced in this post, we included some of the most used functions but these can be customized to the user's discretion.

Share this blog#

Updated on 08 April 2022

👨‍💻 edited and review were on 08.04.202

References and Further readings#

1(1,2)

Downey, A. B. Think DSP. Green Tea Press, 2014.

2

Leakage, 7.2.2 in Lecture 7-The Discrete Fourier Transform. [Online; accessed 13.03.2020]. URL: http://www.robots.ox.ac.uk/~sjrob/Teaching/SP/l7.pdf.

3

Douglas Lyon. The Discrete Fourier Transform, Part 4: Spectral Leakage. Journal of object technology, 2009.