Added some sources

This commit is contained in:
Sebastian 2020-05-22 17:33:30 +02:00
parent fca2c0b86a
commit 9368b1ac84
3 changed files with 104 additions and 6 deletions

View File

@ -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<NumType>,
pos: usize,
@ -12,7 +13,8 @@ where
impl<'a, NumType> FIRFilter<'a, NumType>
where
NumType: Num + Copy {
NumType: Num + Copy,
{
pub fn from<I>(iterator: I, coeffs: &'a [NumType]) -> FIRFilter<'a, NumType>
where
I: Iterator<Item = NumType> + '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<NumType> {
@ -55,8 +58,6 @@ impl<'a, NumType> Iterator for FIRFilter<'a, NumType>
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -2,3 +2,4 @@ mod amdemod;
mod firfilter;
mod resamplers;
mod multiply;
mod sources;

96
src/sources.rs Normal file
View File

@ -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::Item> {
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::Item> {
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<dyn Iterator<Item = f32> + 'a>,
}
impl<'a> VFOSource<'a> {
pub fn new<I>(frequency_iterator: I, sampling_rate: f32) -> VFOSource<'a>
where
I: Iterator<Item = f32> + '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<Self::Item> {
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())
}
}