Skip to content

Commit 5fde448

Browse files
committed
Using detector_freq() for Glossary setup
1 parent 6002632 commit 5fde448

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

src/main.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ use sound::Detector;
2222

2323
use std::f32::consts::PI;
2424

25-
fn detector_freq(index: usize) -> f32 {
26-
let ideal_freq = 15. + 5. * index as f32 + ((index as f32 - 5.) / 16.).exp();
27-
let fft_freq = (ideal_freq / sound::BASE_FREQUENCY).trunc() * sound::BASE_FREQUENCY;
28-
29-
fft_freq
30-
}
31-
3225
fn main() {
3326
let options = App::new("Semantic sound processor")
3427
.version("0.1")
@@ -52,9 +45,9 @@ fn main() {
5245
let mut detectors = Vec::new();
5346

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

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

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

74-
let sum: usize = keys
67+
let entropy: usize = keys
7568
.iter()
7669
.map(|opt|
7770
match opt {
@@ -81,7 +74,7 @@ fn main() {
8174
)
8275
.sum();
8376

84-
println!("total key length {}", sum);
77+
println!("key entropy {}", entropy);
8578

8679
// for (key, value) in dictionary.iter() {
8780
// //println!("{:?} -> {:?}\n", key, value);

src/memory.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,10 @@ impl FragmentKey {
134134
}
135135

136136
pub fn bits(&self) -> &BitVec {
137-
self.0.bits()
137+
&self.0.bits()
138138
}
139139
}
140140

141-
/// Amount of spectrum slices per single fragment
142-
pub const SPECTRA_PER_FRAGMENT: usize = 2;
143-
144141
/// `Fragment` represents several time slices
145142
/// of the spectrum within predefined frequency range.
146143
#[derive(Clone, Debug)]
@@ -150,13 +147,15 @@ pub struct Fragment {
150147

151148
/// Weight of fragment as prototype.
152149
/// Used during merge process.
153-
merge_weight: i32,
150+
merge_weight: usize,
151+
152+
// TODO use_count: Cell<usize>,
154153
}
155154

156155
impl Fragment {
157156
pub fn new() -> Fragment {
158157
Fragment {
159-
spectra: Vec::with_capacity(SPECTRA_PER_FRAGMENT),
158+
spectra: Vec::new(),
160159
merge_weight: 1,
161160
}
162161
}

src/sound.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ pub struct Detector {
5050
pub phase: f32, // phase
5151
}
5252

53+
pub fn detector_freq(index: usize) -> f32 {
54+
let ideal_freq = 15. + 5. * index as f32 + ((index as f32 - 5.) / 16.).exp();
55+
let fft_freq = (ideal_freq / BASE_FREQUENCY).trunc() * BASE_FREQUENCY;
56+
57+
fft_freq
58+
}
59+
5360
impl Detector {
5461
pub fn new(freq: f32, band: f32, amp: f32, phase: f32) -> Detector {
5562
Detector {
@@ -204,14 +211,19 @@ const SLICE_OFFSET: usize = (NUM_POINTS / 2) / SLICES_PER_FRAME;
204211
const SLICES_PER_FRAGMENT: usize = SLICES_PER_FRAME / FRAGMENTS_PER_FRAME;
205212
const FRAGMENT_WINDOW: (f32, f32) = (350., 500.);
206213

207-
const SIMILARITY: usize = 60;
214+
const SIMILARITY: usize = 40;
208215

209216
type KeyVec = Vec<Option<FragmentKey>>;
210217

211218
pub fn build_glossary<'d>(filename: &str, detectors: &'d [Detector]) -> (Glossary<'d>, KeyVec) {
212219
// (100, 199), (200, 299), ... (1900, 1999)
213-
let regions: Vec<_> = (1 .. 33).into_iter().map(|i: u32| (i as f32 * 100., i as f32 * 100. + 99.)).collect();
214-
let mut dictionaries: Vec<_> = regions.iter().map(|r| Dictionary::new(detectors, r.0, r.1)).collect();
220+
//let regions: Vec<_> = (1 .. 33).into_iter().map(|i: u32| (i as f32 * 100., i as f32 * 100. + 99.)).collect();
221+
//let mut dictionaries: Vec<_> = regions.iter().map(|r| Dictionary::new(detectors, r.0, r.1)).collect();
222+
223+
let mut dictionaries: Vec<_> = (1 .. 14).into_iter()
224+
.map(|i| (detector_freq(i*10), detector_freq((i+1)*10)) )
225+
.map(|(low, high)| Dictionary::new(detectors, low, high))
226+
.collect();
215227

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

307319
let plan = dft::Plan::new(dft::Operation::Backward, NUM_POINTS);
308-
let mut max_sample = 0.;
320+
let mut max_sample = 0.6;
309321

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

0 commit comments

Comments
 (0)