UnscentedTransform.cpp
Go to the documentation of this file.
1 // *****************************************************************
2 // Filename: UnscentedTransform.cpp
3 // Copyright: Kai Welke, Chair Prof. Dillmann (IAIM),
4 // Institute for Anthropomatics (IFA),
5 // Humanoids and Intelligence Systems Lab,
6 // Karlsruhe Institute of Technology. All rights reserved.
7 // Author: Kai Welke
8 // Date: 20.01.2010
9 // *****************************************************************
10 
11 
12 #include "UnscentedTransform.h"
13 #include <cstdio>
14 #include <iostream>
15 
16 UnscentedTransform::UnscentedTransform(float fAlpha, float fKappa, float fBeta)
17 {
18  m_fAlpha = fAlpha;
19  m_fBeta = fBeta;
20  m_fKappa = fKappa;
21 }
22 
23 Eigen::MatrixXd UnscentedTransform::getSigmaPoints(const Gaussian& gaussian)
24 {
25  // ctreate required matrices
26  int nNumberDimensions = gaussian.getDimensions();
27  int nNumberSigmaPoints = 2 * nNumberDimensions + 1;
28  Eigen::MatrixXd sigmaPoints(nNumberDimensions, nNumberSigmaPoints);
29  Eigen::VectorXd w_m(nNumberSigmaPoints);
30  Eigen::VectorXd w_c(nNumberSigmaPoints);
31 
32  // calculate lamda factor
33  float fLamda = m_fAlpha * m_fAlpha * (nNumberDimensions - m_fKappa) - nNumberDimensions;
34 
35  // first sigmapoint is mean
36  sigmaPoints.block(0, 0, sigmaPoints.rows(), 1) = gaussian.getMean();
37  w_m(0) = fLamda / (nNumberDimensions + fLamda);
38  w_c(0) = fLamda / (nNumberDimensions + fLamda) + (1 - m_fAlpha * m_fAlpha + m_fBeta);
39 
40  // precalculate square root of matrix
41  Eigen::MatrixXd diff(nNumberDimensions, nNumberDimensions);
42  diff = (nNumberDimensions + fLamda) * gaussian.getCovariance();
43 
44  // square root of diagonal matrix
45  diff = squareRoot(diff);
46 
47  // generate 2n sigmapoints
48  Eigen::VectorXd point1(nNumberDimensions);
49  Eigen::VectorXd point2(nNumberDimensions);
50 
51  float fWeight;
52 
53  for (int i = 0 ; i < nNumberDimensions ; i++)
54  {
55  // points
56  point1 = gaussian.getMean() + diff.block(i, 0, 1, diff.cols()).transpose();
57  point2 = gaussian.getMean() - diff.block(i, 0, 1, diff.cols()).transpose();
58  sigmaPoints.block(0, 2 * i + 1, sigmaPoints.rows(), 1) = point1;
59  sigmaPoints.block(0, 2 * i + 2, sigmaPoints.rows(), 1) = point2;
60 
61  // calculate weight
62  fWeight = 1.0f / (2 * (nNumberDimensions + fLamda));
63  w_m(2 * i + 1) = fWeight;
64  w_m(2 * i + 2) = fWeight;
65  w_c(2 * i + 1) = fWeight;
66  w_c(2 * i + 2) = fWeight;
67  }
68 
69  m_weights_m = w_m;
70  m_weights_c = w_c;
71 
72  return sigmaPoints;
73 }
74 
75 Gaussian UnscentedTransform::extractGaussian(Eigen::MatrixXd processedSigmaPoints)
76 {
77  Gaussian result(processedSigmaPoints.rows());
78 
79  // assemble mean
80  for (int i = 0 ; i < m_weights_c.rows() ; i++)
81  {
82  result.setMean(result.getMean() + m_weights_m(i) * processedSigmaPoints.block(0, i, processedSigmaPoints.rows(), 1));
83  }
84 
85  // assemble cov
86  Eigen::MatrixXd cov = result.getCovariance();
87  cov.setIdentity();
88  result.setCovariance(cov);
89 
90  for (int i = 0 ; i < m_weights_c.rows() ; i++)
91  {
92  result.setCovariance(result.getCovariance() + m_weights_c(i) * ((processedSigmaPoints.block(0, i, processedSigmaPoints.rows(), 1) - result.getMean()) * ((processedSigmaPoints.block(0, i, processedSigmaPoints.rows(), 1) - result.getMean()).transpose())));
93  }
94 
95  return result;
96 }
97 
98 Eigen::MatrixXd UnscentedTransform::squareRoot(Eigen::MatrixXd input)
99 {
100  Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(input);
101 
102  Eigen::MatrixXd sqrtA = es.operatorSqrt();
103 
104  return sqrtA;
105 }
Gaussian::getCovariance
const covariance_type & getCovariance() const
Definition: Gaussian.h:77
UnscentedTransform::getSigmaPoints
Eigen::MatrixXd getSigmaPoints(const Gaussian &gaussian)
Definition: UnscentedTransform.cpp:23
Gaussian::getDimensions
int getDimensions() const
Definition: Gaussian.h:85
UnscentedTransform.h
UnscentedTransform::extractGaussian
Gaussian extractGaussian(Eigen::MatrixXd processedSigmaPoints)
Definition: UnscentedTransform.cpp:75
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
Gaussian::setMean
void setMean(const value_type &mean)
Definition: Gaussian.cpp:245
Gaussian::setCovariance
void setCovariance(const covariance_type &cov)
Definition: Gaussian.cpp:261
Gaussian::getMean
const value_type & getMean() const
Definition: Gaussian.h:81
Gaussian
Definition: Gaussian.h:46
UnscentedTransform::UnscentedTransform
UnscentedTransform(float fAlpha=0.1, float fKappa=0.0f, float fBeta=2.0f)
Definition: UnscentedTransform.cpp:16