Skip to content

Commit

Permalink
removed node
Browse files Browse the repository at this point in the history
  • Loading branch information
klangner committed Jan 30, 2024
1 parent 05aa955 commit 4c375d2
Show file tree
Hide file tree
Showing 24 changed files with 51 additions and 146 deletions.
1 change: 0 additions & 1 deletion benches/gen_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ extern crate criterion;

use criterion::Criterion;

use dsp::node::SourceNode;
use dsp::core::generator::*;


Expand Down
1 change: 0 additions & 1 deletion examples/file_sink.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clap::Parser;
use dsp::node::{SourceNode, SinkNode};
use dsp::core::generator::Sine;
use dsp::core::file::FileSink;

Expand Down
1 change: 0 additions & 1 deletion examples/file_source.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use gnuplot::{Figure, Color, AxesCommon};
use dsp::node::SourceNode;
use dsp::core::file::FileSource;


Expand Down
1 change: 0 additions & 1 deletion examples/heart_rate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fs::File;
use gnuplot::{Figure, Color, AxesCommon};
use dsp::node::ProcessNode;
use dsp::core::correlation::*;


Expand Down
1 change: 0 additions & 1 deletion examples/play.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

use dsp::core::generator::Sine;
use dsp::node::SourceNode;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};


Expand Down
1 change: 0 additions & 1 deletion examples/plot_freq.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use gnuplot::{Figure, Color, AxesCommon};
use clap::Parser;
use dsp::num_complex::Complex32;
use dsp::node::{ProcessNode, SourceNode};
use dsp::core::{generator::Sine, fft::*, complex::*};


Expand Down
1 change: 0 additions & 1 deletion examples/synth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

use dsp::core::generator::Sine;
use dsp::node::SourceNode;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};


Expand Down
1 change: 0 additions & 1 deletion examples/tuner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::env;
use dsp::audio::AudioFileSource;
use gnuplot::{Figure, Color, AxesCommon};
use dsp::node::*;
use dsp::core::{fft::*, complex::*};
use dsp::num_complex::Complex32;
use dsp::spectrum;
Expand Down
1 change: 0 additions & 1 deletion examples/udp_sink.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dsp::node::{SourceNode, SinkNode};
use dsp::core::generator::Sine;
use dsp::core::network::UdpSink;

Expand Down
6 changes: 1 addition & 5 deletions src/audio/audio_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

use audrey::Reader;

use crate::node::SourceNode;


pub struct AudioFileSource {
reader: Reader<std::io::BufReader<std::fs::File>>,
Expand All @@ -15,10 +13,8 @@ impl AudioFileSource {
let reader = audrey::open(file_path).unwrap();
AudioFileSource {reader}
}
}

impl SourceNode<f32> for AudioFileSource {
fn write_buffer(&mut self, buffer: &mut [f32]) {
pub fn write_buffer(&mut self, buffer: &mut [f32]) {
let mut samples = self.reader.samples();
for i in 0..buffer.len() {
if let Some(v) = samples.next() {
Expand Down
12 changes: 3 additions & 9 deletions src/core/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
//!
//!

use crate::{node::ProcessNode, num_complex::Complex32};
use crate::num_complex::Complex32;


/// Implement Real -> complex converter
///
/// Example
///
/// ```
/// use dsp::node::ProcessNode;
/// use dsp::core::complex::RealToComplex;
/// use dsp::num_complex::Complex32;
///
Expand All @@ -29,10 +28,8 @@ impl RealToComplex {
pub fn new() -> RealToComplex {
RealToComplex {}
}
}

impl ProcessNode<f32, Complex32> for RealToComplex {
fn process_buffer(&mut self, input_buffer: &[f32], output_buffer: &mut [Complex32]) {
pub fn process_buffer(&mut self, input_buffer: &[f32], output_buffer: &mut [Complex32]) {
let n = usize::min(input_buffer.len(), output_buffer.len());
for i in 0..n {
output_buffer[i] = Complex32::new(input_buffer[i], 0.);
Expand All @@ -46,7 +43,6 @@ impl ProcessNode<f32, Complex32> for RealToComplex {
/// Example
///
/// ```
/// use dsp::node::ProcessNode;
/// use dsp::core::complex::ComplexToReal;
/// use dsp::num_complex::Complex32;
///
Expand All @@ -65,10 +61,8 @@ impl ComplexToReal {
pub fn new() -> ComplexToReal {
ComplexToReal {}
}
}

impl ProcessNode<Complex32, f32> for ComplexToReal {
fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [f32]) {
pub fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [f32]) {
let n = usize::min(input_buffer.len(), output_buffer.len());
for i in 0..n {
output_buffer[i] = input_buffer[i].norm();
Expand Down
7 changes: 1 addition & 6 deletions src/core/correlation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Calculate (Auto)Correlation
//!
use crate::node::ProcessNode;


pub struct AutoCorrelation {
Expand All @@ -11,17 +10,13 @@ impl AutoCorrelation {
pub fn new(window_size: usize) -> AutoCorrelation {
AutoCorrelation {window_size}
}
}

impl ProcessNode<f32, f32> for AutoCorrelation {

/// Calculate correlation between 2 buffers
///
/// Example
///
/// ```
/// use assert_approx_eq::assert_approx_eq;
/// use dsp::node::{SourceNode, ProcessNode};
/// use dsp::core::generator::Sine;
/// use dsp::core::correlation::AutoCorrelation;
///
Expand All @@ -38,7 +33,7 @@ impl ProcessNode<f32, f32> for AutoCorrelation {
/// assert_approx_eq!(corr_buffer[3], 0., 1e-5f32);
/// assert_approx_eq!(corr_buffer[4], 1.0, 1e-5f32);
/// ```
fn process_buffer(&mut self, input_buffer: &[f32], output_buffer: &mut [f32]) {
pub fn process_buffer(&mut self, input_buffer: &[f32], output_buffer: &mut [f32]) {
let mu: f32 = input_buffer.iter().sum::<f32>() / input_buffer.len() as f32;
let max_offset = usize::min(input_buffer.len()-self.window_size, output_buffer.len());
// auto covariance
Expand Down
12 changes: 3 additions & 9 deletions src/core/fft.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Helper functions for FFT.
use std::sync::Arc;
use crate::{node::ProcessNode, window};
use rustfft::{Fft, FftPlanner};
use crate::num_complex::Complex32;
use crate::{num_complex::Complex32, window};


pub struct ForwardFFT {
Expand Down Expand Up @@ -33,11 +32,9 @@ impl ForwardFFT {
let mut fft = FftPlanner::new();
ForwardFFT { fft: fft.plan_fft_forward(sample_size), window }
}
}

impl ProcessNode<Complex32, Complex32> for ForwardFFT {

fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [Complex32]) {
pub fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [Complex32]) {
let n = usize::min(usize::min(input_buffer.len(), output_buffer.len()), self.window.len());
for i in 0..n {
output_buffer[i] = input_buffer[i].scale(self.window.as_slice()[i]);
Expand All @@ -61,11 +58,9 @@ impl InverseFFT {
fft: fft.plan_fft_inverse(sample_size),
}
}
}

impl ProcessNode<Complex32, Complex32> for InverseFFT {

fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [Complex32]) {
pub fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [Complex32]) {
let n = usize::min(input_buffer.len(), output_buffer.len());
for i in 0..n {
output_buffer[i] = input_buffer[i];
Expand All @@ -80,7 +75,6 @@ impl ProcessNode<Complex32, Complex32> for InverseFFT {
#[cfg(test)]
mod tests {
use super::*;
use crate::node::ProcessNode;

#[test]
fn test_fft() {
Expand Down
10 changes: 2 additions & 8 deletions src/core/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
use std::fs::File;
use byteorder::{ReadBytesExt, WriteBytesExt};
use byteorder::LittleEndian;
use crate::node::{SinkNode, SourceNode};


/// Save binary data into a file
///
/// Example
///
/// ```
/// use dsp::node::SinkNode;
/// use dsp::core::file::FileSink;
///
/// let mut node = FileSink::new("target/file.dat");
Expand All @@ -30,10 +28,8 @@ impl FileSink {
FileSink{file: None}
}
}
}

impl SinkNode<f32> for FileSink {
fn read_buffer(&mut self, input_buffer: &[f32]) {
pub fn read_buffer(&mut self, input_buffer: &[f32]) {
let mut file = self.file.as_ref().unwrap();
for v in input_buffer {
file.write_f32::<LittleEndian>(*v).expect("Can't reead from file");
Expand All @@ -54,10 +50,8 @@ impl FileSource {
let file = File::open(file_name).expect("Can't open file");
FileSource {file}
}
}

impl SourceNode<f32> for FileSource {
fn write_buffer(&mut self, output_buffer: &mut [f32]) {
pub fn write_buffer(&mut self, output_buffer: &mut [f32]) {
for i in 0..output_buffer.len() {
if let Ok(v) = self.file.read_f32::<LittleEndian>() {
output_buffer[i] = v;
Expand Down
12 changes: 7 additions & 5 deletions src/core/fm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//!

use num_complex::Complex32;
use crate::node::ProcessNode;


/// Demodulation block using the conjugate delay method
Expand All @@ -12,7 +11,6 @@ use crate::node::ProcessNode;
///
/// ```
/// use dsp::num_complex::Complex32;
/// use dsp::node::ProcessNode;
/// use dsp::core::fm::QuadratureDetector;
///
/// let mut node = QuadratureDetector::new();
Expand All @@ -28,15 +26,19 @@ impl QuadratureDetector {
pub fn new() -> Self {
Self {last_sample: Complex32::default()}
}
}

impl ProcessNode<Complex32, f32> for QuadratureDetector {
fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [f32]) {
pub fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [f32]) {
let n = usize::min(input_buffer.len(), output_buffer.len());
for i in 0..n {
let v = &input_buffer[i];
output_buffer[i] = (v * self.last_sample.conj()).arg(); // Obtain phase of x[n] * conj(x[n-1])
self.last_sample = *v;
}
}

pub fn process_sample(&mut self, v: &Complex32) -> f32 {
let d = (v * self.last_sample.conj()).arg(); // Obtain phase of x[n] * conj(x[n-1])
self.last_sample = *v;
d
}
}
10 changes: 5 additions & 5 deletions src/core/freq_shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//!

use num_complex::Complex32;
use crate::node::ProcessNode;

use super::generator::Sine;

Expand All @@ -13,7 +12,6 @@ use super::generator::Sine;
///
/// ```
/// use dsp::num_complex::Complex32;
/// use dsp::node::ProcessNode;
/// use dsp::core::generator::Sine;
/// use dsp::core::freq_shift::FrequencyShift;
///
Expand All @@ -32,13 +30,15 @@ impl FrequencyShift {
let offset_signal = Sine::new(freq_offset as f32, sample_rate);
Self {offset_signal}
}
}

impl ProcessNode<Complex32, Complex32> for FrequencyShift {
fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [Complex32]) {
pub fn process_buffer(&mut self, input_buffer: &[Complex32], output_buffer: &mut [Complex32]) {
let n = usize::min(input_buffer.len(), output_buffer.len());
for i in 0..n {
output_buffer[i] = self.offset_signal.next().unwrap() * input_buffer[i];
}
}

pub fn process_sample(&mut self, v: &Complex32) -> Complex32 {
self.offset_signal.next().unwrap() * v
}
}
Loading

0 comments on commit 4c375d2

Please sign in to comment.