rusty-dsp/src/amdemod.rs

55 lines
1.3 KiB
Rust
Raw Permalink Normal View History

2020-03-14 23:23:04 +01:00
use num::traits::Float;
2020-03-14 23:08:57 +01:00
2020-06-03 21:54:48 +02:00
use super::error::DspError;
2020-03-14 23:23:04 +01:00
pub struct SquaringAMDemodulator<'a, NumType>
where
NumType: Float,
{
2020-06-03 21:54:48 +02:00
iterator: Box<dyn Iterator<Item = Result<NumType, DspError>> + 'a>,
2020-03-14 23:08:57 +01:00
}
2020-03-14 23:23:04 +01:00
impl<'a, NumType> SquaringAMDemodulator<'a, NumType>
where
NumType: Float,
{
pub fn from<I>(iterator1: I) -> SquaringAMDemodulator<'a, NumType>
2020-03-14 23:08:57 +01:00
where
2020-06-03 21:54:48 +02:00
I: Iterator<Item = Result<NumType, DspError>> + 'a,
2020-03-14 23:08:57 +01:00
{
SquaringAMDemodulator {
iterator: Box::new(iterator1),
}
}
}
2020-03-14 23:23:04 +01:00
impl<'a, NumType> Iterator for SquaringAMDemodulator<'a, NumType>
where
NumType: Float,
{
2020-06-03 21:54:48 +02:00
type Item = Result<NumType, DspError>;
2020-03-14 23:08:57 +01:00
fn next(&mut self) -> Option<Self::Item> {
match self.iterator.next() {
2020-06-03 21:54:48 +02:00
Some(Ok(x)) => Some(Ok((x * x).sqrt())),
Some(Err(e)) => Some(Err(e)),
2020-03-14 23:08:57 +01:00
None => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_amdemod() {
let test_data = vec![-1_f32, 2_f32, -3_f32, 4_f32, -5_f32];
2020-06-03 21:54:48 +02:00
let demod = SquaringAMDemodulator::from(test_data.into_iter().map(|x| Ok(x)));
2020-03-14 23:08:57 +01:00
2020-06-03 21:54:48 +02:00
let result_data: Result<Vec<f32>, DspError> = demod.collect();
assert_eq!(result_data, Ok(vec![1_f32, 2_f32, 3_f32, 4_f32, 5_f32]));
2020-03-14 23:08:57 +01:00
}
}