Article

# Using Windowing with Discrete Fourier Transforms

Multiply signal data by window sequence values to reduce spectral leakage.

## Overview

Discrete Fourier and cosine transforms, which decompose a signal into its component frequencies and recreate a signal from a component frequency representation, work over vectors of specific lengths. For example, if you're analyzing audio data, you may be supplied with pages of 1024 samples. Discrete Fourier and cosine transforms can accurately approximate the component frequencies that have an integer number of periods, that is, signals where the start and end points join to form a continuous waveform. However, with noninteger period signals, where the endpoints don't meet, the discontinuities appear as false frequency components in a forward transform. This smearing of data is called spectral leakage.

Here you'll learn an approach to reduce spectral leakage when performing transforms over data that includes noninteger period signals. Windowing multiplies a signal by a vector that represents a smooth curve with boundary values of zero or near zero. This technique ensures the endpoints of a signal meet and reduces the discontinuities.

The signal data used here is synthesized from a series of sine waves. In a real-world app, you will most likely acquire signal data from a sensor such as as a microphone.

### Synthesize a Test Signal

Use the following function to generate a composite sine wave from a supplied array of component frequencies and amplitudes:

### Create a Signal with an Integer Number of Periods

The following code shows a Fourier series approximation of a square wave built from a series of sine waves. Each component sine wave has an integer number of periods over the length of the data:

Use the vDSP fast Fourier transform (FFT) to compute the component frequencies of `signal`:

To learn more about computing the frequency components of a signal, see Finding the Component Frequencies in a Composite Sine Wave.

The FFT treats the data set as a single period of a continuous signal. The following visualization wraps the signal around a virtual cylinder to illustrate how the FFT interprets the data, and this figure also shows that the endpoints meet:

The following illustration shows a representation of the signal, in blue, and uses yellow to highlight the imaginary parts, that is, the `imagParts` array, of the result of the forward FFT:

The FFT result clearly shows that the signal is composed of 25 sine waves, represented as spikes in the graph.

### Create a Signal with Noninteger Number of Periods

The following code is subtly different from the previous listing. The code defines noninteger periods, of `5.75`, of the component sine waves:

The following visualization wraps the noninteger period signal around a virtual cylinder and shows the endpoint discontinuities:

The image below shows the results of a transform of this data: the results shows additional, intermediate values that are a result of spectral leakage:

### Create a Windowed Signal with Noninteger Number of Periods

The following code shows the same noninteger period signal, but in this example, you multiply the signal by the result of `window(ofType:usingSequence:count:isHalfWindow:)`.

The illustration below shows the windowed signal in blue, with its boundaries tapered toward zero, and the transformed version with reduced spectral leakage in yellow:

### Select Window Sequence

vDSP provides functions for generating three different windows:

### Create a Sine Wave with Noninteger Period

To understand the different effects of the different windows provided by vDSP, create a single composed of a signal sine wave with a noninteger period:

The illustration below shows the sine wave and the forward FFT result.

Spectral leakage is apparent throughout the rendered FFT result.

### Reduce Spectral Leakage Using a Hann Window

All the above examples have used the Hann window sequence. This is a great-general purpose window that reduces spectral leakage in most cases as illustrated below.

### Reduce Spectral Leakage Using a Hamming Window

The Hamming window, created by passing `vDSP.WindowSequence.hamming` to the `window(ofType:usingSequence:count:isHalfWindow:)` function, unlike the Hann window, doesn't reach zero at its boundaries.

The figure below shows the result of multiplying a signal by a Hamming window: high values around the base frequency in the forward FFT are tighter than the Hann-windowed result, but there is low-level spectral leakage across the entire forward FFT.

The narrower main peak of the Hamming-windowed result provides better discrimination of component sine waves with close frequencies.

### Reduce Spectral Leakage Using a Blackman Window

The Blackman window, created with `vDSP.WindowSequence.blackman`, does a better job of reducing spectral leakage away from the main frequency compared to Hann and Hamming, but has a wider main peak than Hann.

### Fourier and Cosine Transforms

Finding the Component Frequencies in a Composite Sine Wave

Use 1D fast Fourier transform to compute the frequency components of a signal.

Signal Extraction from Noise

Use Accelerate’s discrete cosine transform to remove noise from a signal.

Halftone Descreening with 2D Fast Fourier Transform

Reduce or remove periodic artifacts from images.

Fast Fourier Transforms

Transform vectors and matrices of temporal and spatial domain complex values to the frequency domain and vice versa.

Discrete Fourier Transforms

Transform vectors of temporal and spatial domain complex values to the frequency domain and vice versa.

Discrete Cosine Transforms

Transform vectors of temporal and spatial domain real values to the frequency domain and vice versa.