diff src/EM.cpp @ 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 c0c4a945577a
children ce1d88759557
line wrap: on
line diff
--- 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]];