Skip to content

Instantly share code, notes, and snippets.

@cotramarko
Last active December 9, 2017 21:10
Show Gist options
  • Save cotramarko/2d823aca5e9d062fca7a104b5b39f152 to your computer and use it in GitHub Desktop.
Save cotramarko/2d823aca5e9d062fca7a104b5b39f152 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from kalman_filter import KalmanFilter
from simulate_model import simulate_system, create_model_parameters
np.random.seed(21)
(A, H, Q, R) = create_model_parameters()
K = 20
# initial state
x = np.array([0, 0.1, 0, 0.1])
P = 0 * np.eye(4)
(state, meas) = simulate_system(K, x)
kalman_filter = KalmanFilter(A, H, Q, R, x, P)
est_state = np.zeros((K, 4))
est_cov = np.zeros((K, 4, 4))
for k in range(K):
kalman_filter.predict()
kalman_filter.update(meas[k, :])
(x, P) = kalman_filter.get_state()
est_state[k, :] = x
est_cov[k, ...] = P
plt.figure(figsize=(7, 5))
plt.plot(state[:, 0], state[:, 2], '-bo')
plt.plot(est_state[:, 0], est_state[:, 2], '-ko')
plt.plot(meas[:, 0], meas[:, 1], ':rx')
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.legend(['true state', 'inferred state', 'observed measurement'])
plt.axis('square')
plt.tight_layout(pad=0)
plt.plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment