44 lines
1006 B
Rust
44 lines
1006 B
Rust
use std::iter::FromIterator;
|
|
|
|
pub struct SquaringAMDemodulator<'a> {
|
|
iterator: Box<dyn Iterator<Item = f32> + 'a>,
|
|
}
|
|
|
|
impl<'a> SquaringAMDemodulator<'a> {
|
|
pub fn from<I>(iterator1: I) -> SquaringAMDemodulator<'a>
|
|
where
|
|
I: Iterator<Item = f32> + 'a,
|
|
{
|
|
SquaringAMDemodulator {
|
|
iterator: Box::new(iterator1),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> Iterator for SquaringAMDemodulator<'a> {
|
|
type Item = f32;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
match self.iterator.next() {
|
|
Some(x) => Some((x * x).sqrt()),
|
|
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];
|
|
|
|
let demod = SquaringAMDemodulator::from(test_data.into_iter());
|
|
|
|
let result_data = Vec::from_iter(demod);
|
|
assert_eq!(result_data.len(), 5);
|
|
assert_eq!(result_data, [1_f32, 2_f32, 3_f32, 4_f32, 5_f32]);
|
|
}
|
|
}
|