Search code examples
pythonsmoothingkalman-filter

Kalman smoother in python


My data

1    14.7
2    14.58
3    14.82
4    14.59
5    14.67
...  ...
150  13.76

I already draw graph and upper/lower envelope line. Next, I want to apply Kalman smoother for u/l envelope line. Is there proper example about Kalman smoother??


Solution

  • Here is my implementation of kernel filter, works fine on my data. This code was written based on https://www.kalmanfilter.net/kalman1d.html I hope this helps.

    def kalmanfilter(x,p,z,r):
        # p - estimate unceratininty 
        # r - measurement unceratininty ( σ2 )  
        # z - Measured System State
    
        # Kalman gain calculation
        K =  p/(p+r)
        # estimate current state
        x1 = x + K*(z-x)
        # update current estimate uncertainity
        p1 = (1-K)*p
    
        return (x1,p1)