I want to die

This commit is contained in:
2025-09-24 23:10:28 +02:00
parent f62ef05cb8
commit 00b4756138
9 changed files with 4939 additions and 15151 deletions

View File

@ -3,13 +3,13 @@
use std::f32::consts::PI;
use crate::complex::{Complex, Complex32};
use crate::fft::{self, windows};
use crate::fft::{self, windows, FFTDirection, FFT};
use crate::map;
use crate::nco::Nco;
pub struct BFSKMod<'a, T: Iterator<Item = bool>> {
samples_per_bit: u32,
bandwidth: f32,
deviation: f32,
bit_stream: &'a mut T,
// State
@ -21,10 +21,10 @@ impl<'a, T> BFSKMod<'a, T>
where
T: Iterator<Item = bool>,
{
pub fn new(samples_per_bit: u32, bandwidth: f32, bit_stream: &'a mut T) -> Self {
pub fn new(samples_per_bit: u32, deviation: f32, bit_stream: &'a mut T) -> Self {
BFSKMod {
samples_per_bit,
bandwidth,
deviation,
oscillator: Nco::new(0.0),
bit_stream,
sample_index: samples_per_bit,
@ -37,9 +37,9 @@ where
let bit = self.bit_stream.next()?;
let frequency = if bit {
self.bandwidth / 2.0
self.deviation
} else {
-self.bandwidth / 2.0
-self.deviation
};
self.oscillator.set_frequency(frequency);
}
@ -56,62 +56,36 @@ pub struct BFSKDem {
deviation: f32,
// State
sample_index: u32,
fft: FFT,
//fft: Box<dyn DFT>,
bin_pos: usize,
bin_neg: usize,
}
impl BFSKDem {
pub fn new(samples_per_bit: u32, deviation: f32) -> Self {
// Calculate bin locations :
let bin_index = map(deviation, 0., 2. * PI, 0., samples_per_bit as f32).floor() as u32;
println!("bin_index: {bin_index}");
BFSKDem {
samples_per_bit,
deviation,
sample_index: 0,
//fft: fft::create_fft(samples_per_bit as usize, fft::FFTDirection::Forward),
fft: FFT::new(samples_per_bit as usize, windows::rectangular),
bin_pos: bin_index as usize,
bin_neg: (samples_per_bit - bin_index - 1) as usize, // -deviation = negative frequency = upper half
}
}
pub fn demod(&mut self, baseband: &[Complex32]) -> bool {
assert!(baseband.len() >= self.samples_per_bit as usize);
self.fft.execute(baseband);
/*
self.fft
.get_input()
.iter_mut()
.enumerate()
.for_each(|(i, x)| *x = baseband[i]);
self.fft.execute(windows::rectanguar);
*/
let positive_energy = self.fft.get_output()[self.bin_pos];
let negative_energy = self.fft.get_output()[self.bin_neg];
let bin_id = map(
self.deviation,
0.,
PI,
0.,
(self.samples_per_bit / 2) as f32,
)
.floor() as i32;
let bin_width = 5;
/*
let mut positive_energy = 0.0;
for i in (bin_id - bin_width)..(bin_id + bin_width) {
if i >= 0 && i < self.samples_per_bit as i32 {
positive_energy += self.fft.get_output()[i as usize].mag();
}
}
let mut negative_energy = 0.0;
for i in (self.samples_per_bit as i32 - bin_id - bin_width)
..(self.samples_per_bit as i32 - bin_id + bin_width)
{
if i >= 0 && i < self.samples_per_bit as i32 {
negative_energy += self.fft.get_output()[i as usize].mag();
}
}
return positive_energy < negative_energy;
*/
false
positive_energy.mag() < negative_energy.mag()
}
}