add queen mary DSP library
[ardour2.git] / libs / qm-dsp / maths / KLDivergence.cpp
blob3c3cb134ca02505dd0a987f23e42b38ef8c3294e
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3 /*
4 QM DSP Library
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2008 QMUL
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
16 #include "KLDivergence.h"
18 #include <cmath>
20 double KLDivergence::distanceGaussian(const vector<double> &m1,
21 const vector<double> &v1,
22 const vector<double> &m2,
23 const vector<double> &v2)
25 int sz = m1.size();
27 double d = -2.0 * sz;
28 double small = 1e-20;
30 for (int k = 0; k < sz; ++k) {
32 double kv1 = v1[k] + small;
33 double kv2 = v2[k] + small;
34 double km = (m1[k] - m2[k]) + small;
36 d += kv1 / kv2 + kv2 / kv1;
37 d += km * (1.0 / kv1 + 1.0 / kv2) * km;
40 d /= 2.0;
42 return d;
45 double KLDivergence::distanceDistribution(const vector<double> &d1,
46 const vector<double> &d2,
47 bool symmetrised)
49 int sz = d1.size();
51 double d = 0;
52 double small = 1e-20;
54 for (int i = 0; i < sz; ++i) {
55 d += d1[i] * log10((d1[i] + small) / (d2[i] + small));
58 if (symmetrised) {
59 d += distanceDistribution(d2, d1, false);
62 return d;