annotate src/DistanceMetric.cpp @ 140:cfba9aec7569 refactors

Separate out the raw & conditioned feature outputs (previously only conditioned was available, but we want raw for our tests). Plus some optional debug output
author Chris Cannam
date Thu, 08 Jan 2015 12:11:27 +0000
parents af69db43f5a4
children 7f6f150c1edf 6914a6a01ffc
rev   line source
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@140 25 //#define DEBUG_DISTANCE_METRIC 1
Chris@140 26
Chris@140 27 DistanceMetric::DistanceMetric(DistanceNormalisation norm) :
Chris@140 28 m_norm(norm)
Chris@140 29 {
Chris@140 30 #ifdef DEBUG_DISTANCE_METRIC
Chris@140 31 cerr << "*** DistanceMetric: norm = " << m_norm << endl;
Chris@140 32 #endif
Chris@140 33 }
Chris@140 34
Chris@26 35 double
Chris@26 36 DistanceMetric::calcDistance(const vector<double> &f1,
Chris@26 37 const vector<double> &f2)
Chris@26 38 {
Chris@26 39 double d = 0;
Chris@26 40 double sum = 0;
Chris@26 41
Chris@26 42 int featureSize = f1.size();
Chris@26 43 assert(int(f2.size()) == featureSize);
Chris@26 44
Chris@26 45 for (int i = 0; i < featureSize; i++) {
Chris@26 46 d += fabs(f1[i] - f2[i]);
Chris@116 47 sum += fabs(f1[i]) + fabs(f2[i]);
Chris@26 48 }
Chris@26 49
Chris@26 50 if (sum == 0)
Chris@26 51 return 0;
Chris@26 52 if (m_norm == NormaliseDistanceToSum)
Chris@26 53 return d / sum; // 0 <= d/sum <= 2
Chris@26 54 if (m_norm != NormaliseDistanceToLogSum)
Chris@26 55 return d;
Chris@26 56
Chris@26 57 // note if this were to be restored, it would have to use
Chris@26 58 // totalEnergies vector instead of f1[freqMapSize] which used to
Chris@26 59 // store the total energy:
Chris@26 60 // double weight = (5 + Math.log(f1[freqMapSize] + f2[freqMapSize]))/10.0;
Chris@26 61
Chris@26 62 double weight = (8 + log(sum)) / 10.0;
Chris@133 63
Chris@26 64 if (weight < 0) weight = 0;
Chris@26 65 else if (weight > 1) weight = 1;
Chris@26 66
Chris@26 67 return d / sum * weight;
Chris@26 68 }
Chris@26 69