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@26
|
21
|
Chris@26
|
22 using std::vector;
|
Chris@26
|
23
|
Chris@26
|
24 double
|
Chris@26
|
25 DistanceMetric::calcDistance(const vector<double> &f1,
|
Chris@26
|
26 const vector<double> &f2)
|
Chris@26
|
27 {
|
Chris@26
|
28 double d = 0;
|
Chris@26
|
29 double sum = 0;
|
Chris@26
|
30
|
Chris@26
|
31 int featureSize = f1.size();
|
Chris@26
|
32 assert(int(f2.size()) == featureSize);
|
Chris@26
|
33
|
Chris@26
|
34 for (int i = 0; i < featureSize; i++) {
|
Chris@26
|
35 d += fabs(f1[i] - f2[i]);
|
Chris@26
|
36 sum += f1[i] + f2[i];
|
Chris@26
|
37 }
|
Chris@26
|
38
|
Chris@26
|
39 if (sum == 0)
|
Chris@26
|
40 return 0;
|
Chris@26
|
41 if (m_norm == NormaliseDistanceToSum)
|
Chris@26
|
42 return d / sum; // 0 <= d/sum <= 2
|
Chris@26
|
43 if (m_norm != NormaliseDistanceToLogSum)
|
Chris@26
|
44 return d;
|
Chris@26
|
45
|
Chris@26
|
46 // note if this were to be restored, it would have to use
|
Chris@26
|
47 // totalEnergies vector instead of f1[freqMapSize] which used to
|
Chris@26
|
48 // store the total energy:
|
Chris@26
|
49 // double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0;
|
Chris@26
|
50
|
Chris@26
|
51 double weight = (8 + log(sum)) / 10.0;
|
Chris@26
|
52
|
Chris@26
|
53 if (weight < 0) weight = 0;
|
Chris@26
|
54 else if (weight > 1) weight = 1;
|
Chris@26
|
55
|
Chris@26
|
56 return d / sum * weight;
|
Chris@26
|
57 }
|
Chris@26
|
58
|