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
mod range;
pub use self::range::Range;
mod window;
pub use self::window::Window;
mod rectangular;
pub use self::rectangular::Rectangular;
mod triangular;
pub use self::triangular::Triangular;
mod bartlett;
pub use self::bartlett::Bartlett;
mod parzen;
pub use self::parzen::Parzen;
mod hamming;
pub use self::hamming::Hamming;
mod hann;
pub use self::hann::Hann;
mod welch;
pub use self::welch::Welch;
mod blackman;
pub use self::blackman::Blackman;
mod nuttall;
pub use self::nuttall::Nuttall;
use num::Zero;
use {Precision, Sample, SampleMut};
use strided::{Strided, MutStrided};
pub trait Function {
fn compute(n: Precision, N: Precision) -> Precision;
}
#[inline(always)]
pub fn compute<F, S>(index: usize, width: usize) -> S
where F: Function,
S: SampleMut
{
let mut result = S::zero();
result.set_normalized(F::compute(index as Precision, width as Precision));
result
}
#[inline(always)]
pub fn apply<F, SO, SI, I, R>(range: R, input: I) -> Vec<SO>
where F: Function,
SO: SampleMut,
SI: Sample,
I: Strided<Elem=SI>,
R: Range
{
let input = input.as_stride();
let mut output = vec![SO::zero(); input.len()];
let length = input.len();
debug_assert!(range.is_valid(length));
apply_in::<F, SO, SI, _, _, R>(range, input, &mut *output);
output
}
pub fn apply_in<F, SO, SI, I, O, R>(range: R, input: I, mut output: O)
where F: Function,
SO: SampleMut,
SI: Sample,
I: Strided<Elem=SI>,
O: MutStrided<Elem=SO>,
R: Range
{
let input = input.as_stride();
let mut output = output.as_stride_mut();
let length = input.len();
debug_assert_eq!(input.len(), output.len());
debug_assert!(range.is_valid(length));
for (index, (input, output)) in input.iter().zip(output.iter_mut()).enumerate() {
if index >= range.start().unwrap_or(0) as usize &&
index <= range.end().unwrap_or(length as u32) as usize
{
output.set_normalized(input.normalize()
* F::compute(index as Precision, range.width(length) as Precision));
}
}
}
pub fn apply_on<F, S, IO, R>(range: R, mut data: IO)
where F: Function,
S: SampleMut,
IO: MutStrided<Elem=S>,
R: Range
{
let mut data = data.as_stride_mut();
let length = data.len();
debug_assert!(range.is_valid(length));
for (index, datum) in data.iter_mut().enumerate() {
if index >= range.start().unwrap_or(0) as usize &&
index <= range.end().unwrap_or(length as u32) as usize
{
let value = datum.normalize();
datum.set_normalized(value
* F::compute(index as Precision, range.width(length) as Precision));
}
}
}
pub fn generate<F, S, R>(range: R, size: usize) -> Window<S>
where F: Function,
S: SampleMut,
R: Range
{
let mut output = Window::new(&range, size);
debug_assert!(range.is_valid(size));
for (index, output) in output.iter_mut().enumerate() {
if index >= range.start().unwrap_or(0) as usize &&
index <= range.end().unwrap_or(size as u32) as usize
{
SampleMut::set_normalized(output,
F::compute(index as Precision, range.width(size) as Precision));
}
}
output
}