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>
impl<const N: usize, const HALF: usize> QRFft1D<'_, N, HALF>
Sourcepub fn forward<const BINS: usize>(
&self,
input: &[i16; N],
real: &mut [i16; BINS],
imag: &mut [i16; BINS],
)
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.
Sourcepub fn magnitude_squared<const BINS: usize>(
&self,
real: &[i16; BINS],
imag: &[i16; BINS],
mag_sq: &mut [i32; BINS],
)
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.