Skip to main content

QFft1D

Struct QFft1D 

Source
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>

Source

pub fn forward(&self, real: &mut [i16; N], imag: &mut [i16; N])

Forward transform in place, scaled by 1/N.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<'a, const N: usize, const HALF: usize> Clone for QFft1D<'a, N, HALF>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, const N: usize, const HALF: usize> Copy for QFft1D<'a, N, HALF>

Source§

impl<'a, const N: usize, const HALF: usize> Debug for QFft1D<'a, N, HALF>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, const N: usize, const HALF: usize> Freeze for QFft1D<'a, N, HALF>

§

impl<'a, const N: usize, const HALF: usize> RefUnwindSafe for QFft1D<'a, N, HALF>

§

impl<'a, const N: usize, const HALF: usize> Send for QFft1D<'a, N, HALF>

§

impl<'a, const N: usize, const HALF: usize> Sync for QFft1D<'a, N, HALF>

§

impl<'a, const N: usize, const HALF: usize> Unpin for QFft1D<'a, N, HALF>

§

impl<'a, const N: usize, const HALF: usize> UnsafeUnpin for QFft1D<'a, N, HALF>

§

impl<'a, const N: usize, const HALF: usize> UnwindSafe for QFft1D<'a, N, HALF>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.