0
$\begingroup$

I have implemented with a simple code a Kalman Filter for time domain, based on these:

  1. KG = error_est / (error_est+error_measurment)
  2. estimate = estimate_prev + KG ( measurment - estimate_prev)
  3. error_est = (1 - KG) * error_est_prev

The simple code to implement is :

error_est = 1.0
error_measurment = 1.0
estimates = []
start = 50.0

# loop over data array of numbers such as [1,2,3,4,..]

for index in range(len(data)):

    measurment = data[index]
    prev_estimate = estimates[index-1] if index>0 else start

    KG = error_est / (error_est+error_measurment)
    estimate = prev_estimate + KG * ( measurment -  prev_estimate)
    estimates.append(estimate)
    error_est = (1.0 - KG) * error_est

Now I have 3 questions,

  1. The filter is extremely slow, which mean for array of [1...50] , I will get a result of [1...25], and generally it is much slower than moving average, but i read that it's advantage is that it is fast. What am i doing wrong?

  2. I read that Kalman is used to estimate next result, but how is it possible that a filter ( based on the past) can be used to estimate the next result ?

  3. Are there any parameters you can add to tweak it (such as window in MA) ?

$\endgroup$
1
  • 2
    $\begingroup$ Please edit your question for details and clarity. What are your initial values of "error_est" and "measurement_error" ($p$ and $r$, in the scalar case). Do you mean it settles slower, or that it takes more processor resources? What do you mean by "for an array of [1..50], I will get a result of [1..25]" -- your vector size changes??? Your pseudo-code, as given, with an initial variance of $p_1^- = \infty$, results in a Kalman gain of $k_n = 1/n$, which makes your filter an infinitely long moving average. $\endgroup$
    – TimWescott
    May 23 at 18:30
0
$\begingroup$
  1. Maybe what is extremely slow is python, if you are comparing with a rolling average function implemented in some library.Specially if the dimension of the observations large, each step it will invert a matrix, $O(n^3)$, it seems you have only one observation in your example. Even for similar implementations Kalman filter will be slower than moving average.
  2. The Kalman filter uses the model of the system, from one state it can get a reasonable estimate, for instance if you are tracking a device with GPS data, you may create a model that assumes that the acceleration has limited variance, from the previous measurements of position you can have an estimate of velocity (part of the state), and if from there you can predict where the device will be.
  3. The things you can tweak in Kalman filter is the covariances of the process and the covariances of the observation, this will tell how much the filter should rely in the estimate vs. the observation. For instance if you don't trust your model you can set the model covariance $Q$ large, if your observations are not that accurate you acn increase the observation covariance $R$.
$\endgroup$
3
  • $\begingroup$ Thanks, but i doubt you understand my question, by slow I don't mean slow to calculate, but slow to follow the curve, so for array of 50 numbers 1-50, it will produce array of 50 numbers with values of 1-25, which means it can never climb enough, and after i saw it in action in other examples, i know it should be faster to follow signal. My code i guess is wrong. Moreover, not sure what you mean by tweaking the covariance, where is a cov in my code ? $\endgroup$
    – Shulas
    May 23 at 12:19
  • 1
    $\begingroup$ Hi: I don't have time to look carefully but you're KF updating equations don't look correct. If you google for "kalman filter tony lacey" a nice tutorial with all of the equations will come up. $\endgroup$
    – mark leeds
    May 23 at 13:49
  • $\begingroup$ Furthermore, if you want to follow a ramp closely you probably have to use a second order system, unless you can assume very little covariance for the measurements. $\endgroup$
    – Bob
    May 23 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.