-
Notifications
You must be signed in to change notification settings - Fork 11
/
learn_f0.py
executable file
·48 lines (36 loc) · 1.36 KB
/
learn_f0.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
#!/usr/bin/env python
import math
import numpy
import pickle
import sklearn
import sys
from stf import STF
from mfcc import MFCC
from dtw import DTW
from gmmmap import GMMMap, TrajectoryGMMMap
DIMENSION = 16
if __name__ == '__main__':
if len(sys.argv) < 4:
print 'Usage: %s [list of source stf] [list of target stf] [output file]' % sys.argv[0]
sys.exit()
source_list = open(sys.argv[1]).read().strip().split('\n')
target_list = open(sys.argv[2]).read().strip().split('\n')
assert len(source_list) == len(target_list)
f0_count = [0, 0]
f0_mean = [0.0, 0.0]
f0_square_mean = [0.0, 0.0]
for i in xrange(len(source_list)):
source = STF()
source.loadfile(source_list[i])
target = STF()
target.loadfile(target_list[i])
for idx, stf in enumerate([source, target]):
count = (stf.F0 != 0).sum()
f0_mean[idx] = (f0_mean[idx] * f0_count[idx] + stf.F0[stf.F0 != 0].sum()) / (f0_count[idx] + count)
f0_square_mean[idx] = (f0_square_mean[idx] * f0_count[idx] + (stf.F0[stf.F0 != 0] ** 2).sum()) / (f0_count[idx] + count)
f0_count[idx] += count
f0_deviation = [math.sqrt(f0_square_mean[i] - f0_mean[i] ** 2) for i in xrange(2)]
f0 = (tuple(f0_mean), tuple(f0_deviation))
output = open(sys.argv[3], 'wb')
pickle.dump(f0, output)
output.close()