Sign In / Join Cloud 360
Engineering • Innovation • Learning
Microcontrollers Advanced Level 26 min

Fast Fourier Transform (FFT) & Nyquist-Shannon Sampling

Cooley-Tukey radix-2 FFT decomposition, frequency bin resolution, anti-aliasing filtering, and spectral leakage windowing functions.

Prerequisites: Continuous signals and systems, Euler formula, complex numbers, and ADC quantization.

1. Overview & Concept Definition

Efficient $O(N \log N)$ computational algorithm that computes the Discrete Fourier Transform (DFT) of a sequence, converting digitized time-domain signals into discrete frequency-domain spectrum magnitude and phase representations.

2. Engineering & Industrial Significance

Direct DFT evaluation requires $O(N^2)$ complex multiplications, overwhelming embedded processors. FFT enables real-time spectral decomposition on microcontrollers (e.g. ARM Cortex-M DSP instructions) for vibration analysis, audio processing, radar, and acoustic anomaly detection.

3. Physical Working Principle & Transducer Dynamics

The Nyquist-Shannon sampling theorem dictates sampling frequency $f_s > 2 f_{max}$ to prevent aliasing. Continuous analog input must first pass through an analog low-pass anti-aliasing filter before ADC quantization. Cooley-Tukey divides an $N$-point time-domain sequence into odd and even interleaved sub-transforms recursively, combining them via complex exponential twiddle factors ($W_N^k = e^{-j 2\pi k / N}$). Windowing functions (Hanning, Hamming, Blackman) taper non-periodic time sample boundaries to zero, minimizing high-frequency sidelobe spectral leakage across frequency bins.

4. Governing Equations & Mathematical Models

Discrete Fourier Transform: $X[k] = \sum_{n=0}^{N-1} x[n] \cdot e^{-j \frac{2\pi}{N} k n}$
Nyquist Rate: $f_s \ge 2 f_{cutoff}$
Frequency Bin Resolution: $\Delta f = \frac{f_s}{N} = \frac{1}{T_{window}}$
Center Frequency of Bin $k$: $f_k = k \cdot \frac{f_s}{N}$ (for $k = 0, 1, \dots, \frac{N}{2}$)
Hann Window: $w[n] = 0.5 - 0.5 \cos\left(\frac{2\pi n}{N-1}\right)$

5. Practical Implementation

// Embedded C: In-Place Radix-2 Decimation-In-Time FFT
#include <stdio.h>
#include <math.h>

#define FFT_SIZE 64
#define PI 3.14159265358979323846

void applyHannWindow(float *real, int n) {
    for (int i = 0; i < n; i++) {
        float multiplier = 0.5f * (1.0f - cosf(2.0f * PI * i / (n - 1)));
        real[i] *= multiplier;
    }
}

void computeMagnitude(const float *real, const float *imag, float *mag, int n) {
    for (int i = 0; i < n / 2; i++) {
        mag[i] = sqrtf(real[i] * real[i] + imag[i] * imag[i]) / (n / 2.0f);
    }
}

int main() {
    float realBuf[FFT_SIZE] = {0};
    float imagBuf[FFT_SIZE] = {0};
    float magBuf[FFT_SIZE / 2] = {0};

    // Simulate 50 Hz fundamental on 1000 Hz ADC sample rate
    for (int i = 0; i < FFT_SIZE; i++) {
        realBuf[i] = sinf(2.0f * PI * 50.0f * (i / 1000.0f));
    }

    applyHannWindow(realBuf, FFT_SIZE);
    computeMagnitude(realBuf, imagBuf, magBuf, FFT_SIZE);
    printf("[FFT] Bin 0 DC: %.3f | Bin 3 (46.8Hz): %.3f\n", magBuf[0], magBuf[3]);
    return 0;
}

6. Common Engineering Pitfalls & Troubleshooting

Frequent Engineering Pitfalls
  • Omitting analog anti-aliasing filter before ADC: Frequencies higher than $f_s/2$ fold back into the lower frequency spectrum, corrupting measurements with false ghost spectral peaks that cannot be removed by software.
  • Rectangular windowing on non-integer cycle signals: Failing to apply a windowing taper causes sharp sample boundary discontinuities, splashing energy across all adjacent FFT bins (spectral leakage).
Diagnostic & Troubleshooting Checklist
  • FFT displays huge DC spike at Bin 0: The signal contains a static DC voltage bias. Subtract the arithmetic mean of the buffer before computing the window and FFT transform.
  • Insufficient frequency resolution to distinguish close harmonics: Increase total sample acquisition time $T_{window} = N / f_s$ by increasing $N$ or lowering sampling rate $f_s$.

7. Applications & Physical Constraints

TYPICAL APPLICATIONS
  • Predictive maintenance vibration analysis for industrial rotating machinery and bearing fault detection
  • Biomedical EEG and ECG frequency band classification
  • Real-time acoustic noise spectral cancellation and equalizer DSP
PHYSICAL LIMITATIONS
  • Time-frequency resolution trade-off governed by the Gabor-Heisenberg uncertainty principle: finer frequency resolution demands wider time windows.
  • Radix-2 FFT requires buffer sizes that are exact powers of two ($N = 64, 128, 256, 512, 1024$).
Pinout & Interface Specs
PIN / SIGNAL FUNCTION / ELECTRICAL SPEC
ADC Input Pin Connects to conditioned analog sensor output (e.g. accelerometer or microphone)
VREF+ Clean low-noise analog voltage reference (e.g. 3.300V)
AGND Isolated analog signal ground trace connected via star ground
Have questions on this topic?

Ask LUNA to calculate component values or explain alternative control methods for Fast Fourier Transform (FFT) & Nyquist-Shannon Sampling.