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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use RealNumber;
use std::os::raw::c_void;
use std::mem;
pub trait WindowFunction<T>: Sync
where T: RealNumber
{
fn is_symmetric(&self) -> bool;
fn window(&self, n: usize, length: usize) -> T;
}
pub struct TriangularWindow;
impl<T> WindowFunction<T> for TriangularWindow
where T: RealNumber
{
fn is_symmetric(&self) -> bool {
true
}
fn window(&self, n: usize, length: usize) -> T {
let one = T::one();
let two = T::from(2.0).unwrap();
let n = T::from(n).unwrap();
let length = T::from(length).unwrap();
one - ((n - (length - one) / two) / (length / two)).abs()
}
}
pub struct HammingWindow<T>
where T: RealNumber
{
alpha: T,
beta: T,
}
impl<T> HammingWindow<T>
where T: RealNumber
{
pub fn new(alpha: T) -> Self {
HammingWindow {
alpha: alpha,
beta: (T::one() - alpha),
}
}
pub fn default() -> Self {
Self::new(T::from(0.54).unwrap())
}
}
impl<T> WindowFunction<T> for HammingWindow<T>
where T: RealNumber
{
fn is_symmetric(&self) -> bool {
true
}
fn window(&self, n: usize, length: usize) -> T {
let one = T::one();
let two = T::from(2.0).unwrap();
let pi = two * one.asin();
let n = T::from(n).unwrap();
let length = T::from(length).unwrap();
self.alpha - self.beta * (two * pi * n / (length - one)).cos()
}
}
pub struct ForeignWindowFunction<T>
where T: RealNumber
{
pub window_function: extern "C" fn(*const c_void, usize, usize) -> T,
pub window_data: usize,
pub is_symmetric: bool,
}
impl<T> ForeignWindowFunction<T>
where T: RealNumber
{
pub unsafe fn new(window: extern "C" fn(*const c_void, usize, usize) -> T,
window_data: *const c_void,
is_symmetric: bool)
-> Self {
ForeignWindowFunction {
window_function: window,
window_data: mem::transmute(window_data),
is_symmetric: is_symmetric,
}
}
}
impl<T> WindowFunction<T> for ForeignWindowFunction<T>
where T: RealNumber
{
fn is_symmetric(&self) -> bool {
self.is_symmetric
}
fn window(&self, idx: usize, points: usize) -> T {
let fun = self.window_function;
fun(self.window_data as *const c_void, idx, points)
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::RealNumber;
use std::fmt::Debug;
fn window_test<T, W>(window: W, expected: &[T])
where T: RealNumber + Debug,
W: WindowFunction<T>
{
let mut result = vec![T::zero(); expected.len()];
for i in 0..result.len() {
result[i] = window.window(i, result.len());
}
for i in 0..result.len() {
if (result[i] - expected[i]).abs() > T::from(1e-4).unwrap() {
panic!("assertion failed: {:?} != {:?}", result, expected);
}
}
}
#[test]
fn triangular_window32_test() {
let window = TriangularWindow;
let expected = [0.2, 0.6, 1.0, 0.6, 0.2];
window_test(window, &expected);
}
#[test]
fn hamming_window32_test() {
let hamming = HammingWindow::<f32>::default();
let expected = [0.08, 0.54, 1.0, 0.54, 0.08];
window_test(hamming, &expected);
}
}