title: Kalman filter (one-dimensional): several approaches?
body: <p>I try to understand how the Kalman filter works and because the multi-dimensional variants were too confusing for the beginning I started off with a one-dimensional example.</p>
  
  <p>I found 3 different sources explaining the scenario of a thermometer but all of these scenarios implement slightly different equations and I do not get the point.</p>
  
  <p>I implemented <strong>solution 2</strong> but my kalman filter was not really working (it highly adapted itself to the measurements and not really considered the noise on it).</p>
  
  <p>So, before I waste more time trying solution 1 or 3 (which I have just read until now): <strong>Can someone supply a clean explanation and/or code example for a one dimensional Kalman filter?</strong></p>
  
  <hr>
  
  <p><strong>Solution 1</strong></p>
  
  <pre><code>// x_est: current estimate;           p: current estimate error;
  // a:     constant of the system;    kg: kalman gain
  // z:     current observation; 
  
  // Predict
  x_est   =   a * x_est
  p       =   a * p * a
  
  // Update
  kg      =   p  / (p  + r)
  x_est   =   x_est + kg * (z - x_est)
  p       =   (1 - kg) * p
  </code></pre>
  
  <p>The author (here) only explains that we are changing only current values because there is no need for a thermometer to consider the last value.</p>
  
  <p>So he simplified:</p>
  
  <p><code>p[k] = (1 - kg) * p[k-1]</code> <strong>to</strong> <code>p = (1 - kg) * p</code></p>
  
  <p><code>x_est[k] = x_est[k-1] + kg * (z - x_est[k-1])</code> <strong>to</strong> <code>x_est = x_est + kg * (z - x_est)</code></p>
  
  <p>...and so on...</p>
  
  <p>I do not understand why this is even possible. I thought one of the main parts of the Kalman filter is to consider wether the current observation <em>z</em> is useful or not (via the Kalman gain). So that for a high Kalman gain <code>kg * (z - x_est[k-1])</code> a "big chunk" of the delta <code>z - x_est[k-1]</code> is added to the new estimate. Isn't this whole thing getting pointless, if one always calculates the current values?</p>
  
  <hr>
  
  <p><strong>Solution 2</strong></p>
  
  <pre><code># q: process variance / process noise
  # r: error in measurement
  
  x_est = x_est
  p     = p + q;
  
  k     = p / (p + r);
  x_est = x_est + k * (z – x_est);
  p     = (1 – k) * p;
  </code></pre>
  
  <p>This is pretty much the same, but the author did not even give an explanation why <em>x[k-1]</em> and <em>p[k-1]</em> can be altered to <em>x</em> and <em>p</em>.</p>
  
  <hr>
  
  <p><strong>Solution 3</strong></p>
  
  <pre><code># Q: process variance / process noise
  # R: error in measurement
  
  # prediction
  x_est_kminus1[k] = x_est[k - 1]
  p_kminus1[k]        = p[k - 1] + Q
  
  # update
  kg[k]     = p_kminus1[k] / (p_kminus1[k] + R)
  x_est[k] = x_est_kminus1[k] + kg[k] * (z[k] - x_est_kminus1[k])
  p[k]     = (1 - kg[k]) * p_kminus1[k]
  </code></pre>
  
  <p>In this solution the author had two different lists for <code>x_est</code> (<code>x_est</code> itself and <code>x_est_kminus1</code>) and <code>p</code> (<code>p</code> itself and <code>p_kminus1</code>).</p>
  
  <p>Are two lists needed because otherwise p[k] would be calculated twice (in the prediction and the update step)?</p>