Skip to content

Commit

Permalink
Using detector_freq() for Glossary setup
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7CFE committed Jan 25, 2017
1 parent 6002632 commit 5fde448
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
17 changes: 5 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ use sound::Detector;

use std::f32::consts::PI;

fn detector_freq(index: usize) -> f32 {
let ideal_freq = 15. + 5. * index as f32 + ((index as f32 - 5.) / 16.).exp();
let fft_freq = (ideal_freq / sound::BASE_FREQUENCY).trunc() * sound::BASE_FREQUENCY;

fft_freq
}

fn main() {
let options = App::new("Semantic sound processor")
.version("0.1")
Expand All @@ -52,9 +45,9 @@ fn main() {
let mut detectors = Vec::new();

// Populating detectors from 0Hz to ~1KHz with ~2683Hz
for i in 1 .. 129 {
let freq = detector_freq(i);
let band = 2. * (detector_freq(i+1) - freq);
for i in 1 .. 151 {
let freq = sound::detector_freq(i);
let band = 2. * (sound::detector_freq(i+1) - freq);

// for phase in vec![ -PI, -3.*PI/4., -PI/2., -PI/4., 0., PI/4., PI/2., 3.*PI/4., PI]
for phase in vec![ -PI, -PI/2., 0., PI/2., PI ]
Expand All @@ -71,7 +64,7 @@ fn main() {

let (glossary, keys) = sound::build_glossary(input_filename, &detectors);

let sum: usize = keys
let entropy: usize = keys
.iter()
.map(|opt|
match opt {
Expand All @@ -81,7 +74,7 @@ fn main() {
)
.sum();

println!("total key length {}", sum);
println!("key entropy {}", entropy);

// for (key, value) in dictionary.iter() {
// //println!("{:?} -> {:?}\n", key, value);
Expand Down
11 changes: 5 additions & 6 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,10 @@ impl FragmentKey {
}

pub fn bits(&self) -> &BitVec {
self.0.bits()
&self.0.bits()
}
}

/// Amount of spectrum slices per single fragment
pub const SPECTRA_PER_FRAGMENT: usize = 2;

/// `Fragment` represents several time slices
/// of the spectrum within predefined frequency range.
#[derive(Clone, Debug)]
Expand All @@ -150,13 +147,15 @@ pub struct Fragment {

/// Weight of fragment as prototype.
/// Used during merge process.
merge_weight: i32,
merge_weight: usize,

// TODO use_count: Cell<usize>,
}

impl Fragment {
pub fn new() -> Fragment {
Fragment {
spectra: Vec::with_capacity(SPECTRA_PER_FRAGMENT),
spectra: Vec::new(),
merge_weight: 1,
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ pub struct Detector {
pub phase: f32, // phase
}

pub fn detector_freq(index: usize) -> f32 {
let ideal_freq = 15. + 5. * index as f32 + ((index as f32 - 5.) / 16.).exp();
let fft_freq = (ideal_freq / BASE_FREQUENCY).trunc() * BASE_FREQUENCY;

fft_freq
}

impl Detector {
pub fn new(freq: f32, band: f32, amp: f32, phase: f32) -> Detector {
Detector {
Expand Down Expand Up @@ -204,14 +211,19 @@ const SLICE_OFFSET: usize = (NUM_POINTS / 2) / SLICES_PER_FRAME;
const SLICES_PER_FRAGMENT: usize = SLICES_PER_FRAME / FRAGMENTS_PER_FRAME;
const FRAGMENT_WINDOW: (f32, f32) = (350., 500.);

const SIMILARITY: usize = 60;
const SIMILARITY: usize = 40;

type KeyVec = Vec<Option<FragmentKey>>;

pub fn build_glossary<'d>(filename: &str, detectors: &'d [Detector]) -> (Glossary<'d>, KeyVec) {
// (100, 199), (200, 299), ... (1900, 1999)
let regions: Vec<_> = (1 .. 33).into_iter().map(|i: u32| (i as f32 * 100., i as f32 * 100. + 99.)).collect();
let mut dictionaries: Vec<_> = regions.iter().map(|r| Dictionary::new(detectors, r.0, r.1)).collect();
//let regions: Vec<_> = (1 .. 33).into_iter().map(|i: u32| (i as f32 * 100., i as f32 * 100. + 99.)).collect();
//let mut dictionaries: Vec<_> = regions.iter().map(|r| Dictionary::new(detectors, r.0, r.1)).collect();

let mut dictionaries: Vec<_> = (1 .. 14).into_iter()
.map(|i| (detector_freq(i*10), detector_freq((i+1)*10)) )
.map(|(low, high)| Dictionary::new(detectors, low, high))
.collect();

let plan = dft::Plan::new(dft::Operation::Forward, NUM_POINTS);
let mut reader = hound::WavReader::open(filename).unwrap();
Expand Down Expand Up @@ -305,7 +317,7 @@ pub fn reconstruct(filename: &str, glossary: &Glossary, keys: &KeyVec) {
let mut writer = hound::WavWriter::create(filename, wav_header).unwrap();

let plan = dft::Plan::new(dft::Operation::Backward, NUM_POINTS);
let mut max_sample = 0.;
let mut max_sample = 0.6;

let mut output = Vec::with_capacity(NUM_POINTS);
output.resize(NUM_POINTS, Cplx::default());
Expand Down

0 comments on commit 5fde448

Please sign in to comment.