I want to die
This commit is contained in:
@ -29,6 +29,9 @@ impl DFTAlgorithm for MixedRadixFFT {
|
||||
let qfft = create_fft(q, direction);
|
||||
let pfft = create_fft(p, direction);
|
||||
|
||||
//let qfft = Box::new(NaiveDFT::create(q, direction));
|
||||
//let pfft = Box::new(NaiveDFT::create(p, direction));
|
||||
|
||||
MixedRadixFFT {
|
||||
twiddle_factors: compute_twiddle_factors(size, direction),
|
||||
qfft,
|
||||
|
||||
@ -4,7 +4,7 @@ use std::f32::consts::PI;
|
||||
|
||||
use crate::{
|
||||
complex::Complex32,
|
||||
fft::{create_fft, is_prime , DFTAlgorithm, FFTDirection},
|
||||
fft::{create_fft, dft::NaiveDFT, is_prime, DFTAlgorithm, FFTDirection},
|
||||
};
|
||||
|
||||
pub struct RaderFFT {
|
||||
@ -31,7 +31,8 @@ impl DFTAlgorithm for RaderFFT {
|
||||
let permutations: Box<[usize]> = (0..(size - 1)).map(|i| exp_mod(g, i + 1, size)).collect();
|
||||
|
||||
// Compute fourrier transform of twiddle factors
|
||||
let mut convolution_fft = create_fft(size - 1, FFTDirection::Forward);
|
||||
//let mut convolution_fft = create_fft(size - 1, FFTDirection::Forward);
|
||||
let mut convolution_fft = Box::new(NaiveDFT::create(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>>();
|
||||
@ -42,7 +43,8 @@ impl DFTAlgorithm for RaderFFT {
|
||||
permutations,
|
||||
convolution_operand: convolution_operand.into(),
|
||||
|
||||
convolution_ifft: create_fft(size - 1, FFTDirection::Inverse),
|
||||
//convolution_ifft: create_fft(size - 1, FFTDirection::Inverse),
|
||||
convolution_ifft: Box::new(NaiveDFT::create(size - 1, FFTDirection::Inverse)),
|
||||
convolution_fft,
|
||||
|
||||
output: vec![Complex32::zero(); size].into(),
|
||||
|
||||
@ -1,118 +1,91 @@
|
||||
// Implementation of raders's fft for prime sized ffts
|
||||
|
||||
/*
|
||||
use std::{f32::consts::PI, ops::Deref};
|
||||
|
||||
use super::mixed_radix;
|
||||
use std::f32::consts::PI;
|
||||
|
||||
use crate::{
|
||||
complex::Complex32,
|
||||
fft::{DFT, FFTDirection, create_fft, dft::NaiveDFT, is_prime, windows},
|
||||
fft::{create_fft, dft::NaiveDFT, is_prime, DFTAlgorithm, FFTDirection},
|
||||
};
|
||||
|
||||
pub struct Rader2FFT {
|
||||
input_buffer: Box<[Complex32]>,
|
||||
output_buffer: Box<[Complex32]>,
|
||||
pub struct RaderFFT {
|
||||
permutations: Box<[usize]>,
|
||||
convolution_operand: Box<[Complex32]>,
|
||||
|
||||
convolution_ifft: Box<dyn DFTAlgorithm>,
|
||||
convolution_fft: Box<dyn DFTAlgorithm>,
|
||||
|
||||
output: Box<[Complex32]>,
|
||||
|
||||
size: usize,
|
||||
sub_size: usize,
|
||||
|
||||
// Fourrier transform of the exponential terms
|
||||
convolution_operand: Box<[Complex32]>,
|
||||
convolution_fft: Box<dyn DFT>, // TODO: Use fft
|
||||
permutation: Box<[usize]>,
|
||||
}
|
||||
|
||||
impl DFT for Rader2FFT {
|
||||
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 permutation: Box<[usize]> = (0..(size - 1)).map(|i| exp_mod(g, i + 1, size)).collect();
|
||||
let sub_size = next_pow2((2 * size - 4) - 1);
|
||||
Rader2FFT {
|
||||
input_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
|
||||
output_buffer: vec![Complex32::zero(); sub_size].into_boxed_slice(),
|
||||
let permutations: Box<[usize]> = (0..(size - 1)).map(|i| exp_mod(g, i + 1, size)).collect();
|
||||
|
||||
// Compute fourrier transform of twiddle factors
|
||||
let mut convolution_fft = create_fft(size - 1, FFTDirection::Forward);
|
||||
//let mut convolution_fft = Box::new(NaiveDFT::create(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_operand: convolution_operand.into(),
|
||||
|
||||
convolution_ifft: create_fft(size - 1, FFTDirection::Inverse),
|
||||
//convolution_ifft: Box::new(NaiveDFT::create(size - 1, FFTDirection::Inverse)),
|
||||
convolution_fft,
|
||||
|
||||
output: vec![Complex32::zero(); size].into(),
|
||||
size,
|
||||
sub_size,
|
||||
|
||||
convolution_operand: compute_convolution_operand(size, sub_size, &permutation),
|
||||
//convolution_fft: create_fft(next_pow2((2 * size - 4) - 1)),
|
||||
convolution_fft: Box::new(NaiveDFT::create(sub_size)),
|
||||
permutation,
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(&mut self, window: fn(f32) -> f32) {
|
||||
self.convolution_fft.get_input()[0] = self.input_buffer[self.permutation[self.size - 2]];
|
||||
|
||||
for i in 0..(self.sub_size - self.size + 1) {
|
||||
self.convolution_fft.get_input()[i + 1] = Complex32::zero();
|
||||
}
|
||||
for i in 1..(self.size - 1) {
|
||||
let k = self.permutation[self.size - 1 - i - 1];
|
||||
self.convolution_fft.get_input()[i + self.sub_size - self.size + 1] =
|
||||
self.input_buffer[k] * window(k as f32 / self.size as 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];
|
||||
// Using output as staging buffer
|
||||
self.output[i] = input[k];
|
||||
}
|
||||
|
||||
self.convolution_fft.execute(windows::rectanguar);
|
||||
self.convolution_fft.execute(&self.output);
|
||||
|
||||
// Use output buffer as staging buffer
|
||||
for i in 0..(self.sub_size) {
|
||||
self.output_buffer[i] =
|
||||
self.convolution_fft.get_output()[i] * self.convolution_operand[i];
|
||||
// Compute convolution by multiplying in freq domain
|
||||
for i in 0..(self.size - 1) {
|
||||
// Using output as staging buffer
|
||||
self.output[i] = self.convolution_fft.get_output()[i] * self.convolution_operand[i];
|
||||
}
|
||||
|
||||
for i in 0..(self.sub_size) {
|
||||
self.convolution_fft.get_input()[i] = self.output_buffer[i];
|
||||
}
|
||||
/*
|
||||
self.convolution_fft.get_input()[0] =
|
||||
self.convolution_fft.get_input()[0] + self.input_buffer[0] * window(0.);
|
||||
*/
|
||||
self.convolution_ifft.execute(&self.output);
|
||||
|
||||
// Compute ifft to obtain convolution
|
||||
self.convolution_fft.execute(window);
|
||||
self.output[0] = input[0];
|
||||
|
||||
for i in 0..(self.size - 1) {
|
||||
self.output_buffer[self.permutation[i]] =
|
||||
self.convolution_fft.get_output()[i] / self.sub_size as f32;
|
||||
// Actually compute the output
|
||||
let k = self.permutations[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];
|
||||
}
|
||||
|
||||
self.output_buffer[0] = self
|
||||
.input_buffer
|
||||
.iter()
|
||||
.copied()
|
||||
.enumerate()
|
||||
.map(|(i, x)| x * window(i as f32 / self.size as f32))
|
||||
.sum();
|
||||
}
|
||||
|
||||
fn get_input(&mut self) -> &mut [Complex32] {
|
||||
&mut self.input_buffer
|
||||
}
|
||||
|
||||
fn get_output(&self) -> &[Complex32] {
|
||||
&self.output_buffer
|
||||
&self.output
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compute_convolution_operand(
|
||||
n: usize,
|
||||
sub_size: usize,
|
||||
permutation: &[usize],
|
||||
) -> Box<[Complex32]> {
|
||||
//let mut fft = create_fft(sub_size);
|
||||
let mut fft = NaiveDFT::create(sub_size);
|
||||
|
||||
fft.get_input().iter_mut().enumerate().for_each(|(i, x)| {
|
||||
*x = Complex32::cexp(-2. * PI * (permutation[i % (n - 1)] as f32) / (n as f32))
|
||||
});
|
||||
fft.execute(windows::rectanguar);
|
||||
fft.get_output().iter().copied().collect()
|
||||
}
|
||||
|
||||
pub fn compute_prime_primitive_root(n: usize) -> usize {
|
||||
assert!(is_prime(n));
|
||||
|
||||
@ -123,7 +96,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;
|
||||
}
|
||||
@ -158,12 +131,4 @@ pub fn exp_mod(mut n: usize, mut exp: usize, m: usize) -> usize {
|
||||
r
|
||||
}
|
||||
|
||||
pub fn next_pow2(mut n: usize) -> usize {
|
||||
let mut pow = 0;
|
||||
while n > 0 {
|
||||
n >>= 1;
|
||||
pow += 1;
|
||||
}
|
||||
1 << pow
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user