Redo rader
This commit is contained in:
@ -5,19 +5,18 @@ use std::{f32::consts::PI, ops::Deref};
|
||||
use super::mixed_radix;
|
||||
use crate::{
|
||||
complex::Complex32,
|
||||
fft::{DFT, create_fft, dft::NaiveDFT, is_prime},
|
||||
fft::{DFT, create_fft, dft::NaiveDFT, is_prime, windows},
|
||||
};
|
||||
|
||||
pub struct RaderFFT {
|
||||
input_buffer: Box<[Complex32]>,
|
||||
output_buffer: Box<[Complex32]>,
|
||||
|
||||
size: usize,
|
||||
permutations: Box<[usize]>,
|
||||
convolution_op: Box<[Complex32]>,
|
||||
conv_fft: Box<dyn DFT>,
|
||||
|
||||
// Fourrier transform of the exponential terms
|
||||
convolution_operand: Box<[Complex32]>,
|
||||
convolution_fft: Box<dyn DFT>, // TODO: Use fft
|
||||
permutation: Box<[usize]>,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl DFT for RaderFFT {
|
||||
@ -25,46 +24,57 @@ impl DFT for RaderFFT {
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
assert!(is_prime(size));
|
||||
let g = compute_prime_primitive_root(size);
|
||||
let permutation: Box<[usize]> = (0..(size - 1)).map(|i| exp_mod(g, i + 1, size)).collect();
|
||||
let permutations: Box<[usize]> = (0..(size - 1)).map(|i| exp_mod(g, i, size)).collect();
|
||||
|
||||
let mut conv_fft = Box::new(NaiveDFT::create(size - 1));
|
||||
conv_fft
|
||||
.get_input()
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.for_each(|(i, x)| {
|
||||
*x = Complex32::cexp(-2. * PI * (permutations[i] as f32) / (size as f32))
|
||||
});
|
||||
conv_fft.execute(windows::rectanguar);
|
||||
|
||||
RaderFFT {
|
||||
input_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
|
||||
output_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
|
||||
input_buffer: vec![Complex32::zero(); size].into(),
|
||||
output_buffer: vec![Complex32::zero(); size].into(),
|
||||
|
||||
permutations,
|
||||
convolution_op: conv_fft.get_output().iter().copied().collect(),
|
||||
conv_fft,
|
||||
|
||||
size,
|
||||
|
||||
convolution_operand: compute_convolution_operand(size, &permutation),
|
||||
convolution_fft: create_fft(size - 1),
|
||||
permutation,
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(&mut self) {
|
||||
fn execute(&mut self, window: fn(f32) -> f32) {
|
||||
// Compute fft of input signal
|
||||
for i in 0..(self.size - 1) {
|
||||
self.convolution_fft.get_input()[i] =
|
||||
self.input_buffer[self.permutation[self.size - 1 - i - 1]]
|
||||
let k = self.permutations[i];
|
||||
self.conv_fft.get_input()[i] = self.input_buffer[k];
|
||||
}
|
||||
|
||||
self.convolution_fft.execute();
|
||||
self.conv_fft.execute(windows::rectanguar);
|
||||
|
||||
// Use output buffer as staging buffer
|
||||
for i in 0..(self.size - 1) {
|
||||
self.output_buffer[i] =
|
||||
self.convolution_fft.get_output()[i] * self.convolution_operand[i];
|
||||
self.conv_fft.get_output()[self.size - 1 - i - 1] * self.convolution_op[i];
|
||||
}
|
||||
|
||||
for i in 0..(self.size - 1) {
|
||||
self.convolution_fft.get_input()[i] = self.output_buffer[self.size - 1 - i - 1];
|
||||
//self.conv_fft.get_input()[i] = self.output_buffer[self.size - 1 - i - 1];
|
||||
self.conv_fft.get_input()[i] = self.output_buffer[i];
|
||||
}
|
||||
self.convolution_fft.get_input()[0] =
|
||||
self.convolution_fft.get_input()[0] + self.input_buffer[0];
|
||||
|
||||
// Compute ifft to obtain convolution
|
||||
self.convolution_fft.execute();
|
||||
self.conv_fft.execute(windows::rectanguar);
|
||||
|
||||
for i in 0..(self.size - 1) {
|
||||
self.output_buffer[self.permutation[i]] =
|
||||
self.convolution_fft.get_output()[i] / (self.size - 1) as f32;
|
||||
let k = self.permutations[i];
|
||||
self.output_buffer[k] =
|
||||
(self.conv_fft.get_output()[i] / (self.size - 1) as f32) + self.input_buffer[0];
|
||||
}
|
||||
|
||||
self.output_buffer[0] = self.input_buffer.iter().copied().sum();
|
||||
@ -79,18 +89,6 @@ impl DFT for RaderFFT {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compute_convolution_operand(n: usize, permutation: &[usize]) -> Box<[Complex32]> {
|
||||
//let mut fft = create_fft(n - 1);
|
||||
let mut fft = NaiveDFT::create(n - 1);
|
||||
|
||||
fft.get_input()
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.for_each(|(i, x)| *x = Complex32::cexp(-2. * PI * (permutation[i] as f32) / (n as f32)));
|
||||
fft.execute();
|
||||
fft.get_output().iter().copied().collect()
|
||||
}
|
||||
|
||||
pub fn compute_prime_primitive_root(n: usize) -> usize {
|
||||
assert!(is_prime(n));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user