- 3.0.2 core module.
GaussianNoise.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <time.h>
9 #include <random>
10 
11 namespace ct {
12 namespace core {
13 
15 
23 {
24 public:
26 
30  GaussianNoise(double mean = 0.0, double standardDeviation = 1.0)
31  : rd_(), eng_(rd_()), distr_(mean, standardDeviation)
32  {
33  }
34 
36 
40  double operator()() { return distr_(eng_); }
42 
46  template <size_t size>
47  Eigen::Matrix<double, size, 1> gen()
48  {
49  Eigen::Matrix<double, size, 1> noise;
50 
51  for (size_t i = 0; i < size; i++)
52  {
53  noise(i) = distr_(eng_);
54  }
55 
56  return noise;
57  }
58 
60  void noisify(double& value) { value += this->operator()(); }
62 
66  template <size_t size>
67  void noisify(Eigen::Matrix<double, size, 1>& value)
68  {
69  value += this->gen<size>();
70  }
71 
72 
73 private:
74  std::random_device rd_;
75  std::mt19937 eng_;
76  std::normal_distribution<> distr_;
77 };
78 
79 
80 } // namespace ct
81 } // namespace core
void noisify(Eigen::Matrix< double, size, 1 > &value)
adds Gaussian noise to a vector
Definition: GaussianNoise.h:67
Gaussian noise generator.
Definition: GaussianNoise.h:22
for i
Eigen::Matrix< double, size, 1 > gen()
Vector generator.
Definition: GaussianNoise.h:47
GaussianNoise(double mean=0.0, double standardDeviation=1.0)
Standard constructor.
Definition: GaussianNoise.h:30
void noisify(double &value)
adds Gaussian noise to a single scalar variable
Definition: GaussianNoise.h:60
double operator()()
Scalar generator.
Definition: GaussianNoise.h:40