## linearkalman

Linear Kalman filtering and smoothing

### 4 releases

Uses old Rust 2015

 0.1.3 Feb 8, 2017 Feb 1, 2017 Jan 19, 2017 Jan 19, 2017

#166 in Science

12KB
89 lines

# Kalman filtering and smoothing library written in Rust

Access documentation for the library here. Library is also referenced in Cargo index.

Currently, library provides only time-invariant linear Kalman filtering and smoothing technique is known as fixed-interval smoothing (Rauch-Tung-Striebel smoother) which relies on Kalman filter estimates for the entire dataset. `linearkalman` relies on rulinalg library to implement linear algebra structures and operations and so input data is expected to be a `std::vec::Vec` of Vector objects, i.e. a vector of vectors.

In order to use this library, make sure your `Cargo.toml` file contains the following:

``````[dependencies]
linearkalman = "0.1.3"
``````

Library can then be imported using:

``````extern crate linearkalman;
``````

## Example

Below example assumes 3-dimensional measurement data with an underlying 2-dimensional state space model. With the help of a few macros from rulinalg, a simple attempt to use the library to run Kalman filter and smoother would be as follows.

``````#[macro_use]
extern crate rulinalg;
extern crate linearkalman;

use rulinalg::vector::Vector;
use linearkalman::KalmanFilter;

fn main() {

let kalman_filter = KalmanFilter {
// Process noise covariance
q: matrix![1.0, 0.1;
0.1, 1.0],
// Measurement noise matrix
r: matrix![1.0, 0.2, 0.1;
0.2, 0.8, 0.5;
0.1, 0.5, 1.2],
// Observation matrix
h: matrix![1.0, 0.7;
0.5, 0.7;
0.8, 0.1],
// State transition matrix
f: matrix![0.6, 0.2;
0.1, 0.3],
// Initial guess for state mean at time 1
x0: vector![1.0, 1.0],
// Initial guess for state covariance at time 1
p0: matrix![1.0, 0.0;
0.0, 1.0],
};

let data: Vec<Vector<f64>> = vec![vector![1.04, 2.20, 3.12],
vector![1.11, 2.33, 3.34],
vector![1.23, 2.21, 3.45]];

let run_filter = kalman_filter.filter(&data);
let run_smooth = kalman_filter.smooth(&run_filter.0, &run_filter.1);

// Print filtered and smoothened state variable coordinates
println!("filtered.1,filtered.2,smoothed.1,smoothed.2");
for k in 0..3 {
println!("{:.6},{:.6},{:.6},{:.6}",
&run_filter.0[k].x, &run_filter.0[k].x,
&run_smooth[k].x, &run_smooth[k].x)
}
}
``````

`examples` directory contains code sample which allows to import data from a CSV file and returns filtered and smoothed data to `stdout`.