#[inline(always)]
pub(crate) fn saturate_i16(v: i32) -> i16 {
if v < i32::from(i16::MIN) {
i16::MIN
} else if v > i32::from(i16::MAX) {
i16::MAX
} else {
v as i16
}
}
#[inline(always)]
pub(crate) fn q15_mul(a: i32, b: i32) -> i32 {
(a * b) >> 15
}
#[derive(Clone, Copy, Debug)]
pub struct QFft1D<'a, const N: usize, const HALF: usize> {
pub twiddle_cos: &'a [i16; HALF],
pub twiddle_sin: &'a [i16; HALF],
}
impl<const N: usize, const HALF: usize> QFft1D<'_, N, HALF> {
const STAGES: u32 = {
assert!(N >= 2, "FFT length must be at least 2");
assert!(N.is_power_of_two(), "FFT length must be a power of two");
assert!(HALF == N / 2, "HALF must be N / 2");
N.trailing_zeros()
};
pub fn forward(&self, real: &mut [i16; N], imag: &mut [i16; N]) {
self.transform::<true>(real, imag);
}
pub fn inverse(&self, real: &mut [i16; N], imag: &mut [i16; N]) {
for x in imag.iter_mut() {
*x = x.wrapping_neg();
}
self.transform::<false>(real, imag);
for x in imag.iter_mut() {
*x = x.wrapping_neg();
}
}
pub fn magnitude_squared(&self, real: &[i16; N], imag: &[i16; N], mag_sq: &mut [i32; N]) {
let _ = Self::STAGES;
for ((o, &r), &m) in mag_sq.iter_mut().zip(real).zip(imag) {
let (r, m) = (i64::from(r), i64::from(m));
let sq = r * r + m * m;
*o = if sq > i64::from(i32::MAX) {
i32::MAX
} else {
sq as i32
};
}
}
fn transform<const SCALE: bool>(&self, real: &mut [i16; N], imag: &mut [i16; N]) {
let stages = Self::STAGES;
bit_reverse_permute(real, imag);
let mask = N - 1;
let tw_mask = HALF - 1;
let mut half = 1;
for stage in 0..stages {
let full = half << 1;
let stride = HALF >> stage;
let mut group = 0;
while group < N {
for pair in 0..half {
let k = (pair * stride) & tw_mask;
let w_cos = i32::from(self.twiddle_cos[k]);
let w_sin = i32::from(self.twiddle_sin[k]);
let top = (group + pair) & mask;
let bot = (top + half) & mask;
let bot_real = i32::from(real[bot]);
let bot_imag = i32::from(imag[bot]);
let t_real = q15_mul(w_cos, bot_real) - q15_mul(w_sin, bot_imag);
let t_imag = q15_mul(w_cos, bot_imag) + q15_mul(w_sin, bot_real);
let top_real = i32::from(real[top]);
let top_imag = i32::from(imag[top]);
if SCALE {
real[top] = saturate_i16((top_real + t_real) >> 1);
imag[top] = saturate_i16((top_imag + t_imag) >> 1);
real[bot] = saturate_i16((top_real - t_real) >> 1);
imag[bot] = saturate_i16((top_imag - t_imag) >> 1);
} else {
real[top] = saturate_i16(top_real + t_real);
imag[top] = saturate_i16(top_imag + t_imag);
real[bot] = saturate_i16(top_real - t_real);
imag[bot] = saturate_i16(top_imag - t_imag);
}
}
group += full;
}
half = full;
}
}
}
fn bit_reverse_permute<const N: usize>(real: &mut [i16; N], imag: &mut [i16; N]) {
let mask = N - 1;
let mut j = 0;
for i in 0..N {
if i < j {
let (a, b) = (i & mask, j & mask);
(real[a], real[b]) = (real[b], real[a]);
(imag[a], imag[b]) = (imag[b], imag[a]);
}
let mut bit = N >> 1;
while j & bit != 0 {
j ^= bit;
bit >>= 1;
}
j |= bit;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn reverse(i: usize, bits: u32) -> usize {
(0..bits).fold(0, |r, b| (r << 1) | ((i >> b) & 1))
}
fn check_permutation<const N: usize>() {
let mut real: [i16; N] = core::array::from_fn(|i| i as i16);
let mut imag: [i16; N] = core::array::from_fn(|i| !(i as i16));
bit_reverse_permute(&mut real, &mut imag);
let bits = N.trailing_zeros();
for i in 0..N {
assert_eq!(real[i] as usize, reverse(i, bits), "N={N} i={i}");
assert_eq!(imag[i], !(reverse(i, bits) as i16));
}
}
#[test]
fn bit_reversal_matches_the_table() {
check_permutation::<2>();
check_permutation::<4>();
check_permutation::<8>();
check_permutation::<64>();
check_permutation::<1024>();
check_permutation::<4096>();
}
#[test]
fn saturate_and_q15() {
assert_eq!(saturate_i16(40_000), i16::MAX);
assert_eq!(saturate_i16(-40_000), i16::MIN);
assert_eq!(saturate_i16(-32_768), i16::MIN);
assert_eq!(q15_mul(-32768, -32768), 32768);
assert_eq!(q15_mul(1, -1), -1);
}
#[test]
fn magnitude_saturates_only_at_both_rails() {
static T: [i16; 1] = [32767];
let fft: QFft1D<'_, 2, 1> = QFft1D {
twiddle_cos: &T,
twiddle_sin: &[0],
};
let mut m = [0; 2];
fft.magnitude_squared(&[-32768, -32768], &[-32768, 32767], &mut m);
assert_eq!(m, [i32::MAX, 32768 * 32768 + 32767 * 32767]);
}
}