forked from flashlight/flashlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lib_audio.py
More file actions
64 lines (51 loc) · 1.92 KB
/
test_lib_audio.py
File metadata and controls
64 lines (51 loc) · 1.92 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
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
"""
import itertools
from pathlib import Path
def load_data(filename):
with open(filename) as f:
return [
float(x) for x in itertools.chain.from_iterable(line.split() for line in f)
]
def test_mfcc():
from flashlight.lib.audio import feature as fl_feat
data_path = (
Path(__file__)
.absolute()
.parent.joinpath("../../../flashlight/lib/test/audio/feature/data")
)
wavinput = load_data(data_path.joinpath("sa1.dat"))
# golden features to compare
htkfeatures = load_data(data_path.joinpath("sa1-mfcc.htk"))
assert len(wavinput) > 0
assert len(htkfeatures) > 0
params = fl_feat.FeatureParams()
# define parameters of the featurization
params.sampling_freq = 16000
params.low_freq_filterbank = 0
params.high_freq_filterbank = 8000
params.num_filterbank_chans = 20
params.num_cepstral_coeffs = 13
params.use_energy = False
params.zero_mean_frame = False
params.use_power = False
# apply MFCC featurization
mfcc = fl_feat.Mfcc(params)
features = mfcc.apply(wavinput)
# check that obtained features are the same as golden one
assert len(features) == len(htkfeatures)
assert len(features) % 39 == 0
numframes = len(features) // 39
featurescopy = features.copy()
for f in range(numframes):
for i in range(1, 39):
features[f * 39 + i - 1] = features[f * 39 + i]
features[f * 39 + 12] = featurescopy[f * 39 + 0]
features[f * 39 + 25] = featurescopy[f * 39 + 13]
features[f * 39 + 38] = featurescopy[f * 39 + 26]
differences = [abs(x[0] - x[1]) for x in zip(features, htkfeatures)]
assert max(differences) < 0.4
assert sum(differences) / len(differences) < 0.03