Copy edit of chapter 8.

Note fully done, still have to figure out what to do with the ball
tracking code, and add a control example to the chapter.
This commit is contained in:
Roger Labbe 2015-07-13 14:42:34 -07:00
parent acdc04af34
commit 8b233b4ff5
5 changed files with 768 additions and 884 deletions

File diff suppressed because one or more lines are too long

View File

@ -277,23 +277,21 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The Kalman filter that we have developed to this point is extremely good, but it is also limited. All of the equations are linear, and so the filter can only handle linear problems. But the world is nonlinear, and so the classic filter that we have been studying to this point can have very limited utility.For example, the state transition function is $\\mathbf{x} = \\mathbf{FX}$. Suppose we wanted to track the motion of a weight on a spring. We might be tracking an automobile's active suspension, for example. The equation for the motion with $m$ being the mass, $k$ the spring constant, and $c$ the damping force is \n",
"The Kalman filter that we have developed to this point is extremely good, but it is also limited. All of the equations are linear, and so the filter can only handle linear problems. But the world is nonlinear, and so the classic filter that we have been studying to this point can have very limited utility.For example, the state transition function is $\\mathbf{x}^- = \\mathbf{Fx}$. Suppose we wanted to track the motion of a weight on a spring. We might be tracking an automobile's active suspension, for example. The equation for the motion with $m$ being the mass, $k$ the spring constant, and $c$ the damping force is \n",
"\n",
"$$m\\frac{d^2x}{dt^2} + c\\frac{dx}{dt} +kx = 0$$\n",
"\n",
"which is a second order differential equation. It is not possible to write a linear equation for this equation, and therefore we cannot even formulate a design for the Kalman filter. \n",
"\n",
"The early adopters of Kalman filters needed to apply the filters to nonlinear problems and this fact was not lost on them. For example, radar is inherently nonlinear. Radars measure the slant range to an object, and we are typically interested in the aircraft's position over the ground. We invoke Pythagoras and get the nonlinear equation:\n",
"The early adopters of Kalman filters needed to apply the filters to nonlinear problems and this fact was not lost on them. For example, radars measure the slant range to an object, and we are typically interested in the aircraft's position over the ground. We invoke Pythagoras and get the nonlinear equation:\n",
"\n",
"$$x=\\sqrt{slant^2 - altitude^2}$$\n",
"\n",
"And then, of course, the behavior of the objects being tracked is also nonlinear. So shortly after the idea of the Kalman filter was published by Kalman people began working on how to extend the Kalman filter into nonlinear problems. \n",
"\n",
"It is almost true to state that the only equation you know how to solve is $\\mathbf{Ax}=\\mathbf{b}$. I don't mean you the reader, but the entire mathematical community. We only really know how to do linear algebra. I can give you a linear equation and you can solve it. If I told you your job depended on knowing how to solve the equation $2x+3=4$ you would trivially solve it. If I then told you I needed to know the answer to $2x-3=8$ you would not break out into a sweat because we know all the rules for solving linear equations. I could write a program to randomly generate linear equations and you would be able to solve every one.\n",
"It is almost true to state that the only equation we (anyone) know how to solve is $\\mathbf{Ax}=\\mathbf{b}$. We only really know how to do linear algebra. I can give you a linear equation and you can solve it. If I told you your job depended on knowing how to solve the equation $2x+3=4$ you would trivially solve it. I could write a program to randomly generate linear equations and you would be able to solve every one.\n",
"\n",
"Any of us with a formal mathematical education spent years learning various analytic ways to solve integrals, differential equations and so on. It was all a lie, nearly. Even trivial physical systems produce equations that no one in the world know how to solve analytically. I can give you an equation that you are able to integrate, multiply or add in an $ln$ term, and render it insolvable. \n",
"\n",
"Instead of embracing that fact we spent our days studying ridiculous simplifications of problems. It is stuff like this that leads to jokes about physicists stating things like \"I have the solution! Assume a spherical cow on a frictionless surface in a vacuum...\"\n",
"Anyone with formal mathematical education has spent years learning various analytic ways to solve integrals, differential equations and so on. It was all a lie, nearly. Even trivial physical systems produce equations that no one in the world know how to solve analytically. I can give you an equation that you are able to integrate, multiply or add in an $\\log$ term, and render it insolvable. It is stuff like this that leads to jokes about physicists stating stating the problem \"assume a spherical cow on a frictionless surface in a vacuum...\"\n",
"\n",
"So how do we do things like model airflow over an aircraft in a computer, or predict weather, or track missiles with a Kalman filter? We retreat to what we know: $\\mathbf{Ax}=\\mathbf{b}$. We find some way to linearize the problem, turning it into a set of linear equations, and then use our linear algebra software packages to grind out a solution. It's an approximation, so the answers are approximate. Linearizing a nonlinear problem gives us inexact answers, and in a recursive algorithm like a Kalman filter or weather tracking system these small errors can sometimes reinforce each other at each step, quickly causing the algorithm to spit out nonsense. \n",
"\n",

View File

@ -83,8 +83,13 @@ def plot_measurements(xs, ys=None, color='r', lw=2, label='Measurements', **kwar
def plot_residual_limits(Ps):
std = np.sqrt(Ps)
def plot_residual_limits(Ps, stds=1.):
""" plots standand deviation given in Ps as a yellow shaded region. One std
by default, use stds for a different choice (e.g. stds=3 for 3 standard
deviations.
"""
std = np.sqrt(Ps) * stds
plt.plot(-std, color='k', ls=':', lw=2)
plt.plot(std, color='k', ls=':', lw=2)

View File

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 13 13:20:27 2015
@author: Roger
"""
from filterpy.kalman import UnscentedKalmanFilter as UKF
from filterpy.kalman import JulierSigmaPoints
from math import atan2
import numpy as np
def hx(x):
""" compute measurements corresponding to state x"""
dx = x[0] - hx.radar_pos[0]
dy = x[1] - hx.radar_pos[1]
return np.array([atan2(dy,dx), (dx**2 + dy**2)**.5])
def fx(x,dt):
""" predict state x at 'dt' time in the future"""
return x
def set_radar_pos(pos):
global hx
hx.radar_pos = pos
def sensor_fusion_kf():
global hx, fx
# create unscented Kalman filter with large initial uncertainty
points = JulierSigmaPoints(2, kappa=2)
kf = UKF(2, 2, dt=0.1, hx=hx, fx=fx, points=points)
kf.x = np.array([100, 100.])
kf.P *= 40
return kf

View File

@ -359,8 +359,6 @@ def plot_track(ps, zs, cov, std_scale=1,
plot_P=True, y_lim=None,
title='Kalman Filter'):
print(std_scale)
count = len(zs)
actual = np.linspace(0, count - 1, count)
cov = np.asarray(cov)