RLSFilter 0.0.0
C++ RLS filter
Loading...
Searching...
No Matches
RLSFilter.h
Go to the documentation of this file.
1//
2// Created by Davide Pilastro on 2/19/21.
3//
4
5#ifndef RLS_DYN_MODEL_IDENT_RLS_H
6#define RLS_DYN_MODEL_IDENT_RLS_H
7
8#include <Eigen/Dense>
9#include <type_traits>
10using namespace Eigen;
11
12namespace rls_filter {
13
14template <bool B>
15using EnableIfB = std::enable_if_t<B, int>;
16
21template <typename T, int N>
22class RLSFilter {
23 static_assert((std::is_same<long double, T>::value ||
24 std::is_same_v<double, T> ||
25 std::is_same<float, T>::value),
26 "T must be: long double, double or float");
27
28 public:
29 using VectorXt = Matrix<T, N, 1>;
30 using MatrixXt = Matrix<T, N, N>;
31
32 private:
33 unsigned int n_;
34 T lam_;
41 T err_;
42 unsigned long long count_;
43
44 public:
48 template <int N1 = N, EnableIfB<(N1 > 0)> = 0>
49 RLSFilter(T lam, T delta)
50 : n_(N),
51 lam_(1.0),
52 lam_inv_(1.0),
53 delta_(delta),
54 w_(VectorXt::Zero()),
55 P_(MatrixXt::Identity()),
56 g_(VectorXt::Zero()),
57 err_(0.0),
58 count_(0) {
61 P_ *= delta_;
62 }
63
68 template <int N1 = N, EnableIfB<(N1 == -1)> = 0>
69 RLSFilter(unsigned int n, T lam, T delta)
70 : n_(n),
71 lam_(1.0),
72 lam_inv_(1.0),
73 delta_(delta),
74 w_(VectorXt::Zero(n_)),
75 P_(MatrixXt::Identity(n_, n_)),
76 g_(VectorXt::Zero(n_)),
77 err_(0.0),
78 count_(0) {
81 P_ *= delta_;
82 }
83
87 void update(const VectorXt &x, T y) {
88 err_ = y - predict(x);
89 P_supp_.noalias() = P_ * lam_inv_;
90 g_.noalias() = (P_ * x) / (lam_ + (x.transpose() * P_ * x).value());
91 P_.noalias() = (MatrixXt::Identity(n_, n_) - g_ * x.transpose()) * P_supp_;
92 w_.noalias() += g_ * err_;
93 count_++;
94 };
95
99 T predict(const VectorXt &x) const noexcept { return w_.transpose() * x; };
100
104 if (w0.rows() == n_) {
105 w_ = w0;
106 } else {
107 throw std::invalid_argument("Wrong initial state dimension.");
108 }
109 }
110
113 void setForgettingFactor(double lam) {
114 if ((lam > 0) && (lam <= 1.0)) {
115 lam_ = lam;
116 lam_inv_ = 1.0 / lam_;
117 } else {
118 throw std::invalid_argument(
119 "Invalid forgetting factor (0 < lambda <= 1).");
120 }
121 }
122
126 if (delta > 0.0) {
127 delta_ = delta;
128 } else {
129 throw std::invalid_argument(
130 "Invalid covariance matrix gain factor (delta > 0).");
131 }
132 }
133
136 const VectorXt &estimatedCoefficients() const noexcept { return w_; };
137
140 T a_priori_err() const noexcept { return err_; };
141
144 const VectorXt &gains() const noexcept { return g_; };
145
148 const MatrixXt &P() const noexcept { return P_; };
149
152 unsigned long long count() const noexcept { return count_; };
153
155 void reset() noexcept {
156 w_ = VectorXt::Zero(n_);
157 P_ = MatrixXt::Identity(n_, n_) * delta_;
158 g_ = VectorXt::Zero(n_);
159 err_ = 0.0;
160 count_ = 0;
161 };
162};
163
164} // namespace rls_filter
165
166#endif // RLS_DYN_MODEL_IDENT_RLS_H
const VectorXt & estimatedCoefficients() const noexcept
Definition RLSFilter.h:136
unsigned long long count_
Definition RLSFilter.h:42
T lam_inv_
Definition RLSFilter.h:35
RLSFilter(T lam, T delta)
Definition RLSFilter.h:49
const MatrixXt & P() const noexcept
Definition RLSFilter.h:148
void setForgettingFactor(double lam)
Definition RLSFilter.h:113
T err_
Definition RLSFilter.h:41
Matrix< T, N, N > MatrixXt
Definition RLSFilter.h:30
unsigned long long count() const noexcept
Definition RLSFilter.h:152
T delta_
Definition RLSFilter.h:36
VectorXt w_
Definition RLSFilter.h:37
Matrix< T, N, 1 > VectorXt
Definition RLSFilter.h:29
void update(const VectorXt &x, T y)
Definition RLSFilter.h:87
RLSFilter(unsigned int n, T lam, T delta)
Definition RLSFilter.h:69
void setEstimatedCoefficients(const VectorXt &w0)
Definition RLSFilter.h:103
VectorXt g_
Definition RLSFilter.h:40
void reset() noexcept
Reset filter to initial values.
Definition RLSFilter.h:155
MatrixXt P_
Definition RLSFilter.h:38
void setInitialCovarianceMatrixGain(double delta)
Definition RLSFilter.h:125
T lam_
Definition RLSFilter.h:34
unsigned int n_
Definition RLSFilter.h:33
MatrixXt P_supp_
Definition RLSFilter.h:39
T predict(const VectorXt &x) const noexcept
Definition RLSFilter.h:99
T a_priori_err() const noexcept
Definition RLSFilter.h:140
const VectorXt & gains() const noexcept
Definition RLSFilter.h:144
Definition RLSFilter.h:12
std::enable_if_t< B, int > EnableIfB
Definition RLSFilter.h:15