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
use {FloatSample, Sample};
use core;
use core::marker::PhantomData;
use frame::Frame;
use signal::{self, Signal};
pub trait Type {
fn at_phase<S: Sample>(phase: S) -> S;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Hanning;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rectangle;
#[derive(Clone)]
pub struct Window<F, W>
where F: Frame,
W: Type,
{
pub phase: signal::Phase<signal::ConstHz>,
marker: PhantomData<(F, W)>,
}
#[derive(Clone)]
pub struct Windower<'a, F, W>
where F: 'a + Frame,
W: Type,
{
pub bin: usize,
pub hop: usize,
pub frames: &'a [F],
wttype: PhantomData<W>
}
impl Type for Hanning {
fn at_phase<S: Sample>(phase: S) -> S {
const PI_2: f64 = core::f64::consts::PI * 2.0;
let v = phase.to_float_sample().to_sample() * PI_2;
(0.5 * (1.0 - super::ops::f64::cos(v)))
.to_sample::<S::Float>()
.to_sample::<S>()
}
}
impl Type for Rectangle {
fn at_phase<S: Sample>(_phase: S) -> S {
<S::Float as FloatSample>::identity().to_sample::<S>()
}
}
impl<F, W> Window<F, W>
where F: Frame,
W: Type
{
pub fn new(len: usize) -> Self {
let step = signal::rate(len as f64 - 1.0).const_hz(1.0);
Window {
phase: signal::phase(step),
marker: PhantomData,
}
}
}
impl<'a, F, W> Windower<'a, F, W>
where F: 'a + Frame,
W: Type
{
pub fn new(frames: &'a [F], bin: usize, hop: usize) -> Self {
Windower {
bin: bin,
hop: hop,
frames: frames,
wttype: PhantomData
}
}
}
impl<'a, F> Windower<'a, F, Rectangle>
where F: 'a + Frame,
{
pub fn rectangle(frames: &'a [F], bin: usize, hop: usize) -> Self {
Windower::new(frames, bin, hop)
}
}
impl<'a, F> Windower<'a, F, Hanning>
where F: 'a + Frame,
{
pub fn hanning(frames: &'a [F], bin: usize, hop: usize) -> Self {
Windower::new(frames, bin, hop)
}
}
impl<F, W> Iterator for Window<F, W>
where F: Frame,
W: Type
{
type Item = F;
fn next(&mut self) -> Option<Self::Item> {
let v = W::at_phase(self.phase.next_phase());
let v_f: <F::Sample as Sample>::Float = v.to_sample();
Some(F::from_fn(|_| v_f.to_sample::<F::Sample>()))
}
}
impl<'a, F, W> Iterator for Windower<'a, F, W>
where F: 'a + Frame,
W: Type
{
type Item = signal::MulAmp<core::iter::Cloned<core::slice::Iter<'a, F>>, Window<F::Float, W>>;
fn next(&mut self) -> Option<Self::Item> {
let num_frames = self.frames.len();
if self.bin <= num_frames {
let frames = &self.frames[..self.bin];
let window = Window::new(self.bin);
self.frames = if self.hop < num_frames { &self.frames[self.hop..] } else { &[] };
Some(frames.iter().cloned().mul_amp(window))
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let num_frames = self.frames.len();
if self.bin < num_frames {
if self.hop == 0 {
return (core::usize::MAX, None);
}
let remaining_hop_frames = self.frames.len() - self.bin;
let remaining_iterations = remaining_hop_frames / self.hop;
(remaining_iterations, Some(remaining_iterations))
} else {
(0, Some(0))
}
}
}
pub fn hanning<F>(num_frames: usize) -> Window<F, Hanning>
where F: Frame,
{
Window::new(num_frames)
}
pub fn rectangle<F>(num_frames: usize) -> Window<F, Rectangle>
where F: Frame,
{
Window::new(num_frames)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_window_size() {
let v = [[1f32; 1]; 16];
let windows: Vec<Vec<[f32; 1]>> = Windower::hanning(&v[..], 8, 4).map(|i| i.collect::<Vec<[f32; 1]>>()).collect();
assert_eq!(windows.len(), 3);
}
}