Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96131 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 numpy.random import randn
18
import numpy as np
19
import matplotlib.pyplot as plt
20
from filterpy.kalman import EnsembleKalmanFilter as EnKF
21
from filterpy.common import Q_discrete_white_noise
22
from math import cos, sin
23
24
DO_PLOT = False
25
26
def test_1d_const_vel():
27
28
def hx(x):
29
return np.array([x[0]])
30
31
F = np.array([[1., 1.],[0., 1.]])
32
def fx(x, dt):
33
return np.dot(F, x)
34
35
x = np.array([0., 1.])
36
P = np.eye(2)* 100.
37
f = EnKF(x=x, P=P, dim_z=1, dt=1., N=8, hx=hx, fx=fx)
38
39
std_noise = 10.
40
41
f.R *= std_noise**2
42
f.Q = Q_discrete_white_noise(2, 1., .001)
43
44
measurements = []
45
results = []
46
ps = []
47
48
zs = []
49
for t in range (0,100):
50
# create measurement = t plus white noise
51
z = t + randn()*std_noise
52
zs.append(z)
53
54
f.predict()
55
f.update(np.asarray([z]))
56
57
# save data
58
results.append (f.x[0])
59
measurements.append(z)
60
ps.append(3*(f.P[0,0]**.5))
61
62
results = np.asarray(results)
63
ps = np.asarray(ps)
64
65
if DO_PLOT:
66
plt.plot(results, label='EnKF')
67
plt.plot(measurements, c='r', label='z')
68
plt.plot (results-ps, c='k',linestyle='--', label='3$\sigma$')
69
plt.plot(results+ps, c='k', linestyle='--')
70
plt.legend(loc='best')
71
#print(ps)
72
73
74
75
def test_circle():
76
def hx(x):
77
return np.array([x[0], x[3]])
78
79
F = np.array([[1., 1., .5, 0., 0., 0.],
80
[0., 1., 1., 0., 0., 0.],
81
[0., 0., 1., 0., 0., 0.],
82
[0., 0., 0., 1., 1., .5],
83
[0., 0., 0., 0., 1., 1.],
84
[0., 0., 0., 0., 0., 1.]])
85
86
def fx(x, dt):
87
return np.dot(F, x)
88
89
x = np.array([50., 0., 0, 0, .0, 0.])
90
P = np.eye(6)* 100.
91
f = EnKF(x=x, P=P, dim_z=2, dt=1., N=30, hx=hx, fx=fx)
92
93
std_noise = .1
94
95
f.R *= std_noise**2
96
f.Q[0:3, 0:3] = Q_discrete_white_noise(3, 1., .001)
97
f.Q[3:6, 3:6] = Q_discrete_white_noise(3, 1., .001)
98
99
measurements = []
100
results = []
101
102
zs = []
103
for t in range (0,300):
104
a = t / 300000
105
x = cos(a) * 50.
106
y = sin(a) * 50.
107
# create measurement = t plus white noise
108
z = np.array([x,y])
109
zs.append(z)
110
111
f.predict()
112
f.update(z)
113
114
# save data
115
results.append (f.x)
116
measurements.append(z)
117
118
results = np.asarray(results)
119
measurements = np.asarray(measurements)
120
121
if DO_PLOT:
122
plt.plot(results[:,0], results[:,2], label='EnKF')
123
plt.plot(measurements[:,0], measurements[:,1], c='r', label='z')
124
#plt.plot (results-ps, c='k',linestyle='--', label='3$\sigma$')
125
#plt.plot(results+ps, c='k', linestyle='--')
126
plt.legend(loc='best')
127
plt.axis('equal')
128
#print(ps)
129
130
131
132
133
134
135
if __name__ == '__main__':
136
DO_PLOT = True
137
test_circle ()
138
#test_1d_const_vel()
139
140
141
#test_noisy_1d()
142