blob: 3b8764c4d02a9bb31733aae1ac112119d2ce56a4 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/vad/gmm.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000012
13#include <math.h>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000014
pbos@webrtc.org788acd12014-12-15 09:41:24 +000015namespace webrtc {
16
17static const int kMaxDimension = 10;
18
aluebsecf6b812015-06-25 12:28:48 -070019static void RemoveMean(const double* in,
20 const double* mean_vec,
21 int dimension,
22 double* out) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000023 for (int n = 0; n < dimension; ++n)
24 out[n] = in[n] - mean_vec[n];
25}
26
aluebsecf6b812015-06-25 12:28:48 -070027static double ComputeExponent(const double* in,
28 const double* covar_inv,
pbos@webrtc.org788acd12014-12-15 09:41:24 +000029 int dimension) {
30 double q = 0;
31 for (int i = 0; i < dimension; ++i) {
32 double v = 0;
33 for (int j = 0; j < dimension; j++)
34 v += (*covar_inv++) * in[j];
35 q += v * in[i];
36 }
37 q *= -0.5;
38 return q;
39}
40
41double EvaluateGmm(const double* x, const GmmParameters& gmm_parameters) {
42 if (gmm_parameters.dimension > kMaxDimension) {
43 return -1; // This is invalid pdf so the caller can check this.
44 }
45 double f = 0;
46 double v[kMaxDimension];
47 const double* mean_vec = gmm_parameters.mean;
48 const double* covar_inv = gmm_parameters.covar_inverse;
49
50 for (int n = 0; n < gmm_parameters.num_mixtures; n++) {
51 RemoveMean(x, mean_vec, gmm_parameters.dimension, v);
52 double q = ComputeExponent(v, covar_inv, gmm_parameters.dimension) +
aluebsecf6b812015-06-25 12:28:48 -070053 gmm_parameters.weight[n];
pbos@webrtc.org788acd12014-12-15 09:41:24 +000054 f += exp(q);
55 mean_vec += gmm_parameters.dimension;
56 covar_inv += gmm_parameters.dimension * gmm_parameters.dimension;
57 }
58 return f;
59}
60
61} // namespace webrtc