2015-05-11 03:28:45 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-08-01 17:46:14 +02:00
|
|
|
"""Copyright 2015 Roger R Labbe Jr.
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
|
2015-08-01 17:46:14 +02:00
|
|
|
Code supporting the book
|
2015-05-11 03:28:45 +02:00
|
|
|
|
2015-08-01 17:46:14 +02:00
|
|
|
Kalman and Bayesian Filters in Python
|
|
|
|
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
|
2015-08-01 17:52:48 +02:00
|
|
|
This is licensed under an MIT license. See the LICENSE.txt file
|
2015-08-01 17:46:14 +02:00
|
|
|
for more information.
|
|
|
|
"""
|
2015-05-11 03:28:45 +02:00
|
|
|
|
2015-08-01 17:46:14 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function,
|
|
|
|
unicode_literals)
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
|
2015-08-01 17:46:14 +02:00
|
|
|
import math
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import numpy as np
|
|
|
|
from numpy.random import uniform
|
|
|
|
from numpy.random import randn
|
|
|
|
import scipy.stats
|
|
|
|
import random
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-06-27 17:37:14 +02:00
|
|
|
N = 2000
|
|
|
|
pf = ParticleFilter(N, 100, 100)
|
|
|
|
#pf.particles[:,2] = np.random.randn(pf.N)*np.radians(10) + np.radians(45)
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
z = np.array([20, 20])
|
2015-06-25 06:00:57 +02:00
|
|
|
#pf.create_particles(mean=z, variance=40)
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
mu0 = np.array([0., 0.])
|
2015-08-01 17:46:14 +02:00
|
|
|
plt.plot(pf, weights=False)
|
2015-05-11 03:28:45 +02:00
|
|
|
|
2015-06-25 06:00:57 +02:00
|
|
|
|
2015-06-23 02:56:14 +02:00
|
|
|
fig = plt.gcf()
|
2015-06-27 17:37:14 +02:00
|
|
|
#fig.show()
|
|
|
|
#fig.canvas.draw()
|
|
|
|
#plt.ioff()
|
2015-06-23 02:56:14 +02:00
|
|
|
|
2015-06-27 17:37:14 +02:00
|
|
|
for x in range(10):
|
2015-05-11 03:28:45 +02:00
|
|
|
|
2015-06-25 06:00:57 +02:00
|
|
|
z[0] = x+1 + randn()*0.3
|
|
|
|
z[1] = x+1 + randn()*0.3
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
|
2015-06-27 17:37:14 +02:00
|
|
|
pf.predict((1,1), (0.2, 0.2))
|
2015-06-25 06:00:57 +02:00
|
|
|
pf.weight(z=z, var=.8)
|
|
|
|
neff = pf.neff()
|
|
|
|
|
2015-06-27 17:37:14 +02:00
|
|
|
print('neff', neff)
|
|
|
|
if neff < N/2 or N <= 2000:
|
2015-06-25 06:00:57 +02:00
|
|
|
pf.resample()
|
2015-05-11 03:28:45 +02:00
|
|
|
mu, var = pf.estimate()
|
|
|
|
if x == 0:
|
|
|
|
mu0 = mu
|
2015-06-25 06:00:57 +02:00
|
|
|
#print(mu - z)
|
2015-05-11 03:28:45 +02:00
|
|
|
#print(var)
|
|
|
|
|
2015-06-25 06:00:57 +02:00
|
|
|
plot(pf, weights=True)
|
|
|
|
#plt.plot(z[0], z[1], marker='v', c='r', ms=10)
|
|
|
|
plt.plot(x+1, x+1, marker='*', c='r', ms=10)
|
2015-05-11 03:28:45 +02:00
|
|
|
plt.scatter(mu[0], mu[1], c='g', s=100)#,
|
|
|
|
#s=min(500, abs((1./np.sum(var)))*20), alpha=0.5)
|
2015-06-25 06:00:57 +02:00
|
|
|
plt.plot([0,100], [0,100])
|
2015-05-11 03:28:45 +02:00
|
|
|
plt.tight_layout()
|
2015-06-27 17:37:14 +02:00
|
|
|
plt.pause(.002)
|
2015-06-25 06:00:57 +02:00
|
|
|
|
2015-06-27 17:37:14 +02:00
|
|
|
#fig.canvas.draw()
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
#pf.assign_speed_by_gaussian(1, 1.5)
|
|
|
|
#pf.move(h=[1,1], v=1.4, t=1)
|
|
|
|
#pf.control(mu-mu0)
|
|
|
|
mu0 = mu
|
|
|
|
|
2015-06-27 17:37:14 +02:00
|
|
|
plt.ion()
|
|
|
|
|
2015-05-11 03:28:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|