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
import math
18
from numpy import array, asarray
19
from numpy.random import randn
20
import matplotlib.pyplot as plt
21
22
from filterpy.kalman import UnscentedKalmanFilter as UKF
23
from filterpy.kalman import ScaledUnscentedKalmanFilter as SUKF
24
from filterpy.common import Q_discrete_white_noise
25
26
27
""" This is an example of the bearing only problem. You have a platform,
28
usually a ship, that can only get the bearing to a moving target. Assuming
29
platform is stationary, this is a very difficult problem because there are
30
an infinite number of solutions. The literature is filled with this example,
31
along with proposed solutions (usually, platform makes manuevers).
32
33
"""
34
dt = 0.1
35
y = 20
36
platform_pos=(0,20)
37
38
39
40
sf = SUKF(2, 1, dt, alpha=1.e-4, beta=2., kappa=1.)
41
sf.Q = Q_discrete_white_noise(2, dt, .1)
42
43
44
45
f = UKF(2, 1, dt, kappa=0.)
46
f.Q = Q_discrete_white_noise(2, dt, .1)
47
48
def fx(x,dt):
49
""" state transition function"""
50
51
# pos = pos + vel
52
# vel = vel
53
return array([x[0]+x[1], x[1]])
54
55
56
def hx(x):
57
""" measurement function - convert position to bearing"""
58
59
return math.atan2(platform_pos[1],x[0]-platform_pos[0])
60
61
62
xs_scaled = []
63
xs = []
64
for i in range(300):
65
angle = hx([i+randn()*.1, 0]) + randn()
66
sf.update(angle, hx, fx)
67
xs_scaled.append(sf.x)
68
69
f.predict(fx)
70
f.update(angle, hx)
71
xs.append(f.x)
72
73
74
xs_scaled = asarray(xs_scaled)
75
xs = asarray(xs)
76
77
plt.subplot(211)
78
plt.plot(xs_scaled[:,0],label='scaled')
79
plt.plot(xs[:,0], label='Julier')
80
plt.legend(loc=4)
81
82
plt.subplot(212)
83
plt.plot(xs_scaled[:,1],label='scaled')
84
plt.plot(xs[:,1], label='Julier')
85
plt.legend(loc=4)
86
plt.show()
87
88
89
90