-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaplha-beta-gama.py
150 lines (127 loc) · 4.83 KB
/
aplha-beta-gama.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Progressive implementation of alpha-beta-gama filter
import numpy as np
import matplotlib.pyplot as plt
which_to_plot = 4
# Example 1 - Weighting The Gold
x_0 = 1000
z = [1030, 989, 1017, 1009, 1013, 979, 1008, 1042, 1012, 1011]
x = [x_0]
for i in range(1, 11):
K_n = 1/i
x_i = x[-1] + K_n*(z[i-1] - x[-1])
x.append(x_i)
if which_to_plot == 1:
plt.plot(np.arange(1, 11), z, "-b", label="Measurements")
plt.plot(np.arange(1, 11), x[1:], "-r", label="Estimates")
plt.legend(loc="upper left")
plt.xlabel("Iterations")
plt.ylabel("Weight")
plt.show()
# Example 2 - Tracking The Constant Veloctiy Aircraft On One Dimesnion
dt = 5
alpha = 0.2
beta = 0.1
x_0 = 30000
v_0 = 40
measurement = [30110, 30265, 30740, 30750, 31135, 31015, 31180, 31610, 31960, 31865]
estimation = []
prediction = [[x_0 + v_0*dt, v_0]]
for i in range(10):
x_i = prediction[-1][0] + alpha*(measurement[i] - prediction[-1][0])
v_i = prediction[-1][1] + beta*(measurement[i] - prediction[-1][0])/dt
estimation.append([x_i, v_i])
prediction.append([x_i + v_i*dt, v_i])
estimation = np.array(estimation)
prediction = np.array(prediction)
# print(estimation, end='\n\n')
# print(prediction)
if which_to_plot == 2:
plt.plot(np.arange(1, 11)*dt, measurement, "-b", label="Measurements")
plt.plot(np.arange(1, 11)*dt, estimation[:,0], "-r", label="Estimates")
plt.plot(np.arange(1, 11)*dt, prediction[:-1,0], "-g", label="Prediction")
plt.legend(loc="upper left")
plt.xlabel("Time")
plt.ylabel("Range")
plt.show()
# Example 3 - Tracking Accelerating Aircraft In One Dimension
# The goal of this example is to show the existence of the Lag Error
# in constant acceleration
dt = 5
alpha = 0.2
beta = 0.1
x_0 = 30000
v_0 = 50
measurement = [30160, 30365, 30890, 31050, 31785, 32215, 33130, 34510, 36010, 37265]
estimation = []
prediction = [[x_0 + v_0*dt, v_0]]
for i in range(10):
x_i = prediction[-1][0] + alpha*(measurement[i] - prediction[-1][0])
v_i = prediction[-1][1] + beta*(measurement[i] - prediction[-1][0])/dt
estimation.append([x_i, v_i])
prediction.append([x_i + v_i*dt, v_i])
estimation = np.array(estimation)
prediction = np.array(prediction)
# print(estimation, end='\n\n')
# print(prediction)
if which_to_plot == 3:
range_or_velocity = 1
if range_or_velocity == 1:
plt.plot(np.arange(1, 11)*dt, measurement, "-b", label="Measurements")
plt.plot(np.arange(1, 11)*dt, estimation[:,0], "-r", label="Estimates")
plt.plot(np.arange(1, 11)*dt, prediction[:-1,0], "-g", label="Prediction")
plt.legend(loc="upper left")
plt.xlabel("Time")
plt.ylabel("Range")
plt.show()
elif range_or_velocity == 2:
plt.plot(np.arange(1, 11)*dt, estimation[:,1], "-r", label="Estimates")
plt.plot(np.arange(1, 11)*dt, prediction[:-1,1], "-g", label="Prediction")
plt.legend(loc="upper left")
plt.xlabel("Time")
plt.ylabel("Velocity")
plt.show()
# Example 4 - Tracking Accelerating Aircraft With The Alpha-Beta-Gama Filter
dt = 5
alpha = 0.5
beta = 0.4
gama = 0.1
x_0 = 30000
v_0 = 50
a_0 = 0
measurement = [30160, 30365, 30890, 31050, 31785, 32215, 33130, 34510, 36010, 37265]
estimation = []
prediction = [[x_0 + v_0*dt + 0.5*a_0*dt*dt, v_0 + a_0*dt, a_0]]
for i in range(10):
x_i = prediction[-1][0] + alpha*(measurement[i] - prediction[-1][0])
v_i = prediction[-1][1] + beta*(measurement[i] - prediction[-1][0])/dt
a_i = prediction[-1][2] + gama*(measurement[i] - prediction[-1][0])/(0.5*dt*dt)
estimation.append([x_i, v_i, a_i])
prediction.append([x_i + v_i*dt + 0.5*a_i*dt*dt, v_i + a_i*dt, a_i])
estimation = np.array(estimation)
prediction = np.array(prediction)
# print(estimation, end='\n\n')
# print(prediction)
if which_to_plot == 4:
range_or_velocity_or_acceleration = 1
if range_or_velocity_or_acceleration == 1:
plt.plot(np.arange(1, 11)*dt, measurement, "-b", label="Measurements")
plt.plot(np.arange(1, 11)*dt, estimation[:,0], "-r", label="Estimates")
plt.plot(np.arange(1, 11)*dt, prediction[:-1,0], "-g", label="Prediction")
plt.legend(loc="upper left")
plt.xlabel("Time")
plt.ylabel("Range")
plt.show()
elif range_or_velocity_or_acceleration == 2:
plt.plot(np.arange(1, 11)*dt, estimation[:,1], "-r", label="Estimates")
plt.plot(np.arange(1, 11)*dt, prediction[:-1,1], "-g", label="Prediction")
plt.legend(loc="upper left")
plt.xlabel("Time")
plt.ylabel("Velocity")
plt.show()
elif range_or_velocity_or_acceleration == 3:
plt.plot(np.arange(1, 11)*dt, estimation[:,2], "-r", label="Estimates")
plt.plot(np.arange(1, 11)*dt, prediction[:-1,2], "-g", label="Prediction")
plt.legend(loc="upper left")
plt.xlabel("Time")
plt.ylabel("Acceleration")
plt.show()