changeset 45:e92376d450b0 preshift

Get notes out using pre-shifted templates (not working properly)
author Chris Cannam
date Mon, 07 Apr 2014 13:01:08 +0100
parents eec530c4300d
children ccb1a437a828
files mirex2012-matlab/doMultiF0.m src/EM.cpp src/EM.h src/Silvet.cpp
diffstat 4 files changed, 88 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/mirex2012-matlab/doMultiF0.m	Mon Apr 07 11:02:06 2014 +0100
+++ b/mirex2012-matlab/doMultiF0.m	Mon Apr 07 13:01:08 2014 +0100
@@ -5,8 +5,17 @@
 fprintf('%s',['Preprocessing............']);
 [ph pz sumY] = transcriptionMultipleTemplates(inputFile,12,1.1,1.3);
 fprintf('\n');
+
 fprintf('%s',['Postprocessing...........']);
 pianoRoll = repmat(sumY,88,1).*pz(1:88,:);
+
+pfid = fopen('pitchmatrix.lab','w');
+for i=1:size(pianoRoll,2)
+    fprintf(pfid, '%.2f ', pianoRoll(1:88,i));
+    fprintf(pfid, '\n');
+end;
+fclose(pfid);
+
 pianoRoll = pianoRoll';
 for j=[1:15 74:88] pianoRoll(:,j)=0; end;
 pianoRoll = medfilt1(pianoRoll,3);
--- a/src/EM.cpp	Mon Apr 07 11:02:06 2014 +0100
+++ b/src/EM.cpp	Mon Apr 07 13:01:08 2014 +0100
@@ -31,52 +31,56 @@
 static double epsilon = 1e-16;
 
 EM::EM() :
-    m_notes(SILVET_TEMPLATE_NOTE_COUNT),
-    m_bins(SILVET_TEMPLATE_HEIGHT),
-    m_instruments(SILVET_TEMPLATE_COUNT),
+    m_noteCount(SILVET_TEMPLATE_NOTE_COUNT),
+    m_shiftCount(SILVET_TEMPLATE_MAX_SHIFT * 2 + 1),
+    m_pitchCount(m_noteCount * m_shiftCount),
+    m_binCount(SILVET_TEMPLATE_HEIGHT),
+    m_instrumentCount(SILVET_TEMPLATE_COUNT),
     m_pitchSparsity(1.1),
     m_sourceSparsity(1.3)
 {
-    m_lowest = 0;
-    m_highest = m_notes - 1;
+    m_lowestPitch = 
+        silvet_templates_lowest_note * m_shiftCount;
+    m_highestPitch =
+        silvet_templates_highest_note * m_shiftCount + m_shiftCount - 1;
 
-    for (int i = 0; i < m_instruments; ++i) {
-        if (i == 0 || silvet_templates[i].lowest < m_lowest) {
-            m_lowest = silvet_templates[i].lowest;
-        }
-        if (i == 0 || silvet_templates[i].highest > m_highest) {
-            m_highest = silvet_templates[i].highest;
-        }
-    }
+    m_pitches = V(m_pitchCount);
 
-    m_pitches = V(m_notes);
-
-    for (int n = 0; n < m_notes; ++n) {
+    for (int n = 0; n < m_pitchCount; ++n) {
         m_pitches[n] = drand48();
     }
     
-    m_sources = Grid(m_instruments);
+    m_sources = Grid(m_instrumentCount);
     
-    for (int i = 0; i < m_instruments; ++i) {
-        m_sources[i] = V(m_notes);
-        for (int n = 0; n < m_notes; ++n) {
+    for (int i = 0; i < m_instrumentCount; ++i) {
+        m_sources[i] = V(m_pitchCount);
+        for (int n = 0; n < m_pitchCount; ++n) {
             m_sources[i][n] = (inRange(i, n) ? 1.0 : 0.0);
         }
     }
 
-    m_estimate = V(m_bins);
-    m_q = V(m_bins);
+    m_estimate = V(m_binCount);
+    m_q = V(m_binCount);
 }
 
 EM::~EM()
 {
 }
 
+void
+EM::rangeFor(int instrument, int &minPitch, int &maxPitch)
+{
+    minPitch = silvet_templates[instrument].lowest * m_shiftCount;
+    maxPitch = silvet_templates[instrument].highest * m_shiftCount
+        + m_shiftCount - 1;
+}
+
 bool
-EM::inRange(int instrument, int note)
+EM::inRange(int instrument, int pitch)
 {
-    return (note >= silvet_templates[instrument].lowest &&
-            note <= silvet_templates[instrument].highest);
+    int minPitch, maxPitch;
+    rangeFor(instrument, minPitch, maxPitch);
+    return (pitch >= minPitch && pitch <= maxPitch);
 }
 
 void
@@ -99,27 +103,35 @@
     maximisation(column);
 }
 
+const float *
+EM::templateFor(int instrument, int pitch)
+{
+    int note = pitch / m_shiftCount;
+    int shift = pitch % m_shiftCount;
+    return silvet_templates[instrument].data[note] + shift;
+}
+
 void
 EM::expectation(const V &column)
 {
     cerr << ".";
 
-    for (int i = 0; i < m_bins; ++i) {
+    for (int i = 0; i < m_binCount; ++i) {
         m_estimate[i] = epsilon;
     }
 
-    for (int i = 0; i < m_instruments; ++i) {
-        for (int n = 0; n < m_notes; ++n) {
-            float *w = silvet_templates[i].data[n];
+    for (int i = 0; i < m_instrumentCount; ++i) {
+        for (int n = 0; n < m_pitchCount; ++n) {
+            const float *w = templateFor(i, n);
             double pitch = m_pitches[n];
             double source = m_sources[i][n];
-            for (int j = 0; j < m_bins; ++j) {
+            for (int j = 0; j < m_binCount; ++j) {
                 m_estimate[j] += w[j] * pitch * source;
             }
         }
     }
 
-    for (int i = 0; i < m_bins; ++i) {
+    for (int i = 0; i < m_binCount; ++i) {
         m_q[i] = column[i] / m_estimate[i];
     }
 }
@@ -129,14 +141,14 @@
 {
     V newPitches = m_pitches;
 
-    for (int n = 0; n < m_notes; ++n) {
+    for (int n = 0; n < m_pitchCount; ++n) {
         newPitches[n] = epsilon;
-        if (n >= m_lowest && n <= m_highest) {
-            for (int i = 0; i < m_instruments; ++i) {
-                float *w = silvet_templates[i].data[n];
+        if (n >= m_lowestPitch && n <= m_highestPitch) {
+            for (int i = 0; i < m_instrumentCount; ++i) {
+                const float *w = templateFor(i, n);
                 double pitch = m_pitches[n];
                 double source = m_sources[i][n];
-                for (int j = 0; j < m_bins; ++j) {
+                for (int j = 0; j < m_binCount; ++j) {
                     newPitches[n] += w[j] * m_q[j] * pitch * source;
                 }
             }
@@ -149,14 +161,14 @@
 
     Grid newSources = m_sources;
 
-    for (int i = 0; i < m_instruments; ++i) {
-        for (int n = 0; n < m_notes; ++n) {
+    for (int i = 0; i < m_instrumentCount; ++i) {
+        for (int n = 0; n < m_pitchCount; ++n) {
             newSources[i][n] = epsilon;
             if (inRange(i, n)) {
-                float *w = silvet_templates[i].data[n];
+                const float *w = templateFor(i, n);
                 double pitch = m_pitches[n];
                 double source = m_sources[i][n];
-                for (int j = 0; j < m_bins; ++j) {
+                for (int j = 0; j < m_binCount; ++j) {
                     newSources[i][n] += w[j] * m_q[j] * pitch * source;
                 }
             }
@@ -175,7 +187,7 @@
 EM::report()
 {
     vector<int> sounding;
-    for (int n = 0; n < m_notes; ++n) {
+    for (int n = 0; n < m_pitchCount; ++n) {
         if (m_pitches[n] > 0.05) {
             sounding.push_back(n);
         }
@@ -185,7 +197,7 @@
         cerr << sounding[i] << " ";
         int maxj = -1;
         double maxs = 0.0;
-        for (int j = 0; j < m_instruments; ++j) {
+        for (int j = 0; j < m_instrumentCount; ++j) {
             if (j == 0 || m_sources[j][sounding[i]] > maxs) {
                 maxj = j;
                 maxs = m_sources[j][sounding[i]];
--- a/src/EM.h	Mon Apr 07 11:02:06 2014 +0100
+++ b/src/EM.h	Mon Apr 07 13:01:08 2014 +0100
@@ -47,21 +47,25 @@
     V m_estimate;
     V m_q;
     
-    int m_notes;
-    int m_bins;
-    int m_instruments;
+    int m_noteCount;
+    int m_shiftCount; // 1 + 2 * max template shift
+    int m_pitchCount; // noteCount * shiftCount
+    int m_binCount;
+    int m_instrumentCount;
     
     double m_pitchSparsity;
     double m_sourceSparsity;
 
-    int m_lowest;
-    int m_highest;
+    int m_lowestPitch;
+    int m_highestPitch;
 
     void normalise(V &column);
     void expectation(const V &column);
     void maximisation(const V &column);
 
-    bool inRange(int instrument, int note);
+    const float *templateFor(int instrument, int pitch);
+    void rangeFor(int instrument, int &minPitch, int &maxPitch);
+    bool inRange(int instrument, int pitch);
 };
 
 #endif
--- a/src/Silvet.cpp	Mon Apr 07 11:02:06 2014 +0100
+++ b/src/Silvet.cpp	Mon Apr 07 13:01:08 2014 +0100
@@ -34,6 +34,8 @@
 static int processingBPO = 60;
 static int processingHeight = 545;
 static int processingNotes = 88;
+static int processingShifts = 5;
+static int processingPitches = processingNotes * processingShifts;
 
 Silvet::Silvet(float inputSampleRate) :
     Plugin(inputSampleRate),
@@ -213,9 +215,9 @@
     d.description = "The estimated pitch contribution matrix";
     d.unit = "";
     d.hasFixedBinCount = true;
-    d.binCount = processingNotes;
+    d.binCount = processingPitches;
     d.binNames.clear();
-    for (int i = 0; i < processingNotes; ++i) {
+    for (int i = 0; i < processingPitches; ++i) {
         d.binNames.push_back(noteName(i));
     }
     d.hasKnownExtents = false;
@@ -373,17 +375,18 @@
 
         vector<double> pitches = em.getPitchDistribution();
         
-        for (int j = 0; j < processingNotes; ++j) {
+        for (int j = 0; j < processingPitches; ++j) {
             pitches[j] *= sum;
         }
 
         Feature f;
-        for (int j = 0; j < processingNotes; ++j) {
+        for (int j = 0; j < processingPitches; ++j) {
             f.values.push_back(float(pitches[j]));
         }
         fs[m_pitchOutputNo].push_back(f);
 
         FeatureList noteFeatures = postProcess(pitches);
+
         for (FeatureList::const_iterator fi = noteFeatures.begin();
              fi != noteFeatures.end(); ++fi) {
             fs[m_notesOutputNo].push_back(*fi);
@@ -467,7 +470,12 @@
     vector<double> filtered;
 
     for (int j = 0; j < processingNotes; ++j) {
-        m_postFilter[j]->push(pitches[j]);
+        double noteMax = 0.0;
+        for (int s = 0; s < processingShifts; ++s) {
+            double val = pitches[j * processingShifts + s];
+            if (val > noteMax) noteMax = val;
+        }
+        m_postFilter[j]->push(noteMax);
         filtered.push_back(m_postFilter[j]->get());
     }
 
@@ -485,11 +493,12 @@
 
     set<int> active;
     ValueIndexMap::const_iterator si = strengths.end();
-    for (int j = 0; j < polyphony; ++j) {
+    while (int(active.size()) < polyphony) {
         --si;
         if (si->first < threshold) break;
         cerr << si->second << " : " << si->first << endl;
         active.insert(si->second);
+        if (si == strengths.begin()) break;
     }
 
     // Minimum duration pruning, and conversion to notes. We can only