-
Notifications
You must be signed in to change notification settings - Fork 79
/
util.py
202 lines (144 loc) · 5.35 KB
/
util.py
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
# coding: utf-8
"""
Utilities
=========
Audio files
-----------
.. autosummary::
:toctree: generated/
example_audio_file
Mel-cepstrum analysis
---------------------
.. autosummary::
:toctree: generated/
mcepalpha
"""
from __future__ import division, print_function, absolute_import
import pkg_resources
import numpy as np
# 16kHz, 16bit example audio from cmu_us_awb_arctic
# see COPYING for the license of the audio file.
EXAMPLE_AUDIO = 'example_audio_data/arctic_a0007.wav'
# I originally tried with functools.wraps to create decoraters, but it didn't
# work to me if I use multiple decoratores to decorate a function.
# Specifically, I cannot inspect argspec with a decorated function, so cannot
# get a argment name simply from it. As suggested in the following
# stackoverflow thread, using decorator package.
# https://stackoverflow.com/questions/12558505/preserve-argspec-when-decorating
try:
from inspect import getfullargspec
except:
# python 2.7
from inspect import getargspec as getfullargspec
from decorator import decorator
@decorator
def apply_along_last_axis(func, *args, **kwargs):
"""Apply function along last axis
This is used for extending vector-to-vector operations to matrix-to-matrix
operations. This basically does the following thing in a convenient way:
```py
np.apply_along_axis(func, input_vector, -1, *args, **kwargs)
```
Note: The decorator assumes that the first argment of the function is the
input vector (1d numpy array).
"""
# Get first arg
first_arg_name = getfullargspec(func)[0][0]
has_positional_arg = len(args) > 0
input_arg = args[0] if has_positional_arg else kwargs[first_arg_name]
if input_arg.ndim == 1:
ret = func(*args, **kwargs)
else:
# we need at least 1 positonal argment
if len(args) == 0:
args = kwargs.pop(first_arg_name)
ret = np.apply_along_axis(func, -1, *args, **kwargs)
return ret
@decorator
def automatic_type_conversion(func, *args, **kwargs):
first_arg_name = getfullargspec(func)[0][0]
has_positional_arg = len(args) > 0
input_arg = args[0] if has_positional_arg else kwargs[first_arg_name]
dtypein = input_arg.dtype
# Since C functions can only accept double
if dtypein != np.float64:
if has_positional_arg:
args = tuple(map(lambda v: input_arg.astype(
np.float64) if v[0] == 0 else v[1], enumerate(args)))
else:
kwargs[first_arg_name] = input_arg.astype(np.float64)
return func(*args, **kwargs).astype(dtypein)
def assert_gamma(gamma):
if not (-1 <= gamma <= 0.0):
raise ValueError("unsupported gamma: must be -1 <= gamma <= 0")
def assert_pade(pade):
valid = [4, 5, 6, 7]
if pade not in valid:
raise ValueError("4, 5, 6 or 7 pade approximation is supported")
def assert_stage(stage):
if stage < 1:
raise ValueError("stage >= 1 (-1 <= gamma < 0)")
def ispow2(num):
return ((num & (num - 1)) == 0) and num != 0
def assert_fftlen(fftlen):
if not ispow2(fftlen):
raise ValueError("fftlen must be power of 2")
def example_audio_file():
"""Get the path to an included audio example file.
Examples
--------
>>> from scipy.io import wavfile
>>> fs, x = wavfile.read(pysptk.util.example_audio_file())
>>> import matplotlib.pyplot as plt
>>> plt.plot(x, label="cmu_us_awb_arctic arctic_a0007.wav")
>>> plt.xlim(0, len(x))
>>> plt.legend()
"""
return pkg_resources.resource_filename(__name__, EXAMPLE_AUDIO)
def mcepalpha(fs, start=0.0, stop=1.0, step=0.001, num_points=1000):
"""Compute appropriate frequency warping parameter given a sampling frequency
It would be useful to determine alpha parameter in mel-cepstrum analysis.
The code is traslated from https://bitbucket.org/happyalu/mcep_alpha_calc.
Parameters
----------
fs : int
Sampling frequency
start : float
start value that will be passed to numpy.arange. Default is 0.0.
stop : float
stop value that will be passed to numpy.arange. Default is 1.0.
step : float
step value that will be passed to numpy.arange. Default is 0.001.
num_points : int
Number of points used in approximating mel-scale vectors in fixed-
length.
Returns
-------
alpha : float
frequency warping paramter (offen denoted by alpha)
See Also
--------
pysptk.sptk.mcep
pysptk.sptk.mgcep
"""
alpha_candidates = np.arange(start, stop, step)
mel = _melscale_vector(fs, num_points)
distances = [rms_distance(mel, _warping_vector(alpha, num_points)) for
alpha in alpha_candidates]
return alpha_candidates[np.argmin(distances)]
def _melscale_vector(fs, length):
step = (fs / 2.0) / length
melscalev = 1000.0 / np.log(2) * np.log(1 +
step * np.arange(0, length) / 1000.0)
return melscalev / melscalev[-1]
def _warping_vector(alpha, length):
step = np.pi / length
omega = step * np.arange(0, length)
num = (1 - alpha * alpha) * np.sin(omega)
den = (1 + alpha * alpha) * np.cos(omega) - 2 * alpha
warpfreq = np.arctan(num / den)
warpfreq[warpfreq < 0] += np.pi
return warpfreq / warpfreq[-1]
def rms_distance(v1, v2):
d = v1 - v2
return np.sum(np.abs(d * d)) / len(v1)