pub struct QFft1D<'a, const N: usize, const HALF: usize> {
pub twiddle_cos: &'a [i16; HALF],
pub twiddle_sin: &'a [i16; HALF],
}Expand description
A quantized radix-2 decimation-in-time FFT of length N, in place on i16
real and imaginary buffers (C++ QFFT1D<N>).
For vibration, audio or IMU front ends on cores without a floating-point
unit: butterflies are 16-bit with i32 intermediates, twiddle factors are
Q1.15 (scale 1/32768). The layer only borrows its two twiddle tables, so
they can live in flash as static or const data; the work buffers are
the caller’s, and the transform needs no other memory beyond a few
registers.
HALF is N / 2, the length of each twiddle table: entry k holds
cos(-2πk/N) and sin(-2πk/N) in Q1.15. Stable Rust cannot write [i16; N / 2] for a generic N, so the half length is a parameter of its own.
N must be a power of two of at least 2 and HALF must be N / 2, or
calling any method does not compile. calibration::build_fft_twiddles
(std feature) computes the tables as tinymind (C++) does, so generate them
on the host and embed them.
forward scales each of its log2(N) stages by 1/2, so
the output is the discrete Fourier transform divided by N and the working
values stay within 16 bits. inverse does not scale, so
it undoes forward up to rounding. Each butterfly saturates its results to
i16.
use tinymind::int8::QFft1D;
// Twiddles for N = 8: cos and sin of -2πk/8 for k = 0..4, in Q1.15.
static COS: [i16; 4] = [32767, 23170, 0, -23170];
static SIN: [i16; 4] = [0, -23170, -32768, -23170];
static FFT: QFft1D<'static, 8, 4> = QFft1D { twiddle_cos: &COS, twiddle_sin: &SIN };
// A cosine that completes one cycle over the eight samples.
let mut real = [8000, 5657, 0, -5657, -8000, -5657, 0, 5657];
let mut imag = [0; 8];
FFT.forward(&mut real, &mut imag);
// Scaled by 1/8, its energy lands in bins 1 and 7, each with half the
// amplitude; each stage's halving floors, so a few values are off by one.
assert_eq!(real, [-1, 3999, 0, 0, 0, 0, 0, 4000]);
assert_eq!(imag, [0, -1, 0, 0, 0, 0, 0, 0]);
let mut power = [0i32; 8];
FFT.magnitude_squared(&real, &imag, &mut power);
assert_eq!(power[7], 4000 * 4000);A table of the wrong length does not compile:
static COS: [i16; 8] = [0; 8];
static SIN: [i16; 8] = [0; 8];
let fft: QFft1D<'_, 8, 8> = QFft1D { twiddle_cos: &COS, twiddle_sin: &SIN };
fft.forward(&mut [0; 8], &mut [0; 8]);Fields§
§twiddle_cos: &'a [i16; HALF]twiddle_cos[k] = cos(-2πk/N) in Q1.15.
twiddle_sin: &'a [i16; HALF]twiddle_sin[k] = sin(-2πk/N) in Q1.15.
Implementations§
Source§impl<const N: usize, const HALF: usize> QFft1D<'_, N, HALF>
impl<const N: usize, const HALF: usize> QFft1D<'_, N, HALF>
Sourcepub fn forward(&self, real: &mut [i16; N], imag: &mut [i16; N])
pub fn forward(&self, real: &mut [i16; N], imag: &mut [i16; N])
Forward transform in place, scaled by 1/N.
Sourcepub fn inverse(&self, real: &mut [i16; N], imag: &mut [i16; N])
pub fn inverse(&self, real: &mut [i16; N], imag: &mut [i16; N])
Inverse transform in place, unscaled, so it cancels the 1/N of
forward. Computed as the conjugate of the forward
transform of the conjugate; negating -32768 wraps to -32768, as the
C++ cast does.
Sourcepub fn magnitude_squared(
&self,
real: &[i16; N],
imag: &[i16; N],
mag_sq: &mut [i32; N],
)
pub fn magnitude_squared( &self, real: &[i16; N], imag: &[i16; N], mag_sq: &mut [i32; N], )
Squared magnitude of each bin, real^2 + imag^2, in i32, into the
caller’s mag_sq (C++ magnitudeSquared). The output is in the square
of the working scale;
a Requantizer can rescale it to an int8 grid.
The only sum that does not fit, (-32768)^2 + (-32768)^2 = 2^31,
saturates to i32::MAX.