forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

common.rs39 lines, 1.1 KiBHistoryRawDownload
1//! Helpers shared by the merged `it` test modules.
2
3use std::path::{Path, PathBuf};
4
5pub fn fixture(name: &str) -> PathBuf {
6 Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data").join(name)
7}
8
9fn mean(a: &[f32]) -> f64 {
10 a.iter().map(|v| f64::from(*v)).sum::<f64>() / a.len().max(1) as f64
11}
12
13/// Zero-mean normalised correlation of two equal-length slices.
14pub fn correlation(a: &[f32], b: &[f32]) -> f64 {
15 let n = a.len().min(b.len());
16 if n < 1024 {
17 return 0.0;
18 }
19 let (a, b) = (&a[..n], &b[..n]);
20 let (ma, mb) = (mean(a), mean(b));
21 let (mut num, mut da, mut db) = (0.0f64, 0.0f64, 0.0f64);
22 for (x, y) in a.iter().zip(b) {
23 let x = f64::from(*x) - ma;
24 let y = f64::from(*y) - mb;
25 num += x * y;
26 da += x * x;
27 db += y * y;
28 }
29 let den = (da * db).sqrt();
30 if den == 0.0 { 0.0 } else { num / den }
31}
32
33pub fn deinterleave(samples: &[f32], channels: usize) -> Vec<Vec<f32>> {
34 let mut planes = vec![Vec::with_capacity(samples.len() / channels.max(1)); channels];
35 for (i, &v) in samples.iter().enumerate() {
36 planes[i % channels].push(v);
37 }
38 planes
39}