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,5 +1,8 @@
use std::{
f32::consts::PI, fs::File, io::{Read, Write}, ops::{Add, Div, Mul, Sub}
f32::consts::PI,
fs::File,
io::{Read, Write},
ops::{Add, Div, Mul, Sub},
};
mod bfsk;
@ -15,12 +18,11 @@ use nco::Nco;
use plotters::prelude::*;
use crate::fft::{
DFT, create_fft,
DFT, FFTDirection, create_fft,
dft::NaiveDFT,
mixed_radix::MixedRadixFFT,
prime_factors,
rader::{RaderFFT, compute_prime_primitive_root, exp_mod},
rader2::{Rader2FFT, next_pow2},
radix2::Radix2FFT,
windows,
};
@ -33,135 +35,7 @@ where
((input - in_min.clone()) / (in_max - in_min)) * (out_max - out_min.clone()) + out_min
}
fn euclid_mod(a: f32, m: f32) -> f32 {
let r = a % m;
if r < 0.0 { r + m } else { r }
}
struct QuickLCG(i32);
impl QuickLCG
{
pub fn seed(val: i32) -> QuickLCG
{
QuickLCG(val % 10)
}
pub fn next(&mut self) -> i32
{
self.0 = self.0.overflowing_mul(9321).0.overflowing_add(5672).0 % 10;
self.0
}
}
fn main() {
//test();
simple_test();
}
fn simple_test()
{
let sample_count = 7;
let mut dft = NaiveDFT::create(sample_count);
let mut fft = RaderFFT::create(sample_count);
let mut rand = QuickLCG::seed(2981237);
for (a, b) in dft.get_input().iter_mut().zip(fft.get_input().iter_mut())
{
let re = (rand.next() - 5) as f32;
let im = (rand.next() - 5) as f32;
*a = Complex32::new(re, im);
*b = Complex32::new(re, im);
}
dft.execute(windows::rectanguar);
fft.execute(windows::rectanguar);
for (a, b) in dft.get_output().iter().zip(fft.get_output().iter())
{
println!("{:0<7.3} {:0<7.3} \t {:0<7.3} {:0<7.3}", a.re, a.im, b.re, b.im);
}
}
fn test() {
let freq1 = 2. * PI / 4.0;
let freq2 = 2. * PI / 8.0;
//let sample_count = 71*71;
//let sample_count = 71 * 71;
//let sample_count = 4804;
let sample_count = 4809;
let mut o1 = Nco::new(freq1);
let mut o2 = Nco::new(freq2);
//let mut fft = ::create(sample_count);
//let mut dft = MixedRadixFFT::create(sample_count);
let mut fft = create_fft(sample_count);
//let mut dft = create_fft(sample_count);
for x in fft.get_input().iter_mut() {
*x = o1.cexp() + o2.cexp();
//*y = *x;
o1.step();
o2.step();
}
fft.execute(windows::rectanguar);
//dft.execute(windows::rectanguar);
let root = BitMapBackend::new("out.png", (640, 480)).into_drawing_area();
root.fill(&WHITE).unwrap();
let mut chart = ChartBuilder::on(&root)
.caption("fft", ("sans-serif", 50).into_font())
.margin(5)
.x_label_area_size(30)
.y_label_area_size(30)
.build_cartesian_2d(0f32..(sample_count as f32), -PI..PI)
.unwrap();
//chart.configure_mesh().draw()?;
/*
chart
.draw_series(LineSeries::new(
(0..sample_count)
.zip(dft.get_output().iter())
.map(|(x, y)| (x as f32, (*y).arg() )),
&RED,
))
.unwrap()
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], RED));
*/
chart
.draw_series(LineSeries::new(
(0..sample_count)
.zip(fft.get_output().iter())
.map(|(x, y)| (x as f32, (*y).mag() / sample_count as f32)),
&BLUE,
))
.unwrap()
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], BLUE));
chart
.configure_series_labels()
.background_style(&WHITE.mix(0.8))
.border_style(&BLACK)
.draw()
.unwrap();
root.present().unwrap();
let mut f = File::create("out.csv").unwrap();
for x in fft.get_output().iter()
{
f.write_all(
format!("{},\n", x.mag() / sample_count as f32).to_string().as_bytes()
).unwrap();
}
}
fn main() {}
fn modulate() {
let sample_rate = 44100;