electronic-and-computer-meng

An Introduction to Data Windows and the Motivations for Their Use in DSP

Published 1st May 2023

Abstract

This article introduces data windows and the motivations for their usage in digital signal processing (DSP). We delve into the spectral leakage phenomenon and explore the performance of the finite-length data window and compare it with the more specialised Hanning window. The data window methods are then applied to a problem to illustrate performance improvements. And finally, to conclude, there is a discussion to summarise this topic, including key points and a brief insight into other application areas for data windows in DSP.

Introduction

Almost all real-world applications discretise the induced continuous signal in preparation for processing. An output spanning for a duration too large or infinite makes it computationally impossible to manipulate as a whole, resulting in bottlenecks for DSP techniques such as the Discrete Fourier Transform (DFT), where the calculation requires all data points to be available for analysis. By applying the process of data windowing, we can overcome this bottleneck. Windowing techniques enable the analysis of more computationally able isolated subset segments by selecting a finite-length window. Data windows consist of a mathematical function that can be applied to a signal to induce an amplitude weighting, often between ‘0’ and ‘1’, on each sample in a discrete-time data series in a finite set.

The application of windowing functions to data in the frequency domain is made possible by exploiting a mathematical operation known as convolution. As shown in Equation 1, where x(t) and X(f) is the input signal in time and frequency domains, w(t) and W (f ) is the same for the window function.

The Phenomenon of Spectral Leakage

The DFT of sampled real-world data can result in misleading frequency-domain responses due to a characteristic known as spectral leakage, which causes the analysis output to only approximate the authentic spectra before sampling. By removing the assumption that the signal comprises frequency components that are integer multiples of the fundamental frequencies, we can start to consider leakage.

We can visualise leakage by comparing the frequency response of a sinewave windowed to 3 complete cycles and another to 3.4 [1]. As there isn’t an integer multiple of components for the latter case, the incomplete cycles induce the phenomenon, as shown by the energy ‘leaking’ into the neighbouring frequency bins in Figure 1.

Figure 1: Illustration of Spectral Leakage

Despite not seeing any leakage in the 3-cycle sinewave, we cannot assume its elimination, as leakage is a characteristic of DFT and is always prevalent. We can prove this by performing a zero-padded DFT. Although still prevalent, it is possible to take steps to reduce it by implementing more advanced windowing techniques.

Examples of Data Windows

The finite-length data windowing technique will be defined, followed by the Hanning technique. With MATLAB, the time and frequency response plots for each windowing function can help with the analysis.

The Finite-Length Data Window

Also known as the rectangular (or boxcar, or truncation) data window, this is a simple technique consisting of a rectangular pulse function, where the amplitude is ‘1’ within the window region and ‘0’ outside. The rectangular window can be mathematically defined by Equation 2, where Nw is the window length in samples.

data_width = 64;
rect_start = 0;
rect_duration = 63;
rect_window = zeros(1,data_width); rect_window(rect_start+1 : rect_start+1+(rect_duration)) = ,! 1;
plot(0:1:data_width-1, rect_window, 'LineWidth', 1);
xlim([0 63]);
ylim([0 1.2]);
title('Time Response of Rectangular Window');
ylabel("Amplitude");
xlabel("Time / (s)");

Figure 2: Time and Frequency Response of a Rectangular Window

Shown in Listing 1 is a MATLAB script to generate a rectangular window. Simply, an array of the same length as the signal to apply on to (defined by data_width), filled with zeros except for the window section defined by the rect_start as the start sample index and the rect_duration as the number of samples to span.

X_rect_window = fft(rect_window, 2048); X_rect_window_norm_db = 20*log10( abs(X_rect_window) / ,! max(abs(X_rect_window)) );
plot(0:1:2047, X_rect_window_norm_db, 'LineWidth', 1); title('DFT Frequency Response of Rectangular Window'); xlabel("Frequency Bins / (k)");
ylabel("Normalised Logarithmic Magnitude");
xlim([0 1024]);

The time response and DFT frequency response can help visualise the rectangular window. With the help of MATLAB, we can perform a zero-padded FFT and plot a frequency response normalised and logarithmically scaled, as shown in Listing 2. We perform 2048-point FFT on the 64-point rectangular window. To normalise the peak to 0dB, we take the logarithm of the absolute values divided by the peak value. Illustrated in Figure 2 are the time and frequency responses for the rectangular window, further showing a characteristic feature, with the peak sidelobe level of approximately 13dB above the adjacent sidelobes.

The Hanning Window

A more specialised windowing technique mentioned previously is the Hanning window. Also known as the Hann window, Equation 3 provides a mathematical definition. Where w(n) is the amplitude of the window at index n, and N is the length of the window. Like rectangular windows, the Hann function peaks at an amplitude of ‘1’ but gradually tapers to ‘0’ on both sides due to the cosine component.

Figure 3: Time and Frequency Response of a Hanning Window

Figure 4: Rectangular and Hanning Frequency Response Comparison

data_width = 64;
hann_start = 0;
hann_duration = 63;
hann_index = 0:1:hann_duration;
hann_window = zeros(1,data_width); hann_window(hann_start+1 : hann_start+1+(hann_duration)) = ,! 0.5 - 0.5*cos(2*pi*hann_index/hann_duration); plot(0:1:data_width-1, hann_window, 'LineWidth', 1); xlim([0 63]);
ylim([0 1.2]);
title('Time Response of Hanning Window');
ylabel("Amplitude");
xlabel("Time / (s)");

Listing 3 provides a programmatical definition for the Hanning window in MATLAB. Where data_width defines the width of the whole signal, hann_start and hann_duration define the arbitrary start point and duration. All regions outside the Hanning function will have a zero magnitude, enabling the window application to segment the input signal.

X_hann_window = fft(hann_window, 2048); X_hann_window_norm_db = 20*log10( abs(X_hann_window) / ,! max(abs(X_hann_window)) );
plot(0:1:2047, X_hann_window_norm_db, 'LineWidth', 1); title('DFT Frequency Response of Hanning Window'); xlabel("Frequency Bins / (k)");
ylabel("Normalised Logarithmic Magnitude");
xlim([0 1024]);

Similar to Listing 2 and shown in Listing 4, we can plot the logarithmically scaled frequency response for the Hanning window. Shown in Figure 3 is just that, a frequency response beside the time response. The plot shows the peak sidelobe level of approximately 30dB above the adjacent sidelobes, characteristic of the Hanning window.

Analysing Results

The finite-length window’s frequency response is usually used as a reference when evaluating the performance of another windowing technique. Figure 4 shows the linear- scaled and logarithmic-scaled frequency responses for both windows. Assessing the linear-scaled plot, we can notice a prolonged oscillation in the rectangular window response due to more leakages. On the other hand, the Hanning window levels off quickly. The Hanning window achieves a leakage reduction due to the cosine function tapering, allowing for a much more gradual truncation of the signal, as the sharp truncation in the rectangular window caused by the vertical rising and falling edge causes discontinuity. Discontinuity from the abrupt truncation causes leakage when performing the DFT.

From the normalised logarithmic plot, it is easier to notice that the Hanning window enables more concentration in the signal energy, allowing for leakage suppression, as shown by the lower sidelobes. The Hanning window’s main lobe is wider than the rectangular window, resulting in frequency resolution trade-offs in exchange for better frequency response. The reduced amplitude of the Hanning window is the result of the cosine tapering, as the amplitude of the window decreases towards the truncation edges.

Example Application of Data Windows

Considering two cases where one consists of a signal whose frequency components are of integral fundamental frequency multiples and another whose are not, the rectangular data window’s effect on spectral leakage can be illustrated and compared with that of the Hanning window with an example [2].

In the first case, the definition of the signal x1(n) residing in Equation 4, the window duration Tw is an integer multiple of the period associated with the cosine Ta, allowing for an even concatenation, preserving the periodicity. This signal has a frequency component of 10 at an amplitude of 1 and another at 16 with an amplitude of 0.01.

In the second case, the definition of the signal x2(n) residing in equation 5, the window duration Tw is a non-integer multiple of the period associated with the cosine Ta, resulting in an uneven concatenation. This signal has a frequency component of 10.5 at an amplitude of 1 and another at 16 with an amplitude of 0.01.

Figure 5: Frequency Responses x1 and x2 With a Rectangular Window

Figure 6: Zero-Padded Frequency Responses of x2 With a Rectangular Window

By plotting the frequency response for both these cases, we should see two distinct peaks at the frequency components mentioned before, the amplitude of the leftmost larger than the other. The plot at the top in Figure 5 shows the response for case 1, and as anticipated, we see the respective peaks.

However, the plot at the bottom in Figure 5 portrays a different story for case 2, as leakage is detected and distortion is visible. Zero padding enables the extraction of additional detail from the frequency response. Applying a 1024-point DFT to the 128-point signal in case 2, one would assume the frequency response to peak at the 84th frequency bin and another 40db lower at the 108th bin. However, Figure 6 shows an abhorrent side-effect of leakage called masking, resulting in a masking effect affecting lower amplitude oscillations.

X2_hann = fft(x2.*hann_window, 128);
plot(n, 20*log10( abs(X2_hann) / max(abs(X2_hann)) ) , ,! 'LineWidth', 1);
xlim([0 64]);
ylim([-100 0]);
title('DFT Frequency Response of X2 Hanning Window'); xlabel("Frequency Bins / (k)");
ylabel("Normalised Logarithmic Magnitude");

As the leakage occurs through the sidelobes of the sinc function in the transformed rectangular window, reducing the magnitude of these sidelobes can reduce leakage. We can apply the Hanning window to case 2, using element-wise multiplication in MATLAB, as shown in Listing 5, to show how the tapered edges suppress leakage.

Figure 7: Frequency Responses of x2 With a Hanning Window

Shown in Figure 7 is a frequency response of case 2 with a Hanning window applied, an illustration of how effective a non-rectangular window is at leakage suppression. With the Hanning window, the effect of masking is no longer as big of an issue as before, as we can now detect the fainter amplitude oscillations at the 16th frequency bin.

Discussion

From the sections above in this article, we have proved the advantages that data windows provide us to isolate specific features by segmenting from a large or infinite dataset, thus, enabling applications such as separating individual heartbeats for feature extraction in biomedical analysis or extracting individual spoken words from an audio recording for speech recognition, aiding human-computer interaction.

Rectangular windows, although a simple technique to im- plement, can result in additional spectral leakage due to their sharp truncations, and as we’ve previously explored, a window with tapered truncations, such as the Hanning function, can effectively reduce this phenomenon. There is a vast array of different windowing techniques, each with unique character- istics and use cases.


Function Name Main-lobe Width Sidelobe Width Common Uses
Rectangular 4pi/N -13.32 dB Period signals that are perfectly spaced or for signals shorter than the length of the window.
Hanning 8pi/N -31.56 dB All-purpose window function.
Hamming 8pi/N -43.75 dB All-purpose window function.
Blackman 12pi/N -43.75 dB All-purpose window function.
Parzen 16pi/N -53.05 dB Density estimation, machine learning, pattern classification.

Figure 8: Time and Frequency Response Comparison of Various Window Functions

Above in Table I are the characteristic parameters and use cases for three additional windowing techniques, Hamming, Blackman and Parzen [3]. The sidelobe height is a good metric to judge how effective the windowing function is at suppressing leakage caused by the truncation of the signal, with higher usually representing a better suppression. Similarly, the main lobe width is a valuable way to gauge the resolution of the frequency analysis, where a narrower width usually results in a higher frequency resolution, although with an increased sidelobe trade-off.

Shown in Figure 8 is a comparison of time and frequency responses of the six previously mentioned window functions.

References

[1] R. G. Lyons, “3.8 DFT Leakage,” in Understanding Digital Signal Processing, Upper Saddle River: Pearson, 2011, pp. 81-83.

[2] B. Mulgrew, P. M. Grant, and J. S. Thompson, “9.3.3 The rectangu- lar window,” in Digital Signal Processing: Concepts and Applications, Houndmills: Palgrave, 2001, pp. 254-259.

[3] “Table of window function details,” VRU, 11-Aug-2021. [Online]. Avail- able: https://vru.vibrationresearch.com/lesson/tables-of-window-function- details/. [Accessed: 03-Apr-2023].