1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
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"
20 double KLDivergence::distanceGaussian(const vector
<double> &m1
,
21 const vector
<double> &v1
,
22 const vector
<double> &m2
,
23 const vector
<double> &v2
)
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
;
45 double KLDivergence::distanceDistribution(const vector
<double> &d1
,
46 const vector
<double> &d2
,
54 for (int i
= 0; i
< sz
; ++i
) {
55 d
+= d1
[i
] * log10((d1
[i
] + small
) / (d2
[i
] + small
));
59 d
+= distanceDistribution(d2
, d1
, false);