-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalman_filter.py
82 lines (54 loc) · 2.19 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
75
import matplotlib.pyplot as plt
import numpy as np
from kalman_functions import load_data, get_mean_diff_time,kalman_filter
import pdb
fig, axs = plt.subplots(3,1, figsize=(12, 10), subplot_kw={'projection': '3d'})
def run_kalman(filename):
# Load data
time, u, z, sigma_Q, R, C = load_data(filename)
m = 0.027
# Get the mean time difference
diff_t = get_mean_diff_time(time)
# pdb.set_trace()
# Set up initial state x_hat
x_hat = np.zeros((6,1))
x_hat[:3] = z[0].reshape(3,1)
#P[0] is set to a large value since we don't know the initial state
estimates = kalman_filter(x_hat,np.eye(6)*300,u,z,diff_t,m,sigma_Q,time,R,C)
return estimates
####################################################################
gt_track = run_kalman('kalman_filter_data_mocap.txt')
####################################################################
estimates = run_kalman('kalman_filter_data_high_noise.txt')
#Plot estimated_path
ax = axs[0]
ax.plot(estimates[:, 0], estimates[:, 1], estimates[:, 2],color='b',label = "Estimated")
ax.scatter(gt_track[:,0],gt_track[:,1],gt_track[:,2],color='g', label = "Ground truth", s= 5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()
ax.set_title('Estimated trajectory for high noise')
####################################################################
estimates = run_kalman('kalman_filter_data_low_noise.txt')
#Plot estimated_path
ax = axs[1]
ax.plot(estimates[:, 0], estimates[:, 1], estimates[:, 2],color='b',label = "Estimated")
ax.scatter(gt_track[:,0],gt_track[:,1],gt_track[:,2],color='g', label = "Ground truth",s= 5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()
ax.set_title('Estimated trajectory for low noise')
####################################################################
estimates = run_kalman('kalman_filter_data_velocity.txt')
#Plot estimated_path
ax = axs[2]
ax.plot(estimates[:, 0], estimates[:, 1], estimates[:, 2],color='b',label = "Estimated")
ax.scatter(gt_track[:,0],gt_track[:,1],gt_track[:,2],color='g', label = "Ground truth", s= 5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()
ax.set_title('Estimated trajectory for velocity')
plt.show()