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 __future__ import (absolute_import, division, print_function,
18
unicode_literals)
19
import numpy as np
20
from numpy import random
21
import matplotlib.pyplot as plt
22
from filterpy.kalman import KalmanFilter
23
24
25
if __name__ == '__main__':
26
27
fk = KalmanFilter(dim_x=2, dim_z=1)
28
29
fk.x = np.array([-1., 1.]) # initial state (location and velocity)
30
31
fk.F = np.array([[1.,1.],
32
[0.,1.]]) # state transition matrix
33
34
fk.H = np.array([[1.,0.]]) # Measurement function
35
fk.P = .01 # covariance matrix
36
fk.R = 5 # state uncertainty
37
fk.Q = 0.001 # process uncertainty
38
39
40
zs = [t + random.randn()*4 for t in range (40)]
41
42
mu, cov, _, _ = fk.batch_filter (zs)
43
mus = [x[0] for x in mu]
44
45
M,P,C = fk.rts_smoother(mu, cov)
46
47
48
49
# plot data
50
p1, = plt.plot(zs,'cyan', alpha=0.5)
51
p2, = plt.plot (M[:,0],c='b')
52
p3, = plt.plot (mus,c='r')
53
p4, = plt.plot ([0,len(zs)],[0,len(zs)], 'g') # perfect result
54
plt.legend([p1,p2, p3, p4],
55
["measurement", "RKS", "KF output", "ideal"], loc=4)
56
57
58
plt.show()
59
60
61