zero cost abstraction

This commit is contained in:
2026-06-12 15:15:11 +02:00
parent d3375c0e22
commit b4b2845321
4 changed files with 75 additions and 23 deletions

View File

@ -1,9 +1,9 @@
use num_complex::Complex;
use crate::iq_reader::IqChunk; use crate::iq_reader::IqChunk;
use crate::iq_reader::IqSample;
// Automatic Gain Control // Automatic Gain Control
pub struct Agc { pub struct Agc<I> {
inner: I,
// Previous power estimate // Previous power estimate
pub power_estimate: f32, pub power_estimate: f32,
// Previous gain // Previous gain
@ -16,8 +16,14 @@ pub struct Agc {
pub max_gain: f32, pub max_gain: f32,
} }
impl Agc { impl<I> Agc<I> {
pub fn new(sample_rate: f32, target_power: f32, min_gain: f32, max_gain: f32) -> Self { pub fn new(
inner: I,
sample_rate: f32,
target_power: f32,
min_gain: f32,
max_gain: f32,
) -> Self {
// Target attack time 5 ms // Target attack time 5 ms
let tau_attack = 0.005; let tau_attack = 0.005;
@ -33,6 +39,7 @@ impl Agc {
let current_gain = 1.0; let current_gain = 1.0;
Self { Self {
inner,
power_estimate, power_estimate,
current_gain, current_gain,
target_power, target_power,
@ -77,7 +84,24 @@ impl Agc {
self.current_gain = final_gain; self.current_gain = final_gain;
*z = Complex::new(i * final_gain, q * final_gain); *z = IqSample::new(i * final_gain, q * final_gain);
}
}
}
impl<I, E> Iterator for Agc<I>
where
I: Iterator<Item = Result<IqChunk, E>>,
{
type Item = Result<IqChunk, E>;
fn next(&mut self) -> Option<Self::Item> {
match self.inner.next()? {
Ok(mut chunk) => {
self.process_chunk(&mut chunk);
Some(Ok(chunk))
}
Err(e) => Some(Err(e)),
} }
} }
} }

View File

@ -1,7 +1,8 @@
// Finite Impulse response + Decimation // Finite Impulse response + Decimation
use crate::{iq_reader::IqChunk, iq_reader::IqSample, utils::ring_buffer::RingBuffer}; use crate::{iq_reader::IqChunk, iq_reader::IqSample, utils::ring_buffer::RingBuffer};
pub struct Fir<const N: usize> { pub struct Fir<I, const N: usize> {
inner: I,
// Filter coefs // Filter coefs
pub taps: [f32; N], pub taps: [f32; N],
@ -15,9 +16,10 @@ pub struct Fir<const N: usize> {
pub decimation_index: usize, pub decimation_index: usize,
} }
impl<const N: usize> Fir<N> { impl<I, const N: usize> Fir<I, N> {
pub fn new(taps: [f32; N], decimation_factor: usize) -> Self { pub fn new(inner: I, taps: [f32; N], decimation_factor: usize) -> Self {
Self { Self {
inner,
taps, taps,
history: RingBuffer::<IqSample>::new(N), history: RingBuffer::<IqSample>::new(N),
decimation_factor, decimation_factor,
@ -39,3 +41,20 @@ impl<const N: usize> Fir<N> {
// TODO: Decimation // TODO: Decimation
} }
} }
impl<I, E, const N: usize> Iterator for Fir<I, N>
where
I: Iterator<Item = Result<IqChunk, E>>,
{
type Item = Result<IqChunk, E>;
fn next(&mut self) -> Option<Self::Item> {
match self.inner.next()? {
Ok(mut chunk) => {
self.process_chunk(&mut chunk);
Some(Ok(chunk))
}
Err(e) => Some(Err(e)),
}
}
}

View File

@ -1,31 +1,25 @@
use crate::agc::Agc;
use crate::fir::Fir;
use crate::iq_reader::FileSource; use crate::iq_reader::FileSource;
use crate::pipeline::DspPipelineExt;
use std::error::Error; use std::error::Error;
mod agc; mod agc;
mod fir; mod fir;
mod iq_reader; mod iq_reader;
mod pipeline;
mod utils; mod utils;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let source = FileSource::new("test.iq", 32769)?; let source = FileSource::new("test.iq", 32769)?;
// TODO: make a clean pipline use like GNU radio with iterators (like chunk.agc(...).fir(...)...)
// 20 MSps
let mut agc = Agc::new(20_000_000.0, 0.1, 0.001, 100.0);
// Fir coefs // Fir coefs
let taps = [1.0; 64]; let taps = [1.0; 64];
// Fir
let mut fir = Fir::<64>::new(taps, 4);
// Apply Auto Gain Control let pipeline = source
for chunk_r in source { .agc(20_000_000.0, 1.0, 0.001, 100.0)
let mut chunk = chunk_r?; .fir::<64>(taps, 4);
agc.process_chunk(&mut chunk);
fir.process_chunk(&mut chunk); for chunk_r in pipeline {
let _chunk = chunk_r?;
} }
Ok(()) Ok(())

15
src/pipeline.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::agc::Agc;
use crate::fir::Fir;
use crate::iq_reader::IqChunk;
pub trait DspPipelineExt<E>: Iterator<Item = Result<IqChunk, E>> + Sized {
fn agc(self, sample_rate: f32, target_power: f32, min_gain: f32, max_gain: f32) -> Agc<Self> {
Agc::new(self, sample_rate, target_power, min_gain, max_gain)
}
fn fir<const N: usize>(self, taps: [f32; N], decimation_factor: usize) -> Fir<Self, N> {
Fir::new(self, taps, decimation_factor)
}
}
impl<I, E> DspPipelineExt<E> for I where I: Iterator<Item = Result<IqChunk, E>> {}