1
2
3
4 from gmisclib import erb_scale
5 from gmisclib import Num
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=1.0,
13 do_voicing=1, do_dissonance=True,
14 PlompBouman=False):
15 assert do_dissonance
16 assert not PlompBouman
17 assert do_voicing
18
19 FORMANT_LOW = erb_scale.f_to_erb(60.0)
20 FORMANT_HIGH = erb_scale.f_to_erb(6000.0)
21 assert DT > 0.0 and float(DT)>0.0
22 assert LF > 0.0 and float(LF)>0.0
23 bmin = erb_scale.f_to_erb(80.0)
24 bmax = erb_scale.f_to_erb(7000.0)
25 all_ectrs, all_ps, t0 = percep_spec.block_percep_spec(data, dt, DT,
26 bmin, bmax, M.DB,
27 do_mod=do_voicing,
28 do_dissonance=do_dissonance,
29 PlompBouman=PlompBouman
30 )
31
32 band_indices = [i for (i,ec) in enumerate(all_ectrs) if ec['type']=='band']
33 neural = all_ps.take(band_indices, axis=0)
34 ectrs = [ec for ec in all_ectrs if ec['type']=='band']
35 nband_indices = [i for (i,ec) in enumerate(all_ectrs) if ec['type']!='band']
36 nneural = all_ps.take(nband_indices, axis=0)
37 nectrs = [ec for ec in all_ectrs if ec['type']!='band']
38
39 assert nneural.shape[1]==neural.shape[1]
40 assert neural.shape[1]==all_ps.shape[1]
41 assert neural.shape[0]+nneural.shape[0] == all_ps.shape[0]
42
43 neural_now = Num.average(neural, axis=0)
44 assert neural_now.shape[0] == neural.shape[1]
45 neural_avg = Num.sum(neural_now**2)/Num.sum(neural_now)
46
47 Num.divide(neural, neural_avg, neural)
48
49 Num.divide(neural_now, neural_avg, neural_now)
50
51
52 assert nneural.shape[0] < nneural.shape[1]
53 assert len(nectrs) == nneural.shape[0]
54 for (i,e) in enumerate(nectrs):
55 if e['type'] == 'haspitch':
56 Num.divide(nneural[i,:], neural_avg, nneural[i,:])
57 if e['type'] == 'dissonance':
58 Num.divide(nneural[i,:], neural_avg, nneural[i,:])
59
60
61 o = []
62 descr = []
63 wset = set([0])
64 for vl in [0.05]:
65
66 w = int(round(vl*LF/DT))
67 if not w in wset:
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 wset.add(w)
75
76 wset = set([0])
77 for fel in [0.04]:
78 w = int(round(fel*ELF/DT))
79 if not w in wset:
80 tmpo, tmpd = M.fricative_edge(w, ectrs, neural, neural_now, Nsv,
81 do_abs=True
82 )
83 o.extend(tmpo)
84 descr.extend(tmpd)
85 tmpo, tmpd = M.fricative_edge(ectrs, neural, neural_now, Nsv,
86 do_abs=False, width=w
87 )
88 o.extend(tmpo)
89 descr.extend(tmpd)
90 wset.add(w)
91
92
93 wset = set([0])
94 for hpl in [0.03, 0.06]:
95 w = int(round(hpl*LF/DT))
96 if not w in wset:
97 tmpo, tmpd = M.haspitch(w, nectrs, nneural, neural_now, Nsv)
98 o.extend(tmpo)
99 descr.extend(tmpd)
100 wset.add(w)
101 wset = set([0])
102 for dsl in [0.06]:
103 w = int(round(dsl*LF/DT))
104 if not w in wset:
105 tmpo, tmpd = M.dissonance(w, nectrs, nneural, neural_now, Nsv)
106 o.extend(tmpo)
107 descr.extend(tmpd)
108 wset.add(w)
109
110 assert len(descr)==len(o), "Descriptor mismatch"
111 N = neural[0].shape[0]
112 for (i, (tmp, dsc)) in enumerate(zip(o, descr)):
113 assert tmp.shape == (N,), "Wrong size: %d, %s = %d vs. %d" % (i, str(dsc), tmp.shape[0], N)
114
115 return (o, descr, DT, t0)
116