Removed vectors where possible

Optimized aptsyncer
Removed unecessary modules
This commit is contained in:
Sebastian 2016-11-28 09:33:00 +01:00
parent 3c1ebdb495
commit 604355d906
5 changed files with 44 additions and 107 deletions

View File

@ -28,7 +28,7 @@ pub enum SyncedSample {
}
pub struct APTSyncer<'a> {
state: Vec<f32>,
state: [f32; SYNC_LENGHT],
pos : usize,
nones_read: usize,
max_level : f32,
@ -37,12 +37,12 @@ pub struct APTSyncer<'a> {
impl<'a> APTSyncer<'a> {
pub fn from<I>(mut iterator: I) -> APTSyncer<'a> where I: Iterator<Item=f32> + 'a {
let mut state = Vec::new();
let mut state = [0.0; SYNC_LENGHT];
let mut max_level = 0.0;
for _ in 0..SYNC_LENGHT {
for i in 0..SYNC_LENGHT {
match iterator.next() {
Some(x) => {
state.push(x);
state[i] = x;
max_level = f32::max(x, max_level);
},
None => panic!("Could not retrieve enough samples to prime syncer")
@ -58,17 +58,21 @@ impl<'a> APTSyncer<'a> {
}
}
fn is_marker(&mut self, marker : [bool; 40]) -> bool {
let mut score = 0;
fn is_marker(&mut self) -> (bool, bool) {
let mut is_a = true;
let mut is_b = true;
for i in 0..SYNC_LENGHT {
let sync_pos = (self.pos + i) % SYNC_LENGHT;
let sample = self.state[sync_pos] / self.max_level;
if (sample > 0.5 && marker[i]) || (sample <= 0.5 && !marker[i]) {
score += 1;
is_a = is_a && ((sample > 0.5 && SYNCA_SEQ[i]) || (sample <= 0.5 && !SYNCA_SEQ[i]));
is_b = is_b && ((sample > 0.5 && SYNCB_SEQ[i]) || (sample <= 0.5 && !SYNCB_SEQ[i]));
if !is_a && !is_b {
break;
}
}
return score == 40;
return (is_a, is_b);
}
}
@ -77,8 +81,7 @@ impl<'a> Iterator for APTSyncer<'a> {
fn next(&mut self) -> Option<Self::Item> {
let is_a = self.is_marker(SYNCA_SEQ);
let is_b = self.is_marker(SYNCB_SEQ);
let (is_a, is_b) = self.is_marker();
let sample = self.state[self.pos];
match self.iterator.next() {
@ -88,7 +91,7 @@ impl<'a> Iterator for APTSyncer<'a> {
},
None => self.nones_read += 1
};
if self.nones_read >= SYNC_LENGHT {
return None;
}

View File

@ -1,12 +1,12 @@
pub struct FIRFilter<'a> {
coeffs: Vec<f32>,
coeffs: &'a [f32],
state: Vec<f32>,
pos: usize,
iterator: Box<Iterator<Item=f32> + 'a>
}
impl<'a> FIRFilter<'a> {
pub fn from<I>(iterator: I, coeffs: Vec<f32>) -> FIRFilter<'a> where I: Iterator<Item=f32> + 'a {
pub fn from<I>(iterator: I, coeffs: &'a [f32]) -> FIRFilter<'a> where I: Iterator<Item=f32> + 'a {
let mut state = Vec::new();
for _ in 0..coeffs.len() {
state.push(0.0);

View File

@ -2,10 +2,8 @@ extern crate hound;
extern crate image;
mod utils;
mod sinegen;
mod firfilter;
mod resamplers;
mod mixer;
mod amdemod;
mod aptsyncer;
@ -21,35 +19,7 @@ use aptsyncer::{APTSyncer, SyncedSample};
const LINES_PER_SECOND: u32 = 2;
const PIXELS_PER_LINE: u32 = 2080;
fn main() {
let mut reader = match hound::WavReader::open("noaa19_20160814_mono.wav") {
Err(e) => panic!("Could not open inputfile: {}", e),
Ok(r) => r
};
if reader.spec().channels != 1 {
panic!("Expected a mono file");
}
let sample_rate = reader.spec().sample_rate;
println!("Samplerate: {}", sample_rate);
if sample_rate != 48000 {
panic!("Expected a 48kHz sample rate");
}
println!("{} Samples", reader.len());
let seconds = (reader.len() as f32) / (sample_rate as f32);
let lines = (seconds.ceil() as u32) * LINES_PER_SECOND;
println!("{} or {} lines", seconds, lines);
let mut img = image::ImageBuffer::new(PIXELS_PER_LINE, lines);
let samples = float_sample_iterator(&mut reader);
let coeffs = vec![ -7.383784e-03,
const LOWPASS_COEFFS : [f32; 63] = [ -7.383784e-03,
-3.183046e-03,
2.255039e-03,
7.461166e-03,
@ -113,6 +83,32 @@ fn main() {
-3.183046e-03,
-7.383784e-03];
fn main() {
let mut reader = match hound::WavReader::open("noaa19_20160814_mono.wav") {
Err(e) => panic!("Could not open inputfile: {}", e),
Ok(r) => r
};
if reader.spec().channels != 1 {
panic!("Expected a mono file");
}
let sample_rate = reader.spec().sample_rate;
println!("Samplerate: {}", sample_rate);
if sample_rate != 48000 {
panic!("Expected a 48kHz sample rate");
}
let sample_count = reader.len();
let seconds = (sample_count as f32) / (sample_rate as f32);
let lines = (seconds.ceil() as u32) * LINES_PER_SECOND;
println!("File contains {} seconds or {} lines", seconds, lines);
let mut img = image::ImageBuffer::new(PIXELS_PER_LINE, lines);
let coeffs = &LOWPASS_COEFFS;
let samples = float_sample_iterator(&mut reader);
let demod = SquaringAMDemodulator::from(samples);
let filter = FIRFilter::from(demod, coeffs);

View File

@ -1,32 +0,0 @@
pub struct Mixer<'a> {
iterator1: Box<Iterator<Item=f32> + 'a>,
iterator2: Box<Iterator<Item=f32> + 'a>
}
impl<'a> Mixer<'a> {
pub fn from<I,L>(iterator1: I, iterator2: L) -> Mixer<'a>
where I: Iterator<Item=f32> + 'a, L: Iterator<Item=f32> + 'a {
Mixer {
iterator1: Box::new(iterator1),
iterator2: Box::new(iterator2)
}
}
}
impl<'a> Iterator for Mixer<'a> {
type Item = f32;
fn next(&mut self) -> Option<Self::Item> {
let val1 = match self.iterator1.next() {
Some(x) => x,
None => return None
};
let val2 = match self.iterator2.next() {
Some(x) => x,
None => return None
};
return Some(val1 * val2);
}
}

View File

@ -1,30 +0,0 @@
use std;
pub struct SineGenerator {
freq: f32,
amplitude: f32,
sample_freq: f32,
phase: f32
}
impl SineGenerator {
pub fn new(freq: f32, amplitude: f32, sample_freq: f32) -> SineGenerator {
SineGenerator {
freq: freq,
amplitude: amplitude,
sample_freq: sample_freq,
phase: 0.0
}
}
}
impl Iterator for SineGenerator {
type Item = f32;
fn next(&mut self) -> Option<Self::Item> {
let result = self.amplitude * self.phase.sin();
self.phase += 2.0 * std::f32::consts::PI * self.freq / self.sample_freq;
Some(result)
}
}