-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalman.py
30 lines (26 loc) · 1.02 KB
/
kalman.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
import numpy as np
from numpy.linalg import inv
"""
Theory : http://www.cs.unc.edu/~welch/kalman/kalmanIntro.html
"""
class KalmanFilterDiscrete:
def __init__(self,X,P,A,H,Z,Q,R,B=np.array([0],np.float32),U=np.array([0],np.float32)):
"""
X: State estimates
P: Estimate covariance
A: State model
Z: Measurement
H: Observation model
Q: Process noise covariance
R: Measurement noise covariance
"""
self.X, self.P, self.A, self.B, self.U, self.H, self.Z, self.Q, self.R = X,P,A,B,U,H,Z,Q,R
def project(self): # Time update equations, projects estimates
self.X = self.A @ self.X + self.B @ self.U
self.P = self.A @ self.P @ (self.A.T) + self.Q
return self.X
def update(self,Z): # Measurement update equations
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 -= K @ self.H @ self.P
return K