-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKalmanFilterPurePython.py
More file actions
32 lines (30 loc) · 1.88 KB
/
KalmanFilterPurePython.py
File metadata and controls
32 lines (30 loc) · 1.88 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
import numpy as np
# Implements a linear Kalman filter.
class KalmanFilterLinear:
def __init__(self,_A, _B, _H, _x, _P, _Q, _R):
self.A = _A # State transition matrix.
self.B = _B # Control-input matrix.
self.H = _H # Observation model matrix.
self.current_state_estimate = _x # Initial state estimate.
self.current_prob_estimate = _P # Initial covariance estimate.
self.Q = _Q # Estimated error in process.
self.R = _R # Estimated error in measurements.
self.KG = None
def GetCurrentState(self):
return self.current_state_estimate
def Step(self,control_vector,measurement_vector):
#---------------------------Prediction step-----------------------------
predicted_state_estimate = self.A * self.current_state_estimate + self.B * control_vector
predicted_prob_estimate = (self.A * self.current_prob_estimate) * np.transpose(self.A) + self.Q
#--------------------------Observation step-----------------------------
innovation = measurement_vector - self.H*predicted_state_estimate
self.Innovation = innovation[0, 0]
innovation_covariance = self.H*predicted_prob_estimate*np.transpose(self.H) + self.R
#-----------------------------Update step-------------------------------
kalman_gain = predicted_prob_estimate * np.transpose(self.H) * np.linalg.inv(innovation_covariance)
self.KG = kalman_gain
self.current_state_estimate = predicted_state_estimate + kalman_gain * innovation
# We need the size of the matrix so we can make an identity matrix.
size = self.current_prob_estimate.shape[0]
# eye(n) = nxn identity matrix.
self.current_prob_estimate = (np.eye(size)-kalman_gain*self.H)*predicted_prob_estimate