annotate maths/KLDivergence.cpp @ 298:255e431ae3d4

* Key detector: when returning key strengths, use the peak value of the three underlying chromagram correlations (from 36-bin chromagram) corresponding to each key, instead of the mean. Rationale: This is the same method as used when returning the key value, and it's nice to have the same results in both returned value and plot. The peak performed better than the sum with a simple test set of triads, so it seems reasonable to change the plot to match the key output rather than the other way around. * FFT: kiss_fftr returns only the non-conjugate bins, synthesise the rest rather than leaving them (perhaps dangerously) undefined. Fixes an uninitialised data error in chromagram that could cause garbage results from key detector. * Constant Q: remove precalculated values again, I reckon they're not proving such a good tradeoff.
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 05 Jun 2009 15:12:39 +0000
parents f49be56d3c4e
children 769da847732b
rev   line source
c@256 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@256 2
c@256 3 /*
c@256 4 QM DSP Library
c@256 5
c@256 6 Centre for Digital Music, Queen Mary, University of London.
c@256 7 This file copyright 2008 QMUL
c@256 8 All rights reserved.
c@256 9 */
c@256 10
c@256 11 #include "KLDivergence.h"
c@256 12
c@258 13 #include <cmath>
c@258 14
c@258 15 double KLDivergence::distanceGaussian(const vector<double> &m1,
c@258 16 const vector<double> &v1,
c@258 17 const vector<double> &m2,
c@258 18 const vector<double> &v2)
c@256 19 {
c@256 20 int sz = m1.size();
c@256 21
c@256 22 double d = -2.0 * sz;
c@256 23
c@256 24 for (int k = 0; k < sz; ++k) {
c@256 25 d += v1[k] / v2[k] + v2[k] / v1[k];
c@256 26 d += (m1[k] - m2[k]) * (1.0 / v1[k] + 1.0 / v2[k]) * (m1[k] - m2[k]);
c@256 27 }
c@256 28
c@256 29 d /= 2.0;
c@256 30
c@256 31 return d;
c@256 32 }
c@258 33
c@258 34 double KLDivergence::distanceDistribution(const vector<double> &d1,
c@258 35 const vector<double> &d2,
c@258 36 bool symmetrised)
c@258 37 {
c@258 38 int sz = d1.size();
c@258 39
c@258 40 double d = 0;
c@258 41 double small = 1e-20;
c@258 42
c@258 43 for (int i = 0; i < sz; ++i) {
c@258 44 d += d1[i] * log10((d1[i] + small) / (d2[i] + small));
c@258 45 }
c@258 46
c@258 47 if (symmetrised) {
c@258 48 d += distanceDistribution(d2, d1, false);
c@258 49 }
c@258 50
c@258 51 return d;
c@258 52 }
c@258 53