kalman

package module
v0.0.3-0...-253affc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2020 License: MIT Imports: 3 Imported by: 0

README

Adaptive Kalman filtering in Golang

License GoDoc goreportcard

go get github.com/konimarti/kalman

  • Adaptive Kalman filtering with Rapid Ongoing Stochastic covariance Estimation (ROSE)

  • A helpful introduction to how Kalman filters work, can be found here.

  • Kalman filters are based on a state-space representation of linear, time-invariant systems:

    The next state is defined as

     x(t+1) = A_d * x(t) + B_d * u(t) 
    

    where A_d is the discretized prediction matrix and B_d the control matrix. x(t) is the current state and u(t) the external input. The response (measurement) of the system is y(t):

     y(t)  = C * x(t) + D * u(t) 
    

Using the standard Kalman filter

	// create filter
	filter := kalman.NewFilter(
		lti.Discrete{
			Ad, // prediction matrix (n x n)
			Bd, // control matrix (n x k)
			C,  // measurement matrix (l x n)
			D,  // measurement matrix (l x k)
		},
		kalman.Noise{
			Q, // process model covariance matrix (n x n)
			R, // measurement errors (l x l)
		},
	)

	// create context
	ctx := kalman.Context{
		X, // initial state (n x 1)
		P, // initial process covariance (n x n)
	}

	// get measurement (l x 1) and control (k x 1) vectors
	..

	// apply filter
	filteredMeasurement := filter.Apply(&ctx, measurement, control)
}
Results with standard Kalman filter

Results of Kalman filtering on car example.

See example here.

Results with Rapid Ongoing Stochasic covariance Estimation (ROSE) filter

Results of ROSE filtering.

See example here.

Math behind the Kalman filter
  • Calculation of the Kalman gain and the correction of the state vector ~x(k) and covariance matrix ~P(k):
    ^y(k)  = C * ^x(k) + D * u(k)
    dy(k)  = y(k) - ^y(k)
    K(k) = ^P(k) * C^T * ( C * ^P(k) * C^T + R(k) )^(-1)
    ~x(k) = ^x(k) + K(k) * dy(k)
    ~P(k) = ( I - K(k) * C) * ^P(k)
    
  • Then the next step is predicted for the state ^x(k+1) and the covariance ^P(k+1):
    ^x(k+1) = Ad * ~x(k) + Bd * u(k)
    ^P(k+1) = Ad * ~P(k) * Ad^T + Gd * Q(k) * Gd^T
    

Credits

This software package has been developed for and is in production at Kalkfabrik Netstal.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	X *mat.VecDense // Current system state
	P *mat.Dense    // Current covariance matrix
}

Context contains the current state and covariance of the system

type Filter

type Filter interface {
	Apply(ctx *Context, z, ctrl *mat.VecDense) mat.Vector
	State() mat.Vector
}

Filter interface for using the Kalman filter

func NewFilter

func NewFilter(lti lti.Discrete, nse Noise) Filter

NewFilter returns a Kalman filter

func NewRoseFilter

func NewRoseFilter(lti lti.Discrete, Gd *mat.Dense, gammaR, alphaR, alphaM float64) Filter

NewRoseFilter returns a ROSE Kalman filter Rapid Ongoing Stochasic covariance Estimation (ROSE) Filter lti: discrete linear, time-invariante system Gd: discretized G matrix for system noise gammaR: Gain factor for measurement noise alphaR: Kalman gain for measurment covariance noise alphaM: Kalman gain for covariance M

type Noise

type Noise struct {
	Q *mat.Dense // (discretized) system noise
	R *mat.Dense // measurement noise
}

Noise represents the measurement and system noise

func NewZeroNoise

func NewZeroNoise(q, r int) Noise

NewZeroNoise initializes a Noise struct q: dimension of square matrix Q r: dimension of square matrix R

Directories

Path Synopsis
example
car

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL