use super::{Stream, Tag};
use num_complex::Complex;
pub struct Downsample2CIC5 {
h0: Complex<f32>,
h1: Complex<f32>,
h2: Complex<f32>,
h3: Complex<f32>,
h4: Complex<f32>,
}
impl Downsample2CIC5 {
pub fn new() -> Self {
Self {
h0: Complex::new(0.0, 0.0),
h1: Complex::new(0.0, 0.0),
h2: Complex::new(0.0, 0.0),
h3: Complex::new(0.0, 0.0),
h4: Complex::new(0.0, 0.0),
}
}
}
impl Default for Downsample2CIC5 {
fn default() -> Self {
Self::new()
}
}
impl Stream<Complex<f32>, Complex<f32>> for Downsample2CIC5 {
fn receive(&mut self, data: &[Complex<f32>], _tag: &mut Tag) -> Vec<Complex<f32>> {
if data.len() < 2 {
return data.to_vec();
}
let len = (data.len() / 2) * 2;
let mut output = Vec::with_capacity(len / 2);
let mut i = 0;
while i < len {
let mut z = data[i];
let r0 = z;
z += self.h0;
let r1 = z;
z += self.h1;
let r2 = z;
z += self.h2;
let r3 = z;
z += self.h3;
let r4 = z; z += self.h4;
output.push(z * 0.03125);
z = data[i + 1];
self.h0 = z;
z += r0;
self.h1 = z;
z += r1;
self.h2 = z;
z += r2;
self.h3 = z;
z += r3;
self.h4 = z;
z += r4;
i += 2;
}
output
}
fn reset(&mut self) {
self.h0 = Complex::new(0.0, 0.0);
self.h1 = Complex::new(0.0, 0.0);
self.h2 = Complex::new(0.0, 0.0);
self.h3 = Complex::new(0.0, 0.0);
self.h4 = Complex::new(0.0, 0.0);
}
}
pub struct FilterCIC5 {
h0: Complex<f32>,
h1: Complex<f32>,
h2: Complex<f32>,
h3: Complex<f32>,
h4: Complex<f32>,
}
impl FilterCIC5 {
pub fn new() -> Self {
Self {
h0: Complex::new(0.0, 0.0),
h1: Complex::new(0.0, 0.0),
h2: Complex::new(0.0, 0.0),
h3: Complex::new(0.0, 0.0),
h4: Complex::new(0.0, 0.0),
}
}
}
impl Default for FilterCIC5 {
fn default() -> Self {
Self::new()
}
}
impl Stream<Complex<f32>, Complex<f32>> for FilterCIC5 {
fn receive(&mut self, data: &[Complex<f32>], _tag: &mut Tag) -> Vec<Complex<f32>> {
if data.len() < 2 {
return data.to_vec();
}
let len = (data.len() / 2) * 2;
let mut output = Vec::with_capacity(len);
let mut i = 0;
while i < len {
let mut z = data[i];
let r0 = z;
z += self.h0;
let r1 = z;
z += self.h1;
let r2 = z;
z += self.h2;
let r3 = z;
z += self.h3;
let r4 = z; z += self.h4;
output.push(z * 0.03125);
z = data[i + 1];
self.h0 = z;
z += r0;
self.h1 = z;
z += r1;
self.h2 = z;
z += r2;
self.h3 = z;
z += r3;
self.h4 = z;
z += r4;
output.push(z * 0.03125);
i += 2;
}
output
}
fn reset(&mut self) {
self.h0 = Complex::new(0.0, 0.0);
self.h1 = Complex::new(0.0, 0.0);
self.h2 = Complex::new(0.0, 0.0);
self.h3 = Complex::new(0.0, 0.0);
self.h4 = Complex::new(0.0, 0.0);
}
}