blob: cd8a1a8a375c7edb89a85302ca0b75771e688ab3 [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>
14#include <stdlib.h>
15
pbos@webrtc.org788acd12014-12-15 09:41:24 +000016namespace webrtc {
17
18static const int kMaxDimension = 10;
19
aluebsecf6b812015-06-25 12:28:48 -070020static void RemoveMean(const double* in,
21 const double* mean_vec,
22 int dimension,
23 double* out) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000024 for (int n = 0; n < dimension; ++n)
25 out[n] = in[n] - mean_vec[n];
26}
27
aluebsecf6b812015-06-25 12:28:48 -070028static double ComputeExponent(const double* in,
29 const double* covar_inv,
pbos@webrtc.org788acd12014-12-15 09:41:24 +000030 int dimension) {
31 double q = 0;
32 for (int i = 0; i < dimension; ++i) {
33 double v = 0;
34 for (int j = 0; j < dimension; j++)
35 v += (*covar_inv++) * in[j];
36 q += v * in[i];
37 }
38 q *= -0.5;
39 return q;
40}
41
42double EvaluateGmm(const double* x, const GmmParameters& gmm_parameters) {
43 if (gmm_parameters.dimension > kMaxDimension) {
44 return -1; // This is invalid pdf so the caller can check this.
45 }
46 double f = 0;
47 double v[kMaxDimension];
48 const double* mean_vec = gmm_parameters.mean;
49 const double* covar_inv = gmm_parameters.covar_inverse;
50
51 for (int n = 0; n < gmm_parameters.num_mixtures; n++) {
52 RemoveMean(x, mean_vec, gmm_parameters.dimension, v);
53 double q = ComputeExponent(v, covar_inv, gmm_parameters.dimension) +
aluebsecf6b812015-06-25 12:28:48 -070054 gmm_parameters.weight[n];
pbos@webrtc.org788acd12014-12-15 09:41:24 +000055 f += exp(q);
56 mean_vec += gmm_parameters.dimension;
57 covar_inv += gmm_parameters.dimension * gmm_parameters.dimension;
58 }
59 return f;
60}
61
62} // namespace webrtc