comparison src/DistanceMetric.cpp @ 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 ecfb4ada171b
comparison
equal deleted inserted replaced
140:cfba9aec7569 143:6914a6a01ffc
22 22
23 using namespace std; 23 using namespace std;
24 24
25 //#define DEBUG_DISTANCE_METRIC 1 25 //#define DEBUG_DISTANCE_METRIC 1
26 26
27 DistanceMetric::DistanceMetric(DistanceNormalisation norm) : 27 DistanceMetric::DistanceMetric(Parameters params) :
28 m_norm(norm) 28 m_params(params)
29 { 29 {
30 #ifdef DEBUG_DISTANCE_METRIC 30 #ifdef DEBUG_DISTANCE_METRIC
31 cerr << "*** DistanceMetric: norm = " << m_norm << endl; 31 cerr << "*** DistanceMetric: norm = " << m_params.norm
32 << endl;
32 #endif 33 #endif
33 } 34 }
34 35
35 double 36 double
36 DistanceMetric::calcDistance(const vector<double> &f1, 37 DistanceMetric::calcDistance(const vector<double> &f1,
37 const vector<double> &f2) 38 const vector<double> &f2)
38 { 39 {
39 double d = 0; 40 double d = 0;
41 double sum1 = 0;
42 double sum2 = 0;
40 double sum = 0; 43 double sum = 0;
41 44
42 int featureSize = f1.size(); 45 int featureSize = f1.size();
43 assert(int(f2.size()) == featureSize); 46 assert(int(f2.size()) == featureSize);
44 47
45 for (int i = 0; i < featureSize; i++) { 48 for (int i = 0; i < featureSize; i++) {
46 d += fabs(f1[i] - f2[i]); 49 d += fabs(f1[i] - f2[i]);
47 sum += fabs(f1[i]) + fabs(f2[i]); 50 sum1 += fabs(f1[i]);
51 sum2 += fabs(f2[i]);
48 } 52 }
49 53
50 if (sum == 0) 54 sum = sum1 + sum2;
55
56 if (sum == 0) {
51 return 0; 57 return 0;
52 if (m_norm == NormaliseDistanceToSum) 58 }
53 return d / sum; // 0 <= d/sum <= 2
54 if (m_norm != NormaliseDistanceToLogSum)
55 return d;
56 59
57 // note if this were to be restored, it would have to use 60 double distance = 0;
58 // totalEnergies vector instead of f1[freqMapSize] which used to
59 // store the total energy:
60 // double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0;
61 61
62 double weight = (8 + log(sum)) / 10.0; 62 if (m_params.norm == NormaliseDistanceToSum) {
63
64 distance = d / sum; // 0 <= d/sum <= 2
65
66 } else if (m_params.norm == NormaliseDistanceToLogSum) {
67
68 // note if this were to be restored, it would have to use
69 // totalEnergies vector instead of f1[freqMapSize] which used to
70 // store the total energy:
71 // double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0;
72
73 double weight = (8 + log(sum)) / 10.0;
63 74
64 if (weight < 0) weight = 0; 75 if (weight < 0) weight = 0;
65 else if (weight > 1) weight = 1; 76 else if (weight > 1) weight = 1;
66 77
67 return d / sum * weight; 78 distance = d / sum * weight;
79
80 } else {
81
82 distance = d;
83 }
84
85 return distance;
68 } 86 }
69 87