begin cleaning ffts

This commit is contained in:
2025-09-24 19:24:40 +02:00
parent bd7ae2b19e
commit 3cc4144747
11 changed files with 152 additions and 99 deletions

View File

@ -17,15 +17,7 @@ use fft::rader;
use nco::Nco;
use plotters::prelude::*;
use crate::fft::{
DFT, FFTDirection, create_fft,
dft::NaiveDFT,
mixed_radix::MixedRadixFFT,
prime_factors,
rader::{RaderFFT, compute_prime_primitive_root, exp_mod},
radix2::Radix2FFT,
windows,
};
use crate::bfsk::BFSKDem;
// Utilities
fn map<T>(input: T, in_min: T, in_max: T, out_min: T, out_max: T) -> T
@ -35,14 +27,16 @@ where
((input - in_min.clone()) / (in_max - in_min)) * (out_max - out_min.clone()) + out_min
}
fn main() {}
fn main() {
modulate();
}
fn modulate() {
let sample_rate = 44100;
let mut frequency = 2000.0; //HZ
let mut bandwidth = 500.0; //HZ
let path = "a.jpg";
let path = "s.txt";
let file = File::open(path).unwrap();
let mut bit_stream = file.bytes().flat_map(|byte| {
let byte = byte.unwrap();
@ -80,15 +74,50 @@ fn modulate() {
let prev = Complex::new(0., 0.);
let alpha = 1.0 - (-2.0 * PI * ((1.5 * 0.5 * bandwidth) / sample_rate as f32));
let mut output_samples = vec![];
while let Some(sample) = bfsk.step_modulate() {
let amplitude = i16::MAX as f32;
let c_sample = lo.cexp() * sample;
let filtered = prev + (c_sample - prev) * alpha;
output_samples.push(filtered);
writer
.write_sample((amplitude * c_sample.re) as i16)
.unwrap();
lo.step();
}
writer.finalize().unwrap();
let mut of = File::create("out.txt").unwrap();
let mut bits = vec![];
let mut lodem = Nco::new(-2. * PI * (frequency / sample_rate as f32));
let mut demod = BFSKDem::new(
sample_rate / baud_rate,
PI * (bandwidth / sample_rate as f32),
);
for chunk in output_samples.chunks((sample_rate / baud_rate) as usize) {
let base_chunk: Vec<Complex32> = chunk
.iter()
.map(|x| {
lodem.step();
*x * lodem.cexp()
})
.collect();
let bit = demod.demod(base_chunk.as_slice());
bits.push(bit);
println!("{:?}", bit)
}
for b in bits.chunks(8) {
of.write_all(&[(b[0] as u8)
| ((b[0] as u8) << 1)
| ((b[0] as u8) << 2)
| ((b[0] as u8) << 3)
| ((b[0] as u8) << 4)
| ((b[0] as u8) << 5)
| ((b[0] as u8) << 6)
| ((b[0] as u8) << 7)])
.unwrap();
}
}