use crate::dtype::{Complex64, Complex128};
use std::f64::consts::PI;
use super::bluestein::BluesteinPlan;
use super::dispatch::{FftPlanC64, FftPlanC128};
use super::stockham::{stockham_fft_c64_with, stockham_fft_c128_with};
use super::twiddles::StockhamTwiddles;
#[inline(always)]
fn unpack_theta(k: usize, n: usize) -> f64 {
-2.0 * PI * (k as f64) / (n as f64)
}
#[inline(always)]
fn uses_packing(n: usize) -> bool {
n >= 2 && n.is_power_of_two()
}
enum RfftKindC64 {
Packed {
half: StockhamTwiddles<Complex64>,
unpack: Vec<Complex64>,
},
Bluestein(BluesteinPlan),
}
pub struct RfftPlanC64 {
n: usize,
kind: RfftKindC64,
}
impl RfftPlanC64 {
pub fn new(n: usize) -> Self {
let kind = if uses_packing(n) {
let half_n = n / 2;
let mut unpack = vec![Complex64::default(); half_n];
for (k, w) in unpack.iter_mut().enumerate().skip(1) {
let theta = unpack_theta(k, n);
*w = Complex64::new(theta.cos() as f32, theta.sin() as f32);
}
RfftKindC64::Packed {
half: StockhamTwiddles::new_c64(half_n, false),
unpack,
}
} else {
RfftKindC64::Bluestein(BluesteinPlan::new(n, false))
};
Self { n, kind }
}
pub unsafe fn execute(&self, input: &[f32], output: &mut [Complex64], normalize_factor: f32) {
let n = self.n;
debug_assert_eq!(input.len(), n);
debug_assert_eq!(output.len(), n / 2 + 1);
let (half, unpack) = match &self.kind {
RfftKindC64::Packed { half, unpack } => (half, unpack),
RfftKindC64::Bluestein(plan) => {
plan.execute_rfft_f32(input, output, normalize_factor);
return;
}
};
let half_n = n / 2;
let mut packed: Vec<Complex64> = Vec::with_capacity(half_n);
for k in 0..half_n {
packed.push(Complex64::new(input[2 * k], input[2 * k + 1]));
}
let mut fft_result = vec![Complex64::default(); half_n];
stockham_fft_c64_with(&packed, &mut fft_result, half, 1.0);
output[0] = Complex64::new(
(fft_result[0].re + fft_result[0].im) * normalize_factor,
0.0,
);
for k in 1..half_n {
let z_k = fft_result[k];
let z_nk = fft_result[half_n - k].conj();
let x_even = (z_k + z_nk) * Complex64::new(0.5, 0.0);
let x_odd = (z_k - z_nk) * Complex64::new(0.0, -0.5);
let result = x_even + x_odd * unpack[k];
output[k] = Complex64::new(result.re * normalize_factor, result.im * normalize_factor);
}
output[half_n] = Complex64::new(
(fft_result[0].re - fft_result[0].im) * normalize_factor,
0.0,
);
}
}
enum RfftKindC128 {
Packed {
half: StockhamTwiddles<Complex128>,
unpack: Vec<Complex128>,
},
Bluestein(BluesteinPlan),
}
pub struct RfftPlanC128 {
n: usize,
kind: RfftKindC128,
}
impl RfftPlanC128 {
pub fn new(n: usize) -> Self {
let kind = if uses_packing(n) {
let half_n = n / 2;
let mut unpack = vec![Complex128::default(); half_n];
for (k, w) in unpack.iter_mut().enumerate().skip(1) {
let theta = unpack_theta(k, n);
*w = Complex128::new(theta.cos(), theta.sin());
}
RfftKindC128::Packed {
half: StockhamTwiddles::new_c128(half_n, false),
unpack,
}
} else {
RfftKindC128::Bluestein(BluesteinPlan::new(n, false))
};
Self { n, kind }
}
pub unsafe fn execute(&self, input: &[f64], output: &mut [Complex128], normalize_factor: f64) {
let n = self.n;
debug_assert_eq!(input.len(), n);
debug_assert_eq!(output.len(), n / 2 + 1);
let (half, unpack) = match &self.kind {
RfftKindC128::Packed { half, unpack } => (half, unpack),
RfftKindC128::Bluestein(plan) => {
plan.execute_rfft_f64(input, output, normalize_factor);
return;
}
};
let half_n = n / 2;
let mut packed: Vec<Complex128> = Vec::with_capacity(half_n);
for k in 0..half_n {
packed.push(Complex128::new(input[2 * k], input[2 * k + 1]));
}
let mut fft_result = vec![Complex128::default(); half_n];
stockham_fft_c128_with(&packed, &mut fft_result, half, 1.0);
output[0] = Complex128::new(
(fft_result[0].re + fft_result[0].im) * normalize_factor,
0.0,
);
for k in 1..half_n {
let z_k = fft_result[k];
let z_nk = fft_result[half_n - k].conj();
let x_even = (z_k + z_nk) * Complex128::new(0.5, 0.0);
let x_odd = (z_k - z_nk) * Complex128::new(0.0, -0.5);
let result = x_even + x_odd * unpack[k];
output[k] = Complex128::new(result.re * normalize_factor, result.im * normalize_factor);
}
output[half_n] = Complex128::new(
(fft_result[0].re - fft_result[0].im) * normalize_factor,
0.0,
);
}
}
pub struct IrfftPlanC64 {
fft: FftPlanC64,
}
impl IrfftPlanC64 {
pub fn new(n: usize) -> Self {
Self {
fft: FftPlanC64::new(n, true),
}
}
pub unsafe fn execute(&self, input: &[Complex64], output: &mut [f32], normalize_factor: f32) {
let n = self.fft.n();
debug_assert_eq!(output.len(), n);
let half_n = n / 2;
debug_assert_eq!(input.len(), half_n + 1);
let mut full_spectrum = vec![Complex64::default(); n];
full_spectrum[0] = input[0];
for k in 1..=half_n {
full_spectrum[k] = input[k];
if n - k != k {
full_spectrum[n - k] = input[k].conj();
}
}
let mut ifft_result = vec![Complex64::default(); n];
self.fft
.execute(&full_spectrum, &mut ifft_result, normalize_factor);
for (o, c) in output.iter_mut().zip(ifft_result.iter()) {
*o = c.re;
}
}
}
pub struct IrfftPlanC128 {
fft: FftPlanC128,
}
impl IrfftPlanC128 {
pub fn new(n: usize) -> Self {
Self {
fft: FftPlanC128::new(n, true),
}
}
pub unsafe fn execute(&self, input: &[Complex128], output: &mut [f64], normalize_factor: f64) {
let n = self.fft.n();
debug_assert_eq!(output.len(), n);
let half_n = n / 2;
debug_assert_eq!(input.len(), half_n + 1);
let mut full_spectrum = vec![Complex128::default(); n];
full_spectrum[0] = input[0];
for k in 1..=half_n {
full_spectrum[k] = input[k];
if n - k != k {
full_spectrum[n - k] = input[k].conj();
}
}
let mut ifft_result = vec![Complex128::default(); n];
self.fft
.execute(&full_spectrum, &mut ifft_result, normalize_factor);
for (o, c) in output.iter_mut().zip(ifft_result.iter()) {
*o = c.re;
}
}
}