changeset 38:e5e8e8a96948 branch-tests

before refactoring OPMEllin
author Maria Panteli
date Thu, 14 Sep 2017 15:21:52 +0100
parents 2cc444441f42
children 0e70021f251e
files scripts/OPMellin.py scripts/PitchBihist.py
diffstat 2 files changed, 36 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/OPMellin.py	Thu Sep 14 14:50:00 2017 +0100
+++ b/scripts/OPMellin.py	Thu Sep 14 15:21:52 2017 +0100
@@ -106,8 +106,10 @@
             nfft2 = 2048  # nfft2 does not depend on win2??
             melspectemp = self.melspec
             if ((nfft2 > win2) and (center is False)):
-                # pad the signal by nfft2-win2 so that frame decomposition (n_frames) 
-                # is still based on win2 and not nfft2
+                # in librosa version < 6.0, window is padded to the size of nfft 
+                # so if win2<nfft2 the frames returned are less than expected
+                # solution: pad the signal by (nfft2-win2)/2 on the edges 
+                # then frame decomposition (n_frames) matches the one expected using win2
                 melspectemp = numpy.concatenate([numpy.zeros((nmels,int((nfft2 - win2) // 2))),self.melspec, numpy.zeros((nmels,int((nfft2 - win2) // 2)))],axis=1)
             if melspectemp.shape[1]<nfft2:
                 # pad with zeros to have at least one 8-sec window
--- a/scripts/PitchBihist.py	Thu Sep 14 14:50:00 2017 +0100
+++ b/scripts/PitchBihist.py	Thu Sep 14 15:21:52 2017 +0100
@@ -38,13 +38,12 @@
             return []
         data = np.loadtxt(melodia_file, delimiter=',')
         times, freqs = (data[:, 0], data[:, 1])
-        #melody_sr = 1. / (times[1] - times[0])
         if stop_sec is not None:
             stop_idx = np.where(times < stop_sec)[0]
             times, freqs = times[stop_idx], freqs[stop_idx]
         freqs[freqs<=0] = np.nan
         melody = freqs
-        return melody#, melody_sr
+        return melody
 
 
     def get_melody_matrix(self, melody):
@@ -59,34 +58,6 @@
         return melody_matrix
 
 
-    def bihist_from_melodia(self, filename='sample_melodia.csv', secondframedecomp=True, stop_sec=None):
-        #melody, melody_sr = self.get_melody_from_file(filename, stop_sec=stop_sec)
-        melody = self.get_melody_from_file(filename, stop_sec=stop_sec)
-        if len(melody) == 0:
-            return []
-        melody_matrix = self.get_melody_matrix(melody)
-        bihist = []
-        if secondframedecomp:
-            nbins, norigframes = melody_matrix.shape
-            win2 = int(round(self.win2sec * self.melody_sr))
-            hop2 = int(round(self.hop2sec * self.melody_sr))
-            if norigframes<=win2:
-                nframes = 1
-                win2 = norigframes
-            else:
-                nframes = int(np.ceil((norigframes-win2)/float(hop2)))
-            bihistframes = np.empty((nbins*nbins, nframes))
-            for i in range(nframes):  # loop over all 8-sec frames
-                frame = melody_matrix[:, (i*hop2):(i*hop2+win2)]
-                bihist = self.bihistogram(frame)
-                bihist = np.reshape(bihist, -1)
-                bihistframes[:, i] = bihist
-            bihist = bihistframes
-        else:
-            bihist = self.bihistogram(melody_matrix)
-        return bihist
-
-
     def bihistogram(self, spec, spec_sr=None, winsec=0.5, align=True):
         if spec_sr is None:
             # assume spec is melody_matrix with default sr
@@ -120,6 +91,36 @@
         return B
 
 
+    def bihist_from_melodia(self, filename='sample_melodia.csv', secondframedecomp=True, stop_sec=None):
+        melody = self.get_melody_from_file(filename, stop_sec=stop_sec)
+        if len(melody) == 0:
+            return []
+        melody_matrix = self.get_melody_matrix(melody)
+        bihist = []
+        if secondframedecomp:
+            nbins, norigframes = melody_matrix.shape
+            win2 = int(round(self.win2sec * self.melody_sr))
+            hop2 = int(round(self.hop2sec * self.melody_sr))
+            if norigframes<=win2:
+                nframes = 1
+                win2 = norigframes
+            else:
+                nframes = int(np.ceil((norigframes-win2)/float(hop2)))
+            bihistframes = np.empty((nbins*nbins, nframes))
+            for i in range(nframes):  # loop over all 8-sec frames
+                frame = melody_matrix[:, (i*hop2):(i*hop2+win2)]
+                bihist = self.bihistogram(frame)
+                bihist = np.reshape(bihist, -1)
+                bihistframes[:, i] = bihist
+            bihist = bihistframes
+        else:
+            bihist = self.bihistogram(melody_matrix)
+        return bihist
+
+
 if __name__ == '__main__':
     pb = PitchBihist()
-    pb.bihist_from_melodia(filename='vamp_melodia.csv')
+    melody = np.concatenate([660 * np.ones(250), 440 * np.ones(500)])
+    melody_matrix = pb.get_melody_matrix(melody)
+    pb.bihistogram(melody_matrix, align=True)
+    
\ No newline at end of file