- 3.0.2 core module.
UniformNoise.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 
21 {
22 public:
24 
28  UniformNoise(const double mean = 0.0, const double r = 1.0) : rd_(), eng_(rd_()), distr_(mean - r, mean + r) {}
30 
34  double operator()() { return distr_(eng_); }
36 
40  template <size_t size>
41  Eigen::Matrix<double, size, 1> gen()
42  {
43  Eigen::Matrix<double, size, 1> noise;
44 
45  for (size_t i = 0; i < size; i++)
46  {
47  noise(i) = distr_(eng_);
48  }
49 
50  return noise;
51  }
52 
54  void noisify(double& value) { value += this->operator()(); }
56 
60  template <size_t size>
61  void noisify(Eigen::Matrix<double, size, 1>& value)
62  {
63  value += this->gen<size>();
64  }
65 
66 
67 private:
68  std::random_device rd_;
69  std::mt19937 eng_;
70  std::uniform_real_distribution<> distr_;
71 };
72 
73 
74 } // namespace ct
75 } // namespace core
UniformNoise(const double mean=0.0, const double r=1.0)
Standard constructor.
Definition: UniformNoise.h:28
void noisify(Eigen::Matrix< double, size, 1 > &value)
adds uniform noise to a vector
Definition: UniformNoise.h:61
double operator()()
Scalar generator.
Definition: UniformNoise.h:34
for i
Uniform noise generator.
Definition: UniformNoise.h:20
Eigen::Matrix< double, size, 1 > gen()
Vector generator.
Definition: UniformNoise.h:41
void noisify(double &value)
adds uniform noise to a single scalar variable
Definition: UniformNoise.h:54