use crate::spectrum_mul::DctSpectrumMul;
use crate::util::{
DctSample, create_dct2_3, force_cast_real_scratch_to_complex, try_vec, validate_scratch,
};
use crate::{PxdctError, PxdctExecutor};
use num_complex::Complex;
use num_traits::AsPrimitive;
use std::sync::Arc;
use zaft::{FftDirection, FftExecutor};
pub(crate) struct Dst3Fft<T> {
twiddles: Vec<Complex<T>>,
fft_executor: Arc<dyn FftExecutor<T> + Send + Sync>,
execution_length: usize,
spectrum_mul: Arc<dyn DctSpectrumMul<T> + Send + Sync>,
fft_scratch_size: usize,
}
create_dct2_3!(Dst3Fft);
impl<T: DctSample> PxdctExecutor<T> for Dst3Fft<T> {
fn execute(&self, data: &mut [T]) -> Result<(), PxdctError> {
let mut scratch = try_vec![T::default(); self.scratch_size()];
self.execute_with_scratch(data, &mut scratch)
}
fn execute_with_scratch(&self, data: &mut [T], scratch: &mut [T]) -> Result<(), PxdctError> {
if !data.len().is_multiple_of(self.execution_length) {
return Err(PxdctError::InvalidSizeMultiplier(
data.len(),
self.execution_length,
));
}
let full_scratch = validate_scratch!(scratch, self.scratch_size());
let (unprepared_scratch, c1) = full_scratch.split_at_mut(self.execution_length * 2);
let scratch = force_cast_real_scratch_to_complex(unprepared_scratch, self.execution_length);
let fft_scratch = force_cast_real_scratch_to_complex(c1, self.fft_scratch_size);
for chunk in data.chunks_exact_mut(self.execution_length) {
self.spectrum_mul
.mul_spectrum_and_half_rev(chunk, &self.twiddles, scratch);
self.fft_executor
.execute_with_scratch(scratch, fft_scratch)
.map_err(|x| PxdctError::FftError(x.to_string()))?;
let even_end = chunk.len().div_ceil(2);
for (dst, src) in chunk
.iter_mut()
.step_by(2)
.zip(scratch.iter())
.take(even_end)
{
*dst = src.re;
}
if self.execution_length > 1 {
let odd_end = self.execution_length - self.execution_length % 2;
let buffer = &mut chunk[..odd_end];
let data_cutoff = &scratch[even_end..even_end + self.execution_length / 2];
for (dst, src) in buffer
.iter_mut()
.rev()
.step_by(2)
.zip(data_cutoff.iter())
.take(self.execution_length / 2)
{
*dst = -src.re;
}
}
}
Ok(())
}
fn execute_into(&self, input: &[T], output: &mut [T]) -> Result<(), PxdctError> {
let mut scratch = try_vec![T::default(); self.scratch_size()];
self.execute_into_with_scratch(input, output, &mut scratch)
}
fn execute_into_with_scratch(
&self,
input: &[T],
output: &mut [T],
scratch: &mut [T],
) -> Result<(), PxdctError> {
use crate::util::validate_oof_sizes;
validate_oof_sizes!(input, output, self.execution_length);
let full_scratch = validate_scratch!(scratch, self.scratch_size());
let (unprepared_scratch, c1) = full_scratch.split_at_mut(self.execution_length * 2);
let scratch = force_cast_real_scratch_to_complex(unprepared_scratch, self.execution_length);
let fft_scratch = force_cast_real_scratch_to_complex(c1, self.fft_scratch_size);
for (src, dst) in input
.chunks_exact(self.execution_length)
.zip(output.chunks_exact_mut(self.execution_length))
{
self.spectrum_mul
.mul_spectrum_and_half_rev(src, &self.twiddles, scratch);
self.fft_executor
.execute_with_scratch(scratch, fft_scratch)
.map_err(|x| PxdctError::FftError(x.to_string()))?;
let even_end = src.len().div_ceil(2);
for (dst, src) in dst.iter_mut().step_by(2).zip(scratch.iter()).take(even_end) {
*dst = src.re;
}
if self.execution_length > 1 {
let odd_end = self.execution_length - self.execution_length % 2;
let buffer = &mut dst[..odd_end];
let data_cutoff = &scratch[even_end..even_end + self.execution_length / 2];
for (dst, src) in buffer
.iter_mut()
.rev()
.step_by(2)
.zip(data_cutoff.iter())
.take(self.execution_length / 2)
{
*dst = -src.re;
}
}
}
Ok(())
}
fn length(&self) -> usize {
self.execution_length
}
#[inline]
fn scratch_size(&self) -> usize {
self.execution_length * 2 + self.fft_scratch_size * 2
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::PxdctExecutor;
#[test]
fn test_dst_14() {
let mut array = vec![0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.];
let dct0 = Dst3Fft::<f32>::new(array.len()).unwrap();
static CONTROL: [f32; 14] = [
70.61587,
-11.852797,
1.5265555,
-2.914215,
0.16376793,
-1.4512646,
0.0035410523,
-0.89648175,
0.02109158,
-0.6027041,
0.08578658,
-0.41479754,
0.1722637,
-0.27886772,
];
dct0.execute(&mut array).unwrap();
array.iter().zip(CONTROL.iter()).enumerate().for_each(|(i, (&x, &c))| {
assert!((x - c).abs() < 1e-4, "Difference to control values exceeded 1e-4 when it shouldn't, value {x}, control {c} at {i}");
})
}
#[test]
fn test_dst_15() {
let mut array = vec![
0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14.,
];
let dct0 = Dst3Fft::<f32>::new(array.len()).unwrap();
static CONTROL: [f32; 15] = [
81.73187,
-13.372743,
1.8660278,
-3.2491655,
0.23165353,
-1.6127288,
0.013795137,
-1.0,
0.009045204,
-0.68013763,
0.06014249,
-0.47876883,
0.133973,
-0.33546165,
0.22448115,
];
dct0.execute(&mut array).unwrap();
array.iter().zip(CONTROL.iter()).enumerate().for_each(|(i, (&x, &c))| {
assert!((x - c).abs() < 1e-4, "Difference to control values exceeded 1e-4 when it shouldn't, value {x}, control {c} at {i}");
})
}
}