Finishes fft interface + algorithms
This commit is contained in:
@ -1,82 +1,85 @@
|
||||
// Implementation of raders's fft for prime sized ffts
|
||||
|
||||
use std::{f32::consts::PI, ops::Deref};
|
||||
use std::f32::consts::PI;
|
||||
|
||||
use super::mixed_radix;
|
||||
use crate::{
|
||||
complex::Complex32,
|
||||
fft::{DFT, FFTDirection, create_fft, dft::NaiveDFT, is_prime, windows},
|
||||
fft::{create_fft, is_prime , DFTAlgorithm, FFTDirection},
|
||||
};
|
||||
|
||||
pub struct RaderFFT {
|
||||
permutations: Box<[usize]>,
|
||||
convolution_op: Box<[Complex32]>,
|
||||
staging_buffer: Box<[Complex32]>,
|
||||
inv_fft: Box<dyn DFT>,
|
||||
conv_fft: Box<dyn DFT>,
|
||||
convolution_operand: Box<[Complex32]>,
|
||||
|
||||
convolution_ifft: Box<dyn DFTAlgorithm>,
|
||||
convolution_fft: Box<dyn DFTAlgorithm>,
|
||||
|
||||
output: Box<[Complex32]>,
|
||||
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl DFT for RaderFFT {
|
||||
impl DFTAlgorithm for RaderFFT {
|
||||
fn create(size: usize, direction: FFTDirection) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
assert!(is_prime(size));
|
||||
|
||||
// Primitive root and its powers
|
||||
let g = compute_prime_primitive_root(size);
|
||||
let permutations: Box<[usize]> = (0..(size - 1)).map(|i| exp_mod(g, i + 1, size)).collect();
|
||||
|
||||
let mut conv_fft = create_fft(size - 1, FFTDirection::Forward);
|
||||
//let mut conv_fft = create_fft(size - 1);
|
||||
let mut convolution_op = vec![Complex32::zero(); size - 1];
|
||||
let conv_fft_input: Vec<Complex32> = (0..(size - 1))
|
||||
.map(|i| {
|
||||
Complex32::cexp(
|
||||
-2. * direction.sign() * PI * (permutations[i] as f32) / (size as f32),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
conv_fft.execute(&conv_fft_input, &mut convolution_op, windows::rectangular);
|
||||
// Compute fourrier transform of twiddle factors
|
||||
let mut convolution_fft = create_fft(size - 1, FFTDirection::Forward);
|
||||
let mut convolution_operand = (0..(size - 1))
|
||||
.map(|i| {Complex32::cexp(-2. * direction.sign() * PI * (permutations[i] as f32) / (size as f32))})
|
||||
.collect::<Vec<Complex32>>();
|
||||
convolution_fft.execute(&convolution_operand);
|
||||
convolution_operand = Vec::from(convolution_fft.get_output());
|
||||
|
||||
RaderFFT {
|
||||
permutations,
|
||||
convolution_op: convolution_op.into(),
|
||||
staging_buffer: vec![Complex32::zero(); size - 1].into(),
|
||||
inv_fft: create_fft(size - 1, FFTDirection::Inverse),
|
||||
conv_fft,
|
||||
convolution_operand: convolution_operand.into(),
|
||||
|
||||
convolution_ifft: create_fft(size - 1, FFTDirection::Inverse),
|
||||
convolution_fft,
|
||||
|
||||
output: vec![Complex32::zero(); size].into(),
|
||||
size,
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(&mut self, input: &[Complex32], output: &mut [Complex32], window: fn(f32) -> f32) {
|
||||
fn execute(&mut self, input: &[Complex32]) {
|
||||
// Compute fft of input signal
|
||||
for i in 0..(self.size - 1) {
|
||||
let k = self.permutations[self.size - 1 - i - 1];
|
||||
self.staging_buffer[i] = input[k] * window(k as f32 / (self.size as f32));
|
||||
// Using output as staging buffer
|
||||
self.output[i] = input[k];
|
||||
}
|
||||
|
||||
self.conv_fft
|
||||
.execute(&self.staging_buffer, output, windows::rectangular);
|
||||
self.convolution_fft.execute(&self.output);
|
||||
|
||||
// Compute convolution by multiplying in freq domain
|
||||
for i in 0..(self.size - 1) {
|
||||
self.staging_buffer[i] = output[i] * self.convolution_op[i];
|
||||
// Using output as staging buffer
|
||||
self.output[i] = self.convolution_fft.get_output()[i] * self.convolution_operand[i];
|
||||
}
|
||||
|
||||
self.inv_fft
|
||||
.execute(&self.staging_buffer, output, windows::rectangular);
|
||||
self.convolution_ifft.execute(&self.output);
|
||||
|
||||
self.output[0] = input[0];
|
||||
|
||||
for i in 0..(self.size - 1) {
|
||||
// Actually compute the output
|
||||
let k = self.permutations[i];
|
||||
self.staging_buffer[k - 1] = output[i];
|
||||
self.output[k] = (self.convolution_ifft.get_output()[i] / (self.size - 1) as f32) + input[0];
|
||||
self.output[0] = self.output[0] + input[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
output[0] = input[0] * window(0.0);
|
||||
for i in 0..(self.size - 1) {
|
||||
output[i + 1] = (self.staging_buffer[i] / (self.size - 1) as f32) + input[0];
|
||||
output[0] = output[0] + (input[i + 1] * window((i + 1) as f32 / self.size as f32));
|
||||
}
|
||||
fn get_output(&self) -> &[Complex32] {
|
||||
&self.output
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,7 +93,7 @@ pub fn compute_prime_primitive_root(n: usize) -> usize {
|
||||
// Find multiplicative order of i
|
||||
let mut val = i;
|
||||
let mut order = 1;
|
||||
for j in 0..n {
|
||||
for _ in 0..n {
|
||||
if val == 1 {
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user