- 3.0.2 core module.
QuantizationNoise.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 namespace ct {
9 namespace core {
10 
12 
18 {
19 public:
22  {
25  };
26 
28 
33  QuantizationNoise(double bias = 0.0, double quantizationInterval = 1.0, QuantizationMethod method = FLOOR)
34  : quantizationInterval_(quantizationInterval),
35  quantizationRest_(1, 0.0),
36  bias_(bias),
37  quantizationMethod_(method)
38  {
39  }
40 
42 
49  void noisify(double& value, size_t index = 0)
50  {
51  if (index >= quantizationRest_.size())
52  quantizationRest_.resize(index, 0);
53 
54  double currentValue = value + quantizationRest_[index];
55  double nQuantsDouble = currentValue / quantizationInterval_;
56  int nQuants;
57 
58  switch (quantizationMethod_)
59  {
60  case FLOOR:
61  {
62  nQuants = std::floor(nQuantsDouble);
63  break;
64  }
65  case ROUND:
66  {
67  nQuants = std::round(nQuantsDouble);
68  break;
69  }
70  default:
71  {
72  throw std::runtime_error("Unknown quantization method chosen");
73  }
74  }
75 
76  double outputValue = nQuants * quantizationInterval_;
77 
78  quantizationRest_[index] = currentValue - outputValue;
79 
80  value = outputValue + bias_;
81  }
82 
84 
89  template <size_t size>
90  void noisify(Eigen::Matrix<double, size, 1>& value)
91  {
92  for (size_t i = 0; i < size; i++)
93  {
94  noisify(value(i), i);
95  }
96  }
97 
99 
102  void reset() { quantizationRest_.clear(); }
103 private:
104  double quantizationInterval_;
105  std::vector<double> quantizationRest_;
106  double bias_;
107  QuantizationMethod quantizationMethod_;
108 };
109 
110 
111 } // namespace ct
112 } // namespace core
Definition: QuantizationNoise.h:24
void noisify(double &value, size_t index=0)
Applies quantization to a value.
Definition: QuantizationNoise.h:49
Quantization of data.
Definition: QuantizationNoise.h:17
void reset()
Resets the quantization.
Definition: QuantizationNoise.h:102
Definition: QuantizationNoise.h:23
QuantizationMethod
The quanitization method.
Definition: QuantizationNoise.h:21
for i
void noisify(Eigen::Matrix< double, size, 1 > &value)
Applies quantization to a vector.
Definition: QuantizationNoise.h:90
QuantizationNoise(double bias=0.0, double quantizationInterval=1.0, QuantizationMethod method=FLOOR)
Default constructor.
Definition: QuantizationNoise.h:33