Skip to content

File syn_fft.c

FileList > dsp > syn_fft.c

Go to the source code of this file

Fixed-point Q16.16 Radix-2 Decimation-in-Time Fast Fourier Transform implementation.

  • #include "../util/syn_assert.h"
  • #include "syn_fft.h"

Public Static Attributes

Type Name
const q16_t g_sin_table = /* multi line expression */
Quarter-sine lookup table for N=256 FFT (Q16 fixed-point).

Public Functions

Type Name
SYN_Status syn_dsp_fft (q16_t * real, q16_t * imag, uint16_t n)
Computes the in-place Radix-2 Decimation-in-Time FFT.
SYN_Status syn_fft_find_peaks (const q16_t * mag, uint16_t n_bins, q16_t sample_rate_hz, SYN_FFTPeak * peaks, uint8_t max_peaks, uint8_t * num_peaks_found)
Find dominant peaks in magnitude spectrum.
SYN_Status syn_fft_magnitude_spectrum (const q16_t * real, const q16_t * imag, q16_t * mag, uint16_t n)
Compute magnitude spectrum: mag[k] = sqrt(real[k]^2 + imag[k]^2).
SYN_Status syn_fft_thd (const q16_t * mag, uint16_t n_bins, uint16_t fundamental_bin, uint8_t max_harmonics, q16_t * thd_pct)
Compute Total Harmonic Distortion (THD) percentage.
SYN_Status syn_fft_window_blackman (q16_t * out, uint16_t n)
Generate Blackman-Harris window: w[i] = 0.42 - 0.5*cos(2*pi*i/(n-1)) + 0.08*cos(4*pi*i/(n-1)).
SYN_Status syn_fft_window_hamming (q16_t * out, uint16_t n)
Generate Hamming window: w[i] = 0.54 - 0.46 * cos(2*pi*i / (n-1)).
SYN_Status syn_fft_window_hanning (q16_t * out, uint16_t n)
Generate Hanning window: w[i] = 0.5 * (1 - cos(2*pi*i / (n-1))).

Public Static Functions

Type Name
void bit_reverse_sort (q16_t * real, q16_t * imag, uint16_t n, uint16_t stages)
Reorder samples by bit-reversed index.
void get_twiddle (uint16_t k, uint16_t stage, q16_t * wr, q16_t * wi)
Compute twiddle factor (cos, -sin) for a butterfly stage.
uint16_t reverse_bits (uint16_t x, uint16_t stages)
Reverse the low bits of a value.

Public Static Attributes Documentation

variable g_sin_table

Quarter-sine lookup table for N=256 FFT (Q16 fixed-point).

const q16_t g_sin_table[65];


Public Functions Documentation

function syn_dsp_fft

Computes the in-place Radix-2 Decimation-in-Time FFT.

SYN_Status syn_dsp_fft (
    q16_t * real,
    q16_t * imag,
    uint16_t n
) 

n must be a power of 2 (e.g., 8, 16, 32, 64, 128, 256). Max supported size is 256.

Parameters:

  • real Array of real parts (size n).
  • imag Array of imaginary parts (size n).
  • n Size of the FFT (must be power of 2).

Returns:

SYN_OK on success, SYN_ERROR on invalid parameters (e.g. not power of 2).


function syn_fft_find_peaks

Find dominant peaks in magnitude spectrum.

SYN_Status syn_fft_find_peaks (
    const q16_t * mag,
    uint16_t n_bins,
    q16_t sample_rate_hz,
    SYN_FFTPeak * peaks,
    uint8_t max_peaks,
    uint8_t * num_peaks_found
) 

Parameters:

  • mag Magnitude array (size n_bins).
  • n_bins Number of bins (typically n/2 + 1).
  • sample_rate_hz Sampling rate in Hz (Q16.16).
  • peaks Output peak array.
  • max_peaks Capacity of peaks array.
  • num_peaks_found Output count of detected peaks.

Returns:

SYN_OK on success, SYN_INVALID_PARAM on invalid inputs.


function syn_fft_magnitude_spectrum

Compute magnitude spectrum: mag[k] = sqrt(real[k]^2 + imag[k]^2).

SYN_Status syn_fft_magnitude_spectrum (
    const q16_t * real,
    const q16_t * imag,
    q16_t * mag,
    uint16_t n
) 

Parameters:

  • real Real part array (size n).
  • imag Imaginary part array (size n).
  • mag Output magnitude array (size n/2 + 1).
  • n FFT size (number of points).

Returns:

SYN_OK on success, SYN_INVALID_PARAM on invalid inputs.


function syn_fft_thd

Compute Total Harmonic Distortion (THD) percentage.

SYN_Status syn_fft_thd (
    const q16_t * mag,
    uint16_t n_bins,
    uint16_t fundamental_bin,
    uint8_t max_harmonics,
    q16_t * thd_pct
) 

THD = sqrt(sum(V_h^2)) / V_1 * 100%

Parameters:

  • mag Magnitude array (size n_bins).
  • n_bins Number of bins.
  • fundamental_bin Bin index of the fundamental frequency.
  • max_harmonics Number of harmonics to sum (e.g. 5).
  • thd_pct Output THD percentage in Q16.16 (e.g., 5.2% = Q16_FROM_FLOAT(5.2)).

Returns:

SYN_OK on success, SYN_INVALID_PARAM on invalid inputs.


function syn_fft_window_blackman

Generate Blackman-Harris window: w[i] = 0.42 - 0.5*cos(2*pi*i/(n-1)) + 0.08*cos(4*pi*i/(n-1)).

SYN_Status syn_fft_window_blackman (
    q16_t * out,
    uint16_t n
) 

Parameters:

  • out Output buffer of size n.
  • n Window length.

Returns:

SYN_OK on success, SYN_INVALID_PARAM if NULL or n <= 1.


function syn_fft_window_hamming

Generate Hamming window: w[i] = 0.54 - 0.46 * cos(2*pi*i / (n-1)).

SYN_Status syn_fft_window_hamming (
    q16_t * out,
    uint16_t n
) 

Parameters:

  • out Output buffer of size n.
  • n Window length.

Returns:

SYN_OK on success, SYN_INVALID_PARAM if NULL or n <= 1.


function syn_fft_window_hanning

Generate Hanning window: w[i] = 0.5 * (1 - cos(2*pi*i / (n-1))).

SYN_Status syn_fft_window_hanning (
    q16_t * out,
    uint16_t n
) 

Parameters:

  • out Output buffer of size n.
  • n Window length.

Returns:

SYN_OK on success, SYN_INVALID_PARAM if NULL or n <= 1.


Public Static Functions Documentation

function bit_reverse_sort

Reorder samples by bit-reversed index.

static void bit_reverse_sort (
    q16_t * real,
    q16_t * imag,
    uint16_t n,
    uint16_t stages
) 

Parameters:

  • real Real components.
  • imag Imaginary components.
  • n Number of samples.
  • stages log2(n).

function get_twiddle

Compute twiddle factor (cos, -sin) for a butterfly stage.

static void get_twiddle (
    uint16_t k,
    uint16_t stage,
    q16_t * wr,
    q16_t * wi
) 

Parameters:

  • k Butterfly index within group (0 <= k < m/2).
  • stage Current FFT stage (1-based).
  • wr [out] Twiddle real (cosine).
  • wi [out] Twiddle imaginary (-sine).

The Cooley-Tukey butterfly calls this with k < 2^(stage-1), so idx = (256*k) >> stage is always < 128. Only quadrants 0 and 1 of the sine table are reachable.


function reverse_bits

Reverse the low bits of a value.

static uint16_t reverse_bits (
    uint16_t x,
    uint16_t stages
) 

Parameters:

  • x Value to reverse.
  • stages Number of bits.

Returns:

Bit-reversed value.



The documentation for this class was generated from the following file src/syntropic/dsp/syn_fft.c