-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.rs
107 lines (89 loc) · 2.94 KB
/
lib.rs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#[macro_use]
extern crate ndarray;
use ndarray::prelude::*;
pub fn convolve(data: ArrayView1<f64>, window: ArrayView1<f64>) -> Array1<f64> {
let padded = stack![
Axis(0),
Array1::zeros(window.len() / 2),
data,
Array1::zeros(window.len() / 2)
];
let mut w = window.view();
w.invert_axis(Axis(0));
padded
.windows(w.len())
.into_iter()
.map(|x| (&x * &w).sum())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use float_cmp::*;
#[test]
fn convolve_odd_odd() {
let data = array![1., 2., 3.];
let window = array![0., 1., 0.5];
let expected = array![1., 2.5, 4.];
for (exp, res) in expected.iter().zip(&convolve(data.view(), window.view())) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}
#[test]
fn convolve_odd_odd2() {
let data = array![1., 2., 3., 4., 5.];
let window = array![2., 1., 0., 1., 0.5];
let result = convolve(data.view(), window.view());
let expected = array![8., 12., 16.5, 9., 5.5];
for (exp, res) in expected.iter().zip(&result) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}
#[test]
fn convolve_even_odd() {
let data = array![1., 2., 3., 4.];
let window = array![0., 1., 0.5];
let expected = array![1., 2.5, 4., 5.5];
for (exp, res) in expected.iter().zip(&convolve(data.view(), window.view())) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}
#[test]
fn convolve_even_even() {
let data = array![1., 2., 3., 4.];
let window = array![1., 0.5];
let expected = array![1., 2.5, 4., 5.5];
for (exp, res) in expected.iter().zip(&convolve(data.view(), window.view())) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}
#[test]
fn convolve_even_even2() {
let data = array![1., 2., 3., 4.];
let window = array![1., 0., 1., 0.5];
let result = convolve(data.view(), window.view());
let expected = array![2., 4., 6.5, 4.];
for (exp, res) in expected.iter().zip(&result) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}
#[test]
fn convolve_odd_even() {
let data = array![1., 2., 3., 4., 5.];
let window = array![1., 0.5];
let expected = array![1., 2.5, 4., 5.5, 7.];
for (exp, res) in expected.iter().zip(&convolve(data.view(), window.view())) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}
#[test]
fn convolve_bigger_window() {
let data = array![1., 2., 3.];
let window = array![1., 0., 1., 0.5];
let result = convolve(data.view(), window.view());
let expected = array![2., 4., 2.5, 4.];
for (exp, res) in expected.iter().zip(&result) {
assert!(approx_eq!(f64, *exp, *res, ulps = 2));
}
}
}