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,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;