Skip to main content

QRFft1D

Struct QRFft1D 

Source
pub struct QRFft1D<'a, const N: usize, const HALF: usize> {
    pub twiddle_cos: &'a [i16; HALF],
    pub twiddle_sin: &'a [i16; HALF],
}
Expand description

A quantized real-input FFT of length N (C++ QRFFT1D<N>): N real i16 samples in, the N / 2 + 1 bins that are not redundant out, scaled by 1/N like QFft1D::forward.

The spectrum of a real signal is conjugate-symmetric, so a complex FFT of it, with the imaginary part zeroed, spends half its input and half its output on nothing. This one packs the N samples into N / 2 complex values (even samples real, odd samples imaginary), runs an N / 2-point complex FFT on them and separates the two halves in a final split step. At N = 256 that is 448 butterflies (64 of them without a multiply) and 128 split outputs instead of QFft1D’s 1,024 butterflies.

It takes the same twiddle tables as QFft1D<N>: HALF is N / 2, and entry k holds cos(-2πk/N) and sin(-2πk/N) in Q1.15 (calibration::build_fft_twiddles::<N, HALF>(), std feature). The inner transform reads every second entry. N must be a power of two of at least 2 and HALF must be N / 2, or calling any method does not compile.

The bins go to two caller arrays of BINS = N / 2 + 1 entries, which are also the inner transform’s work space: stable Rust cannot write [i16; N / 2 + 1] for a generic N, so their length is a parameter of each method, and a length other than BINS does not compile. Bin 0 (DC) and bin N / 2 (Nyquist) are real, so their imaginary parts are 0. Bin k for k > N / 2 is the conjugate of bin N - k.

Scaling and headroom: packing two real samples into one complex value lets a value of the inner transform reach sqrt(2) times full scale. So the first inner stage, whose twiddle is exactly 1, computes (a ± b) >> 2 from the samples instead of >> 1, and the split step does not halve its result. No input then saturates an intermediate value; the split step saturates only where rounding reaches the i16 rails. The cost is precision: against an exact DFT divided by N, the largest bin error is about 1.3 to 1.7 times QFft1D’s on the same real input (about 9 LSB at N = 256 on random and full-scale input, where QFft1D’s is about 6). Use QFft1D with a zeroed imaginary buffer when that matters more than the halved work. Rounding is QFft1D’s: every shift floors, and every Q1.15 product is (a * b) >> 15.

use tinymind::int8::QRFft1D;

// Twiddles for N = 8 (the same tables QFft1D<8, 4> takes).
static COS: [i16; 4] = [32767, 23170, 0, -23170];
static SIN: [i16; 4] = [0, -23170, -32768, -23170];
static FFT: QRFft1D<'static, 8, 4> = QRFft1D { twiddle_cos: &COS, twiddle_sin: &SIN };

// A cosine that completes one cycle over the eight samples.
let input = [8000, 5657, 0, -5657, -8000, -5657, 0, 5657];
let mut real = [0; QRFft1D::<8, 4>::BINS];
let mut imag = [0; QRFft1D::<8, 4>::BINS];
FFT.forward(&input, &mut real, &mut imag);
// Scaled by 1/8, bin 1 holds half the amplitude (bin 7, its mirror, is
// not computed); the floored shifts leave a few values off by a few LSB.
assert_eq!(real, [0, 3998, 0, -1, 0]);
assert_eq!(imag, [0, -1, 0, 0, 0]);
let mut power = [0i32; 5];
FFT.magnitude_squared(&real, &imag, &mut power);
assert_eq!(power[1], 3998 * 3998 + 1);

Output arrays of the wrong length do not compile:

ⓘ
static COS: [i16; 4] = [0; 4];
static SIN: [i16; 4] = [0; 4];
let fft: QRFft1D<'_, 8, 4> = QRFft1D { twiddle_cos: &COS, twiddle_sin: &SIN };
fft.forward(&[0; 8], &mut [0; 4], &mut [0; 4]);

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> QRFft1D<'_, N, HALF>

Source

pub const BINS: usize

The number of bins, N / 2 + 1: the length of the output arrays.

Source

pub fn forward<const BINS: usize>( &self, input: &[i16; N], real: &mut [i16; BINS], imag: &mut [i16; BINS], )

Forward real FFT, scaled by 1/N: bins 0 to N / 2 of input’s spectrum into real and imag. input is left unchanged.

Source

pub fn magnitude_squared<const BINS: usize>( &self, real: &[i16; BINS], imag: &[i16; BINS], mag_sq: &mut [i32; BINS], )

Squared magnitude of each of the BINS bins, real^2 + imag^2, in i32, saturated to i32::MAX as in QFft1D::magnitude_squared.

Trait Implementations§

Source§

impl<'a, const N: usize, const HALF: usize> Clone for QRFft1D<'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 QRFft1D<'a, N, HALF>

Source§

impl<'a, const N: usize, const HALF: usize> Debug for QRFft1D<'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 QRFft1D<'a, N, HALF>

§

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

§

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

§

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

§

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

§

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

§

impl<'a, const N: usize, const HALF: usize> UnwindSafe for QRFft1D<'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.