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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
use crate::window_size::WindowSize;
use std::{collections::VecDeque, f32::consts::PI};
pub trait WindowFunction: Send {
/// Apply the window in-place
fn apply(&mut self, data: &mut VecDeque<f32>);
/// Reverse the window in-place
fn reverse(&mut self, data: &mut [f32]);
/// Yields the amount of samples still required to apply the window
fn needed(&self) -> usize;
/// Updates the window size
fn window_size(&mut self, window_size: WindowSize);
}
pub struct RectangularWindow {
window_size: usize,
}
impl RectangularWindow {
pub fn new(window_size: &WindowSize) -> Self {
Self {
window_size: window_size.inner(),
}
}
}
impl WindowFunction for RectangularWindow {
fn apply(&mut self, _data: &mut VecDeque<f32>) {}
fn reverse(&mut self, _data: &mut [f32]) {}
fn needed(&self) -> usize {
self.window_size
}
fn window_size(&mut self, _window_size: WindowSize) {}
}
pub struct HannWindow {
window_size: usize,
function: Vec<f32>,
normalize: Vec<f32>,
previous: Vec<f32>,
overlap_add: Vec<f32>,
}
impl HannWindow {
pub fn new(window_size: &WindowSize) -> Self {
let window_size = window_size.inner();
let half_window_size = window_size / 2;
let window_size_f32 = window_size as f32;
// 50% overlap
let overlap_add = vec![0.0; window_size];
// with capacity is important for the first needed samples
let previous = Vec::with_capacity(window_size);
let function = (0..window_size)
.map(|i| {
let i = i as f32;
0.5 * (1.0 - f32::cos((2.0 * PI * i) / (window_size_f32 - 1.0)))
})
.collect::<Vec<f32>>();
let normalize = (0..half_window_size)
.map(|i| {
(function[i] * function[i])
+ (function[i + half_window_size] * function[i + half_window_size])
})
.collect::<Vec<f32>>();
Self {
window_size,
function,
normalize,
previous,
overlap_add,
}
}
}
impl WindowFunction for HannWindow {
fn apply(&mut self, data: &mut VecDeque<f32>) {
let half_window_size = self.window_size / 2;
// the full window size is needed to window over the samples the first time
if self.previous.is_empty() {
// store the latter half for when only half the size is needed
for sample in data.iter().skip(half_window_size).copied() {
self.previous.push(sample);
}
// apply the function
for (i, sample) in data.iter_mut().enumerate() {
*sample *= self.function[i];
}
return;
}
// In a real time "sliding window" it's necessary to add in the previous samples
// to get the full resolution
for sample in self.previous.iter().rev().copied() {
data.push_front(sample);
}
self.previous.clear();
// keep this iteration's samples for the next window
for sample in data.iter().skip(half_window_size).copied() {
self.previous.push(sample);
}
// apply the function
for (i, sample) in data.iter_mut().enumerate() {
*sample *= self.function[i];
}
}
fn reverse(&mut self, data: &mut [f32]) {
let half_window_size = self.window_size / 2;
for i in 0..self.window_size {
self.overlap_add[i] += data[i] * self.function[i];
}
for i in 0..half_window_size {
if self.normalize[i] > 1e-6 {
data[i] = self.overlap_add[i] / self.normalize[i];
} else {
data[i] = 0.0;
}
}
self.overlap_add.rotate_left(half_window_size);
for sample in &mut self.overlap_add[half_window_size..] {
*sample = 0.0;
}
}
fn needed(&self) -> usize {
if self.previous.is_empty() {
self.window_size
} else {
self.window_size / 2
}
}
fn window_size(&mut self, window_size: WindowSize) {
let window_size = window_size.inner();
let half_window_size = window_size / 2;
let window_size_f32 = window_size as f32;
self.overlap_add.resize(window_size, 0.0);
self.previous.clear();
self.previous.reserve_exact(window_size);
self.function = (0..window_size)
.map(|i| {
let i = i as f32;
0.5 * (1.0 - f32::cos((2.0 * PI * i) / (window_size_f32 - 1.0)))
})
.collect::<Vec<f32>>();
self.normalize = (0..half_window_size)
.map(|i| {
(self.function[i] * self.function[i])
+ (self.function[i + half_window_size] * self.function[i + half_window_size])
})
.collect::<Vec<f32>>();
}
}
pub struct BlackmanHarrisWindow {
window_size: usize,
function: Vec<f32>,
normalize: Vec<f32>,
previous: Vec<f32>,
overlap_add: Vec<f32>,
}
impl BlackmanHarrisWindow {
const A_0: f32 = 0.35875;
const A_1: f32 = 0.48829;
const A_2: f32 = 0.14128;
const A_3: f32 = 0.01168;
pub fn new(window_size: &WindowSize) -> Self {
let window_size = window_size.inner();
let quarter_window_size = window_size / 4;
let three_quarters_window_size = 3 * quarter_window_size;
let window_size_f32 = window_size as f32;
let overlap_add = vec![0.0; window_size];
let previous = Vec::with_capacity(three_quarters_window_size);
let function = (0..window_size)
.map(|i| {
let i = i as f32;
let two = f32::cos((2.0 * PI * i) / (window_size_f32 - 1.0));
let four = f32::cos((4.0 * PI * i) / (window_size_f32 - 1.0));
let six = f32::cos((6.0 * PI * i) / (window_size_f32 - 1.0));
Self::A_0 - (Self::A_1 * two) + (Self::A_2 * four) - (Self::A_3 * six)
})
.collect::<Vec<f32>>();
#[rustfmt::skip]
let normalize = (0..quarter_window_size)
.map(|i| {
(function[i] * function[i])
+ (function[i + quarter_window_size] * function[i + quarter_window_size])
+ (function[i + 2 * quarter_window_size] * function[i + 2 * quarter_window_size])
+ (function[i + 3 * quarter_window_size] * function[i + 3 * quarter_window_size])
})
.collect::<Vec<f32>>();
Self {
window_size,
function,
normalize,
previous,
overlap_add,
}
}
}
impl WindowFunction for BlackmanHarrisWindow {
fn apply(&mut self, data: &mut VecDeque<f32>) {
let quarter_window_size = self.window_size / 4;
if self.previous.is_empty() {
for sample in data.iter().skip(quarter_window_size).copied() {
self.previous.push(sample);
}
for (i, sample) in data.iter_mut().enumerate() {
*sample *= self.function[i];
}
return;
}
for sample in self.previous.iter().rev().copied() {
data.push_front(sample);
}
self.previous.clear();
for sample in data.iter().skip(quarter_window_size).copied() {
self.previous.push(sample);
}
for (i, sample) in data.iter_mut().enumerate() {
*sample *= self.function[i];
}
}
fn reverse(&mut self, data: &mut [f32]) {
let quarter_window_size = self.window_size / 4;
let three_quarters_window_size = 3 * quarter_window_size;
for i in 0..self.window_size {
self.overlap_add[i] += data[i] * self.function[i];
}
for i in 0..quarter_window_size {
if self.normalize[i] > 1e-6 {
data[i] = self.overlap_add[i] / self.normalize[i];
} else {
data[i] = 0.0;
}
}
self.overlap_add.rotate_left(quarter_window_size);
for sample in &mut self.overlap_add[three_quarters_window_size..] {
*sample = 0.0;
}
}
fn needed(&self) -> usize {
if self.previous.is_empty() {
self.window_size
} else {
self.window_size / 4
}
}
fn window_size(&mut self, window_size: WindowSize) {
let window_size = window_size.inner();
let quarter_window_size = window_size / 4;
let three_quarters_window_size = 3 * quarter_window_size;
let window_size_f32 = window_size as f32;
self.overlap_add.resize(window_size, 0.0);
self.previous.clear();
self.previous.reserve_exact(window_size);
self.function = (0..window_size)
.map(|i| {
let i = i as f32;
let two = f32::cos((2.0 * PI * i) / (window_size_f32 - 1.0));
let four = f32::cos((4.0 * PI * i) / (window_size_f32 - 1.0));
let six = f32::cos((6.0 * PI * i) / (window_size_f32 - 1.0));
Self::A_0 - (Self::A_1 * two) + (Self::A_2 * four) - (Self::A_3 * six)
})
.collect::<Vec<f32>>();
self.normalize = (0..quarter_window_size)
.map(|i| {
(self.function[i] * self.function[i])
+ (self.function[i + quarter_window_size]
* self.function[i + quarter_window_size])
+ (self.function[i + 2 * quarter_window_size]
* self.function[i + 2 * quarter_window_size])
+ (self.function[i + 3 * quarter_window_size]
* self.function[i + 3 * quarter_window_size])
})
.collect::<Vec<f32>>();
}
}
|