Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872# -*- coding: utf-8 -*-1"""2Created on Wed Jun 4 12:33:38 201434@author: rlabbe5"""67from __future__ import print_function,division8from filterpy.kalman import KalmanFilter9import numpy as np10import matplotlib.pyplot as plt11import baseball12from numpy.random import randn1314def ball_filter6(dt,R=1., Q = 0.1):15f1 = KalmanFilter(dim=6)16g = 101718f1.F = np.mat ([[1., dt, dt**2, 0, 0, 0],19[0, 1., dt, 0, 0, 0],20[0, 0, 1., 0, 0, 0],21[0, 0, 0., 1., dt, -0.5*dt*dt*g],22[0, 0, 0, 0, 1., -g*dt],23[0, 0, 0, 0, 0., 1.]])2425f1.H = np.mat([[1,0,0,0,0,0],26[0,0,0,0,0,0],27[0,0,0,0,0,0],28[0,0,0,1,0,0],29[0,0,0,0,0,0],30[0,0,0,0,0,0]])313233f1.R = np.mat(np.eye(6)) * R3435f1.Q = np.zeros((6,6))36f1.Q[2,2] = Q37f1.Q[5,5] = Q38f1.x = np.mat([0, 0 , 0, 0, 0, 0]).T39f1.P = np.eye(6) * 50.40f1.B = 0.41f1.u = 04243return f1444546def ball_filter4(dt,R=1., Q = 0.1):47f1 = KalmanFilter(dim=4)48g = 104950f1.F = np.mat ([[1., dt, 0, 0,],51[0, 1., 0, 0],52[0, 0, 1., dt],53[0, 0, 0., 1.]])5455f1.H = np.mat([[1,0,0,0],56[0,0,0,0],57[0,0,1,0],58[0,0,0,0]])59606162f1.B = np.mat([[0,0,0,0],63[0,0,0,0],64[0,0,1.,0],65[0,0,0,1.]])6667f1.u = np.mat([[0],68[0],69[-0.5*g*dt**2],70[-g*dt]])7172f1.R = np.mat(np.eye(4)) * R7374f1.Q = np.zeros((4,4))75f1.Q[1,1] = Q76f1.Q[3,3] = Q77f1.x = np.mat([0, 0 , 0, 0]).T78f1.P = np.eye(4) * 50.79return f1808182def plot_ball_filter6 (f1, zs, skip_start=-1, skip_end=-1):83xs, ys = [],[]84pxs, pys = [],[]8586for i,z in enumerate(zs):87m = np.mat([z[0], 0, 0, z[1], 0, 0]).T8889f1.predict ()9091if i == skip_start:92x2 = xs[-2]93x1 = xs[-1]94y2 = ys[-2]95y1 = ys[-1]9697if i >= skip_start and i <= skip_end:98#x,y = baseball.predict (x2, y2, x1, y1, 1/30, 1/30* (i-skip_start+1))99x,y = baseball.predict (xs[-2], ys[-2], xs[-1], ys[-1], 1/30, 1/30)100101m[0] = x102m[3] = y103#print ('skip', i, f1.x[2],f1.x[5])104105f1.update(m)106107108'''109if i >= skip_start and i <= skip_end:110#f1.x[2] = -0.1111#f1.x[5] = -17.112pass113else:114f1.update (m)115116'''117118xs.append (f1.x[0,0])119ys.append (f1.x[3,0])120pxs.append (z[0])121pys.append(z[1])122123if i > 0 and z[1] < 0:124break;125126p1, = plt.plot (xs, ys, 'r--')127p2, = plt.plot (pxs, pys)128plt.axis('equal')129plt.legend([p1,p2], ['filter', 'measurement'], 2)130plt.xlim([0,xs[-1]])131plt.show()132133134135def plot_ball_filter4 (f1, zs, skip_start=-1, skip_end=-1):136xs, ys = [],[]137pxs, pys = [],[]138139for i,z in enumerate(zs):140m = np.mat([z[0], 0, z[1], 0]).T141142f1.predict ()143144if i == skip_start:145x2 = xs[-2]146x1 = xs[-1]147y2 = ys[-2]148y1 = ys[-1]149150if i >= skip_start and i <= skip_end:151#x,y = baseball.predict (x2, y2, x1, y1, 1/30, 1/30* (i-skip_start+1))152x,y = baseball.predict (xs[-2], ys[-2], xs[-1], ys[-1], 1/30, 1/30)153154m[0] = x155m[2] = y156157f1.update (m)158159160'''161if i >= skip_start and i <= skip_end:162#f1.x[2] = -0.1163#f1.x[5] = -17.164pass165else:166f1.update (m)167168'''169170xs.append (f1.x[0,0])171ys.append (f1.x[2,0])172pxs.append (z[0])173pys.append(z[1])174175if i > 0 and z[1] < 0:176break;177178p1, = plt.plot (xs, ys, 'r--')179p2, = plt.plot (pxs, pys)180plt.axis('equal')181plt.legend([p1,p2], ['filter', 'measurement'], 2)182plt.xlim([0,xs[-1]])183plt.show()184185186start_skip = 20187end_skip = 60188189def run_6():190dt = 1/30191noise = 0.0192f1 = ball_filter6(dt, R=.16, Q=0.1)193plt.cla()194195x,y = baseball.compute_trajectory(v_0_mph = 100., theta=50., dt=dt)196197198znoise = [(i+randn()*noise,j+randn()*noise) for (i,j) in zip(x,y)]199200plot_ball_filter6 (f1, znoise, start_skip, end_skip)201202203def run_4():204dt = 1/30205noise = 0.0206f1 = ball_filter4(dt, R=.16, Q=0.1)207plt.cla()208209x,y = baseball.compute_trajectory(v_0_mph = 100., theta=50., dt=dt)210211212znoise = [(i+randn()*noise,j+randn()*noise) for (i,j) in zip(x,y)]213214plot_ball_filter4 (f1, znoise, start_skip, end_skip)215216217if __name__ == "__main__":218run_4()219220