Working FFTs

This commit is contained in:
2025-09-24 09:03:13 +02:00
parent d191b912b0
commit bd7ae2b19e
9 changed files with 15071 additions and 5003 deletions

View File

@ -1,21 +1,23 @@
use crate::complex::Complex32;
use crate::fft::DFT;
use crate::fft::{DFT, FFTDirection};
use std::f32::consts::PI;
pub struct NaiveDFT {
output_buffer: Box<[Complex32]>,
input_buffer: Box<[Complex32]>,
direction: FFTDirection,
size: usize,
}
impl DFT for NaiveDFT {
fn create(size: usize) -> Self
fn create(size: usize, direction: FFTDirection) -> Self
where
Self: Sized,
{
NaiveDFT {
output_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
input_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
direction,
size,
}
}
@ -25,11 +27,13 @@ impl DFT for NaiveDFT {
*out = Complex32::zero();
for (i, inp) in self.input_buffer.iter().enumerate() {
*out = *out
+ ((*inp * Complex32::cexp(-2. * PI * (i * freq) as f32 / self.size as f32))
+ ((*inp
* Complex32::cexp(
-2. * self.direction.sign() * PI * (i * freq) as f32 / self.size as f32,
))
* window(i as f32 / self.size as f32));
}
}
}
fn get_input(&mut self) -> &mut [Complex32] {
@ -40,18 +44,3 @@ impl DFT for NaiveDFT {
&self.output_buffer
}
}
impl NaiveDFT
{
pub fn execute_inv(&mut self, window: fn(f32) -> f32) {
for (freq, out) in self.output_buffer.iter_mut().enumerate() {
*out = Complex32::zero();
for (i, inp) in self.input_buffer.iter().enumerate() {
*out = *out
+ ((*inp * Complex32::cexp(2. * PI * (i * freq) as f32 / self.size as f32))
* window(i as f32 / self.size as f32));
}
}
}
}

View File

@ -4,7 +4,7 @@ use std::f32::consts::PI;
use crate::{
complex::Complex32,
fft::{DFT, create_fft, dft::NaiveDFT, prime_factors, windows},
fft::{DFT, FFTDirection, create_fft, dft::NaiveDFT, prime_factors, windows},
};
pub struct MixedRadixFFT {
@ -23,23 +23,20 @@ pub struct MixedRadixFFT {
}
impl DFT for MixedRadixFFT {
fn create(size: usize) -> Self {
fn create(size: usize, direction: FFTDirection) -> Self {
let q = decide_radix_factor(size);
let p = size / q;
println!("{} {}", p, q);
// TODO: Figure out why it does not work in the other direction ...
//let (p, q) = (q, p);
let qfft = create_fft(q);
let pfft = create_fft(p);
let qfft = create_fft(q, direction);
let pfft = create_fft(p, direction);
let qfft = Box::new(NaiveDFT::create(q));
let pfft = Box::new(NaiveDFT::create(p));
//let qfft = Box::new(NaiveDFT::create(q, direction));
//let pfft = Box::new(NaiveDFT::create(p, direction));
MixedRadixFFT {
input_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
output_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
size,
twiddle_factors: compute_twiddle_factors(size),
twiddle_factors: compute_twiddle_factors(size, direction),
qfft,
pfft,
@ -92,11 +89,11 @@ impl DFT for MixedRadixFFT {
}
}
fn compute_twiddle_factors(size: usize) -> Box<[Complex32]> {
fn compute_twiddle_factors(size: usize, direction: FFTDirection) -> Box<[Complex32]> {
let mut factors = vec![Complex32::zero(); size].into_boxed_slice();
for i in 0..size {
factors[i] = Complex32::cexp(-2. * PI * i as f32 / (size as f32));
factors[i] = Complex32::cexp(-2. * direction.sign() * PI * i as f32 / (size as f32));
}
factors
}

View File

@ -5,7 +5,7 @@ use std::{f32::consts::PI, ops::Deref};
use super::mixed_radix;
use crate::{
complex::Complex32,
fft::{DFT, create_fft, dft::NaiveDFT, is_prime, windows},
fft::{DFT, FFTDirection, create_fft, dft::NaiveDFT, is_prime, windows},
};
pub struct RaderFFT {
@ -14,13 +14,14 @@ pub struct RaderFFT {
permutations: Box<[usize]>,
convolution_op: Box<[Complex32]>,
conv_fft: Box<NaiveDFT>,
inv_fft: Box<dyn DFT>,
conv_fft: Box<dyn DFT>,
size: usize,
}
impl DFT for RaderFFT {
fn create(size: usize) -> Self
fn create(size: usize, direction: FFTDirection) -> Self
where
Self: Sized,
{
@ -28,14 +29,16 @@ impl DFT for RaderFFT {
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 = Box::new(NaiveDFT::create(size - 1));
let mut conv_fft = create_fft(size - 1, FFTDirection::Forward);
//let mut conv_fft = create_fft(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))
*x = Complex32::cexp(
-2. * direction.sign() * PI * (permutations[i] as f32) / (size as f32),
)
});
conv_fft.execute(windows::rectanguar);
@ -45,6 +48,7 @@ impl DFT for RaderFFT {
permutations,
convolution_op: conv_fft.get_output().iter().copied().collect(),
inv_fft: create_fft(size - 1, FFTDirection::Inverse),
conv_fft,
size,
@ -61,21 +65,20 @@ impl DFT for RaderFFT {
self.conv_fft.execute(windows::rectanguar);
for i in 0..(self.size - 1) {
self.output_buffer[i] =
self.conv_fft.get_output()[i] * self.convolution_op[i];
self.output_buffer[i] = self.conv_fft.get_output()[i] * self.convolution_op[i];
}
for i in 0..(self.size - 1) {
//self.conv_fft.get_input()[i] = self.output_buffer[i];
self.conv_fft.get_input()[i] = -self.output_buffer[self.size - 1 - i - 1];
self.inv_fft.get_input()[i] = self.output_buffer[i];
}
self.conv_fft.execute(windows::rectanguar);
self.inv_fft.execute(windows::rectanguar);
for i in 0..(self.size - 1) {
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.inv_fft.get_output()[i] / (self.size - 1) as f32) + self.input_buffer[0];
}
self.output_buffer[0] = self.input_buffer.iter().copied().sum();

View File

@ -1,11 +1,12 @@
// Implementation of raders's fft for prime sized ffts
/*
use std::{f32::consts::PI, ops::Deref};
use super::mixed_radix;
use crate::{
complex::Complex32,
fft::{DFT, create_fft, dft::NaiveDFT, is_prime, windows},
fft::{DFT, FFTDirection, create_fft, dft::NaiveDFT, is_prime, windows},
};
pub struct Rader2FFT {
@ -22,7 +23,7 @@ pub struct Rader2FFT {
}
impl DFT for Rader2FFT {
fn create(size: usize) -> Self
fn create(size: usize, direction: FFTDirection) -> Self
where
Self: Sized,
{
@ -165,3 +166,4 @@ pub fn next_pow2(mut n: usize) -> usize {
}
1 << pow
}
*/

View File

@ -1,19 +1,20 @@
// Cooley-Tukey algorithm
use crate::complex::Complex32;
use crate::fft::DFT;
use crate::fft::{DFT, FFTDirection};
use std::f32::consts::PI;
pub struct Radix2FFT {
output_buffer: Box<[Complex32]>,
input_buffer: Box<[Complex32]>,
direction: FFTDirection,
size: usize,
length: usize,
}
impl DFT for Radix2FFT {
// Size as power of two
fn create(size: usize) -> Self {
fn create(size: usize, direction: FFTDirection) -> Self {
if !is_power_of_two(size) {
panic!("Tried to create a Radix2 FFT with a non power of two size.");
}
@ -22,6 +23,7 @@ impl DFT for Radix2FFT {
output_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
input_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
size: size.ilog2() as usize,
direction,
length: size,
}
}
@ -41,7 +43,7 @@ impl DFT for Radix2FFT {
// Compute current polynomial at each unit root
let a = self.output_buffer[s + i];
let b = self.output_buffer[s + i + mid_point];
let angle = -2. * PI * (i as f32) / (pol_length as f32);
let angle = -2. * self.direction.sign() * PI * (i as f32) / (pol_length as f32);
let phasor = Complex32::cexp(angle);
self.output_buffer[i + s] = a + phasor * b;
self.output_buffer[i + s + mid_point] = a - phasor * b;