GMM.h
Go to the documentation of this file.
1 
18 #pragma once
19 
20 #include <boost/random.hpp>
21 #include <time.h>
23 #include <Eigen/Core>
24 
25 namespace ASR
26 {
27 
32 class GMM
33 {
34 private:
35  std::vector<ASR::MultidimensionalGaussianPtr> models;
36  std::vector<float> weights;
37  std::vector<float> histogramm;
38  boost::mt19937 rng;
39 
40 
47  float gen_random_float(float min, float max)
48  {
49  boost::uniform_real<float> u(min, max);
50  boost::variate_generator<boost::mt19937&, boost::uniform_real<float> > gen(rng, u);
51  return gen();
52  }
53 
54 public:
55  GMM()
56  {
57  // Do some initialzations
58  rng.seed(time(0));
59  histogramm.push_back(0.0f);
60  }
61 
68  void addGaussianModel(float weight, std::vector<float> mean, MatrixXf covariance)
69  {
70  //ROS_INFO("Adding gaussian to gmm. weight=%.2f", weight);
71 
72  weights.push_back(weight);
74  histogramm.push_back(histogramm.at(histogramm.size()-1) + weight );
75  }
76 
77 
86  void sampleRandomValues(std::vector<float> &x)
87  {
88  float sum = histogramm.at(histogramm.size()-1);
89 
90  if (sum < .99f || sum > 1.01f)
91  {
92  ROS_INFO("Sum of the gaussian weights not 1. Please normailze the weight of the distibutions!");
93  return;
94  }
95 
96  // Get one vector of samples
97  // The models are sampled with the stored weights
98  // Get xi € [0,1)
99  float xi = gen_random_float(0,1);
100 
101  unsigned int index = 0;
102 
103  // find the index in the histogeramm at which data > xi
104  while (histogramm.at(index) < xi)
105  {
106  index++;
107  }
108 
109  // Decrease index by one to get the last element which is smaller than xi
110  index--;
111 
112  //ROS_INFO("Sampling from model at(%i), xi=%.2f", index, xi);
113 
114  if(index > models.size()) index = models.size();
115 
116  // Now sample from the gaussian at counter
117  models.at(index)->sampleRandomValues(x);
118  }
119 
126  {
127  ROS_ASSERT(index < models.size());
128  return models.at(index);
129  }
130 };
131 
133 
134 }
f
GMM()
Definition: GMM.h:55
Definition: GMM.h:32
std::vector< ASR::MultidimensionalGaussianPtr > models
Definition: GMM.h:35
boost::mt19937 rng
Definition: GMM.h:38
std::vector< float > weights
Definition: GMM.h:36
#define ROS_INFO(...)
std::vector< float > histogramm
Definition: GMM.h:37
float gen_random_float(float min, float max)
Definition: GMM.h:47
boost::shared_ptr< GMM > GMM_Ptr
Definition: GMM.h:132
ASR::MultidimensionalGaussianPtr getModel(unsigned int index)
Definition: GMM.h:125
#define ROS_ASSERT(cond)
void sampleRandomValues(std::vector< float > &x)
Definition: GMM.h:86
void addGaussianModel(float weight, std::vector< float > mean, MatrixXf covariance)
Definition: GMM.h:68


asr_recognizer_prediction_psm
Author(s): Braun Kai, Meißner Pascal
autogenerated on Wed Feb 19 2020 03:31:28