Chris@26
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@26
|
2
|
Chris@26
|
3 /*
|
Chris@26
|
4 Vamp feature extraction plugin using the MATCH audio alignment
|
Chris@26
|
5 algorithm.
|
Chris@26
|
6
|
Chris@26
|
7 Centre for Digital Music, Queen Mary, University of London.
|
Chris@26
|
8 This file copyright 2007 Simon Dixon, Chris Cannam and QMUL.
|
Chris@26
|
9
|
Chris@26
|
10 This program is free software; you can redistribute it and/or
|
Chris@26
|
11 modify it under the terms of the GNU General Public License as
|
Chris@26
|
12 published by the Free Software Foundation; either version 2 of the
|
Chris@26
|
13 License, or (at your option) any later version. See the file
|
Chris@26
|
14 COPYING included with this distribution for more information.
|
Chris@26
|
15 */
|
Chris@26
|
16
|
Chris@26
|
17 #include "DistanceMetric.h"
|
Chris@26
|
18
|
Chris@26
|
19 #include <cassert>
|
Chris@26
|
20 #include <cmath>
|
Chris@133
|
21 #include <iostream>
|
Chris@26
|
22
|
Chris@133
|
23 using namespace std;
|
Chris@26
|
24
|
Chris@26
|
25 double
|
Chris@26
|
26 DistanceMetric::calcDistance(const vector<double> &f1,
|
Chris@26
|
27 const vector<double> &f2)
|
Chris@26
|
28 {
|
Chris@26
|
29 double d = 0;
|
Chris@26
|
30 double sum = 0;
|
Chris@26
|
31
|
Chris@26
|
32 int featureSize = f1.size();
|
Chris@26
|
33 assert(int(f2.size()) == featureSize);
|
Chris@26
|
34
|
Chris@26
|
35 for (int i = 0; i < featureSize; i++) {
|
Chris@26
|
36 d += fabs(f1[i] - f2[i]);
|
Chris@116
|
37 sum += fabs(f1[i]) + fabs(f2[i]);
|
Chris@26
|
38 }
|
Chris@26
|
39
|
Chris@26
|
40 if (sum == 0)
|
Chris@26
|
41 return 0;
|
Chris@26
|
42 if (m_norm == NormaliseDistanceToSum)
|
Chris@26
|
43 return d / sum; // 0 <= d/sum <= 2
|
Chris@26
|
44 if (m_norm != NormaliseDistanceToLogSum)
|
Chris@26
|
45 return d;
|
Chris@26
|
46
|
Chris@26
|
47 // note if this were to be restored, it would have to use
|
Chris@26
|
48 // totalEnergies vector instead of f1[freqMapSize] which used to
|
Chris@26
|
49 // store the total energy:
|
Chris@26
|
50 // double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0;
|
Chris@26
|
51
|
Chris@26
|
52 double weight = (8 + log(sum)) / 10.0;
|
Chris@133
|
53
|
Chris@26
|
54 if (weight < 0) weight = 0;
|
Chris@26
|
55 else if (weight > 1) weight = 1;
|
Chris@26
|
56
|
Chris@26
|
57 return d / sum * weight;
|
Chris@26
|
58 }
|
Chris@26
|
59
|