Bayes Filters Library
WhiteNoiseAcceleration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
9 #include <BayesFilters/utils.h>
10 
11 #include <cmath>
12 #include <utility>
13 
14 #include <Eigen/Cholesky>
15 
16 using namespace bfl;
17 using namespace Eigen;
18 
19 
21 {
22  ImplData
23  (
24  const Dim dim,
25  const double sampling_interval,
26  const double tilde_q,
27  const unsigned int seed
28  ) :
29  T_(sampling_interval),
30  tilde_q_(tilde_q),
31  seed_(seed),
32  generator_(std::mt19937_64(seed_)),
33  distribution_(std::normal_distribution<double>(0.0, 1.0)),
34  gauss_rnd_sample_([&] { return (distribution_) (generator_); })
35  {
36  double q11 = 1.0 / 3.0 * std::pow(T_, 3.0);
37  double q2 = 1.0 / 2.0 * std::pow(T_, 2.0);
38 
39  Matrix2d F;
40  F << 1.0, T_,
41  0.0, 1.0;
42 
43  Matrix2d Q;
44  Q << q11, q2,
45  q2, T_;
46 
47  switch (dim)
48  {
49  case Dim::OneD:
50  {
51  F_ = F;
52 
53  Q_ = Q;
54 
55  state_description_ = VectorDescription(2);
56 
57  break;
58  }
59 
60  case Dim::TwoD:
61  {
62  F_ = MatrixXd(4, 4);
63  F_ << F, Matrix2d::Zero(),
64  Matrix2d::Zero(), F;
65 
66  Q_ = MatrixXd(4, 4);
67  Q_ << Q, Matrix2d::Zero(),
68  Matrix2d::Zero(), Q;
69 
70  state_description_ = VectorDescription(4);
71 
72  break;
73  }
74 
75  case Dim::ThreeD:
76  {
77  F_ = MatrixXd(6, 6);
78  F_ << F, Matrix2d::Zero(), Matrix2d::Zero(),
79  Matrix2d::Zero(), F, Matrix2d::Zero(),
80  Matrix2d::Zero(), Matrix2d::Zero(), F;
81 
82  Q_ = MatrixXd(6, 6);
83  Q_ << Q, Matrix2d::Zero(), Matrix2d::Zero(),
84  Matrix2d::Zero(), Q, Matrix2d::Zero(),
85  Matrix2d::Zero(), Matrix2d::Zero(), Q;
86 
87  state_description_ = VectorDescription(6);
88 
89  break;
90  }
91  }
92 
93  Q_ *= tilde_q_;
94 
95  LDLT<MatrixXd> chol_ldlt(Q_);
96  sqrt_Q_ = (chol_ldlt.transpositionsP() * MatrixXd::Identity(Q_.rows(), Q_.cols())).transpose() * chol_ldlt.matrixL() * chol_ldlt.vectorD().real().cwiseSqrt().asDiagonal();
97  }
98 
102  double T_;
103 
107  double tilde_q_;
108 
113 
117  Eigen::MatrixXd F_;
118 
122  Eigen::MatrixXd Q_;
123 
127  Eigen::MatrixXd sqrt_Q_;
128 
132  unsigned int seed_;
133 
137  std::mt19937_64 generator_;
138 
142  std::normal_distribution<double> distribution_;
143 
148  std::function<double()> gauss_rnd_sample_;
149 };
150 
151 
153 (
154  const Dim dim,
155  const double sampling_interval,
156  const double tilde_q
157 ) noexcept :
158  WhiteNoiseAcceleration(dim, sampling_interval, tilde_q, 1)
159 { }
160 
161 
163 (
164  const Dim dim,
165  const double sampling_interval,
166  const double tilde_q,
167  const unsigned int seed
168 ) noexcept :
169  pimpl_(utils::make_unique<ImplData>(dim, sampling_interval, tilde_q, seed))
170 { }
171 
172 
174 
175 
177 
178 
180 
181 
182 bool WhiteNoiseAcceleration::setProperty(const std::string& property)
183 {
184  return false;
185 }
186 
187 
189 {
190  return pimpl_->state_description_;
191 }
192 
193 
194 MatrixXd WhiteNoiseAcceleration::getNoiseSample(const std::size_t num)
195 {
196  MatrixXd rand_vectors(4, num);
197  for (int i = 0; i < rand_vectors.size(); i++)
198  *(rand_vectors.data() + i) = pimpl_->gauss_rnd_sample_();
199 
200  return pimpl_->sqrt_Q_ * rand_vectors;
201 }
202 
203 
205 {
206  return pimpl_->Q_;
207 }
208 
209 
211 {
212  return pimpl_->F_;
213 }
214 
215 
216 VectorXd WhiteNoiseAcceleration::getTransitionProbability(const Ref<const MatrixXd>& prev_states, const Ref<const MatrixXd>& cur_states)
217 {
218  return utils::multivariate_gaussian_density(prev_states, prev_states.col(0), pimpl_->Q_);
219 }
bfl::WhiteNoiseAcceleration::~WhiteNoiseAcceleration
virtual ~WhiteNoiseAcceleration() noexcept
bfl::WhiteNoiseAcceleration::operator=
WhiteNoiseAcceleration & operator=(const WhiteNoiseAcceleration &state_model) noexcept=delete
bfl::WhiteNoiseAcceleration::ImplData::distribution_
std::normal_distribution< double > distribution_
Normal distribution for random number generation functions.
Definition: WhiteNoiseAcceleration.cpp:142
bfl::WhiteNoiseAcceleration::ImplData::seed_
unsigned int seed_
Seed of the random number generator.
Definition: WhiteNoiseAcceleration.cpp:132
bfl::WhiteNoiseAcceleration::ImplData::T_
double T_
Sampling interval in [time].
Definition: WhiteNoiseAcceleration.cpp:102
bfl::WhiteNoiseAcceleration::ImplData
Definition: WhiteNoiseAcceleration.cpp:20
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::WhiteNoiseAcceleration
Definition: WhiteNoiseAcceleration.h:22
bfl::WhiteNoiseAcceleration::getStateDescription
VectorDescription getStateDescription() override
Returns the vector description of the output of the state equation.
Definition: WhiteNoiseAcceleration.cpp:188
bfl::WhiteNoiseAcceleration::ImplData::state_description_
VectorDescription state_description_
State description.
Definition: WhiteNoiseAcceleration.cpp:112
bfl::WhiteNoiseAcceleration::ImplData::F_
Eigen::MatrixXd F_
State transition matrix.
Definition: WhiteNoiseAcceleration.cpp:117
bfl::WhiteNoiseAcceleration::getTransitionProbability
Eigen::VectorXd getTransitionProbability(const Eigen::Ref< const Eigen::MatrixXd > &prev_states, const Eigen::Ref< const Eigen::MatrixXd > &cur_states) override
Definition: WhiteNoiseAcceleration.cpp:216
bfl::WhiteNoiseAcceleration::getNoiseCovarianceMatrix
Eigen::MatrixXd getNoiseCovarianceMatrix() override
Definition: WhiteNoiseAcceleration.cpp:204
utils.h
bfl::WhiteNoiseAcceleration::ImplData::Q_
Eigen::MatrixXd Q_
Convariance matrix of the additive white noise of the state model.
Definition: WhiteNoiseAcceleration.cpp:122
bfl::utils::multivariate_gaussian_density
Eigen::VectorXd multivariate_gaussian_density(const Eigen::MatrixBase< Derived > &input, const Eigen::Ref< const Eigen::VectorXd > &mean, const Eigen::Ref< const Eigen::MatrixXd > &covariance)
Evaluate a multivariate Gaussian probability density function.
Definition: utils.h:419
bfl::WhiteNoiseAcceleration::getNoiseSample
Eigen::MatrixXd getNoiseSample(const std::size_t num) override
Definition: WhiteNoiseAcceleration.cpp:194
bfl::WhiteNoiseAcceleration::ImplData::gauss_rnd_sample_
std::function< double()> gauss_rnd_sample_
Random number generator function from a Normal distribution.
Definition: WhiteNoiseAcceleration.cpp:148
WhiteNoiseAcceleration.h
bfl::WhiteNoiseAcceleration::ImplData::generator_
std::mt19937_64 generator_
Random number generator.
Definition: WhiteNoiseAcceleration.cpp:137
bfl::WhiteNoiseAcceleration::ImplData::tilde_q_
double tilde_q_
Power spectral density [length]^2/[time]^3.
Definition: WhiteNoiseAcceleration.cpp:107
bfl::WhiteNoiseAcceleration::getStateTransitionMatrix
Eigen::MatrixXd getStateTransitionMatrix() override
Definition: WhiteNoiseAcceleration.cpp:210
bfl::VectorDescription
Definition: VectorDescription.h:18
bfl::WhiteNoiseAcceleration::ImplData::sqrt_Q_
Eigen::MatrixXd sqrt_Q_
Square root matrix of R_.
Definition: WhiteNoiseAcceleration.cpp:127
bfl::WhiteNoiseAcceleration::WhiteNoiseAcceleration
WhiteNoiseAcceleration(const Dim dim, const double sampling_interval, const double tilde_q) noexcept
Definition: WhiteNoiseAcceleration.cpp:153
bfl::WhiteNoiseAcceleration::Dim
Dim
Definition: WhiteNoiseAcceleration.h:25