-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKalmanfilter.cpp
More file actions
38 lines (27 loc) · 765 Bytes
/
Copy pathKalmanfilter.cpp
File metadata and controls
38 lines (27 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "Matrix.h"
#include <vector>
#include "Kalmanfilter.h"
using namespace std;
void Kalmanfilter::predict(const Matrix& u) {
// Predict state
Matrix F_prime = F.transpose() ;
x_hat = F * x_hat + B * u;
// Predict error covariance
P = F * P * F_prime + Q;
}
void Kalmanfilter::update(const Matrix y, double dt, const Matrix A) {
// Compute residual
Matrix A_prime = A.transpose() ;
A_prime = P*A_prime;
Matrix residual = y - A * x_hat;
Matrix S = A * A_prime + R;
// Compute Kalman gain
Matrix K = P * A.transpose() * S.inverse();
// Update state estimate
x_hat = x_hat + K * residual;
// Update error covariance
Matrix I(n);
P = (I - K * A) * P;
// Update time
t += dt;
}