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