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
333
334
335
336
337
338
339
340
341
342
343
use *;
use *;
use *;
use RealField;
use Debug;
use *;
use crate*;
use Num;
use SubsetOf;
use ;
use FromIterator;
/// Two-dimensional Fourier transform
/*impl<N> Forward<Image<N>> for Fourier2D<N>
where
N : Scalar + From<f32> + Num,
Complex<N> : Scalar
{
type Output = Image<Complex<N>>;
fn forward_mut(&self, src : &Image<N>, dst : &mut Self::Output) {
self.plan.apply_forward(src.as_ref(), dst.as_mut())
.map_err(|e| panic!("{}", e) );
}
fn forward(&self, src : &Image<N>) -> Self::Output {
let zero = N::from(0.0 as f32);
let (nrows, ncols) = self.plan.shape();
let mut dst = Image::new_constant(nrows, ncols, Complex::new(zero.clone(), zero));
self.forward_mut(src, &mut dst);
dst
}
}
impl<N> Backward<Image<Complex<N>>> for Fourier2D<N>
where
N : Scalar + From<f32> + Num,
Complex<N> : Scalar
{
type Output = Image<N>;
fn backward_mut(&self, src : &Image<Complex<N>>, dst : &mut Self::Output) {
self.plan.apply_backward(src.as_ref(), dst.as_mut())
.map_err(|e| panic!("{}", e) );
}
fn backward(&self, src : &Image<Complex<N>>) -> Self::Output {
let (nrows, ncols) = self.plan.shape();
let mut dst = Image::new_constant(nrows, ncols, N::from(0.0 as f32));
self.backward_mut(src, &mut dst);
dst
}
}*/
/// Output of a two-dimensional Fourier transform.
/// Slice over an image spectrum.
/*impl<N> From<Vec<N>> for ImagePyramid<N>
where
N : Scalar
{
fn from(s : Vec<N>) -> Self {
Self{ buf : DVector::from_vec(s) }
}
}*/
/*/// If the FFT is built with a window, perform the short-time fourier transform.
/// The transform yields an interator over separate windows. If the FFT is built
/// without a window, the iterator yield a single element. The second element is
/// an overlap, informing if the window should be over contiguous or overlapping
/// signal segments (which is necessary if you are studying transitions that are
/// too large relative to the window and might be missed if they are over window
/// transitions). Each transition point is a categorical (or bernoulli for binary
/// transitions) with a dirichlet (beta) prior.
/// Self will decompose the signal at windows of size len.
/// Signal is shift-invariant within windows at the cost
/// of reduced spatial resolution. Larger window sizes
/// increase spatial resolution at each window at the cost
/// of not being able to examine short-scale temporal
/// changes. After setting the window, take FFT only of the
/// updated window, leaving past data at their old state.
pub enum Window {
/// The box window simply tiles the temporal/spatial
/// domain and perform the Fourier transform separately
/// for each segment.
Box(usize, Overlap),
/// The Hanning window tiles the temporal/spatial
/// domain and pre-multiplies the signal with the
/// a box-like window, but that is attenuated with
/// a cosine decay function at the borders to guarantee
/// neighboring segments are continuous on the circular domain.
Hanning(usize, Overlap),
/// A gabor window pre-multiply the signal with a
/// gaussian before applying the FFT. The effect is
/// the same of taking the dot-product of the signal
/// with separate, localized gabor functions (complex exponentials
/// which don't quite decay to zero but get very close to).
Gabor(usize, Overlap)
}
struct WindowContent<N, C>
where
N : Scalar + Into<f64>,
C : Dim
{
win : Window,
buf : Option<DMatrix<N, Dynamic, C, VecStorage<N, Dynamic, C>>>
}
impl<N> WindowContent<N, U1>
where
N : Scalar + Into<f64> + From<f32>
{
pub fn new(win : Window) -> Self {
match &win {
Window::Box(n, _) => {
Self{ buf : None, win }
},
Window::Hanning(n, _) => {
Self{ buf : Some(hann(n)), win }
},
Window::Gabor(n, _) => {
unimplemented!()
}
}
}
}
impl<N> WindowContent<N, Dynamic>
where
N : Scalar + Into<f64> + From<f32>
{
pub fn new(win : Window) -> Self {
match &win {
Window::Box(n, _) => {
Self{ buf : None, win }
},
Window::Hanning(n, _) => {
Self{ buf : hann2(n), win }
},
Window::Gabor(n, _) => {
unimplemented!()
}
}
}
}
impl<N, C> WindowContent<N, C>
where
N : Scalar + Into<f64> + From<f32>
C : Dim
{
pub fn apply(
signal : &Matrix<f64, Dynamic, C, S>
) -> Matrix<f64, Dynamic, C, VecStorage<f64, Dynamic, C>>
where
S : Storage<f64, Dynamic, C>,
Matrix<f64, Dynamic, C, S> : WindowIterate<N, S>
{
match self.overlap {
Self::Box(n, overlap) => panic!("Box window does not require re-allocation"),
Self::Hanning(n, overlap) | Self::Gabor(n, overlap) => {
let step_sz = match overlap {
Overlap::None => 1,
Overlap::Half => n / 2,
Overlap::Quarter => n / 4
};
// TODO extend this matrix rows/cols if signal has overlap
let mut dst = signal.clone();
for (win, i) in signal.windows().step_by(step_sz).enumerate() {
win.component_mul_to(&self.buf.unwrap(), dst.slice_mut(pos));
}
dst
}
}
}
}*/