pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "modules/audio_processing/vad/gmm.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 12 | |
| 13 | #include <math.h> |
| 14 | #include <stdlib.h> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 16 | #include "typedefs.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | static const int kMaxDimension = 10; |
| 21 | |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 22 | static void RemoveMean(const double* in, |
| 23 | const double* mean_vec, |
| 24 | int dimension, |
| 25 | double* out) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 26 | for (int n = 0; n < dimension; ++n) |
| 27 | out[n] = in[n] - mean_vec[n]; |
| 28 | } |
| 29 | |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 30 | static double ComputeExponent(const double* in, |
| 31 | const double* covar_inv, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 32 | int dimension) { |
| 33 | double q = 0; |
| 34 | for (int i = 0; i < dimension; ++i) { |
| 35 | double v = 0; |
| 36 | for (int j = 0; j < dimension; j++) |
| 37 | v += (*covar_inv++) * in[j]; |
| 38 | q += v * in[i]; |
| 39 | } |
| 40 | q *= -0.5; |
| 41 | return q; |
| 42 | } |
| 43 | |
| 44 | double EvaluateGmm(const double* x, const GmmParameters& gmm_parameters) { |
| 45 | if (gmm_parameters.dimension > kMaxDimension) { |
| 46 | return -1; // This is invalid pdf so the caller can check this. |
| 47 | } |
| 48 | double f = 0; |
| 49 | double v[kMaxDimension]; |
| 50 | const double* mean_vec = gmm_parameters.mean; |
| 51 | const double* covar_inv = gmm_parameters.covar_inverse; |
| 52 | |
| 53 | for (int n = 0; n < gmm_parameters.num_mixtures; n++) { |
| 54 | RemoveMean(x, mean_vec, gmm_parameters.dimension, v); |
| 55 | double q = ComputeExponent(v, covar_inv, gmm_parameters.dimension) + |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 56 | gmm_parameters.weight[n]; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 57 | f += exp(q); |
| 58 | mean_vec += gmm_parameters.dimension; |
| 59 | covar_inv += gmm_parameters.dimension * gmm_parameters.dimension; |
| 60 | } |
| 61 | return f; |
| 62 | } |
| 63 | |
| 64 | } // namespace webrtc |