A few DSP blocks
This commit is contained in:
38
src/main.rs
38
src/main.rs
@ -1,3 +1,9 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use ntw_dsp::Frequency;
|
||||
use ntw_dsp::blocks::map::Map;
|
||||
use ntw_dsp::blocks::nco::ComplexNco;
|
||||
use ntw_dsp::generation::Nco;
|
||||
use ntw_flowgraph::Block;
|
||||
use ntw_flowgraph::BlockWork;
|
||||
use ntw_flowgraph::graph::Graph;
|
||||
@ -39,7 +45,7 @@ impl BlockWork for VecSource
|
||||
|
||||
fn ready(&self) -> bool
|
||||
{
|
||||
self.vector.len() > 0 && self.out.rb.vacant_len() > 0
|
||||
!self.vector.is_empty() && self.out.vacant_len() > 0
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,15 +79,13 @@ impl BlockWork for Adder
|
||||
&& let Some(b) = self.in_b.rb.try_pop()
|
||||
&& self.out.rb.vacant_len() > 0
|
||||
{
|
||||
let _ = self.out.rb.try_push(a + b);
|
||||
let _ = self.out.try_push(a + b);
|
||||
}
|
||||
}
|
||||
|
||||
fn ready(&self) -> bool
|
||||
{
|
||||
self.in_a.rb.occupied_len() > 0
|
||||
&& self.in_b.rb.occupied_len() > 0
|
||||
&& self.out.rb.vacant_len() > 0
|
||||
self.in_a.available_len() > 0 && self.in_b.available_len() > 0 && self.out.vacant_len() > 0
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,13 +99,13 @@ impl Adder
|
||||
}
|
||||
|
||||
#[derive(Block)]
|
||||
pub struct PrintSink
|
||||
pub struct PrintSink<T>
|
||||
{
|
||||
#[input]
|
||||
stream: In<u32>,
|
||||
stream: In<T>,
|
||||
}
|
||||
|
||||
impl BlockWork for PrintSink
|
||||
impl<T: Display> BlockWork for PrintSink<T>
|
||||
{
|
||||
fn work(&mut self)
|
||||
{
|
||||
@ -113,13 +117,13 @@ impl BlockWork for PrintSink
|
||||
|
||||
fn ready(&self) -> bool
|
||||
{
|
||||
self.stream.rb.occupied_len() > 0
|
||||
self.stream.available_len() > 0
|
||||
}
|
||||
}
|
||||
|
||||
impl PrintSink
|
||||
impl<T> PrintSink<T>
|
||||
{
|
||||
pub fn new(stream: In<u32>) -> PrintSink
|
||||
pub fn new(stream: In<T>) -> PrintSink<T>
|
||||
{
|
||||
PrintSink { stream }
|
||||
}
|
||||
@ -127,17 +131,15 @@ impl PrintSink
|
||||
|
||||
fn main()
|
||||
{
|
||||
let (vector_a, a) = VecSource::new((0..15).collect());
|
||||
let (vector_b, b) = VecSource::new((0..15).collect());
|
||||
let (adder, sum) = Adder::new(a, b);
|
||||
let printer = PrintSink::new(sum);
|
||||
let (nco, out) = ComplexNco::<f32>::new(Nco::new(Frequency::from_rad(0.001)));
|
||||
let (map, im) = Map::new(out, |x| x.im);
|
||||
let printer = PrintSink::new(im);
|
||||
|
||||
let mut graph = Graph::new();
|
||||
|
||||
graph.add_block(printer);
|
||||
graph.add_block(adder);
|
||||
graph.add_block(vector_a);
|
||||
graph.add_block(vector_b);
|
||||
graph.add_block(map);
|
||||
graph.add_block(nco);
|
||||
|
||||
graph.run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user