Skip to content

Commit

Permalink
Update FIR filter implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
minseongg committed Oct 2, 2024
1 parent 61f6752 commit fcef704
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions hazardflow-designs/src/examples/fir_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ use crate::prelude::*;
use crate::std::*;

impl<P: Copy + Default> Valid<P> {
/// Window combinator
/// Window combinator.
///
/// It takes a stream of input value P and return the latest N values.
fn window<const N: usize>(self) -> Valid<Array<P, N>>
where [(); N + 1]: {
self.fsm_map(P::default().repeat::<N>(), |ip, s| {
let ep = s.append(ip.repeat::<1>()).clip_const::<N>(0);
let s_next = ep;
where
[(); N - 1]:,
[(); 1 + (N - 1)]:,
{
self.fsm_map(P::default().repeat::<{ N - 1 }>(), |ip, s| {
let ep = ip.repeat::<1>().append(s).resize::<N>();
let s_next = ep.clip_const::<{ N - 1 }>(0);
(ep, s_next)
})
}
}

impl<const N: usize> Valid<Array<u32, N>> {
/// Weight combinator
fn weight(self, weight: [u32; N]) -> Valid<Array<u32, N>> {
self.map(|ip| ip.zip(Array::from(weight)).map(|(ele, weight)| ele * weight))
}

/// Sum combinator
/// It will add up all the elements within an array.
/// Sum combinator.
///
/// It adds up all the elements within an array.
fn sum(self) -> Valid<u32> {
self.map(|ip| ip.fold_assoc(|e1, e2| e1 + e2))
self.map(|ip| ip.fold(0, |acc, e| acc + e))
}
}

/// FIR filter implementation
#[synthesize]
pub fn fir_filter(input: Valid<u32>) -> Valid<u32> {
input.window::<3>().weight([4, 2, 3]).sum()
input.window::<3>().map(|ip| ip.zip(Array::from([4, 2, 3])).map(|(e, wt)| e * wt)).sum()
}

0 comments on commit fcef704

Please sign in to comment.