Browse Source

abstract out fourier, change buffer length

canon
Hazel Levine 9 months ago
parent
commit
9fabfcda31
5 changed files with 49 additions and 23 deletions
  1. +1
    -1
      Cargo.toml
  2. +38
    -0
      src/fourier.rs
  3. +1
    -0
      src/lib.rs
  4. +7
    -20
      src/main.rs
  5. +2
    -2
      src/pulse.rs

+ 1
- 1
Cargo.toml View File

@ -6,8 +6,8 @@ edition = "2018"
[dependencies]
anyhow = "1.0"
apodize = "1.0"
libpulse-binding = "2.0"
libpulse-simple-binding = "2.0"
termion = "1"
tui = "0.9"
rustfft = "3.0"

+ 38
- 0
src/fourier.rs View File

@ -0,0 +1,38 @@
use rustfft::num_complex::Complex;
use rustfft::num_traits::Zero;
use rustfft::FFTplanner;
#[derive(Debug)]
pub struct Fourier {
pub buf: Vec<Complex<f32>>,
}
impl Fourier {
pub fn new(recording: &[u8]) -> Fourier {
let mut input: Vec<Complex<f32>> = recording
.iter()
.map(|&val| Complex::new(val as f32, 0.0))
.collect();
let mut output: Vec<Complex<f32>> = vec![Complex::zero(); input.len()];
let mut planner = FFTplanner::new(false);
let fft = planner.plan_fft(input.len());
fft.process(&mut input, &mut output);
Fourier { buf: output }
}
pub fn normalize(&self) -> Fourier {
Fourier {
buf: self
.buf
.iter()
.map(|val| Complex::new(val.re / 2048.0, val.im))
.collect(),
}
}
pub fn analyze(&self) {
}
}

+ 1
- 0
src/lib.rs View File

@ -1,2 +1,3 @@
pub mod event;
pub mod fourier;
pub mod pulse;

+ 7
- 20
src/main.rs View File

@ -1,27 +1,14 @@
use std::sync::Arc;
use anyhow::Result;
use rustfft::num_complex::Complex;
use rustfft::num_traits::Zero;
use rustfft::FFTplanner;
use wizja::pulse::PARecorder;
use wizja::{fourier::Fourier, pulse::PARecorder};
fn main() -> Result<()> {
let mut rec = PARecorder::new()?;
rec.update()?;
let mut input: Vec<Complex<f32>> = rec
.buf
.iter()
.map(|&val| Complex::new(val as f32, 0.0))
.collect();
let mut transformed: Vec<Complex<f32>> = vec![Complex::zero(); 10000];
let mut planner = FFTplanner::new(false);
let fft = planner.plan_fft(10000);
fft.process(&mut input, &mut transformed);
let mut rec = PARecorder::new(2048)?;
println!("{:?}", transformed);
loop {
rec.update()?;
Ok(())
let fourier = Fourier::new(&rec.buf).normalize();
println!("{:?}", fourier);
}
}

+ 2
- 2
src/pulse.rs View File

@ -18,7 +18,7 @@ pub struct PARecorder {
}
impl PARecorder {
pub fn new() -> Result<PARecorder> {
pub fn new(buf_size: usize) -> Result<PARecorder> {
assert!(SPEC.is_valid());
let srv = Simple::new(
@ -38,7 +38,7 @@ impl PARecorder {
Ok(PARecorder {
srv,
buf: vec![0; 10000],
buf: vec![0; buf_size],
})
}


Loading…
Cancel
Save