mirror of
https://github.com/ratspeak/rsLXST
synced 2026-11-22 23:10:37 +00:00
codec2: share immutable FFT coefficients outside stream RAM
This commit is contained in:
1 parent
7dfb98e1c8
commit
5c9c945300
6 files changed
+8923
-22
No files matched your search
@@ -33,3 +33,14 @@ are distinct qualification gates.
|
||||
|
||||
See [NOTICE](NOTICE), [UPSTREAM.json](UPSTREAM.json) and [licenses](licenses/)
|
||||
for source provenance, modifications and original license notices.
|
||||
|
||||
FFT coefficient tables are shared read-only data, generated using the pinned
|
||||
`libm` and the original native initialisation formulas. Per-stream state keeps
|
||||
only integer selectors; encoder/decoder histories and mutable FFT scratch remain
|
||||
independent. A bit-for-bit table test covers every supported size and direction.
|
||||
Regenerate the checked-in coefficients with:
|
||||
|
||||
```sh
|
||||
cargo run -p lxst-codec2 --example generate_fft_tables --locked --offline > crates/lxst-codec2/src/native/fft_coefficients.rs
|
||||
cargo fmt -p lxst-codec2
|
||||
```
|
||||
@@ -0,0 +1,53 @@
|
||||
//! Regenerate with cargo run -p lxst-codec2 --example generate_fft_tables --locked --offline.
|
||||
//! Preserve the native initialiser's floating-point operation order exactly.
|
||||
fn complex(name: &str, phases: impl Iterator<Item = f32>, length: usize) {
|
||||
println!("pub(super) static {name}: [kiss_fft_cpx; {length}] = [");
|
||||
for phase in phases {
|
||||
println!(
|
||||
" kiss_fft_cpx {{ r: f32::from_bits(0x{:08x}), i: f32::from_bits(0x{:08x}) }},",
|
||||
libm::cosf(phase).to_bits(),
|
||||
libm::sinf(phase).to_bits()
|
||||
);
|
||||
}
|
||||
println!("];\n");
|
||||
}
|
||||
fn main() {
|
||||
println!(
|
||||
"// Generated by examples/generate_fft_tables.rs with pinned libm 0.2.16.\n// Immutable native FFT coefficients; no approximation or host trigonometry.\nuse super::inner::kiss_fft_cpx;\n"
|
||||
);
|
||||
for n in [128, 256, 512] {
|
||||
for inverse in [false, true] {
|
||||
complex(
|
||||
&format!(
|
||||
"COMPLEX_{n}_{}",
|
||||
if inverse { "INVERSE" } else { "FORWARD" }
|
||||
),
|
||||
(0..n).map(|i| {
|
||||
let mut phase = -2.0 * core::f64::consts::PI * i as f64 / n as f64;
|
||||
if inverse {
|
||||
phase *= -1.0;
|
||||
}
|
||||
phase as f32
|
||||
}),
|
||||
n,
|
||||
);
|
||||
}
|
||||
}
|
||||
for n in [128, 256] {
|
||||
for inverse in [false, true] {
|
||||
complex(
|
||||
&format!("REAL_{n}_{}", if inverse { "INVERSE" } else { "FORWARD" }),
|
||||
(0..n / 2).map(|i| {
|
||||
let mut phase = (-core::f64::consts::PI
|
||||
* (f64::from((i + 1) as f32 / n as f32) + 0.5))
|
||||
as f32;
|
||||
if inverse {
|
||||
phase *= -1.0;
|
||||
}
|
||||
phase
|
||||
}),
|
||||
n / 2,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
//! Adapted from the pinned pure-Rust Codec2 port recorded in `UPSTREAM.json`.
|
||||
//! Initialise directly in caller-owned storage; the state is too large to create
|
||||
//! as a temporary on an MCU task stack. Encoding/decoding and reset never allocate.
|
||||
//! Immutable FFT coefficients are shared in read-only storage; stream histories
|
||||
//! and mutable transform scratch remain independently owned.
|
||||
#![no_std]
|
||||
|
||||
mod buffer;
|
||||
@@ -65,7 +67,7 @@ pub struct Codec {
|
||||
impl Codec {
|
||||
/// Initialise in final, aligned caller storage without a whole-state copy.
|
||||
pub fn initialise(storage: &mut MaybeUninit<Self>, mode: Mode) -> &mut Self {
|
||||
// SAFETY: all native fields are integers, floats, arrays, fixed buffers
|
||||
// SAFETY: all native fields are integers, floats, booleans, arrays, fixed buffers
|
||||
// (zero length is valid), or Codec2Mode (repr(u8), first variant is 0).
|
||||
// There are no references, pointers, heap owners or Drop implementations.
|
||||
// Thus zero bytes form a valid value. We finish logical initialisation
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,113 @@
|
||||
// FFT coefficients depend only on transform size/direction. Keep them in
|
||||
// read-only flash; each stream retains just zero-valid integer selectors.
|
||||
// Histories and transform scratch remain private to their Codec instance.
|
||||
use super::{fft_coefficients::*, inner::kiss_fft_cpx};
|
||||
use core::ops::Deref;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) struct ComplexTable {
|
||||
length: usize,
|
||||
inverse: bool,
|
||||
}
|
||||
impl ComplexTable {
|
||||
pub(super) fn initialise(&mut self, length: usize, inverse: bool) {
|
||||
assert!(matches!(length, 128 | 256 | 512));
|
||||
self.length = length;
|
||||
self.inverse = inverse;
|
||||
}
|
||||
}
|
||||
impl Deref for ComplexTable {
|
||||
type Target = [kiss_fft_cpx];
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match (self.length, self.inverse) {
|
||||
(0, _) => &[],
|
||||
(128, false) => &COMPLEX_128_FORWARD,
|
||||
(128, true) => &COMPLEX_128_INVERSE,
|
||||
(256, false) => &COMPLEX_256_FORWARD,
|
||||
(256, true) => &COMPLEX_256_INVERSE,
|
||||
(512, false) => &COMPLEX_512_FORWARD,
|
||||
(512, true) => &COMPLEX_512_INVERSE,
|
||||
_ => unreachable!("invalid fixed FFT table"),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) struct RealTable {
|
||||
nfft: usize,
|
||||
inverse: bool,
|
||||
}
|
||||
impl RealTable {
|
||||
pub(super) fn initialise(&mut self, nfft: usize, inverse: bool) {
|
||||
assert!(matches!(nfft, 128 | 256));
|
||||
self.nfft = nfft;
|
||||
self.inverse = inverse;
|
||||
}
|
||||
}
|
||||
impl Deref for RealTable {
|
||||
type Target = [kiss_fft_cpx];
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match (self.nfft, self.inverse) {
|
||||
(0, _) => &[],
|
||||
(128, false) => &REAL_128_FORWARD,
|
||||
(128, true) => &REAL_128_INVERSE,
|
||||
(256, false) => &REAL_256_FORWARD,
|
||||
(256, true) => &REAL_256_INVERSE,
|
||||
_ => unreachable!("invalid fixed real FFT table"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
// Compare every stored bit to the original runtime initialiser, including
|
||||
// signed zero and forward/inverse phase operation order. No approximations.
|
||||
#[test]
|
||||
fn flash_tables_match_native_initialisation() {
|
||||
for inverse in [false, true] {
|
||||
for nfft in [128, 256, 512] {
|
||||
let mut table = ComplexTable {
|
||||
length: 0,
|
||||
inverse: false,
|
||||
};
|
||||
assert!(table.is_empty());
|
||||
table.initialise(nfft, inverse);
|
||||
assert_eq!(table.len(), nfft);
|
||||
for (i, value) in table.iter().enumerate() {
|
||||
let mut phase = -2.0 * core::f64::consts::PI * i as f64 / nfft as f64;
|
||||
if inverse {
|
||||
phase *= -1.0;
|
||||
}
|
||||
let original = kiss_fft_cpx::kf_cexp(phase as f32);
|
||||
assert_eq!(
|
||||
(value.r.to_bits(), value.i.to_bits()),
|
||||
(original.r.to_bits(), original.i.to_bits())
|
||||
);
|
||||
}
|
||||
}
|
||||
for nfft in [128, 256] {
|
||||
let mut table = RealTable {
|
||||
nfft: 0,
|
||||
inverse: false,
|
||||
};
|
||||
assert!(table.is_empty());
|
||||
table.initialise(nfft, inverse);
|
||||
assert_eq!(table.len(), nfft / 2);
|
||||
for (i, value) in table.iter().enumerate() {
|
||||
let mut phase = (-core::f64::consts::PI
|
||||
* (f64::from((i + 1) as f32 / nfft as f32) + 0.5))
|
||||
as f32;
|
||||
if inverse {
|
||||
phase *= -1.0;
|
||||
}
|
||||
let original = kiss_fft_cpx::kf_cexp(phase);
|
||||
assert_eq!(
|
||||
(value.r.to_bits(), value.i.to_bits()),
|
||||
(original.r.to_bits(), original.i.to_bits())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(core::mem::size_of::<crate::Codec>() < 17000);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
use super::{Buffer, FloatMath};
|
||||
use core::f32::consts::PI;
|
||||
|
||||
mod fft_coefficients;
|
||||
mod fft_tables;
|
||||
mod kiss_fft;
|
||||
mod nlp;
|
||||
mod quantise;
|
||||
@@ -147,21 +149,14 @@ mod inner {
|
||||
pub nfft: usize,
|
||||
pub inverse: i32,
|
||||
pub factors: [usize; 2 * MAXFACTORS],
|
||||
pub twiddles: Buffer<kiss_fft_cpx, N>,
|
||||
pub twiddles: fft_tables::ComplexTable,
|
||||
}
|
||||
impl<const N: usize> kiss_fft_state<N> {
|
||||
pub fn initialise(&mut self, nfft: usize, inverse_fft: i32) {
|
||||
assert!((nfft == 128 || nfft == 256 || nfft == 512) && nfft <= N);
|
||||
self.nfft = nfft;
|
||||
self.inverse = inverse_fft;
|
||||
self.twiddles.initialise(nfft, kiss_fft_cpx::new());
|
||||
for i in 0..nfft {
|
||||
let mut phase = -2.0 * core::f64::consts::PI * i as f64 / nfft as f64;
|
||||
if inverse_fft != 0 {
|
||||
phase *= -1.0;
|
||||
}
|
||||
self.twiddles[i] = kiss_fft_cpx::kf_cexp(phase as f32);
|
||||
}
|
||||
self.twiddles.initialise(nfft, inverse_fft != 0);
|
||||
let mut n = nfft;
|
||||
let mut p = 4;
|
||||
let floor_sqrt = (n as f32).dsp_sqrt().dsp_floor() as usize;
|
||||
@@ -193,24 +188,14 @@ mod inner {
|
||||
pub struct kiss_fftr_state {
|
||||
pub substate: kiss_fft_state<256>,
|
||||
pub tmpbuf: Buffer<kiss_fft_cpx, 256>,
|
||||
pub super_twiddles: Buffer<kiss_fft_cpx, 128>,
|
||||
pub super_twiddles: fft_tables::RealTable,
|
||||
}
|
||||
impl kiss_fftr_state {
|
||||
pub fn initialise(&mut self, nfft: usize, inverse_fft: i32) {
|
||||
let nfft = nfft / 2;
|
||||
self.substate.initialise(nfft, inverse_fft);
|
||||
self.tmpbuf.initialise(nfft, kiss_fft_cpx::new());
|
||||
self.super_twiddles
|
||||
.initialise(nfft / 2, kiss_fft_cpx::new());
|
||||
for i in 0..nfft / 2 {
|
||||
let mut phase = (-core::f64::consts::PI
|
||||
* (f64::from((i + 1) as f32 / nfft as f32) + 0.5))
|
||||
as f32;
|
||||
if inverse_fft != 0 {
|
||||
phase *= -1.0;
|
||||
}
|
||||
self.super_twiddles[i] = kiss_fft_cpx::kf_cexp(phase);
|
||||
}
|
||||
self.super_twiddles.initialise(nfft, inverse_fft != 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user