<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Space Station Lambda &#187; Mathematics</title>
	<atom:link href="http://blog.viridian-project.de/sections/mathematics/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.viridian-project.de</link>
	<description>Leslie P. Polzer on code, music, literature, design and free software business.</description>
	<lastBuildDate>Wed, 19 May 2010 07:44:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Kalman filtering</title>
		<link>http://blog.viridian-project.de/2008/01/08/kalman-filtering/</link>
		<comments>http://blog.viridian-project.de/2008/01/08/kalman-filtering/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 14:10:38 +0000</pubDate>
		<dc:creator>Leslie</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[beautiful code]]></category>
		<category><![CDATA[engineering]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java bashing]]></category>
		<category><![CDATA[java sucks]]></category>
		<category><![CDATA[kalman]]></category>
		<category><![CDATA[kalman filter]]></category>
		<category><![CDATA[programming pearl]]></category>
		<category><![CDATA[programming pearls]]></category>
		<category><![CDATA[q]]></category>

		<guid isPermaLink="false">http://blog.viridian-project.de/2008/01/08/kalman-filtering/</guid>
		<description><![CDATA[Engineering reality often confronts us with systems that can&#8217;t be modelled very well with fixed formulas. Sometimes we don&#8217;t even know some of the system equation&#8217;s variables; think of signal noise, for example. Enter the Kalman filtering algorithm. Given a number of observable and non-observable states, a Kalman filter tries to predict the non-observables ones. [...]]]></description>
			<content:encoded><![CDATA[<p>Engineering reality often confronts us with systems that can&#8217;t be modelled very well with fixed formulas.<br />
Sometimes we don&#8217;t even know some of the system equation&#8217;s variables; think of signal noise, for example.</p>
<p>Enter the Kalman filtering algorithm. Given a number of observable and non-observable states, a Kalman filter tries to predict the non-observables ones.</p>
<p>Normally the state vectors have a dimension greater than 1, so you have to fiddle around with matrices. We once implemented the filter in Java; it was astonishingly simple, but the Java idiosyncrasy of making even the most simple code look complicated diminished this simplicity considerably. Here&#8217;s the core of it, i.e. the code minus the various “object-oriented” boilerplate methods:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> filter<span style="color: #009900;">&#40;</span>State state<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> MatrixException <span style="color: #009900;">&#123;</span>
&nbsp;
    setStateTransitionMatrix<span style="color: #009900;">&#40;</span>delta, state.<span style="color: #006633;">getAcceleration</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    x <span style="color: #339933;">=</span> lastX<span style="color: #339933;">;</span>
&nbsp;
    y.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, state.<span style="color: #006633;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    y.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, state.<span style="color: #006633;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    y.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">0</span>, state.<span style="color: #006633;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getZ</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* PREDICT */</span>
    x <span style="color: #339933;">=</span> A.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>E.<span style="color: #006633;">isZeroMatrix</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>R <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>R.<span style="color: #006633;">isZeroMatrix</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        E <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>E<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">transpose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Q<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Matrix K <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">/* CORRECT */</span>
        K <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>E.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>B.<span style="color: #006633;">transpose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>B.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>E<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>B.<span style="color: #006633;">transpose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>R<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>K.<span style="color: #006633;">isZeroMatrix</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            x <span style="color: #339933;">=</span> x.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>K.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>y.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span>B.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            E <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>I.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span>K.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>B<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>E<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
    lastX <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
    setState<span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And now you may leave the torture chamber and take a look at <a href="http://www.gbkr.com/subjects/q/kalman.html">an implementation in q</a>. Beautiful!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.viridian-project.de/2008/01/08/kalman-filtering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
