Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96129 views
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sat Jan 17 15:04:08 2015
4
5
@author: rlabbe
6
"""
7
8
9
10
from numpy.random import randn
11
import numpy as np
12
from numpy import array
13
import matplotlib.pyplot as plt
14
from filterpy.common import Q_discrete_white_noise
15
from filterpy.kalman import KalmanFilter
16
from math import sqrt
17
import math
18
import random
19
20
pos_var = 1.
21
dop_var = 2.
22
dt = 1/20
23
24
25
def rand_student_t(df, mu=0, std=1):
26
"""return random number distributed by student's t distribution with
27
`df` degrees of freedom with the specified mean and standard deviation.
28
"""
29
x = random.gauss(0, std)
30
y = 2.0*random.gammavariate(0.5*df, 2.0)
31
return x / (math.sqrt(y/df)) + mu
32
33
34
35
np.random.seed(124)
36
class ConstantVelocityObject(object):
37
def __init__(self, x0, vel, noise_scale):
38
self.x = np.array([x0, vel])
39
self.noise_scale = noise_scale
40
self.vel = vel
41
42
43
def update(self, dt):
44
pnoise = abs(randn()*self.noise_scale)
45
if self.x[1] > self.vel:
46
pnoise = -pnoise
47
48
self.x[1] += pnoise
49
self.x[0] += self.x[1]*dt
50
51
return self.x.copy()
52
53
54
def sense_pos(self):
55
return self.x[0] + [randn()*self.noise_scale[0]]
56
57
58
def vel(self):
59
return self.x[1] + [randn()*self.noise_scale[1]]
60
61
62
def sense(x, noise_scale=(1., 0.01)):
63
return x + [randn()*noise_scale[0], randn()*noise_scale[1]]
64
65
def sense_t(x, noise_scale=(1., 0.01)):
66
return x + [rand_student_t(2)*noise_scale[0],
67
rand_student_t(2)*noise_scale[1]]
68
69
70
71
72
73
R2 = np.zeros((2, 2))
74
R1 = np.zeros((1, 1))
75
76
R2[0, 0] = pos_var
77
R2[1, 1] = dop_var
78
79
R1[0,0] = dop_var
80
81
H2 = np.array([[1., 0.], [0., 1.]])
82
H1 = np.array([[0., 1.]])
83
84
85
86
kf = KalmanFilter(2, 1)
87
kf.F = array([[1, dt],
88
[0, 1]])
89
kf.H = array([[1, 0],
90
[0, 1]])
91
kf.Q = Q_discrete_white_noise(2, dt, .02)
92
kf.x = array([0., 0.])
93
kf.R = R1
94
kf.H = H1
95
kf.P *= 10
96
97
98
random.seed(1234)
99
track = []
100
zs = []
101
ests = []
102
sensor_noise = (sqrt(pos_var), sqrt(dop_var))
103
obj = ConstantVelocityObject(0., 1., noise_scale=.18)
104
105
for i in range(30000):
106
x = obj.update(dt/10)
107
z = sense_t(x, sensor_noise)
108
if i % 10 != 0:
109
continue
110
111
if i % 60 == 87687658760:
112
kf.predict()
113
kf.update(z, H=H2, R=R2)
114
else:
115
kf.predict()
116
kf.update(z[1], H=H1, R=R1)
117
118
ests.append(kf.x)
119
120
track.append(x)
121
zs.append(z)
122
123
124
track = np.asarray(track)
125
zs = np.asarray(zs)
126
ests = np.asarray(ests)
127
128
plt.figure()
129
plt.subplot(211)
130
plt.plot(track[:,0], label='track')
131
plt.plot(zs[:,0], label='measurements')
132
plt.plot(ests[:,0], label='filter')
133
plt.legend(loc='best')
134
135
plt.subplot(212)
136
plt.plot(track[:,1], label='track')
137
plt.plot(zs[:,1], label='measurements')
138
plt.plot(ests[:,1], label='filter')
139
plt.show()
140
141
142