use num_complex::Complex;
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Transform {
Fft,
Ifft,
UnscaledIfft,
SqrtScaledFft,
SqrtScaledIfft,
}
impl Transform {
pub fn is_forward(&self) -> bool {
match self {
Self::Fft | Self::SqrtScaledFft => true,
Self::Ifft | Self::UnscaledIfft | Self::SqrtScaledIfft => false,
}
}
pub fn inverse(&self) -> Option<Self> {
match self {
Self::Fft => Some(Self::Ifft),
Self::Ifft => Some(Self::Fft),
Self::SqrtScaledFft => Some(Self::SqrtScaledIfft),
Self::SqrtScaledIfft => Some(Self::SqrtScaledFft),
Self::UnscaledIfft => None,
}
}
}
pub trait Fft {
type Real: Copy;
fn size(&self) -> usize;
fn transform_in_place(&self, input: &mut [Complex<Self::Real>], transform: Transform);
fn transform(
&self,
input: &[Complex<Self::Real>],
output: &mut [Complex<Self::Real>],
transform: Transform,
) {
assert_eq!(input.len(), self.size());
assert_eq!(output.len(), self.size());
output.copy_from_slice(input);
self.transform_in_place(output, transform);
}
fn fft_in_place(&self, input: &mut [Complex<Self::Real>]) {
self.transform_in_place(input, Transform::Fft);
}
fn ifft_in_place(&self, input: &mut [Complex<Self::Real>]) {
self.transform_in_place(input, Transform::Ifft);
}
fn fft(&self, input: &[Complex<Self::Real>], output: &mut [Complex<Self::Real>]) {
self.transform(input, output, Transform::Fft);
}
fn ifft(&self, input: &[Complex<Self::Real>], output: &mut [Complex<Self::Real>]) {
self.transform(input, output, Transform::Ifft);
}
}