Repository navigation
Expand file tree
/
Copy pathKF.py
More file actions
70 lines (57 loc) · 2.41 KB
/
Copy pathKF.py
File metadata and controls
70 lines (57 loc) · 2.41 KB
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
from GaussianFilter import *
class KF(GaussianFilter):
"""
Kalman Filter class. Implements the :class:`GaussianFilter` interface for the particular case of the Kalman Filter.
"""
def __init__(self, Ak, Bk, Hk, Vk, x0, P0, *args):
"""
Constructor of the KF class.
:param Ak: Transition matrix of the motion model
:param Bk: Input matrix of the motion model
:param Hk: Observation matrix of the observation model
:param Vk: Noise projection matrix of the motion model
:param x0: initial mean of the state vector
:param P0: initial covariance matrix
:param args: arguments to be passed to the parent class
"""
self.Ak = Ak
self.Bk = Bk
self.Hk = Hk
self.Vk = Vk
self.xk_1 = x0
self.Pk_1 = P0
super().__init__(x0, P0, *args)
def Prediction(self, uk, Qk, xk_1=None, Pk_1=None):
"""
Prediction step of the Kalman Filter.
:param uk: input vector
:param Qk: covariance matrix of the motion model noise
:param xk_1: previous mean state vector
:param Pk_1: previous covariance matrix
:return xk_bar, Pk_bar: current mean state vector and covariance matrix
"""
# logging for plotting
self.xk_1 = xk_1 if xk_1 is not None else self.xk_1
self.Pk_1 = Pk_1 if Pk_1 is not None else self.Pk_1
self.uk = uk
self.Qk = Qk # store the input and noise covariance for logging
# KF equations begin here
# TODO: To be implemented by the student
return self.xk_bar, self.Pk_bar # returns the predicted state vector
def Update(self, zk, Rk, xk_bar=None, Pk_bar=None):
"""
Update step of the Kalman Filter.
:param zk: observation vector
:param Rk: covariance of the observation model noise
:param xk_bar: predicted mean state vector
:param Pk_bar: predicted covariance matrix
:return xk.Pk: current mean state vector and covariance matrix
"""
# logging for plotting
self.xk_bar = xk_bar if xk_bar is not None else self.xk_bar
self.Pk_bar = Pk_bar if Pk_bar is not None else self.Pk_bar
self.zk = zk
self.Rk = Rk # store the observation and noise covariance for logging
# KF equations begin here
# TODO: To be implemented by the student
return self.xk, self.Pk