diff --git a/src/fir.rs b/src/fir.rs index 79ee66a..d1d2ef5 100644 --- a/src/fir.rs +++ b/src/fir.rs @@ -1,27 +1,41 @@ // Finite Impulse response + Decimation -use crate::utils::ring_buffer::RingBuffer; -use num_complex::Complex32; +use crate::{iq_reader::IqChunk, iq_reader::IqSample, utils::ring_buffer::RingBuffer}; pub struct Fir { // Filter coefs pub taps: [f32; N], // Ring Buffer of samples - pub history: RingBuffer, + pub history: RingBuffer, - decimation_factor: usize, + // Factor of decimation + pub decimation_factor: usize, - // When to keep a sample - decimator_counter: usize, + // Track decimation + pub decimation_index: usize, } impl Fir { - fn new(taps: [f32; N], decimation_factor: usize) -> Self { + pub fn new(taps: [f32; N], decimation_factor: usize) -> Self { Self { taps, - history: RingBuffer::new(N), + history: RingBuffer::::new(N), decimation_factor, - decimator_counter: 0, + decimation_index: 0, } } + + pub fn process_chunk(&mut self, chunk: &mut IqChunk) { + for iq in chunk.samples.iter_mut() { + self.history.push(*iq); + let mut y_n = IqSample::default(); + for k in 0..N { + if let Some(sample) = self.history.read_at(k) { + y_n += *sample * self.taps[k]; + } + } + *iq = y_n + } + // TODO: Decimation + } } diff --git a/src/main.rs b/src/main.rs index ac8a70d..af40885 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use crate::agc::Agc; +use crate::fir::Fir; use crate::iq_reader::FileSource; use std::error::Error; @@ -10,17 +11,21 @@ mod utils; fn main() -> Result<(), Box> { let source = FileSource::new("test.iq", 32769)?; - // for chunk in source { - // println!("{chunk :?}"); - // } + // 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 + let taps = [1.0; 64]; + // Fir + let mut fir = Fir::<64>::new(taps, 4); + // Apply Auto Gain Control for chunk_r in source { let mut chunk = chunk_r?; agc.process_chunk(&mut chunk); + fir.process_chunk(&mut chunk); } Ok(()) diff --git a/src/utils/ring_buffer.rs b/src/utils/ring_buffer.rs index 67e29b8..3030615 100644 --- a/src/utils/ring_buffer.rs +++ b/src/utils/ring_buffer.rs @@ -21,13 +21,13 @@ impl RingBuffer { } } - pub fn write(&mut self, value: T) { + pub fn push(&mut self, value: T) { self.data[self.head] = value; self.head = (self.head + 1) % self.capacity; self.size = (self.size + 1).min(self.capacity); } - pub fn read(&mut self) -> Option { + pub fn pop(&mut self) -> Option { if self.size == 0 { return None; } @@ -55,7 +55,7 @@ impl RingBuffer { pub fn write_read(&mut self, value: T, delay: usize) -> Option { let delayed = self.read_at(delay).copied(); - self.write(value); + self.push(value); delayed }