Package lib :: Module fvsimple
[frames] | no frames]

Source Code for Module lib.fvsimple

  1  #!/usr/bin/env python 
  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=True, PlompBouman=False):
14 FORMANT_LOW = erb_scale.f_to_erb(60.0) 15 FORMANT_HIGH = erb_scale.f_to_erb(6000.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(80.0) 19 bmax = erb_scale.f_to_erb(7000.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=do_dissonance, 24 PlompBouman=PlompBouman 25 ) 26 27 band_indices = [i for (i,ec) in enumerate(all_ectrs) if ec['type']=='band'] 28 # print 'band_indices=', band_indices 29 neural = all_ps.take(band_indices, axis=0) 30 ectrs = [ec for ec in all_ectrs if ec['type']=='band'] 31 # print 'ectrs=', ectrs 32 nband_indices = [i for (i,ec) in enumerate(all_ectrs) if ec['type']!='band'] 33 # print 'nband_indices=', nband_indices 34 nneural = all_ps.take(nband_indices, axis=0) 35 nectrs = [ec for ec in all_ectrs if ec['type']!='band'] 36 # print 'nectrs=', nectrs 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 # pylab.plot(neural_now) 44 # pylab.title('neural_now') 45 # pylab.show() 46 assert neural_now.shape[0] == neural.shape[1] 47 neural_avg = numpy.square(neural_now).sum()/numpy.sum(neural_now) 48 # print 'neural_avg=', neural_avg 49 numpy.divide(neural, neural_avg, neural) 50 numpy.divide(neural_now, neural_avg, neural_now) 51 # pylab.plot(neural_now) 52 # pylab.title('normed neural_now') 53 # pylab.show() 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 # print '# neural_avg=', neural_avg 64 o = [] 65 descr = [] 66 tmpo, tmpd = M.vowel(1, ectrs, neural, neural_now, Nsv, 67 formant_low=FORMANT_LOW, 68 formant_high=FORMANT_HIGH 69 ) 70 o.extend(tmpo) 71 descr.extend(tmpd) 72 assert len(descr)==len(o), "Descriptor mismatch" 73 74 tmpo, tmpd = M.spectral_entropy(1, ectrs, neural, neural_now, Nsv) 75 o.extend(tmpo) 76 descr.extend(tmpd) 77 assert len(descr)==len(o), "Descriptor mismatch" 78 79 80 if do_voicing: 81 Hpl = 0.03 82 w = int(round(Hpl*LF/DT)) 83 tmpo, tmpd = M.haspitch(w, nectrs, nneural, neural_now, Nsv) 84 o.extend(tmpo) 85 descr.extend(tmpd) 86 87 assert len(descr)==len(o), "Descriptor mismatch" 88 if do_dissonance: 89 Dsl = 0.04 90 w = int(round(Dsl*LF/DT)) 91 tmpo, tmpd = M.dissonance(w, nectrs, nneural, neural_now, Nsv) 92 o.extend(tmpo) 93 descr.extend(tmpd) 94 95 assert len(descr)==len(o), "Descriptor mismatch" 96 N = neural[0].shape[0] 97 for (i, (tmp, dsc)) in enumerate(zip(o, descr)): 98 assert tmp.shape == (N,), "Wrong size: %d, %s = %d vs. %d" % (i, str(dsc), tmp.shape[0], N) 99 100 return (o, descr, DT, t0)
101