1
2
3
4 import numpy
5 from gmisclib import erb_scale
6 from gpk_voicing import percep_spec
7 from gpk_voicing import fv_misc as M
8 SillyWidthException = M.SillyWidthException
9
10
11 -def feature_vec(data, dt, DT,
12 LF=1.0, Nsv=M.NSV, ELF=None,
13 do_voicing=1, do_dissonance=False, PlompBouman=True):
14 FORMANT_LOW = erb_scale.f_to_erb(200.0)
15 FORMANT_HIGH = erb_scale.f_to_erb(4000.0)
16 assert DT > 0.0 and float(DT)>0.0
17 assert LF > 0.0 and float(LF)>0.0
18 bmin = erb_scale.f_to_erb(100.0)
19 bmax = erb_scale.f_to_erb(6000.0)
20 all_ectrs, all_ps, t0 = percep_spec.perceptual_spec(data, dt, DT,
21 bmin, bmax, M.DB,
22 do_mod=do_voicing,
23 do_dissonance=False,
24 PlompBouman=PlompBouman
25 )
26
27 band_indices = [i for (i,ec) in enumerate(all_ectrs) if ec['type']=='band']
28
29 neural = all_ps.take(band_indices, axis=0)
30 ectrs = [ec for ec in all_ectrs if ec['type']=='band']
31
32 nband_indices = [i for (i,ec) in enumerate(all_ectrs) if ec['type']!='band']
33
34 nneural = all_ps.take(nband_indices, axis=0)
35 nectrs = [ec for ec in all_ectrs if ec['type']!='band']
36
37
38 assert nneural.shape[1]==neural.shape[1]
39 assert neural.shape[1]==all_ps.shape[1]
40 assert neural.shape[0]+nneural.shape[0] == all_ps.shape[0]
41
42 neural_now = numpy.average(neural, axis=0)
43
44
45
46 assert neural_now.shape[0] == neural.shape[1]
47 neural_avg = numpy.square(neural_now).sum()/numpy.sum(neural_now)
48
49 numpy.divide(neural, neural_avg, neural)
50 numpy.divide(neural_now, neural_avg, neural_now)
51
52
53
54
55 assert nneural.shape[0] < nneural.shape[1]
56 assert len(nectrs) == nneural.shape[0]
57 for (i,e) in enumerate(nectrs):
58 if e['type'] == 'haspitch':
59 numpy.divide(nneural[i,:], neural_avg, nneural[i,:])
60 if e['type'] == 'dissonance':
61 numpy.divide(nneural[i,:], neural_avg, nneural[i,:])
62
63
64 o = []
65 descr = []
66 Vl = 0.06
67 w = int(round(Vl*LF/DT))
68 tmpo, tmpd = M.vowel(w, ectrs, neural, neural_now, Nsv,
69 formant_low=FORMANT_LOW,
70 formant_high=FORMANT_HIGH
71 )
72 o.extend(tmpo)
73 descr.extend(tmpd)
74 assert len(descr)==len(o), "Descriptor mismatch"
75
76 if do_dissonance:
77 raise RuntimeError, "Assumes dissonance=False"
78
79 if do_voicing:
80 Hpl = 0.07
81 w = int(round(Hpl*LF/DT))
82 tmpo, tmpd = M.haspitch(w, nectrs, nneural, neural_now, Nsv)
83 o.extend(tmpo)
84 descr.extend(tmpd)
85
86 assert len(descr)==len(o), "Descriptor mismatch"
87 N = neural[0].shape[0]
88 for (i, (tmp, dsc)) in enumerate(zip(o, descr)):
89 assert tmp.shape == (N,), "Wrong size: %d, %s = %d vs. %d" % (i, str(dsc), tmp.shape[0], N)
90
91 return (o, descr, DT, t0)
92