diff --git a/src/fm_demod.rs b/src/fm_demod.rs index 9a030af..1441f43 100644 --- a/src/fm_demod.rs +++ b/src/fm_demod.rs @@ -1,4 +1,6 @@ -use crate::iq_reader::{IqChunk, IqSample}; +use crate::iq_reader::IqSample; + +pub type Phase = f32; pub struct FmDemod { pub inner: I, @@ -13,9 +15,10 @@ impl FmDemod { } } - pub fn process_sample(&mut self, sample: IqSample) -> IqSample { - // TODO: FM Demodulation - todo!(); + pub fn process_sample(&mut self, sample: IqSample) -> Phase { + let phase_diff = sample * self.prev_sample.conj(); + self.prev_sample = sample; + phase_diff.arg() } } @@ -23,7 +26,7 @@ impl Iterator for FmDemod where I: Iterator>, { - type Item = Result; + type Item = Result; fn next(&mut self) -> Option { match self.inner.next()? { diff --git a/src/iq_reader.rs b/src/iq_reader.rs index f370bf5..75e7855 100644 --- a/src/iq_reader.rs +++ b/src/iq_reader.rs @@ -6,18 +6,18 @@ use std::io::{BufReader, ErrorKind, Read}; pub type IqSample = Complex; // Data chunk -#[derive(Debug)] -pub struct IqChunk { - pub samples: Vec, -} - -impl IqChunk { - pub fn new() -> Self { - IqChunk { - samples: Vec::::new(), - } - } -} +// #[derive(Debug)] +// pub struct IqChunk { +// pub samples: Vec, +// } +// +// impl IqChunk { +// pub fn new() -> Self { +// IqChunk { +// samples: Vec::::new(), +// } +// } +// } pub struct FileSource { // Buffer diff --git a/src/main.rs b/src/main.rs index 9cf43ee..ca4972d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,11 +17,12 @@ fn main() -> Result<(), Box> { let pipeline = source .agc(20_000_000.0, 1.0, 0.001, 100.0) - .fir::<64>(taps, 4); + .fir::<64>(taps, 4) + .demodulate(); - for sample_r in pipeline { - let sample = sample_r?; - println!("sample : {} + i{}", sample.re, sample.im); + for phase_r in pipeline { + let phase = phase_r?; + println!("phase : {}", phase); } Ok(()) diff --git a/src/pipeline.rs b/src/pipeline.rs index 56bae3e..2c91cbc 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -1,5 +1,6 @@ use crate::agc::Agc; use crate::fir::Fir; +use crate::fm_demod::FmDemod; use crate::iq_reader::IqSample; pub trait DspPipelineExt: Iterator> + Sized { @@ -10,6 +11,10 @@ pub trait DspPipelineExt: Iterator> + Sized { fn fir(self, taps: [f32; N], decimation_factor: usize) -> Fir { Fir::new(self, taps, decimation_factor) } + + fn demodulate(self) -> FmDemod { + FmDemod::new(self) + } } impl DspPipelineExt for I where I: Iterator> {}