changeset 143:6914a6a01ffc refactors

Transplant the distance metric parameter structure from silence_penalty branch (even though normalisation is currently the only thing in it)
author Chris Cannam
date Fri, 16 Jan 2015 10:18:00 +0000
parents cfba9aec7569
children 254e16f831ee
files src/DistanceMetric.cpp src/DistanceMetric.h src/FeatureConditioner.h src/MatchPipeline.cpp src/MatchPipeline.h src/MatchVampPlugin.cpp src/MatchVampPlugin.h src/Matcher.cpp src/Matcher.h test/TestDistanceMetric.cpp
diffstat 10 files changed, 73 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/DistanceMetric.cpp	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/DistanceMetric.cpp	Fri Jan 16 10:18:00 2015 +0000
@@ -24,11 +24,12 @@
 
 //#define DEBUG_DISTANCE_METRIC 1
 
-DistanceMetric::DistanceMetric(DistanceNormalisation norm) :
-    m_norm(norm)
+DistanceMetric::DistanceMetric(Parameters params) :
+    m_params(params)
 {
 #ifdef DEBUG_DISTANCE_METRIC
-    cerr << "*** DistanceMetric: norm = " << m_norm << endl;
+    cerr << "*** DistanceMetric: norm = " << m_params.norm
+         << endl;
 #endif
 }
 
@@ -37,6 +38,8 @@
 			     const vector<double> &f2)
 {
     double d = 0;
+    double sum1 = 0;
+    double sum2 = 0;
     double sum = 0;
 
     int featureSize = f1.size();
@@ -44,26 +47,41 @@
     
     for (int i = 0; i < featureSize; i++) {
         d += fabs(f1[i] - f2[i]);
-        sum += fabs(f1[i]) + fabs(f2[i]);
+        sum1 += fabs(f1[i]);
+        sum2 += fabs(f2[i]);
     }
 
-    if (sum == 0)
+    sum = sum1 + sum2;
+
+    if (sum == 0) {
         return 0;
-    if (m_norm == NormaliseDistanceToSum)
-        return d / sum; // 0 <= d/sum <= 2
-    if (m_norm != NormaliseDistanceToLogSum)
-        return d;
+    }
 
-    // note if this were to be restored, it would have to use
-    // totalEnergies vector instead of f1[freqMapSize] which used to
-    // store the total energy:
-    //	double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0;
+    double distance = 0;
 
-    double weight = (8 + log(sum)) / 10.0;
+    if (m_params.norm == NormaliseDistanceToSum) {
+
+        distance = d / sum; // 0 <= d/sum <= 2
+
+    } else if (m_params.norm == NormaliseDistanceToLogSum) {
+
+        // note if this were to be restored, it would have to use
+        // totalEnergies vector instead of f1[freqMapSize] which used to
+        // store the total energy:
+        //	double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0;
+
+        double weight = (8 + log(sum)) / 10.0;
     
-    if (weight < 0) weight = 0;
-    else if (weight > 1) weight = 1;
+        if (weight < 0) weight = 0;
+        else if (weight > 1) weight = 1;
 
-    return d / sum * weight;
+        distance = d / sum * weight;
+
+    } else {
+
+        distance = d;
+    }
+    
+    return distance;
 }
 
--- a/src/DistanceMetric.h	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/DistanceMetric.h	Fri Jan 16 10:18:00 2015 +0000
@@ -36,7 +36,17 @@
         NormaliseDistanceToLogSum,
     };
 
-    DistanceMetric(DistanceNormalisation norm);
+    struct Parameters {
+
+        Parameters() :
+            norm(NormaliseDistanceToSum)
+        {}
+
+        /** Normalisation for distance metrics. */
+        DistanceNormalisation norm;
+    };
+    
+    DistanceMetric(Parameters params);
     
     /** Calculates the Manhattan distance between two vectors, with an
      *  optional normalisation by the combined values in the
@@ -52,7 +62,7 @@
 			const std::vector<double> &f2);
     
 private:
-    DistanceNormalisation m_norm;
+    Parameters m_params;
 };
 
 #endif
--- a/src/FeatureConditioner.h	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/FeatureConditioner.h	Fri Jan 16 10:18:00 2015 +0000
@@ -63,7 +63,7 @@
 	Parameters() :
 	    norm(NormaliseToSum1),
 	    order(OutputRectifiedDerivative),
-	    silenceThreshold(0.0),
+	    silenceThreshold(0.01),
 	    decay(0.99)
 	{}
 
--- a/src/MatchPipeline.cpp	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/MatchPipeline.cpp	Fri Jan 16 10:18:00 2015 +0000
@@ -19,13 +19,14 @@
 
 MatchPipeline::MatchPipeline(FeatureExtractor::Parameters feParams,
 			     FeatureConditioner::Parameters fcParams,
+                             DistanceMetric::Parameters dParams,
 			     Matcher::Parameters matchParams) :
     m_fe1(feParams),
     m_fe2(feParams),
     m_fc1(fcParams),
     m_fc2(fcParams),
-    m_pm1(matchParams, 0),
-    m_pm2(matchParams, &m_pm1),
+    m_pm1(matchParams, dParams, 0),
+    m_pm2(matchParams, dParams, &m_pm1),
     m_feeder(&m_pm1, &m_pm2),
     m_lastFrameIn1(0),
     m_lastFrameIn2(0),
--- a/src/MatchPipeline.h	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/MatchPipeline.h	Fri Jan 16 10:18:00 2015 +0000
@@ -38,6 +38,7 @@
      */
     MatchPipeline(FeatureExtractor::Parameters feParams,
 		  FeatureConditioner::Parameters fcParams,
+                  DistanceMetric::Parameters dParams,
 		  Matcher::Parameters matchParams);
 
     ~MatchPipeline();
--- a/src/MatchVampPlugin.cpp	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/MatchVampPlugin.cpp	Fri Jan 16 10:18:00 2015 +0000
@@ -63,7 +63,9 @@
     m_feParams(inputSampleRate, m_blockSize),
     m_defaultFeParams(inputSampleRate, m_blockSize),
     m_fcParams(),
-    m_defaultFcParams()
+    m_defaultFcParams(),
+    m_dParams(),
+    m_defaultDParams()
 {
     if (inputSampleRate < sampleRateMin) {
         std::cerr << "MatchVampPlugin::MatchVampPlugin: input sample rate "
@@ -174,7 +176,7 @@
     desc.description = "Type of normalisation to use for distance metric";
     desc.minValue = 0;
     desc.maxValue = 2;
-    desc.defaultValue = (int)m_defaultParams.distanceNorm;
+    desc.defaultValue = (int)m_defaultDParams.norm;
     desc.isQuantized = true;
     desc.quantizeStep = 1;
     desc.valueNames.clear();
@@ -266,7 +268,7 @@
     } else if (name == "framenorm") {
         return (int)m_fcParams.norm;
     } else if (name == "distnorm") {
-        return (int)m_params.distanceNorm;
+        return (int)m_dParams.norm;
     } else if (name == "usespecdiff") {
         return (int)m_fcParams.order;
     } else if (name == "usechroma") {
@@ -294,7 +296,7 @@
     } else if (name == "framenorm") {
         m_fcParams.norm = (FeatureConditioner::Normalisation)(int(value + 0.1));
     } else if (name == "distnorm") {
-        m_params.distanceNorm = (DistanceMetric::DistanceNormalisation)(int(value + 0.1));
+        m_dParams.norm = (DistanceMetric::DistanceNormalisation)(int(value + 0.1));
     } else if (name == "usespecdiff") {
         m_fcParams.order = (FeatureConditioner::OutputOrder)(int(value + 0.1));
     } else if (name == "usechroma") {
@@ -330,7 +332,7 @@
     m_params.hopTime = m_stepTime;
     m_feParams.fftSize = m_blockSize;
 
-    m_pipeline = new MatchPipeline(m_feParams, m_fcParams, m_params);
+    m_pipeline = new MatchPipeline(m_feParams, m_fcParams, m_dParams, m_params);
 }
 
 bool
--- a/src/MatchVampPlugin.h	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/MatchVampPlugin.h	Fri Jan 16 10:18:00 2015 +0000
@@ -90,6 +90,9 @@
     FeatureConditioner::Parameters m_fcParams;
     FeatureConditioner::Parameters m_defaultFcParams;
 
+    DistanceMetric::Parameters m_dParams;
+    DistanceMetric::Parameters m_defaultDParams;
+
     mutable int m_pathOutNo;
     mutable int m_abOutNo;
     mutable int m_baOutNo;
--- a/src/Matcher.cpp	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/Matcher.cpp	Fri Jan 16 10:18:00 2015 +0000
@@ -25,13 +25,13 @@
 
 //#define DEBUG_MATCHER 1
 
-Matcher::Matcher(Parameters parameters, Matcher *p) :
+Matcher::Matcher(Parameters parameters, DistanceMetric::Parameters dparams,
+                 Matcher *p) :
     m_params(parameters),
-    m_metric(parameters.distanceNorm)
+    m_metric(dparams)
 {
 #ifdef DEBUG_MATCHER
-    cerr << "*** Matcher: distanceNorm = " << parameters.distanceNorm
-         << ", hopTime = " << parameters.hopTime
+    cerr << "*** Matcher: hopTime = " << parameters.hopTime
          << ", blockTime = " << parameters.blockTime
          << ", maxRunCount = " << parameters.maxRunCount
          << ", diagonalWeight = " << parameters.diagonalWeight << endl;
--- a/src/Matcher.h	Thu Jan 08 12:11:27 2015 +0000
+++ b/src/Matcher.h	Fri Jan 16 10:18:00 2015 +0000
@@ -55,16 +55,12 @@
     struct Parameters {
 
         Parameters(double hopTime_) :
-            distanceNorm(DistanceMetric::NormaliseDistanceToLogSum),
             hopTime(hopTime_),
             blockTime(10.0),
             maxRunCount(3),
             diagonalWeight(2.0)
         {}
 
-        /** Type of distance metric normalisation */
-        DistanceMetric::DistanceNormalisation distanceNorm;
-
         /** Spacing of audio frames (determines the amount of overlap or
          *  skip between frames). This value is expressed in
          *  seconds.
@@ -108,7 +104,7 @@
      *  between the two matchers (currently one possesses the distance
      *  matrix and optimal path matrix).
      */
-    Matcher(Parameters parameters, Matcher *p);
+    Matcher(Parameters params, DistanceMetric::Parameters dparams, Matcher *p);
 
     /** Destructor for Matcher.
      */
--- a/test/TestDistanceMetric.cpp	Thu Jan 08 12:11:27 2015 +0000
+++ b/test/TestDistanceMetric.cpp	Fri Jan 16 10:18:00 2015 +0000
@@ -26,7 +26,9 @@
 
 BOOST_AUTO_TEST_CASE(nonorm)
 {
-    DistanceMetric dm(DistanceMetric::NoDistanceNormalisation);
+    DistanceMetric::Parameters params;
+    params.norm = DistanceMetric::NoDistanceNormalisation;
+    DistanceMetric dm(params);
     vector<double>
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),
@@ -42,7 +44,9 @@
 
 BOOST_AUTO_TEST_CASE(sum)
 {
-    DistanceMetric dm(DistanceMetric::NormaliseDistanceToSum);
+    DistanceMetric::Parameters params;
+    params.norm = DistanceMetric::NormaliseDistanceToSum;
+    DistanceMetric dm(params);
     vector<double>
 	e1 = getTestFeature(1),
 	e2 = getTestFeature(2),