changeset 185:a17b22abd551 re-minimise

Code now builds, DistanceMetric tests fail
author Chris Cannam
date Thu, 26 Feb 2015 10:28:23 +0000
parents 6c12db195986
children af6120a32063
files src/DistanceMetric.cpp src/DistanceMetric.h src/FeatureConditioner.cpp src/FeatureConditioner.h src/MatchVampPlugin.cpp test/TestDistanceMetric.cpp test/TestFeatureConditioner.cpp test/TestFeatureExtractor.cpp
diffstat 8 files changed, 59 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/src/DistanceMetric.cpp	Thu Feb 26 09:55:28 2015 +0000
+++ b/src/DistanceMetric.cpp	Thu Feb 26 10:28:23 2015 +0000
@@ -24,6 +24,24 @@
 
 //#define DEBUG_DISTANCE_METRIC 1
 
+template <> uint8_t
+DistanceMetric::scaleIntoRange(double distance)
+{
+    return uint8_t(m_params.scale * distance);
+}
+
+template <> float
+DistanceMetric::scaleIntoRange(double distance)
+{
+    return float(distance);
+}
+
+template <> double
+DistanceMetric::scaleIntoRange(double distance)
+{
+    return distance;
+}
+
 DistanceMetric::DistanceMetric(Parameters params) :
     m_params(params)
 {
@@ -61,7 +79,7 @@
         }
         if (d > 1.0) d = 1.0;
         
-        return scaleIntoRange(d); // normalisation param ignored
+        return scaleIntoRange<distance_t>(d); // normalisation param ignored
     }
 
     if (m_params.metric == Manhattan) {
@@ -85,7 +103,7 @@
     }
     
     if (sum == 0) {
-        return scaleIntoRange(0);
+        return scaleIntoRange<distance_t>(0);
     }
 
     double distance = 0;
@@ -113,15 +131,5 @@
         distance = d;
     }
     
-    return scaleIntoRange(distance);
+    return scaleIntoRange<distance_t>(distance);
 }
-
-distance_t
-DistanceMetric::scaleIntoRange(double distance)
-{
-    if (sizeof(distance_t) == sizeof(distance)) {
-        return distance_t(distance);
-    } else {
-        return distance_t(m_params.scale * distance);
-    }
-}
--- a/src/DistanceMetric.h	Thu Feb 26 09:55:28 2015 +0000
+++ b/src/DistanceMetric.h	Thu Feb 26 10:28:23 2015 +0000
@@ -94,7 +94,8 @@
     
 private:
     Parameters m_params;
-    distance_t scaleIntoRange(double distance);
+
+    template <typename T> T scaleIntoRange(double);
 };
 
 #endif
--- a/src/FeatureConditioner.cpp	Thu Feb 26 09:55:28 2015 +0000
+++ b/src/FeatureConditioner.cpp	Thu Feb 26 10:28:23 2015 +0000
@@ -34,8 +34,8 @@
 #endif
 }
 
-vector<double>
-FeatureConditioner::process(const vector<double> &feature)
+feature_t
+FeatureConditioner::process(const feature_t &feature)
 {
     if (m_prev.empty()) {
 	m_prev.resize(feature.size(), 0.0);
@@ -49,7 +49,7 @@
 
     int size = static_cast<int>(feature.size());
     
-    vector<double> out(size, 0.0);
+    feature_t out(size, 0.0);
 
     double totalEnergy = 0;
 
@@ -94,11 +94,11 @@
 	}
     } else if (m_params.norm == NormaliseToSum1) {
         for (int i = 0; i < size; i++) { 
-            out[i] /= totalEnergy;
+            out[i] = featurebin_t(out[i] / totalEnergy);
 	}
     } else if (m_params.norm == NormaliseToLTAverage) {
         for (int i = 0; i < size; i++) {
-            out[i] /= m_ltAverage;
+            out[i] = featurebin_t(out[i] / m_ltAverage);
 	}
     }
 
--- a/src/FeatureConditioner.h	Thu Feb 26 09:55:28 2015 +0000
+++ b/src/FeatureConditioner.h	Thu Feb 26 10:28:23 2015 +0000
@@ -16,7 +16,7 @@
 #ifndef FEATURE_CONDITIONER_H
 #define FEATURE_CONDITIONER_H
 
-#include <vector>
+#include "Types.h"
 
 /**
  * Take a series of feature vectors and apply conditioning of some
@@ -96,7 +96,7 @@
     /**
      * Process the given feature and return the conditioned feature.
      */
-    std::vector<double> process(const std::vector<double> &feature);
+    feature_t process(const feature_t &feature);
 
 protected:
     Parameters m_params;
@@ -106,7 +106,7 @@
 
     /** The most recent feature, used for calculating the feature to
      *  feature difference. This is therefore not yet normalised. */
-    std::vector<double> m_prev;
+    feature_t m_prev;
 };
 
 #endif
--- a/src/MatchVampPlugin.cpp	Thu Feb 26 09:55:28 2015 +0000
+++ b/src/MatchVampPlugin.cpp	Thu Feb 26 10:28:23 2015 +0000
@@ -637,10 +637,10 @@
 
     FeatureSet returnFeatures;
 
-    vector<double> f1, f2;
+    feature_t f1, f2;
     m_pipeline->extractFeatures(f1, f2);
 
-    vector<double> cf1, cf2;
+    feature_t cf1, cf2;
     m_pipeline->extractConditionedFeatures(cf1, cf2);
 
     Feature f;
--- a/test/TestDistanceMetric.cpp	Thu Feb 26 09:55:28 2015 +0000
+++ b/test/TestDistanceMetric.cpp	Thu Feb 26 10:28:23 2015 +0000
@@ -12,12 +12,12 @@
 
 #include <boost/test/unit_test.hpp>
 
-static vector<double> getTestFeature(double m)
+static feature_t getTestFeature(double m)
 {
-    vector<double> f;
-    double fd[] = { 0, 1, 2, 3 };
+    feature_t f;
+    int fd[] = { 0, 1, 2, 3 };
     for (int i = 0; i < 4; ++i) {
-	f.push_back(fd[i] * m);
+	f.push_back(featurebin_t(fd[i] * m));
     }
     return f;
 }
@@ -29,7 +29,7 @@
     DistanceMetric::Parameters params;
     params.norm = DistanceMetric::NoDistanceNormalisation;
     DistanceMetric dm(params);
-    vector<double>
+    feature_t
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),
 	e0 = getTestFeature(0);
@@ -49,7 +49,7 @@
     DistanceMetric::Parameters params;
     params.norm = DistanceMetric::NormaliseDistanceToSum;
     DistanceMetric dm(params);
-    vector<double>
+    feature_t
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),
 	e0 = getTestFeature(0);
--- a/test/TestFeatureConditioner.cpp	Thu Feb 26 09:55:28 2015 +0000
+++ b/test/TestFeatureConditioner.cpp	Thu Feb 26 10:28:23 2015 +0000
@@ -12,12 +12,12 @@
 
 #include <boost/test/unit_test.hpp>
 
-static vector<double> getTestFeature(double m)
+static feature_t getTestFeature(double m)
 {
-    vector<double> f;
-    double fd[] = { 0, 1, 2, 3 };
+    feature_t f;
+    int fd[] = { 0, 1, 2, 3 };
     for (int i = 0; i < 4; ++i) {
-	f.push_back(fd[i] * m);
+	f.push_back(featurebin_t(fd[i] * m));
     }
     return f;
 }
@@ -29,14 +29,14 @@
     FeatureConditioner::Parameters params;
     params.norm = FeatureConditioner::NoNormalisation;
     params.order = FeatureConditioner::OutputFeatures;
-    vector<double>
+    feature_t
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),
 	e0 = getTestFeature(0);
 
     params.silenceThreshold = 1.0;
     FeatureConditioner fc(params);
-    vector<double> out = fc.process(e1);
+    feature_t out = fc.process(e1);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), e1.begin(), e1.end());
     out = fc.process(e2);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), e2.begin(), e2.end());
@@ -54,14 +54,14 @@
     FeatureConditioner::Parameters params;
     params.norm = FeatureConditioner::NoNormalisation;
     params.order = FeatureConditioner::OutputRectifiedDerivative;
-    vector<double>
+    feature_t
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),
 	e0 = getTestFeature(0);
 
     params.silenceThreshold = 1.0;
     FeatureConditioner fc(params);
-    vector<double> out = fc.process(e1);
+    feature_t out = fc.process(e1);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), e1.begin(), e1.end());
     out = fc.process(e2);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), e1.begin(), e1.end());
@@ -85,7 +85,7 @@
     FeatureConditioner::Parameters params;
     params.norm = FeatureConditioner::NoNormalisation;
     params.order = FeatureConditioner::OutputDerivative;
-    vector<double>
+    feature_t
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),
 	e3 = getTestFeature(3),
@@ -93,7 +93,7 @@
 
     params.silenceThreshold = 1.0;
     FeatureConditioner fc(params);
-    vector<double> out = fc.process(e1);
+    feature_t out = fc.process(e1);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), e1.begin(), e1.end());
     out = fc.process(e2);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), e1.begin(), e1.end());
@@ -117,7 +117,7 @@
     FeatureConditioner::Parameters params;
     params.norm = FeatureConditioner::NormaliseToSum1;
     params.order = FeatureConditioner::OutputFeatures;
-    vector<double>
+    feature_t
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),
 	en = getTestFeature(1.0/6.0),
@@ -125,7 +125,7 @@
 
     params.silenceThreshold = 1.0;
     FeatureConditioner fc(params);
-    vector<double> out = fc.process(e1);
+    feature_t out = fc.process(e1);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), en.begin(), en.end());
     out = fc.process(e2);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), en.begin(), en.end());
@@ -143,7 +143,7 @@
     FeatureConditioner::Parameters params;
     params.norm = FeatureConditioner::NormaliseToSum1;
     params.order = FeatureConditioner::OutputRectifiedDerivative;
-    vector<double>
+    feature_t
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),
 	en = getTestFeature(1.0/6.0),
@@ -152,7 +152,7 @@
 
     params.silenceThreshold = 1.0;
     FeatureConditioner fc(params);
-    vector<double> out = fc.process(e1);
+    feature_t out = fc.process(e1);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), en.begin(), en.end());
     out = fc.process(e2);
     BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), en2.begin(), en2.end());
--- a/test/TestFeatureExtractor.cpp	Thu Feb 26 09:55:28 2015 +0000
+++ b/test/TestFeatureExtractor.cpp	Thu Feb 26 10:28:23 2015 +0000
@@ -35,7 +35,7 @@
 
 BOOST_AUTO_TEST_SUITE(TestFeatureExtractor)
 
-void checkAlternateProcessType(FeatureExtractor &fe, vector<double> expected,
+void checkAlternateProcessType(FeatureExtractor &fe, feature_t expected,
 			       vector<double> real, vector<double> imag)
 {
     vector<float> in;
@@ -43,7 +43,7 @@
 	in.push_back(float(real[i]));
 	in.push_back(float(imag[i]));
     }
-    vector<double> alt = fe.process(&in[0]);
+    feature_t alt = fe.process(&in[0]);
     BOOST_CHECK_EQUAL_COLLECTIONS(alt.begin(), alt.end(),
 				  expected.begin(), expected.end());
 }
@@ -83,7 +83,7 @@
 		real[hs-bin-1] += 5.0;
 		imag[hs-bin-1] += 5.0;
 	
-		vector<double> out = fe.process(real, imag);
+		feature_t out = fe.process(real, imag);
 
 		checkAlternateProcessType(fe, out, real, imag);
 
@@ -107,7 +107,7 @@
 
 		double cutoff = (17.0 * rate) / sz;
 		
-		vector<double> expected(fsz);
+		feature_t expected(fsz);
 
 		double infreq1 = (double(bin) * rate) / sz;
 
@@ -159,7 +159,7 @@
 	real[hs-bin-1] += 5.0;
 	imag[hs-bin-1] += 5.0;
 
-	vector<double> out = fe.process(real, imag);
+	feature_t out = fe.process(real, imag);
 
 	checkAlternateProcessType(fe, out, real, imag);
 
@@ -187,7 +187,7 @@
 	// mapped linearly by MIDI pitch into the next 49 bins;
 	// everything above goes into the last bin, for 84 bins total.
 
-	vector<double> expected(fsz);
+	feature_t expected(fsz);
 
 	if (bin == hs-bin-1) {
 	    expected[bin2warped(bin, rate, sz)] += 450;