comparison scripts/PitchBihist.py @ 38:e5e8e8a96948 branch-tests

before refactoring OPMEllin
author Maria Panteli
date Thu, 14 Sep 2017 15:21:52 +0100
parents 2cc444441f42
children 0e70021f251e
comparison
equal deleted inserted replaced
37:2cc444441f42 38:e5e8e8a96948
36 def get_melody_from_file(self, melodia_file, stop_sec=None): 36 def get_melody_from_file(self, melodia_file, stop_sec=None):
37 if not os.path.exists(melodia_file): 37 if not os.path.exists(melodia_file):
38 return [] 38 return []
39 data = np.loadtxt(melodia_file, delimiter=',') 39 data = np.loadtxt(melodia_file, delimiter=',')
40 times, freqs = (data[:, 0], data[:, 1]) 40 times, freqs = (data[:, 0], data[:, 1])
41 #melody_sr = 1. / (times[1] - times[0])
42 if stop_sec is not None: 41 if stop_sec is not None:
43 stop_idx = np.where(times < stop_sec)[0] 42 stop_idx = np.where(times < stop_sec)[0]
44 times, freqs = times[stop_idx], freqs[stop_idx] 43 times, freqs = times[stop_idx], freqs[stop_idx]
45 freqs[freqs<=0] = np.nan 44 freqs[freqs<=0] = np.nan
46 melody = freqs 45 melody = freqs
47 return melody#, melody_sr 46 return melody
48 47
49 48
50 def get_melody_matrix(self, melody): 49 def get_melody_matrix(self, melody):
51 n_bins = 60 50 n_bins = 60
52 n_frames = len(melody) 51 n_frames = len(melody)
55 melody_matrix = np.zeros((n_bins, n_frames)) 54 melody_matrix = np.zeros((n_bins, n_frames))
56 for time, pitch in enumerate(melody_octave): 55 for time, pitch in enumerate(melody_octave):
57 if not np.isnan(pitch): 56 if not np.isnan(pitch):
58 melody_matrix[int(pitch), time] = 1 57 melody_matrix[int(pitch), time] = 1
59 return melody_matrix 58 return melody_matrix
60
61
62 def bihist_from_melodia(self, filename='sample_melodia.csv', secondframedecomp=True, stop_sec=None):
63 #melody, melody_sr = self.get_melody_from_file(filename, stop_sec=stop_sec)
64 melody = self.get_melody_from_file(filename, stop_sec=stop_sec)
65 if len(melody) == 0:
66 return []
67 melody_matrix = self.get_melody_matrix(melody)
68 bihist = []
69 if secondframedecomp:
70 nbins, norigframes = melody_matrix.shape
71 win2 = int(round(self.win2sec * self.melody_sr))
72 hop2 = int(round(self.hop2sec * self.melody_sr))
73 if norigframes<=win2:
74 nframes = 1
75 win2 = norigframes
76 else:
77 nframes = int(np.ceil((norigframes-win2)/float(hop2)))
78 bihistframes = np.empty((nbins*nbins, nframes))
79 for i in range(nframes): # loop over all 8-sec frames
80 frame = melody_matrix[:, (i*hop2):(i*hop2+win2)]
81 bihist = self.bihistogram(frame)
82 bihist = np.reshape(bihist, -1)
83 bihistframes[:, i] = bihist
84 bihist = bihistframes
85 else:
86 bihist = self.bihistogram(melody_matrix)
87 return bihist
88 59
89 60
90 def bihistogram(self, spec, spec_sr=None, winsec=0.5, align=True): 61 def bihistogram(self, spec, spec_sr=None, winsec=0.5, align=True):
91 if spec_sr is None: 62 if spec_sr is None:
92 # assume spec is melody_matrix with default sr 63 # assume spec is melody_matrix with default sr
118 B = np.roll(B, -ref, axis=0) 89 B = np.roll(B, -ref, axis=0)
119 B = np.roll(B, -ref, axis=1) 90 B = np.roll(B, -ref, axis=1)
120 return B 91 return B
121 92
122 93
94 def bihist_from_melodia(self, filename='sample_melodia.csv', secondframedecomp=True, stop_sec=None):
95 melody = self.get_melody_from_file(filename, stop_sec=stop_sec)
96 if len(melody) == 0:
97 return []
98 melody_matrix = self.get_melody_matrix(melody)
99 bihist = []
100 if secondframedecomp:
101 nbins, norigframes = melody_matrix.shape
102 win2 = int(round(self.win2sec * self.melody_sr))
103 hop2 = int(round(self.hop2sec * self.melody_sr))
104 if norigframes<=win2:
105 nframes = 1
106 win2 = norigframes
107 else:
108 nframes = int(np.ceil((norigframes-win2)/float(hop2)))
109 bihistframes = np.empty((nbins*nbins, nframes))
110 for i in range(nframes): # loop over all 8-sec frames
111 frame = melody_matrix[:, (i*hop2):(i*hop2+win2)]
112 bihist = self.bihistogram(frame)
113 bihist = np.reshape(bihist, -1)
114 bihistframes[:, i] = bihist
115 bihist = bihistframes
116 else:
117 bihist = self.bihistogram(melody_matrix)
118 return bihist
119
120
123 if __name__ == '__main__': 121 if __name__ == '__main__':
124 pb = PitchBihist() 122 pb = PitchBihist()
125 pb.bihist_from_melodia(filename='vamp_melodia.csv') 123 melody = np.concatenate([660 * np.ones(250), 440 * np.ones(500)])
124 melody_matrix = pb.get_melody_matrix(melody)
125 pb.bihistogram(melody_matrix, align=True)
126