Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96129 views
1
# -*- coding: utf-8 -*-
2
"""Copyright 2015 Roger R Labbe Jr.
3
4
FilterPy library.
5
http://github.com/rlabbe/filterpy
6
7
Documentation at:
8
https://filterpy.readthedocs.org
9
10
Supporting book at:
11
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
12
13
This is licensed under an MIT license. See the readme.MD file
14
for more information.
15
"""
16
17
from __future__ import (absolute_import, division, print_function,
18
unicode_literals)
19
20
21
import numpy as np
22
import scipy.linalg as linalg
23
import matplotlib.pyplot as plt
24
from GetRadar import GetRadar
25
from filterpy.kalman import UnscentedKalmanFilter as UKF
26
from filterpy.common import Q_discrete_white_noise
27
28
29
30
def fx(x, dt):
31
""" state transition function for sstate [downrange, vel, altitude]"""
32
F = np.array([[1., dt, 0.],
33
[0., 1., 0.],
34
[0., 0., 1.]])
35
36
return np.dot(F, x)
37
38
39
def hx(x):
40
""" returns slant range based on downrange distance and altitude"""
41
42
return (x[0]**2 + x[2]**2)**.5
43
44
45
if __name__ == "__main__":
46
47
dt = 0.05
48
49
radarUKF = UKF(dim_x=3, dim_z=1, dt=dt, kappa=0.)
50
radarUKF.Q *= Q_discrete_white_noise(3, 1, .01)
51
radarUKF.R *= 10
52
radarUKF.x = np.array([0., 90., 1100.])
53
radarUKF.P *= 100.
54
55
t = np.arange(0, 20+dt, dt)
56
n = len(t)
57
xs = []
58
rs = []
59
for i in range(n):
60
r = GetRadar(dt)
61
rs.append(r)
62
63
radarUKF.update(r, hx, fx)
64
65
xs.append(radarUKF.x)
66
67
xs = np.asarray(xs)
68
69
plt.subplot(311)
70
plt.plot(t, xs[:, 0])
71
plt.title('distance')
72
73
plt.subplot(312)
74
plt.plot(t, xs[:, 1])
75
plt.title('velocity')
76
77
plt.subplot(313)
78
plt.plot(t, xs[:, 2])
79
plt.title('altitude')
80
81