changeset 151:fc06b6f33021

double -> float in EM (to test)
author Chris Cannam
date Wed, 14 May 2014 19:38:36 +0100
parents d2bc51cc7f57
children 74f14efe032f
files data/include/templates.h src/EM.cpp src/EM.h src/Silvet.cpp yeti/scratch/generateTemplatesC.yeti
diffstat 5 files changed, 57 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/data/include/templates.h	Wed May 14 18:09:06 2014 +0100
+++ b/data/include/templates.h	Wed May 14 19:38:36 2014 +0100
@@ -15,7 +15,7 @@
     const char *name;
     int lowest;
     int highest;
-    double data[SILVET_TEMPLATE_NOTE_COUNT][SILVET_TEMPLATE_SIZE];
+    float data[SILVET_TEMPLATE_NOTE_COUNT][SILVET_TEMPLATE_SIZE];
 } silvet_template_t;
 
 static int silvet_templates_lowest_note = 15;
--- a/src/EM.cpp	Wed May 14 18:09:06 2014 +0100
+++ b/src/EM.cpp	Wed May 14 19:38:36 2014 +0100
@@ -31,7 +31,7 @@
 
 using namespace breakfastquay;
 
-static double epsilon = 1e-16;
+static float epsilon = 1e-10;
 
 EM::EM(bool useShifts) :
     m_noteCount(SILVET_TEMPLATE_NOTE_COUNT),
@@ -45,15 +45,15 @@
     m_lowestPitch(silvet_templates_lowest_note),
     m_highestPitch(silvet_templates_highest_note)
 {
-    m_pitches = allocate<double>(m_noteCount);
-    m_updatePitches = allocate<double>(m_noteCount);
+    m_pitches = allocate<float>(m_noteCount);
+    m_updatePitches = allocate<float>(m_noteCount);
     for (int n = 0; n < m_noteCount; ++n) {
         m_pitches[n] = drand48();
     }
 
     if (useShifts) {
-        m_shifts = allocate_channels<double>(m_shiftCount, m_noteCount);
-        m_updateShifts = allocate_channels<double>(m_shiftCount, m_noteCount);
+        m_shifts = allocate_channels<float>(m_shiftCount, m_noteCount);
+        m_updateShifts = allocate_channels<float>(m_shiftCount, m_noteCount);
         for (int f = 0; f < m_shiftCount; ++f) {
             for (int n = 0; n < m_noteCount; ++n) {
                 m_shifts[f][n] = drand48();
@@ -64,16 +64,16 @@
         m_updateShifts = 0;
     }
     
-    m_sources = allocate_channels<double>(m_sourceCount, m_noteCount);
-    m_updateSources = allocate_channels<double>(m_sourceCount, m_noteCount);
+    m_sources = allocate_channels<float>(m_sourceCount, m_noteCount);
+    m_updateSources = allocate_channels<float>(m_sourceCount, m_noteCount);
     for (int i = 0; i < m_sourceCount; ++i) {
         for (int n = 0; n < m_noteCount; ++n) {
             m_sources[i][n] = (inRange(i, n) ? 1.0 : 0.0);
         }
     }
 
-    m_estimate = allocate<double>(m_binCount);
-    m_q = allocate<double>(m_binCount);
+    m_estimate = allocate<float>(m_binCount);
+    m_q = allocate<float>(m_binCount);
 }
 
 EM::~EM()
@@ -104,16 +104,16 @@
 }
 
 void
-EM::normaliseColumn(double *column, int size)
+EM::normaliseColumn(float *column, int size)
 {
-    double sum = v_sum(column, size);
+    float sum = v_sum(column, size);
     v_scale(column, 1.0 / sum, size);
 }
 
 void
-EM::normaliseGrid(double **grid, int size1, int size2)
+EM::normaliseGrid(float **grid, int size1, int size2)
 {
-    double *denominators = allocate_and_zero<double>(size2);
+    float *denominators = allocate_and_zero<float>(size2);
 
     for (int i = 0; i < size1; ++i) {
         for (int j = 0; j < size2; ++j) {
@@ -131,15 +131,15 @@
 void
 EM::iterate(const double *column)
 {
-    double *norm = allocate<double>(m_binCount);
-    v_copy(norm, column, m_binCount);
+    float *norm = allocate<float>(m_binCount);
+    v_convert(norm, column, m_binCount);
     normaliseColumn(norm, m_binCount);
     expectation(norm);
     maximisation(norm);
     deallocate(norm);
 }
 
-const double *
+const float *
 EM::templateFor(int instrument, int note, int shift)
 {
     if (m_shifts) {
@@ -151,7 +151,7 @@
 }
 
 void
-EM::expectation(const double *column)
+EM::expectation(const float *column)
 {
 //    cerr << ".";
 
@@ -159,23 +159,23 @@
 
     for (int f = 0; f < m_shiftCount; ++f) {
 
-        const double *shiftIn = m_shifts ? m_shifts[f] : 0;
+        const float *shiftIn = m_shifts ? m_shifts[f] : 0;
 
         for (int i = 0; i < m_sourceCount; ++i) {
 
-            const double *sourceIn = m_sources[i];
+            const float *sourceIn = m_sources[i];
 
             int lowest, highest;
             rangeFor(i, lowest, highest);
 
             for (int n = lowest; n <= highest; ++n) {
 
-                const double source = sourceIn[n];
-                const double shift = shiftIn ? shiftIn[n] : 1.0;
-                const double pitch = m_pitches[n];
+                const float source = sourceIn[n];
+                const float shift = shiftIn ? shiftIn[n] : 1.0;
+                const float pitch = m_pitches[n];
 
-                const double factor = pitch * source * shift;
-                const double *w = templateFor(i, n, f);
+                const float factor = pitch * source * shift;
+                const float *w = templateFor(i, n, f);
 
                 v_add_with_gain(m_estimate, w, factor, m_binCount);
             }
@@ -191,7 +191,7 @@
 }
 
 void
-EM::maximisation(const double *column)
+EM::maximisation(const float *column)
 {
     v_set(m_updatePitches, epsilon, m_noteCount);
 
@@ -205,34 +205,34 @@
         }
     }
 
-    double *contributions = allocate<double>(m_binCount);
+    float *contributions = allocate<float>(m_binCount);
 
     for (int f = 0; f < m_shiftCount; ++f) {
 
-        const double *shiftIn = m_shifts ? m_shifts[f] : 0;
-        double *shiftOut = m_shifts ? m_updateShifts[f] : 0;
+        const float *shiftIn = m_shifts ? m_shifts[f] : 0;
+        float *shiftOut = m_shifts ? m_updateShifts[f] : 0;
 
         for (int i = 0; i < m_sourceCount; ++i) {
 
-            const double *sourceIn = m_sources[i];
-            double *sourceOut = m_updateSources[i];
+            const float *sourceIn = m_sources[i];
+            float *sourceOut = m_updateSources[i];
 
             int lowest, highest;
             rangeFor(i, lowest, highest);
 
             for (int n = lowest; n <= highest; ++n) {
 
-                const double shift = shiftIn ? shiftIn[n] : 1.0;
-                const double source = sourceIn[n];
-                const double pitch = m_pitches[n];
+                const float shift = shiftIn ? shiftIn[n] : 1.0;
+                const float source = sourceIn[n];
+                const float pitch = m_pitches[n];
 
-                const double factor = pitch * source * shift;
-                const double *w = templateFor(i, n, f);
+                const float factor = pitch * source * shift;
+                const float *w = templateFor(i, n, f);
 
                 v_copy(contributions, w, m_binCount);
                 v_multiply(contributions, m_q, m_binCount);
 
-                double total = factor * v_sum(contributions, m_binCount);
+                float total = factor * v_sum(contributions, m_binCount);
 
                 m_updatePitches[n] += total;
                 sourceOut[n] += total;
--- a/src/EM.h	Wed May 14 18:09:06 2014 +0100
+++ b/src/EM.h	Wed May 14 19:38:36 2014 +0100
@@ -38,7 +38,7 @@
      * Return the estimated distribution after the current iteration.
      * Like the input, this will have getBinCount() values.
      */
-    const double *getEstimate() const {
+    const float *getEstimate() const {
 	return m_estimate;
     }
 
@@ -46,7 +46,7 @@
      * Return the pitch distribution for the current estimate.  The
      * returned array has getNoteCount() values.
      */
-    const double *getPitchDistribution() const {
+    const float *getPitchDistribution() const {
 	return m_pitches;
     }
     
@@ -55,40 +55,40 @@
      * returned pointer refers to getSourceCount() arrays of
      * getNoteCount() values.
      */
-    const double *const *getSources() const {
+    const float *const *getSources() const {
 	return m_sources; 
     }
 
 private:
-    double *m_pitches;
-    double **m_shifts;
-    double **m_sources;
+    float *m_pitches;
+    float **m_shifts;
+    float **m_sources;
 
-    double *m_updatePitches;
-    double **m_updateShifts;
-    double **m_updateSources;
+    float *m_updatePitches;
+    float **m_updateShifts;
+    float **m_updateSources;
 
-    double *m_estimate;
-    double *m_q;
+    float *m_estimate;
+    float *m_q;
     
     const int m_noteCount;
     const int m_shiftCount; // 1 + 2 * max template shift
     const int m_binCount;
     const int m_sourceCount;
     
-    const double m_pitchSparsity;
-    const double m_sourceSparsity;
+    const float m_pitchSparsity;
+    const float m_sourceSparsity;
 
     const int m_lowestPitch;
     const int m_highestPitch;
 
-    void normaliseColumn(double *column, int size);
-    void normaliseGrid(double **grid, int size1, int size2);
+    void normaliseColumn(float *column, int size);
+    void normaliseGrid(float **grid, int size1, int size2);
 
-    void expectation(const double *column); // size is m_binCount
-    void maximisation(const double *column); // size is m_binCount
+    void expectation(const float *column); // size is m_binCount
+    void maximisation(const float *column); // size is m_binCount
 
-    const double *templateFor(int instrument, int note, int shift);
+    const float *templateFor(int instrument, int note, int shift);
     void rangeFor(int instrument, int &minPitch, int &maxPitch);
     bool inRange(int instrument, int pitch);
 };
--- a/src/Silvet.cpp	Wed May 14 18:09:06 2014 +0100
+++ b/src/Silvet.cpp	Wed May 14 19:38:36 2014 +0100
@@ -429,7 +429,7 @@
             em.iterate(filtered.at(i).data());
         }
         
-        const double *pitches = em.getPitchDistribution();
+        const float *pitches = em.getPitchDistribution();
 
         //!!! note: check the CQ output (and most immediately, the sum values here) against the MATLAB implementation
         
--- a/yeti/scratch/generateTemplatesC.yeti	Wed May 14 18:09:06 2014 +0100
+++ b/yeti/scratch/generateTemplatesC.yeti	Wed May 14 19:38:36 2014 +0100
@@ -106,7 +106,7 @@
         "    const char *name;",
         "    int lowest;",
         "    int highest;",
-        "    double data[SILVET_TEMPLATE_NOTE_COUNT][SILVET_TEMPLATE_SIZE];",
+        "    float data[SILVET_TEMPLATE_NOTE_COUNT][SILVET_TEMPLATE_SIZE];",
         "} silvet_template_t;",
         "",
         "static int silvet_templates_lowest_note = \(overallLowest);",