-
Notifications
You must be signed in to change notification settings - Fork 3
/
kalman_filter.py
74 lines (66 loc) · 1.94 KB
/
kalman_filter.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# KalmanFilter
# Author: Du Ang
# Based on https://github.com/dougszumski/KalmanFilter/blob/master/kalman_filter.py by Doug Szumski.
# Differences with the original version:
# - add control term
# - use numpy multiplication
# Materials references: http://www.bzarg.com/p/how-a-kalman-filter-works-in-pictures/
# Date: July 1, 2018
import numpy as np
from numpy.linalg import inv
class KalmanFilter:
"""
Simple Kalman filter
"""
def __init__(self, X, F, Q, Z, H, R, P, B=np.array([0]), M=np.array([0])):
"""
Initialise the filter
Args:
X: State estimate
P: Estimate covariance
F: State transition model
B: Control matrix
M: Control vector
Q: Process noise covariance
Z: Measurement of the state X
H: Observation model
R: Observation noise covariance
"""
self.X = X
self.P = P
self.F = F
self.B = B
self.M = M
self.Q = Q
self.Z = Z
self.H = H
self.R = R
def predict(self):
"""
Predict the future state
Args:
self.X: State estimate
self.P: Estimate covariance
self.B: Control matrix
self.M: Control vector
Returns:
updated self.X
"""
# Project the state ahead
self.X = self.F @ self.X + self.B @ self.M
self.P = self.F @ self.P @ self.F.T + self.Q
return self.X
def correct(self, Z):
"""
Update the Kalman Filter from a measurement
Args:
self.X: State estimate
self.P: Estimate covariance
Z: State measurement
Returns:
updated X
"""
K = self.P @ self.H.T @ inv(self.H @ self.P @ self.H.T + self.R)
self.X += K @ (Z - self.H @ self.X)
self.P = self.P - K @ self.H @ self.P
return self.X