-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNWFE.py
More file actions
411 lines (321 loc) · 16.2 KB
/
Copy pathNWFE.py
File metadata and controls
411 lines (321 loc) · 16.2 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import numpy as np
class KalmanFilter:
def __init__(self):
"""Initialize Kalman Filter with default values."""
self.state = np.zeros(2) # Initial state vector [position]
self.uncertainty = np.eye(4) # Initial uncertainty covariance matrix
self.measurement_noise = 0.2 # Standard deviation of measurement noise
self.process_noise = 1e-8 # Process noise
def reset(self, measurement):
"""Reset the filter with an initial measurement."""
self.state = np.array(measurement[:2]) # Initialize state with measurement
self.uncertainty = np.eye(2) # Reset uncertainty
return self.state
def predict(self):
"""Predict the next state and uncertainty."""
F = np.eye(2) # State transition model (identity matrix for static object)
Q = np.eye(2) * self.process_noise # Process noise covariance matrix
self.state = F @ self.state # State prediction (no change for static model)
self.uncertainty = F @ self.uncertainty @ F.T + Q # Uncertainty prediction
def calculate_kalman_gain(self, measurement_uncertainty):
"""Calculate the Kalman Gain."""
return self.uncertainty @ np.linalg.inv(
self.uncertainty + measurement_uncertainty
)
def update_state(self, measurement, kalman_gain):
"""Update the state estimate."""
self.state = self.state + kalman_gain @ (measurement - self.state)
def update_uncertainty(self, kalman_gain):
"""Update the uncertainty covariance."""
self.uncertainty = (np.eye(2) - kalman_gain) @ self.uncertainty
def update(self, dt, measurement):
"""Perform a full update cycle: predict and update."""
self.predict()
# Measurement update step
measurement = np.array(measurement[:2])
measurement_uncertainty = np.eye(2) * self.measurement_noise**2
# Calculate Kalman gain
kalman_gain = self.calculate_kalman_gain(measurement_uncertainty)
# Update state
self.update_state(measurement, kalman_gain)
# Update uncertainty
self.update_uncertainty(kalman_gain)
return self.state
class FilterRandomNoise:
def __init__(self, process_noise=1e-6):
"""Initialize Filter for random noise with default values."""
self.state = np.zeros(2) # Initial state vector [position]
self.uncertainty = np.eye(2) # Initial uncertainty covariance matrix
self.process_noise = process_noise # Process noise
def reset(self, measurement):
"""Reset the filter with an initial measurement."""
self.state = np.array(measurement[:2]) # Initialize state with measurement
self.uncertainty = np.eye(2) # Reset uncertainty
return self.state
def predict(self):
"""Predict the next state and uncertainty."""
F = np.eye(2) # State transition model (identity matrix for static object)
Q = np.eye(2) * self.process_noise # Process noise covariance matrix
self.state = F @ self.state # State prediction (no change for static model)
self.uncertainty = F @ self.uncertainty @ F.T + Q # Uncertainty prediction
def calculate_kalman_gain(self, measurement_uncertainty):
"""Calculate the Kalman Gain."""
return self.uncertainty @ np.linalg.inv(
self.uncertainty + measurement_uncertainty
)
def update_state(self, measurement, kalman_gain):
"""Update the state estimate."""
self.state = self.state + kalman_gain @ (measurement - self.state)
def update_uncertainty(self, kalman_gain):
"""Update the uncertainty covariance."""
self.uncertainty = (np.eye(2) - kalman_gain) @ self.uncertainty
def update(self, dt, measurement):
"""Perform a full update cycle: predict and update."""
self.predict()
# Measurement update step
measurement_position = np.array(measurement[:2])
measurement_covariance = np.array(measurement[2:]).reshape(2, 2)
# Calculate Kalman gain
kalman_gain = self.calculate_kalman_gain(measurement_covariance)
# Update state
self.update_state(measurement_position, kalman_gain)
# Update uncertainty
self.update_uncertainty(kalman_gain)
return self.state
class AngularKalmanFilter:
def __init__(self):
"""Initialize Angular Kalman Filter with default values."""
self.state = np.zeros(2) # Initial state vector [x, y]
self.uncertainty = np.eye(2) # Initial uncertainty covariance matrix
self.process_noise = 1e-8 # Process noise
self.measurement_noise_r = 0.1 # Measurement noise standard deviation for radius (r)
self.measurement_noise_phi = 0.05 # Measurement noise standard deviation for angle (phi)
def reset(self, measurement):
"""Reset the filter with an initial measurement in polar coordinates."""
r, phi = measurement
self.state = self.polar_to_cartesian(r, phi) # Convert polar to Cartesian coordinates
self.uncertainty = np.eye(2) # Reset uncertainty
return self.state
def predict(self):
"""Predict the next state and uncertainty."""
F = np.eye(2) # State transition model (identity matrix for static object)
Q = np.eye(2) * self.process_noise # Process noise covariance matrix
self.state = F @ self.state # State prediction (no change for static model)
self.uncertainty = F @ self.uncertainty @ F.T + Q # Uncertainty prediction
def update(self, dt, measurement):
"""Perform a full update cycle: predict and update."""
self.predict()
# Convert polar measurement to Cartesian coordinates
r, phi = measurement
measurement_cartesian = self.polar_to_cartesian(r, phi)
# Measurement noise covariance matrix in Cartesian coordinates
R = np.array([[0.0100, 0.0000], [0.0000, 0.0025]])
# Calculate Kalman gain
kalman_gain = self.calculate_kalman_gain(R)
# Update state
self.update_state(measurement_cartesian, kalman_gain)
# Update uncertainty
self.update_uncertainty(kalman_gain)
return self.state
def calculate_kalman_gain(self, measurement_uncertainty):
"""Calculate the Kalman Gain."""
return self.uncertainty @ np.linalg.inv(
self.uncertainty + measurement_uncertainty
)
def update_state(self, measurement, kalman_gain):
"""Update the state estimate."""
self.state = self.state + kalman_gain @ (measurement - self.state)
def update_uncertainty(self, kalman_gain):
"""Update the uncertainty covariance."""
self.uncertainty = (np.eye(2) - kalman_gain) @ self.uncertainty
@staticmethod
def polar_to_cartesian(r, phi):
"""Convert polar coordinates to Cartesian coordinates."""
x = r * np.cos(phi)
y = r * np.sin(phi)
return np.array([x, y])
class ConstantVelocityKalmanFilter:
def __init__(self, process_noise=1e-6, measurement_noise=0.69):
"""Initialize Constant Velocity Kalman Filter with default values."""
self.dt = 1 # Default time step
self.state = np.zeros(4) # Initial state vector [x, y, vx, vy]
self.uncertainty = np.eye(4) # Initial uncertainty covariance matrix
self.process_noise = process_noise # Process noise
self.measurement_noise = measurement_noise # Measurement noise
def reset(self, measurement):
"""Reset the filter with an initial measurement."""
self.state[:2] = np.array(measurement[:2]) # Initialize position part of the state
self.state[2:] = 0 # Initial velocity is unknown, set to zero
self.uncertainty = np.eye(4) / 2 # Reset uncertainty
return self.state[:2] # Return only the position part
def predict(self, dt):
"""Predict the next state and uncertainty."""
self.dt = dt
F = np.array([[1, 0, dt, 0], [0, 1, 0, dt], [0, 0, 1, 0], [0, 0, 0, 1]])
Q = np.eye(4) * self.process_noise # Process noise covariance matrix
self.state = F @ self.state # State prediction
self.uncertainty = F @ self.uncertainty @ F.T + Q # Uncertainty prediction
def calculate_kalman_gain(self, measurement_uncertainty):
"""Calculate the Kalman Gain."""
H = np.eye(2, 4) # Measurement matrix (mapping state to measurement space)
return (
self.uncertainty
@ H.T
@ np.linalg.inv(H @ self.uncertainty @ H.T + measurement_uncertainty)
)
def update_state(self, measurement, kalman_gain):
"""Update the state estimate."""
H = np.eye(2, 4) # Measurement matrix
self.state = self.state + kalman_gain @ (measurement - H @ self.state)
def update_uncertainty(self, kalman_gain):
"""Update the uncertainty covariance."""
H = np.eye(2, 4) # Measurement matrix
self.uncertainty = (np.eye(4) - kalman_gain @ H) @ self.uncertainty
def update(self, dt, measurement):
"""Perform a full update cycle: predict and update."""
self.predict(dt)
measurement = np.array(measurement[:2])
measurement_uncertainty = np.eye(2) * self.measurement_noise**2
kalman_gain = self.calculate_kalman_gain(measurement_uncertainty)
self.update_state(measurement, kalman_gain)
self.update_uncertainty(kalman_gain)
return self.state[:2]
class ConstantVelocityMultiMeasurementKalmanFilter:
def __init__(self, process_noise=2e-4, measurement_noise=0.00001):
"""Initialize Constant Velocity Multi-Measurement Kalman Filter."""
self.state = np.zeros(4) # Initial state vector [x, y, vx, vy]
self.uncertainty = np.eye(2) # Initial uncertainty covariance matrix
self.process_noise = process_noise # Process noise
self.measurement_noise = measurement_noise # Measurement noise
def reset(self, measurement):
"""Reset the filter with initial measurements."""
self.state[:2] = np.mean(measurement[:10].reshape(-1, 2), axis=0)
self.state[2:] = 0 # Initial velocity is unknown, set to zero
self.uncertainty = np.eye(4) # Reset uncertainty
return self.state[:2] # Return only the position part
def predict(self, dt):
"""Predict the next state and uncertainty."""
F = np.array([[1, 0, dt, 0], [0, 1, 0, dt], [0, 0, 1, 0], [0, 0, 0, 1]])
Q = np.eye(4) * self.process_noise # Process noise covariance matrix
self.state = F @ self.state # State prediction
self.uncertainty = F @ self.uncertainty @ F.T + Q # Uncertainty prediction
def calculate_kalman_gain(self, measurement_uncertainty):
"""Calculate the Kalman Gain."""
H = np.eye(2, 4) # Measurement matrix (mapping state to measurement space)
S = H @ self.uncertainty @ H.T + measurement_uncertainty
K = self.uncertainty @ H.T @ np.linalg.inv(S)
return K
def update_state(self, measurement, kalman_gain):
"""Update the state estimate."""
H = np.eye(2, 4) # Measurement matrix
innovation = measurement - H @ self.state
self.state = self.state + kalman_gain @ innovation
def update_uncertainty(self, kalman_gain):
"""Update the uncertainty covariance."""
H = np.eye(2, 4) # Measurement matrix
self.uncertainty = (np.eye(4) - kalman_gain @ H) @ self.uncertainty
def update(self, dt, measurement):
"""Perform a full update cycle: predict and update."""
self.predict(dt)
measurements = np.array(measurement[:10]).reshape(-1, 2)
measurement_uncertainties = np.array(measurement[10:]).reshape(-1, 2)
# Compute average measurement and combined measurement uncertainty
avg_measurement = np.mean(measurements, axis=0)
combined_uncertainty = np.zeros((2, 2))
for i in range(len(measurements)):
combined_uncertainty += np.diag(measurement_uncertainties[i]) ** 2
combined_uncertainty = (
combined_uncertainty / len(measurements)
+ np.eye(2) * self.measurement_noise
)
kalman_gain = self.calculate_kalman_gain(combined_uncertainty)
self.update_state(avg_measurement, kalman_gain)
self.update_uncertainty(kalman_gain)
return self.state[:2]
class ConstantTurnRateKalmanFilter:
def __init__(self, process_noise=0.01, measurement_noise=0.01, alpha=0.69):
"""Initialize Constant Turn Rate Kalman Filter with default values."""
self.dt = 1 # Default time step
self.state = np.zeros(7) # Initial state vector [x, y, vx, vy, ax, ay, w]
self.uncertainty = np.eye(7) # Initial uncertainty covariance matrix
self.process_noise = np.diag(
[1e-2, 1e-3, 1e-2, 1e-2, 1e-2, 1e-2, 1e-2]
) # Process noise tuning
self.measurement_noise = measurement_noise # Measurement noise
self.alpha = alpha # Smoothing factor for EWMA (Exponential Weighted Moving Average)
self.smooth_position = np.zeros(2) # Smoothed position
def reset(self, measurement):
"""Reset the filter with initial measurements."""
x_init = np.mean(measurement[:10:2])
y_init = np.mean(measurement[1:10:2])
self.state = np.array(
[x_init, y_init, 0, 0, 0, 0, 0]
) # Initial position, zero velocity and turn rate
self.uncertainty = np.eye(7) # High initial uncertainty
self.smooth_position = np.array(
[x_init, y_init]
) # Initialize smoothed position
return self.state[:2] # Return only the position
def predict(self, dt):
"""Predict the next state and uncertainty."""
self.dt = dt
theta = self.state[6] * dt # Dynamic turn rate * dt
cos_theta = np.cos(theta)
sin_theta = np.sin(theta)
# State transition matrix incorporating dynamic turn rate
F = np.array(
[
[1, 0, dt, 0, 0.5 * dt**2, 0, 0],
[0, 1, 0, dt, 0, 0.5 * dt**2, 0],
[0, 0, 1, 0, dt, 0, 0],
[0, 0, 0, 1, 0, dt, 0],
[0, 0, 0, 0, cos_theta, -sin_theta, 0],
[0, 0, 0, 0, sin_theta, cos_theta, 0],
[0, 0, 0, 0, 0, 0, 1],
]
)
self.state = F @ self.state # State prediction
self.uncertainty = F @ self.uncertainty @ F.T + self.process_noise # Uncertainty prediction
return self.state
def calculate_kalman_gain(self, measurement_uncertainty):
"""Calculate the Kalman Gain."""
H = np.zeros((10, 7))
for i in range(5):
H[2 * i, 0] = 1
H[2 * i + 1, 1] = 1
S = H @ self.uncertainty @ H.T + measurement_uncertainty
K = self.uncertainty @ H.T @ np.linalg.inv(S)
return K
def update_state(self, measurement, kalman_gain):
"""Update the state estimate."""
H = np.zeros((10, 7))
for i in range(5):
H[2 * i, 0] = 1
H[2 * i + 1, 1] = 1
innovation = measurement[:10] - H @ self.state
self.state = self.state + kalman_gain @ innovation
def update_uncertainty(self, kalman_gain):
"""Update the uncertainty covariance."""
H = np.zeros((10, 7))
for i in range(5):
H[2 * i, 0] = 1
H[2 * i + 1, 1] = 1
self.uncertainty = (np.eye(7) - kalman_gain @ H) @ self.uncertainty
def update(self, dt, measurement):
"""Perform a full update cycle: predict and update."""
self.predict(dt)
measurement_uncertainties = measurement[10:].reshape(-1, 2)
# Compute combined measurement uncertainty
combined_uncertainty = np.zeros((10, 10))
for i in range(len(measurement_uncertainties)):
combined_uncertainty[2 * i : 2 * i + 2, 2 * i : 2 * i + 2] = (
np.diag(measurement_uncertainties[i]) ** 2
)
kalman_gain = self.calculate_kalman_gain(combined_uncertainty)
self.update_state(measurement, kalman_gain)
self.update_uncertainty(kalman_gain)
# Exponentially weighted moving average (EWMA) for smoothing
self.smooth_position = (
self.alpha * self.state[:2] + (1 - self.alpha) * self.smooth_position
)
return self.smooth_position