use crate::config::*;
use realfft::{RealFftPlanner, RealToComplex};
use std::sync::Arc;
#[derive(Clone)]
pub struct FFT {
fft: Arc<dyn RealToComplex<Flt>>,
timescratch: Vec<Flt>,
half_nfft_rounded: usize,
nfftF: Flt,
}
impl FFT {
pub fn newFromNFFT(nfft: usize) -> FFT {
let mut planner = RealFftPlanner::<Flt>::new();
let fft = planner.plan_fft_forward(nfft);
Self::new(fft)
}
pub fn new(fft: Arc<dyn RealToComplex<Flt>>) -> FFT {
let nfft = fft.len();
let timescratch = vec![0.; nfft];
FFT {
fft,
timescratch,
half_nfft_rounded: nfft / 2,
nfftF: nfft as Flt,
}
}
pub fn process<'a, T>(&mut self, time: &[Flt], freq: T)
where
T: Into<ArrayViewMut<'a, Cflt, Ix1>>,
{
let mut freq = freq.into();
self.timescratch.copy_from_slice(time);
let _ = self
.fft
.process(&mut self.timescratch, freq.as_slice_mut().unwrap());
freq[0] /= self.nfftF;
freq[self.half_nfft_rounded] /= self.nfftF;
freq.slice_mut(s![1..self.half_nfft_rounded])
.par_mapv_inplace(|x| 2. * x / self.nfftF);
}
}