Plots and text for g-h filter chapter.

Still needs work, but I am generally happy with the content.
This commit is contained in:
Roger Labbe 2014-05-17 21:40:32 -07:00
parent 234b331b45
commit 3b8654f70b
6 changed files with 1216 additions and 911 deletions

33
Signals_and_Noise.ipynb Normal file
View File

@ -0,0 +1,33 @@
{
"metadata": {
"name": "",
"signature": "sha256:2b562cd882f0e19a9a7799763a6bf0d8986d23e7d0d648364928721d3c8a1026"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center><h1>Kalman and Bayesian Filters in Python</h1></center>\n",
"\n",
"<p>\n",
"<center><a href =\"http://nbviewer.ipython.org/urls/raw.github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/master/toc.ipynb\">Table of Contents</a></center>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:b105d6995f19073d8376cf55418d5b1ba33b4d192a9eda1b6733add7091d54b3"
"signature": "sha256:410ce21bf24ce671ae0a839a3fed5bea5ddb8cc7d05fb2719f7e8b1d57126411"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -249,7 +249,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#Introduction\n",
"#Discrete Bayes Filter\n",
"\n",
"The Kalman filter belongs to a family of filters called *bayesian filters*. Without going into"
]
},
@ -257,7 +258,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tracking a Dog\n",
"### Tracking a Dog\n",
"\n",
"Let us begin with a simple problem. We have a dog friendly workspace, and so people bring their dogs to work. However, occasionally the dogs wander out of your office and down the halls. We want to be able to track them. So during a hackathon somebody created a little sonar sensor to attach to the dog's collar. It emits a signal, listens for the echo, and based on how quickly an echo comes back we can tell whether the dog is in front of an open doorway or not. It also senses when the dog walks, and reports in which direction the dog has moved. It connects to our network via wifi and sends an update once a second.\n",
"\n",

File diff suppressed because one or more lines are too long

118
gh_internal.py Normal file
View File

@ -0,0 +1,118 @@
import numpy as np
import pylab as plt
from matplotlib.patches import Circle, Rectangle, Polygon, Arrow, FancyArrow
def create_predict_update_chart(box_bg = '#CCCCCC',
arrow1 = '#88CCFF',
arrow2 = '#88FF88'):
plt.figure(figsize=(6,6), facecolor='w')
ax = plt.axes((0, 0, 1, 1),
xticks=[], yticks=[], frameon=False)
#ax.set_xlim(0, 10)
#ax.set_ylim(0, 10)
pc = Circle((4,5), 0.5, fc=box_bg)
uc = Circle((6,5), 0.5, fc=box_bg)
ax.add_patch (pc)
ax.add_patch (uc)
plt.text(4,5, "Predict\nStep",ha='center', va='center', fontsize=14)
plt.text(6,5, "Update\nStep",ha='center', va='center', fontsize=14)
#btm
ax.annotate('',
xy=(4.1, 4.5), xycoords='data',
xytext=(6, 4.5), textcoords='data',
size=20,
arrowprops=dict(arrowstyle="simple",
fc="0.6", ec="none",
patchB=pc,
patchA=uc,
connectionstyle="arc3,rad=-0.5"))
#top
ax.annotate('',
xy=(6, 5.5), xycoords='data',
xytext=(4.1, 5.5), textcoords='data',
size=20,
arrowprops=dict(arrowstyle="simple",
fc="0.6", ec="none",
patchB=uc,
patchA=pc,
connectionstyle="arc3,rad=-0.5"))
ax.annotate('Measurement ($\mathbf{z_k}$)',
xy=(6.3, 5.4), xycoords='data',
xytext=(6,6), textcoords='data',
size=18,
arrowprops=dict(arrowstyle="simple",
fc="0.6", ec="none"))
ax.annotate('',
xy=(4.0, 3.5), xycoords='data',
xytext=(4.0,4.5), textcoords='data',
size=18,
arrowprops=dict(arrowstyle="simple",
fc="0.6", ec="none"))
ax.annotate('Initial\nConditions ($\mathbf{x_0}$)',
xy=(4.0, 5.5), xycoords='data',
xytext=(2.5,6.5), textcoords='data',
size=18,
arrowprops=dict(arrowstyle="simple",
fc="0.6", ec="none"))
plt.text (4,3.4,'State Estimate ($\mathbf{\hat{x}_k}$)',
ha='center', va='center', fontsize=18)
plt.axis('equal')
#plt.axis([0,8,0,8])
plt.show()
def plot_estimate_chart_1():
ax = plt.axes()
ax.annotate('', xy=[1,159], xytext=[0,158],
arrowprops=dict(arrowstyle='->', ec='r',shrinkA=6, lw=3,shrinkB=5))
plt.scatter ([0], [158], c='b')
plt.scatter ([1], [159], c='r')
plt.show()
def plot_estimate_chart_2():
ax = plt.axes()
ax.annotate('', xy=[1,159], xytext=[0,158],
arrowprops=dict(arrowstyle='->',
ec='r', lw=3, shrinkA=6, shrinkB=5))
plt.scatter ([0], [158.0], c='k',s=128)
plt.scatter ([1], [164.2], c='b',s=128)
plt.scatter ([1], [159], c='r', s=128)
plt.text (1.0, 158.8, "prediction ($\hat{x}_{t})$", ha='center',va='top',fontsize=18,color='red')
plt.text (1.0, 164.4, "measurement ($z$)",ha='center',va='bottom',fontsize=18,color='blue')
plt.text (0, 157.8, "estimate ($\hat{x}_{t-1}$)", ha='center', va='top',fontsize=18)
plt.show()
def plot_estimate_chart_3():
ax = plt.axes()
ax.annotate('', xy=[1,159], xytext=[0,158],
arrowprops=dict(arrowstyle='->',
ec='r', lw=3, shrinkA=6, shrinkB=5))
ax.annotate('', xy=[1,159], xytext=[1,164.2],
arrowprops=dict(arrowstyle='-',
ec='k', lw=1, shrinkA=8, shrinkB=8))
est_y = ((164.2-158)*.8 + 158)
plt.scatter ([0,1], [158.0,est_y], c='k',s=128)
plt.scatter ([1], [164.2], c='b',s=128)
plt.scatter ([1], [159], c='r', s=128)
plt.text (1.0, 158.8, "prediction ($\hat{x}_{t})$", ha='center',va='top',fontsize=18,color='red')
plt.text (1.0, 164.4, "measurement ($z$)",ha='center',va='bottom',fontsize=18,color='blue')
plt.text (0, 157.8, "estimate ($\hat{x}_{t-1}$)", ha='center', va='top',fontsize=18)
plt.text (0.95, est_y, "new estimate ($\hat{x}_{t}$)", ha='right', va='center',fontsize=18)
plt.show()
if __name__ == '__main__':
create_predict_update_chart()

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:62a110cf58205ec15bdd741effe2f73f0740853316b38e5a0a3edbc2d1cd7157"
"signature": "sha256:e5b629cac25efb2c3f414af72bb7b250b7a1a8bb7c1497c44a76c76ad4206ff6"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -21,12 +21,18 @@
"\n",
"Motivation for the book. Where to download, how to use.\n",
"\n",
"** add something about teaching methodology. exploration vs regurgitation.\n",
"\n",
"[**Introduction**](http://nbviewer.ipython.org/urls/raw.github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/master/Introduction.ipynb)\n",
" \n",
"Introduction to the Kalman filter. Explanation of the idea behind this book.\n",
"Yes, it is more or less the preface restated. will edit and delete one or the other.\n",
"\n",
"[**Chapter 0: Signals and Noise**](Nada)\n",
"\n",
"A brief introduction to signals and noise. Nomenclature and sample code.\n",
"\n",
"\n",
"\n",
"[**Chapter 1: The g-h Filter**](http://nbviewer.ipython.org/urls/raw.github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/master/g-h_filter.ipynb)\n",
"\n",