Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96131 views
1
# -*- coding: utf-8 -*-
2
3
"""Copyright 2015 Roger R Labbe Jr.
4
5
FilterPy library.
6
http://github.com/rlabbe/filterpy
7
8
Documentation at:
9
https://filterpy.readthedocs.org
10
11
Supporting book at:
12
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
13
14
This is licensed under an MIT license. See the readme.MD file
15
for more information.
16
"""
17
18
from __future__ import (absolute_import, division, print_function,
19
unicode_literals)
20
21
22
import matplotlib.pyplot as plt
23
import numpy.random as random
24
from numpy.random import randn
25
from math import sqrt
26
import numpy as np
27
from filterpy.kalman import ExtendedKalmanFilter
28
from numpy import array, eye, asarray
29
30
from filterpy.examples import RadarSim
31
32
33
34
35
def H_of(x):
36
""" compute Jacobian of H matrix for state x """
37
38
horiz_dist = x[0]
39
altitude = x[2]
40
41
denom = sqrt(horiz_dist**2 + altitude**2)
42
43
# dh_ddist = horiz_dist/denom
44
# dh_dvel = 0
45
# dh_dalt = altitude/denom
46
return array ([[horiz_dist/denom, 0., altitude/denom]])
47
48
49
def hx(x):
50
""" takes a state variable and returns the measurement that would
51
correspond to that state.
52
"""
53
54
return sqrt(x[0]**2 + x[2]**2)
55
56
57
dt = 0.05
58
proccess_error = 0.05
59
60
rk = ExtendedKalmanFilter(dim_x=3, dim_z=1)
61
62
63
rk.F = eye(3) + array ([[0, 1, 0],
64
[0, 0, 0],
65
[0, 0, 0]])*dt
66
67
68
69
70
rk.x = array([-10., 90., 1100.])
71
rk.R *= 10
72
rk.Q = array([[0, 0, 0],
73
[0, 1, 0],
74
[0, 0, 1]]) * 0.001
75
76
rk.P *= 50
77
78
DO_PLOT = True
79
80
rs = []
81
xs = []
82
radar = RadarSim(dt)
83
ps = []
84
85
pos = []
86
87
for i in range(int(20/dt)):
88
z = radar.get_range(proccess_error)
89
pos.append(radar.pos)
90
91
rk.update(asarray([z]), H_of, hx, R=hx(rk.x)*proccess_error)
92
ps.append(rk.P)
93
rk.predict()
94
95
xs.append(rk.x)
96
rs.append(z)
97
98
xs = asarray(xs)
99
ps = asarray(ps)
100
rs = asarray(rs)
101
102
p_pos = ps[:,0,0]
103
p_vel = ps[:,1,1]
104
p_alt = ps[:,2,2]
105
pos = asarray(pos)
106
107
if DO_PLOT:
108
109
plt.subplot(311)
110
plt.plot(xs[:,0])
111
plt.ylabel('position')
112
113
plt.subplot(312)
114
plt.plot(xs[:,1])
115
plt.ylabel('velocity')
116
117
plt.subplot(313)
118
#plt.plot(xs[:,2])
119
#plt.ylabel('altitude')
120
121
122
plt.plot(p_pos)
123
plt.plot(-p_pos)
124
plt.plot(xs[:,0]-pos)
125