-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbasic.py
More file actions
377 lines (296 loc) · 9.99 KB
/
basic.py
File metadata and controls
377 lines (296 loc) · 9.99 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from __future__ import division
import types
import numpy as np
import scipy as sp
import scikits.samplerate as samplerate
import dataprocessor
from externaldps import *
class Resample(dataprocessor.DataProcessor):
"""Resamples input using scikits.samplerate.
Attributes
----------
ratio : float
Resampling ratio. For best results len(frame)*ratio should be
an integer.
type : str
resample type (see scikits.samplerate.resample)
verbose : bool
See Also
--------
scikits.samplerate.resample
"""
def __init__(self, ratio=None, type='sinc_fastest', verbose=False):
self.ratio = ratio
self.type = type
self.verbose = verbose
def process_frame(self, frame):
if self.ratio is None:
return frame
else:
return samplerate.resample(frame, self.ratio, self.type,
self.verbose)
class Normalize(dataprocessor.DataProcessor):
"""Normalize each frame using a norm of the given order.
Attributes
----------
ord : see numpy.linalg.norm
Order of the norm.
See Also
--------
numpy.linalg.norm
"""
def __init__(self, ord=None):
self.ord = ord
def process_frame(self, frame):
return frame / (np.linalg.norm(frame, self.ord) + 1e-16)
class Mono(dataprocessor.DataProcessor):
"""Convert multichannel frames to mono.
Takes the mean across all channels.
"""
def process_frame(self, frame):
if frame.ndim > 1:
mono_frame = frame.mean(1)
else:
mono_frame = frame
return mono_frame
#class Preemphasize(dataprocessor.DataProcessor): # or just filter()
# pass
# essentially a simple buffer - works for matrices too... (really row features)
class Framer(dataprocessor.DataProcessor):
"""Turn an arbitrary length sequence of samples into regularly spaced frames.
Handles zero padding of final frames.
Attributes
----------
nwin : int
Length of frame (window) in samples.
nhop : int
Number of samples to skip between adjacent frames (hopsize).
Defaults to `nwin`.
See Also
--------
OverlapAdd : Inverse of Framer.
"""
def __init__(self, nwin, nhop=None):
self.nwin = nwin
if nhop is None:
nhop = nwin
self.nhop = nhop
def process_sequence(self, samples):
# Is samples a list instead of a generator?
if not 'next' in dir(samples):
samples = (x for x in [samples])
# nhop cannot be less than 1 for normal behavior
noverlap = self.nwin - self.nhop
buf = samples.next().copy()
while len(buf) < self.nwin:
buf = np.concatenate((buf, samples.next()))
frame = buf[:self.nwin]
buf = buf[self.nwin:]
while True:
yield frame.copy()
frame[:noverlap] = frame[self.nhop:]
try:
while len(buf) < self.nhop:
buf = np.concatenate((buf, samples.next()))
except StopIteration:
break
frame[noverlap:] = buf[:self.nhop]
buf = buf[self.nhop:]
# Read remaining few samples from file and yield the remaining
# zero padded frames.
frame[noverlap:noverlap + len(buf)] = buf
frame[noverlap + len(buf):] = 0
nremaining_frames = int(np.ceil((1.0*noverlap + len(buf)) / self.nhop))
for n in xrange(nremaining_frames):
yield frame.copy()
frame[:noverlap] = frame[self.nhop:]
frame[noverlap:] = 0
class OverlapAdd(dataprocessor.DataProcessor):
"""Perform overlap-add resynthesis of a sequence of frames.
Inverse of Framer().
Attributes
----------
nwin : int
Length of frame (window) in samples.
nhop : int
Number of samples to skip between adjacent frames (hopsize).
Defaults to `nwin`.
See Also
--------
Framer
"""
def __init__(self, nwin=512, nhop=None):
self.nwin = nwin
if nhop is None:
nhop = nwin
self.nhop = nhop
def process_sequence(self, frames):
# nhop cannot be less than 1 for normal behavior
noverlap = self.nwin - self.nhop
# off by one error somewhere here
buf = np.zeros(self.nwin)
for frame in frames:
buf += frame
yield buf[:self.nhop].copy()
buf[:noverlap] = buf[self.nhop:]
buf[noverlap:] = 0
nremaining_frames = int(noverlap / self.nhop) - 1
for n in range(nremaining_frames):
yield buf[:self.nhop].copy()
buf[:noverlap] = buf[self.nhop:]
class Window(dataprocessor.DataProcessor):
"""Multiply frames by a constant window function.
Attributes
----------
winfun : function of the form fun(winlen), returns array of length winlen
Function to generate a window of a given length. Defaults to
numpy.hamming.
See Also
--------
numpy.hamming : Hamming window function
numpy.ones : Rectangular window function
"""
def __init__(self, winfun=np.hanning):
self.winfun = winfun
def process_sequence(self, frames):
win = None
for frame in frames:
if win is None:
win = self.winfun(len(frame))
yield win * frame
class RMS(dataprocessor.DataProcessor):
"""Compute root-mean-square energy in decibels of each frame in sequence."""
def process_frame(self, frame):
return 20*np.log10(np.sqrt(np.mean(frame**2)))
class DB(dataprocessor.DataProcessor):
"""Convert frames to decibels.
Attributes
----------
minval : float
All values below minval are clipped to minval.
See Also
--------
IDB
"""
def __init__(self, minval=-100.0):
self.minval = minval
def process_frame(self, frame):
spectrum = 20*np.log10(np.abs(frame))
spectrum[spectrum < self.minval] = self.minval
return spectrum
class IDB(dataprocessor.DataProcessor):
"""Convert frames from decibels to linear units.
Inverse of DB.
See Also
--------
DB
"""
def process_frame(self, frame):
return 10.0 ** (frame / 20)
class Log(dataprocessor.DataProcessor):
"""Take the logarithm of each frame.
Attributes
----------
minval : float
All values below minval are clipped to minval.
"""
def __init__(self, minval=-5.0):
self.minval = minval
def process_frame(self, frame):
return np.maximum(np.log(frame), self.minval)
class Filterbank(dataprocessor.DataProcessor):
"""Warp STFT frames by passing them through the given filterbank.
Attributes
----------
fb : array_like
Matrix of filterbank weights. Each incoming frame is
multiplied by this matrix.
"""
def __init__(self, fb):
self.fb = fb
def process_frame(self, frame):
return np.dot(self.fb, frame)
# compound feature extractors:
def STFT(nfft, nwin=None, nhop=None, winfun=np.hanning):
"""Compute the Short-time Fourier Transform of incoming samples.
Parameters
----------
nfft : int
FFT length to use.
nwin : int
Length of each window in samples. Defaults to `nfft`.
nhop : int
Number of samples to skip between adjacent frames (hopsize).
Defaults to `nwin`.
winfun : function of the form fun(winlen), returns array of length winlen
Function to generate a window of a given length. Defaults to
numpy.hamming.
See Also
--------
ISTFT : Inverse STFT.
"""
if nwin is None:
nwin = nfft
return dataprocessor.Pipeline(Framer(nwin, nhop), Window(winfun),
RFFT(nfft))
def ISTFT(nfft, nwin=None, nhop=None, winfun=np.hanning):
"""Compute inverse Short-time Fourier Transform of incoming frames.
Parameters
----------
nfft : int
FFT length to use.
nwin : int
Length of each window in samples. Defaults to `nfft`.
nhop : int
Number of samples to skip between adjacent frames (hopsize).
Defaults to `nwin`.
winfun : function of the form fun(winlen), returns array of length winlen
Function to generate a window of a given length. Defaults to
numpy.hamming.
See Also
--------
STFT : Forward STFT.
"""
if nwin is None:
nwin = nfft
return dataprocessor.Pipeline(IRFFT(nfft), Window(winfun),
OverlapAdd(nwin, nhop))
def LogSpec(nfft, nwin=None, nhop=None, winfun=np.hanning):
"""Compute the log power spectrum of incoming samples in decibels.
Parameters
----------
nfft : int
FFT length to use.
nwin : int
Length of each window in samples. Defaults to `nfft`.
nhop : int
Number of samples to skip between adjacent frames (hopsize).
Defaults to `nwin`.
winfun : function of the form fun(winlen), returns array of length winlen
Function to generate a window of a given length. Defaults to
numpy.hamming.
See Also
--------
STFT : Short-time Fourier transform.
"""
return dataprocessor.Pipeline(STFT(nfft, nwin, nhop, winfun), DB())
def PowSpec(nfft, nwin=None, nhop=None, winfun=np.hanning):
"""Compute the power spectrum of incoming samples.
Parameters
----------
nfft : int
FFT length to use.
nwin : int
Length of each window in samples. Defaults to `nfft`.
nhop : int
Number of samples to skip between adjacent frames (hopsize).
Defaults to `nwin`.
winfun : function of the form fun(winlen), returns array of length winlen
Function to generate a window of a given length. Defaults to
numpy.hamming.
See Also
--------
STFT : Short-time Fourier transform.
"""
return dataprocessor.Pipeline(STFT(nfft, nwin, nhop, winfun), Abs(),
Square())