Skip to content

Commit 812ff7e

Browse files
authored
First Kalman Filter
Still works in finite loops, no IMU implementation, has matplotlib, no time sleep
1 parent 667bbbb commit 812ff7e

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

KalmanFilterIMU.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
plt.rcParams['figure.figsize'] = (10, 8)
5+
6+
# intial parameters
7+
n_iter = 200
8+
sigma = 0.05
9+
sz = (n_iter,) # size of array
10+
x = 0.5 # truth value (typo in example at top of p. 13 calls this z)
11+
z = np.zeros(n_iter)
12+
z[:50] = np.random.normal(x,sigma,size=50) # observations (normal about x, sigma=0.1)
13+
z[50:100] = np.random.normal(0,sigma,size=50)
14+
z[100:150] = np.random.normal(x,sigma,size=50)
15+
z[150:200] = np.random.normal(0,sigma,size=50)
16+
testNewTruth = False
17+
confirmNewTruth = False
18+
indexVal = 0
19+
totalVelocity = 0.0
20+
totalVelocityMeasured = 0.0
21+
22+
Q = 1e-5 # process variance
23+
24+
# allocate space for arrays
25+
xhat=np.zeros(sz) # a posteri estimate of x
26+
P=np.zeros(sz) # a posteri error estimate
27+
xhatminus=np.zeros(sz) # a priori estimate of x
28+
Pminus=np.zeros(sz) # a priori error estimate
29+
G=np.zeros(sz) # gain or blending factor
30+
31+
R = sigma**2 # estimate of measurement variance, change to see effect
32+
33+
# intial guesses
34+
xhat[0] = 0.0
35+
P[0] = 1.0
36+
37+
for k in range(1,n_iter):
38+
39+
if (confirmNewTruth and abs(z[k]-z[k-1])<3*sigma):
40+
confirmNewTruth = False
41+
xhatminus[k] = 0.0
42+
Pminus[k] = 1.0+Q
43+
indexVal = 0
44+
else:
45+
# time update
46+
xhatminus[k] = xhat[k-1]
47+
Pminus[k] = P[k-1]+Q
48+
49+
if (testNewTruth):
50+
if (abs(z[k]-z[k-1])<3*sigma):
51+
confirmNewTruth = True
52+
testNewTruth = False
53+
54+
# measurement update
55+
G[k] = Pminus[k]/( Pminus[k]+R)
56+
xhat[k] = xhatminus[k]+G[k]*(z[k]-xhatminus[k])
57+
P[k] = (1-G[k])*Pminus[k]
58+
59+
#Velocity calculation
60+
totalVelocity += 1/2*(xhat[k]+xhat[k-1])*1.0
61+
totalVelocityMeasured += 1/2*(z[k]+z[k-1])*1.0
62+
63+
# checking for different truth
64+
if (abs(z[k]-xhat[k])>3*sigma and indexVal>3):
65+
testNewTruth = True
66+
67+
indexVal+=1
68+
69+
print("Total Velocity" + str(totalVelocity))
70+
print("Total Velocity Measured" + str(totalVelocityMeasured))
71+
72+
73+
plt.figure()
74+
plt.plot(z,'k+',label='noisy measurements')
75+
plt.plot(xhat,'b-',label='a posteri estimate')
76+
plt.axhline(x,color='g',label='truth value 1')
77+
plt.axhline(0,color='g',label='truth value 2')
78+
plt.axvline(50,color='g')
79+
plt.axvline(100,color='g')
80+
plt.axvline(150,color='g')
81+
plt.legend()
82+
plt.title('Estimate vs. iteration step', fontweight='bold')
83+
plt.xlabel('Iteration')
84+
plt.ylabel('Voltage')
85+
86+
#plt.figure()
87+
#valid_iter = range(1,n_iter) # Pminus not valid at step 0
88+
#plt.plot(valid_iter,Pminus[valid_iter],label='a priori error estimate')
89+
#plt.title('Estimated $\it{\mathbf{a \ priori}}$ error vs. iteration step', fontweight='bold')
90+
#plt.xlabel('Iteration')
91+
#plt.ylabel('$(Voltage)^2$')
92+
#plt.setp(plt.gca(),'ylim',[0,.01])
93+
plt.show()

0 commit comments

Comments
 (0)