-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathwindow.rs
More file actions
73 lines (65 loc) · 1.89 KB
/
window.rs
File metadata and controls
73 lines (65 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#![cfg(feature = "window")]
use dasp_frame::Frame;
use dasp_signal::window::{self, Windower};
#[cfg(feature = "window-hann")]
#[test]
fn test_window_at_phase() {
let window = window::hann::<f64>(9);
let expected = [
0.0, 0.1464, 0.5000, 0.8536, 1., 0.8536, 0.5000, 0.1464, 0., 0.1464,
];
for (r, e) in window.zip(&expected) {
println!("Expected: {}\t\tFound: {}", e, r);
assert!((r - e).abs() < 0.001);
}
}
#[test]
fn test_windower() {
let data = [0.1f64, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4];
let expected = [
[0.1f64, 0.1],
[0.1, 0.2],
[0.2, 0.2],
[0.2, 0.3],
[0.3, 0.3],
[0.3, 0.4],
[0.4, 0.4],
];
let windower = Windower::rectangle(&data, 2, 1);
for (chunk, expected_chunk) in windower.zip(&expected) {
for (r, e) in chunk.zip(expected_chunk.iter()) {
for (r_chan, e_chan) in r.channels().zip(e.channels()) {
println!("Expected: {}\t\tFound: {}", e_chan, r_chan);
assert!((r_chan - e_chan).abs() < 0.001);
}
}
}
}
#[cfg(feature = "window-hann")]
#[test]
fn test_window_size() {
let v = [1f32; 16];
let windows: Vec<Vec<_>> = Windower::hann(&v, 8, 4)
.map(|i| i.take(8).collect::<Vec<f32>>())
.take(3)
.collect();
assert_eq!(windows.len(), 3);
}
#[cfg(feature = "window-hann")]
#[test]
fn test_window_iterator() {
let v = [1f32; 16];
let windower = Windower::hann(&v, 8, 4);
let mut window_count = 0;
let mut sample_count = 0;
for window in windower {
window_count += 1;
for sample in window {
sample_count += 1
}
}
// || - first window, {} - second window, [] - third window, s - samples
// | s, s, s, s, { s, s, s, s | [ s, s, s, s, } s, s, s, s, ]
assert_eq!(window_count, 3);
assert_eq!(sample_count, 3 * 8);
}