numr 0.10.0

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
Documentation
//! Bluestein (Chirp-Z) FFT for arbitrary sizes.
//!
//! The chirp and kernel-spectrum TABLES live in
//! [`crate::algorithm::fft_bluestein::BluesteinTables`], shared with every other
//! backend so a CPU and a GPU transform of the same size cannot disagree about
//! the chirp. This file is the CPU execution around them.
//!
//! Everything is accumulated in f64 (`Complex128`) regardless of the caller's
//! dtype; the f32 entry points narrow only on the final store.

use crate::algorithm::fft_bluestein::BluesteinTables;
use crate::dtype::{Complex64, Complex128};

use super::stockham::stockham_fft_c128_with;
use super::twiddles::StockhamTwiddles;

/// A [`BluesteinTables`] plus the CPU convolution that executes it.
///
/// Bluestein rewrites an N-point DFT as a cyclic convolution of length
/// `M = (2N - 1).next_power_of_two()`, which the radix-2 Stockham kernel
/// evaluates directly. This makes every size `N >= 1` available without padding
/// the signal, which would change the frequency grid.
///
/// The plan also owns the forward and inverse M-point twiddle tables, so the
/// two radix-2 transforms per row reuse them instead of rebuilding them.
pub(super) struct BluesteinPlan {
    tables: BluesteinTables,
    forward_m: StockhamTwiddles<Complex128>,
    inverse_m: StockhamTwiddles<Complex128>,
}

impl BluesteinPlan {
    /// Build a plan for an N-point transform.
    ///
    /// # Panics
    ///
    /// Panics if `n == 0`.
    pub(super) fn new(n: usize, inverse: bool) -> Self {
        let mut kernel_twiddles = None;
        let tables = BluesteinTables::new(n, inverse, |kernel| {
            let twiddles = StockhamTwiddles::new_c128(kernel.len(), false);
            let mut spectrum = vec![Complex128::default(); kernel.len()];
            // SAFETY: `kernel.len()` is a power of two by construction of M, and
            // `spectrum` is allocated to exactly that length.
            unsafe {
                stockham_fft_c128_with(kernel, &mut spectrum, &twiddles, 1.0);
            }
            kernel_twiddles = Some(twiddles);
            spectrum
        });
        // The kernel spectrum above is a forward M-point transform, so its table is reused.
        let forward_m =
            kernel_twiddles.unwrap_or_else(|| StockhamTwiddles::new_c128(tables.m, false));
        let inverse_m = StockhamTwiddles::new_c128(tables.m, true);
        Self {
            tables,
            forward_m,
            inverse_m,
        }
    }

    /// Transform length. Kept as an accessor because the tables own the field.
    pub(super) fn n(&self) -> usize {
        self.tables.n
    }

    /// Build the zero-padded, chirp-premultiplied input buffer.
    fn premultiply<I: Iterator<Item = Complex128>>(&self, samples: I) -> Vec<Complex128> {
        let mut a = vec![Complex128::default(); self.tables.m];
        for (k, x) in samples.enumerate().take(self.n()) {
            a[k] = x * self.tables.chirp[k];
        }
        a
    }

    /// Cyclic convolution of `a` with the precomputed kernel, via the radix-2 FFT.
    fn convolve(&self, a: &[Complex128]) -> Vec<Complex128> {
        let m = self.tables.m;

        let mut spectrum = vec![Complex128::default(); m];
        unsafe {
            stockham_fft_c128_with(a, &mut spectrum, &self.forward_m, 1.0);
        }

        for (s, k) in spectrum.iter_mut().zip(self.tables.kernel_spectrum.iter()) {
            *s *= *k;
        }

        let mut conv = vec![Complex128::default(); m];
        unsafe {
            stockham_fft_c128_with(&spectrum, &mut conv, &self.inverse_m, 1.0 / m as f64);
        }
        conv
    }

    /// Full unnormalized N-point transform, in f64.
    fn transform<I: Iterator<Item = Complex128>>(&self, samples: I) -> Vec<Complex128> {
        let a = self.premultiply(samples);
        let conv = self.convolve(&a);
        (0..self.n())
            .map(|k| self.tables.chirp[k] * conv[k])
            .collect()
    }

    /// Complex64 (f32) transform. Input and output must both have length N.
    pub(super) fn execute_c64(
        &self,
        input: &[Complex64],
        output: &mut [Complex64],
        normalize_factor: f32,
    ) {
        debug_assert_eq!(input.len(), self.n());
        debug_assert_eq!(output.len(), self.n());

        let result = self.transform(
            input
                .iter()
                .map(|c| Complex128::new(c.re as f64, c.im as f64)),
        );

        let nf = normalize_factor as f64;
        for k in 0..self.n() {
            output[k] = Complex64::new((result[k].re * nf) as f32, (result[k].im * nf) as f32);
        }
    }

    /// Complex128 (f64) transform. Input and output must both have length N.
    pub(super) fn execute_c128(
        &self,
        input: &[Complex128],
        output: &mut [Complex128],
        normalize_factor: f64,
    ) {
        debug_assert_eq!(input.len(), self.n());
        debug_assert_eq!(output.len(), self.n());

        let result = self.transform(input.iter().copied());

        for k in 0..self.n() {
            output[k] = Complex128::new(
                result[k].re * normalize_factor,
                result[k].im * normalize_factor,
            );
        }
    }

    /// Real-to-complex forward transform, keeping the first `N/2 + 1` bins (f32).
    pub(super) fn execute_rfft_f32(
        &self,
        input: &[f32],
        output: &mut [Complex64],
        normalize_factor: f32,
    ) {
        debug_assert_eq!(input.len(), self.n());
        debug_assert_eq!(output.len(), self.n() / 2 + 1);

        let result = self.transform(input.iter().map(|&x| Complex128::new(x as f64, 0.0)));

        let nf = normalize_factor as f64;
        for k in 0..self.n() / 2 + 1 {
            output[k] = Complex64::new((result[k].re * nf) as f32, (result[k].im * nf) as f32);
        }
    }

    /// Real-to-complex forward transform, keeping the first `N/2 + 1` bins (f64).
    pub(super) fn execute_rfft_f64(
        &self,
        input: &[f64],
        output: &mut [Complex128],
        normalize_factor: f64,
    ) {
        debug_assert_eq!(input.len(), self.n());
        debug_assert_eq!(output.len(), self.n() / 2 + 1);

        let result = self.transform(input.iter().map(|&x| Complex128::new(x, 0.0)));

        for k in 0..self.n() / 2 + 1 {
            output[k] = Complex128::new(
                result[k].re * normalize_factor,
                result[k].im * normalize_factor,
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bluestein_plan_direct_three_point() {
        // A 3-point plan used directly, checked against the closed-form DFT.
        let plan = BluesteinPlan::new(3, false);
        assert_eq!(plan.n(), 3);
        let x = [
            Complex128::new(1.0, 0.0),
            Complex128::new(2.0, 0.0),
            Complex128::new(3.0, 0.0),
        ];
        let mut y = [Complex128::default(); 3];
        plan.execute_c128(&x, &mut y, 1.0);

        // numpy.fft.fft([1,2,3]) == [6, -1.5+0.8660254j, -1.5-0.8660254j]
        assert!((y[0].re - 6.0).abs() < 1e-12 && y[0].im.abs() < 1e-12);
        assert!((y[1].re + 1.5).abs() < 1e-12 && (y[1].im - 0.866_025_403_784_438_6).abs() < 1e-12);
        assert!((y[2].re + 1.5).abs() < 1e-12 && (y[2].im + 0.866_025_403_784_438_6).abs() < 1e-12);
    }
}