Git Product home page Git Product logo

extended-kalman-filter's Introduction

extended-kalman-filter

Extended Kalman Filter implemented in Java with easy representation of model and observation functions

One-dimensional example of moving object

Let's start with a simple example of object which is moving in one dimension. Object has position x and velocity v. Object starts at unknown position and unknown velocity. Object is being observed at time points i=0,1..10 at positions y0=0, y1=1, ... y10=10. The task is to estimate the position and velocity at time i=10.

Defining state equations

State equation is defined as follows

equation

where state vector x and system function f are defined as

equation

equation

In this simple example function f is linear. Nevertheless the library is written for general non-linear Extended Kalman Filter case. That is why we need to provide jacobian of f

equation

In Java the above formulas can be provided by subclassing the ProcessModel class:

public class Linear1dProcessModel extends ProcessModel {

	@Override
	public int stateDimension() {
		return 2;
	}

	@Override
	public void initialState(double[][] x) {
		x[0][0] = 0;
		x[1][0] = 0;
	}

	@Override
	public void initialStateCovariance(double[][] cov) {
		cov[0][0] = 1000;
		cov[0][1] = 0;
		cov[1][0] = 0;
		cov[1][1] = 1000;
	}

	@Override
	public void stateFunction(double[][] x, double[][] f) {
		f[0][0] = x[1][0];
		f[1][0] = 0;
	}

	@Override
	public void stateFunctionJacobian(double[][] x, double[][] j) {
		j[0][0] = 0;
		j[0][1] = 1;
		j[1][0] = 0;
		j[1][1] = 0;
	}

	@Override
	public void processNoiseCovariance(double[][] cov) {
		cov[0][0] = 1;
		cov[0][1] = 0;
		cov[1][0] = 0;
		cov[1][1] = 1;
	}

	public double getX() {
		return getState()[0][0];
	}

	public double getV() {
		return getState()[1][0];
	}
}

As you can see, one can access state estimate variables by calling getState() from the parent class

Defining observation equations

Observation equation is defined as follows

equation

where in our example we are observing position so h is defined as

equation

Jacobian of function h is computed as

equation

Above formulas are implemented in Java by subclassing the ObservationModel:

public class Linear1dObservationModel extends ObservationModel {

	private double mx;

	public void setPosition(double x) {
		this.mx = x;
	}

	@Override
	public int observationDimension() {
		return 1;
	}

	@Override
	public int stateDimension() {
		return 2;
	}

	@Override
	public void observationMeasurement(double[][] y) {
		y[0][0] = mx;
	}

	@Override
	public void observationModel(double[][] x, double[][] h) {
		h[0][0] = x[0][0];
	}

	@Override
	public void observationModelJacobian(double[][] j) {
		j[0][0] = 1;
		j[0][1] = 0;
	}

	@Override
	public void observationNoiseCovariance(double[][] cov) {
		cov[0][0] = 1;
	}
}

Running the code

Below code shows how to use defined process model, observation model and Kalman filter to compute estimates of position and velocity at particular time

public class Linear1dModelTest {

	@Test
	public void test() {
		Linear1dProcessModel model = new Linear1dProcessModel();
		Linear1dObservationModel obs = new Linear1dObservationModel();
		KalmanFilter filter = new KalmanFilter(model);

		for (int i = 0; i <= 10; ++i) {
			double time = i;
			obs.setPosition(i);
			filter.update(time, obs);
		}

		double x = model.getState()[0][0];
		double v = model.getState()[1][0];

		Assert.assertEquals(10, x, 1e-3);
		Assert.assertEquals(1, v, 1e-3);
	}
}

extended-kalman-filter's People

Contributors

wmlynar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.