Raders algorithm

This commit is contained in:
2025-09-21 19:42:51 +02:00
parent 79f03a071a
commit 01e3657b55
5 changed files with 4975 additions and 4968 deletions

View File

@ -1,78 +1,74 @@
// Implementation of raders's fft for prime sized ffts
use std::f32::consts::PI;
use std::{f32::consts::PI, ops::Deref};
use crate::{complex::Complex32, fft::{dft::NaiveDFT, mixed_radix::is_prime, DFT}};
use super::mixed_radix;
use crate::{
complex::Complex32,
fft::{DFT, create_fft, dft::NaiveDFT, is_prime},
};
pub struct RaderFFT {
input_buffer: Box<[Complex32]>,
output_buffer: Box<[Complex32]>,
pub struct RaderFFT
{
input_buffer: Box<[Complex32]>,
output_buffer: Box<[Complex32]>,
size: usize,
g: usize,
// Fourrier transform of the exponential terms
convolution_operand: Box<[Complex32]>,
convolution_fft: NaiveDFT, // TODO: Use fft
convolution_fft: Box<dyn DFT>, // TODO: Use fft
permutation: Box<[usize]>,
}
impl DFT for RaderFFT
{
impl DFT for RaderFFT {
fn create(size: usize) -> Self
where Self: Sized {
let g = compute_prime_primitive_root(size);
RaderFFT
{
input_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
output_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
where
Self: Sized,
{
let g = compute_prime_primitive_root(size);
let permutation: Box<[usize]> = (0..(size - 1)).map(|i| exp_mod(g, i + 1, size)).collect();
RaderFFT {
input_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
output_buffer: vec![Complex32::zero(); size].into_boxed_slice(),
size,
g,
convolution_operand: compute_convolution_operand(size, g),
convolution_fft: NaiveDFT::create(size - 1),
}
size,
g,
convolution_operand: compute_convolution_operand(size, &permutation),
convolution_fft: Box::new(NaiveDFT::create(size - 1)),
permutation,
}
}
fn execute(&mut self) {
// Copy to convolution fft with permutation
for i in 0..(self.size - 1)
{
self.convolution_fft.get_input()[i] = self.input_buffer[exp_mod(self.g, self.size - 1 - i - 1, self.size)];
for i in 0..(self.size - 1) {
self.convolution_fft.get_input()[i] =
self.input_buffer[self.permutation[self.size - 1 - i - 1]]
}
self.convolution_fft.execute();
// Convolve (use output buffer as staging buffer)
self.output_buffer
.iter_mut()
.skip(1)
.zip(self.convolution_operand.iter())
.zip(self.convolution_fft.get_output().iter())
.for_each(|((dest, a), b)| *dest = *a * *b);
// Add first sample as DC-offset
//self.output_buffer[1] = self.output_buffer[1] + self.input_buffer[0];
// Copy to input
self
.convolution_fft
.get_input()
.iter_mut()
.zip(self.output_buffer.iter().skip(1))
.for_each(|(dest, x)| *dest = - *x);
self.convolution_fft.execute(); // Inverse fft
// Copy to output buffer : n - 1 terms to copy
for i in 1..(self.size)
{
self.output_buffer[i] =
self.convolution_fft.get_output()[exp_mod(self.g, i, self.size) - 1] / (self.size as f32 - 1.) + self.input_buffer[0];
// 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];
}
for i in 0..(self.size - 1) {
self.convolution_fft.get_input()[i] = self.output_buffer[self.size - 1 - i - 1];
}
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();
for i in 0..(self.size - 1) {
self.output_buffer[self.permutation[i]] =
self.convolution_fft.get_output()[i] / (self.size - 1) as f32;
}
self.output_buffer[0] = self.input_buffer.iter().copied().sum();
}
@ -83,46 +79,39 @@ impl DFT for RaderFFT
fn get_output(&self) -> &[Complex32] {
&self.output_buffer
}
}
pub fn compute_convolution_operand(n: usize, g: usize) -> Box<[Complex32]>
{
println!("TODO: Change to better fft");
let mut fft = NaiveDFT::create(n - 1); //TODO: Use fft
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 * (exp_mod(g, i + 1, n) as f32) / (n as f32)));
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().map(|x| *x).collect::<Vec<_>>().into_boxed_slice()
fft.get_output().iter().copied().collect()
}
pub fn compute_prime_primitive_root(n: usize) -> usize
{
pub fn compute_prime_primitive_root(n: usize) -> usize {
assert!(is_prime(n));
let phi = n - 1; // Euler's totient for n prime
// Test all candidates
for i in 1..(n + 1)
{
// Find multiplicative order of i
for i in 1..(n + 1) {
// Find multiplicative order of i
let mut val = i;
let mut order = 1;
for j in 0..n
{
if val == 1
{
for j in 0..n {
if val == 1 {
break;
}
val = (val * i) % n;
order += 1;
}
if order == phi
{
if order == phi {
return i;
}
}
@ -130,31 +119,21 @@ pub fn compute_prime_primitive_root(n: usize) -> usize
unreachable!("Prime must have primitive root");
}
pub fn exp_mod(n: usize, exp: usize, m: usize) -> usize
{
let mut num = n % m;
let mut acc = 1;
let mut exp = exp;
if exp == 0
{
return 1;
pub fn exp_mod(mut n: usize, mut exp: usize, m: usize) -> usize {
if m == 1 {
return 0;
}
while exp != 1
{
if exp % 2 == 0
{
num = (num * num) % m;
exp /= 2;
}
else
{
acc = (acc * n) % m;
exp -= 1;
num = num * num % m;
exp /= 2;
n %= m;
let mut r = 1;
while exp > 0 {
if exp % 2 == 1 {
r = (r * n) % m;
}
n = (n * n) % m;
exp >>= 1;
}
(num * acc) % m
r
}