You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "modules/audio_processing/vad/gmm.h"
  11. #include <math.h>
  12. namespace webrtc {
  13. static const int kMaxDimension = 10;
  14. static void RemoveMean(const double* in,
  15. const double* mean_vec,
  16. int dimension,
  17. double* out) {
  18. for (int n = 0; n < dimension; ++n)
  19. out[n] = in[n] - mean_vec[n];
  20. }
  21. static double ComputeExponent(const double* in,
  22. const double* covar_inv,
  23. int dimension) {
  24. double q = 0;
  25. for (int i = 0; i < dimension; ++i) {
  26. double v = 0;
  27. for (int j = 0; j < dimension; j++)
  28. v += (*covar_inv++) * in[j];
  29. q += v * in[i];
  30. }
  31. q *= -0.5;
  32. return q;
  33. }
  34. double EvaluateGmm(const double* x, const GmmParameters& gmm_parameters) {
  35. if (gmm_parameters.dimension > kMaxDimension) {
  36. return -1; // This is invalid pdf so the caller can check this.
  37. }
  38. double f = 0;
  39. double v[kMaxDimension];
  40. const double* mean_vec = gmm_parameters.mean;
  41. const double* covar_inv = gmm_parameters.covar_inverse;
  42. for (int n = 0; n < gmm_parameters.num_mixtures; n++) {
  43. RemoveMean(x, mean_vec, gmm_parameters.dimension, v);
  44. double q = ComputeExponent(v, covar_inv, gmm_parameters.dimension) +
  45. gmm_parameters.weight[n];
  46. f += exp(q);
  47. mean_vec += gmm_parameters.dimension;
  48. covar_inv += gmm_parameters.dimension * gmm_parameters.dimension;
  49. }
  50. return f;
  51. }
  52. } // namespace webrtc