File 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).
Public Functions Documentation¶
function syn_dsp_fft¶
Computes the in-place Radix-2 Decimation-in-Time FFT.
n must be a power of 2 (e.g., 8, 16, 32, 64, 128, 256). Max supported size is 256.
Parameters:
realArray of real parts (size n).imagArray of imaginary parts (size n).nSize 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:
magMagnitude array (size n_bins).n_binsNumber of bins (typically n/2 + 1).sample_rate_hzSampling rate in Hz (Q16.16).peaksOutput peak array.max_peaksCapacity of peaks array.num_peaks_foundOutput 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:
realReal part array (size n).imagImaginary part array (size n).magOutput magnitude array (size n/2 + 1).nFFT 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:
magMagnitude array (size n_bins).n_binsNumber of bins.fundamental_binBin index of the fundamental frequency.max_harmonicsNumber of harmonics to sum (e.g. 5).thd_pctOutput 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)).
Parameters:
outOutput buffer of size n.nWindow 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)).
Parameters:
outOutput buffer of size n.nWindow 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))).
Parameters:
outOutput buffer of size n.nWindow 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.
Parameters:
realReal components.imagImaginary components.nNumber of samples.stageslog2(n).
function get_twiddle¶
Compute twiddle factor (cos, -sin) for a butterfly stage.
Parameters:
kButterfly index within group (0 <= k < m/2).stageCurrent 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.
Parameters:
xValue to reverse.stagesNumber of bits.
Returns:
Bit-reversed value.
The documentation for this class was generated from the following file src/syntropic/dsp/syn_fft.c