From 9368b1ac845b655675a416e74758d13cde99fb99 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Fri, 22 May 2020 17:33:30 +0200 Subject: [PATCH] Added some sources --- src/firfilter.rs | 13 ++++--- src/lib.rs | 1 + src/sources.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 src/sources.rs diff --git a/src/firfilter.rs b/src/firfilter.rs index 90b45fd..adc7530 100644 --- a/src/firfilter.rs +++ b/src/firfilter.rs @@ -3,7 +3,8 @@ use std::iter::FromIterator; pub struct FIRFilter<'a, NumType> where - NumType: Num + Copy { + NumType: Num + Copy, +{ coeffs: &'a [NumType], state: Vec, pos: usize, @@ -12,7 +13,8 @@ where impl<'a, NumType> FIRFilter<'a, NumType> where - NumType: Num + Copy { + NumType: Num + Copy, +{ pub fn from(iterator: I, coeffs: &'a [NumType]) -> FIRFilter<'a, NumType> where I: Iterator + 'a, @@ -32,8 +34,9 @@ where } impl<'a, NumType> Iterator for FIRFilter<'a, NumType> - where - NumType: Num + Copy { +where + NumType: Num + Copy, +{ type Item = NumType; fn next(&mut self) -> Option { @@ -55,8 +58,6 @@ impl<'a, NumType> Iterator for FIRFilter<'a, NumType> } } - - #[cfg(test)] mod tests { use super::*; diff --git a/src/lib.rs b/src/lib.rs index b4509fb..e80ae33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,3 +2,4 @@ mod amdemod; mod firfilter; mod resamplers; mod multiply; +mod sources; diff --git a/src/sources.rs b/src/sources.rs new file mode 100644 index 0000000..6e5f37b --- /dev/null +++ b/src/sources.rs @@ -0,0 +1,96 @@ +use std::f32::consts::PI; + +pub struct SinSource { + sampling_rate: f32, + frequency: f32, + phase_acc: f32, +} + +impl SinSource { + pub fn new(frequency: f32, sampling_rate: f32) -> SinSource { + SinSource { + sampling_rate: sampling_rate, + frequency: frequency, + phase_acc: 0.0, + } + } +} + +impl Iterator for SinSource { + type Item = f32; + + fn next(&mut self) -> Option { + self.phase_acc += (1.0 / self.sampling_rate) * self.frequency * 2.0 * PI; + if self.phase_acc > 2.0 * PI { + self.phase_acc -= 2.0 * PI; + } + + Some(self.phase_acc.sin()) + } +} + +pub struct CosSource { + sampling_rate: f32, + frequency: f32, + phase_acc: f32, +} + +impl CosSource { + pub fn new(frequency: f32, sampling_rate: f32) -> CosSource { + CosSource { + sampling_rate: sampling_rate, + frequency: frequency, + phase_acc: 0.0, + } + } +} + +impl Iterator for CosSource { + type Item = f32; + + fn next(&mut self) -> Option { + self.phase_acc += (1.0 / self.sampling_rate) * self.frequency * 2.0 * PI; + if self.phase_acc > 2.0 * PI { + self.phase_acc -= 2.0 * PI; + } + + Some(self.phase_acc.cos()) + } +} + +pub struct VFOSource<'a> { + sampling_rate: f32, + phase_acc: f32, + frequency_iterator: Box + 'a>, +} + +impl<'a> VFOSource<'a> { + pub fn new(frequency_iterator: I, sampling_rate: f32) -> VFOSource<'a> + where + I: Iterator + 'a, + { + VFOSource { + sampling_rate: sampling_rate, + phase_acc: 0.0, + frequency_iterator: Box::new(frequency_iterator), + } + } +} + +impl<'a> Iterator for VFOSource<'a> { + type Item = f32; + + fn next(&mut self) -> Option { + let current_frequency = match self.frequency_iterator.next() { + Some(x) => x, + None => return None, + }; + + self.phase_acc += (1.0 / self.sampling_rate) * current_frequency * 2.0 * PI; + if self.phase_acc > 2.0 * PI { + self.phase_acc -= 2.0 * PI; + } + + Some(self.phase_acc.sin()) + } +}