How to perform spectral analysis of a signal using Teensy 4.1.

Motivation and Goal

The Fourier Family Tree

Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT)

As presented in the previous section, among the categories of the Fourier Transform family, the DFT is unique in presenting discrete data in both time and frequency domains. Since computers have a finite memory capacity for data storage, the DFT category is the only one that can be processed by computers.

At this point, it is important to highlight that all categories within the Fourier Transform family involve signals that extend to positive and negative infinity [1]. Even when considering discrete, periodic signals (replicas) in the time and frequency domains—which extend to positive and negative infinity—it is possible to calculate the DFT from a finite set of samples; this is because, due to periodicity in both domains, this finite set of samples or calculated values repeats over time or frequency, making it unnecessary to know the entire domain.

The fundamental DFT equation is: \[ X[k] = \sum_{n=0}^{N-1} x[n] \, e^{-j 2\pi \frac{k}{N} n} \] Considering Euler's formula, we have \( e^{-j\theta} = \cos(\theta) - j\sin(\theta) \), then the exponential term is really just a pair of reference sine and cosine waves at frequency k (DFT basis functions), which are correlated with the x[n] signal.

The DFT acts like a frequency scanner, testing a sampled signal against reference sine and cosine patterns across incremental frequencies. At each step, it calculates the correlation by multiplying the signal by both reference patterns and summing the results—where a high sum reveals that the frequency is present, and cancellation to near-zero means it is absent. Testing two perpendicular patterns (sine and cosine) at once allows the DFT to accurately measure both the total strength (magnitude) and the time alignment (phase) of the wave, regardless of where its peaks start.

In terms of computational operations, a standard DFT requires roughly \(N^2\) complex multiplications and additions for N samples because it evaluates every frequency bin independently from scratch. The FFT slashes this workload down to about \(N \log_2(N)\) operations by exploiting mathematical symmetries in the sine and cosine waves, recursively breaking the signal into smaller even and odd sets to reuse intermediate calculations. For a 1,024-point dataset, this drops the required calculations from 1,048,576 operations down to 10,240—computing the exact same correlation map about 100 times faster. Thus, as previously mentioned, this project will use the CMSIS-DSP library, which includes a function for calculating the FFT.

In addition to the Sampling Theorem, there is another aspect that the designer must consider when implementing code for spectral analysis. Based on the application—which determines the frequency range of interest for the investigation—and in accordance with the Sampling Theorem, the sampling frequency and anti-aliasing filter are determined. Then, the number of samples—or, in other words, the DFT length—must be defined. This will determine the frequency resolution of the FFT. It is determined by a simple formula:$$\Delta f = \frac{f_s}{N}$$Where: \( \Delta f \) is the frequency resolution (bin width), \(f_s \) is the sampling frequency and N is the number of FFT points.

The bin width will correspond to the frequency step of the basis functions, which are correlated with the input signal. If the signal under analysis contains components that match the basis functions, a perfect correspondence will be identified in the frequency domain—as is the case with the 60 Hz component of the input signal shown in Fig. 6.

However, if the signal contains components that do not match the discrete frequencies of the basis functions, the phenomenon known as "spectral leakage" occurs. To illustrate this effect, Fig. 6 shows a spectral component ranging from 177.5 Hz to 182.5 Hz. The corresponding FFT uses 256 points and a sampling frequency of 1280 Hz, resulting in a frequency bin of 5 Hz. Note that the component coincides with the fundamental frequency (36 × 5 Hz = 180 Hz) only when it has a frequency of 180 Hz. At 177.5 Hz or 182.5 Hz, the component's frequency falls in the center of a discrete frequency interval (or bin). This misalignment results in peak reduction and spectral leakage. Spectral leakage transforms a sharp, well-defined peak into a broad hump.

Fig. 6 - Spectral Leakage

Windowing is used to mitigate the problem of spectral leakage. The idea behind this technique is to widen the bin (main lobe) and apply sharp attenuation beyond a certain distance from the bin center (side lobes). There are several standard window functions used in digital signal processing, each striking a different balance between main-lobe width (frequency resolution) and side-lobe suppression (leakage control). Fig. 7 shows the effect of applying Hann windowing. Note that spectral leakage is not eliminated, but rather reduced.

Fig. 7 - Spectral Leakage Reduction applying a Hann window

According to [1], windowing makes the peaks that match and those that do not match the frequencies of the basis functions more similar, the tails are greatly reduced and it reduces the resolution in the spectrum by making the peaks wider. This last effect is not good, which implies a tradeoff between resolution (the width of the peak) and spectral leakage (the amplitude of the tails). It is important to emphasize that windowing cannot fix a poorly chosen DFT length. So we need a proper DFT length to see the detail we want and a window to keep the spectrum clean.

Whether or not windowing is used will depend on the application—which could involve general-purpose equipment (such as an oscilloscope) for sampling unknown signals, or, for instance, sampling power grid signals characterized by a fundamental component with a highly stable frequency. In the case of power grid signals, if the FFT length corresponds to an exact number of grid cycles, the fundamental frequency and its harmonics will align with the basis functions, resulting in a bin-centered peak. This is known as coherent sampling. When the number of cycles does not fit the FFT length exactly, we have non-coherent sampling, resulting in a spread peak. Instead of a clean needle, it forms a wider hill or hump.

Fig. 8 shows the FFT for a signal with a 60 Hz fundamental component, including third and seventh harmonic components. Graph 1a represents coherent sampling, resulting in components aligned with the bin centers. Graph 1b represents non-coherent sampling, where spectral leakage is observed alongside a reduction in peak amplitudes (the third harmonic drops from 0.4 to 0.34). It is important to note that the power grid does not typically exhibit such a large frequency deviation (0.5 Hz); the aim here was simply to highlight the effect of spectral leakage.

Fig. 8 - Coherent and non-coherent sampling

To mitigate spectral leakage, Hann windowing is applied to the signal in Fig. 8, and the result is shown in Fig. 9. We observe a degradation for coherent sampling—specifically, an increase in peak width, although the peak value itself remains unaffected—but a significant improvement for non-coherent sampling, where spectral leakage is reduced compared to Fig. 8 and the reduction in peak amplitude is smaller (from 0.4 to 0.37). Since such a large frequency deviation (0.5 Hz) is unlikely to occur with grid frequencies, windowing can contribute to a better response without the need to worry about grid frequency oscillations.

Fig. 9 - Coherent and non-coherent sampling with Hann Window

Notes:


The Teensy 4.1 Web-based Spectrum Analyzer Code


Observations and Recommendations



References

[1] Steven W. Smith, The Scientist and Engineer's Guide to Digital Signal Processing, Second Edition, California Technical Publishing, ISBN 0-9660176-6-8.

[2] PJRC Electronics Projects - Teensy® 4.1 Development Board