Pflib - An Object Oriented Matlab Toolbox For Particle Filtering
Pflib - An Object Oriented Matlab Toolbox For Particle Filtering
Pflib - An Object Oriented Matlab Toolbox For Particle Filtering
Filtering
Lingji Chena , Chihoon Leeb , Amarjit Budhirajab and Raman K. Mehraa
a ScientificSystems Company Inc., Woburn, MA, USA;
b University of North Carolina at Chapel Hill, Chapel Hill, NC, USA
ABSTRACT
Under a United States Army Small Business Technology Transfer (STTR) project, we have developed a MATLAB
toolbox called PFLib to facilitate the exploration, learning and use of Particle Filters by a general user. This
paper describes its object oriented design and programming interface. The software is available under a GNU
GPL license.
Keywords: Particle Filter, Extended Kalman Filter, software, MATLAB toolbox, object oriented, Graphical
User Interface (GUI)
1. INTRODUCTION
Recent years have witnessed tremendous interest in nonlinear filtering techniques in general and particle filters
in particular. Many algorithms have been reported in literature, and some are available in the form of pseudo
code or even real source code. However, there does not seem to be a unified software package that contains major
algorithms and options and is readily available. Practitioners who are interested in exploring the use of particle
filters for their problems often only have the choices of gleaning the Web or writing the code from scratch, neither
of which is very attractive.
It is true that one of the advantages of particle filters is their ease of implementation. Still writing code from
scratch takes a non-trivial amount of time and resources. A routine that calculates the definite integral of a
function is not difficult to implement, but we as users no longer write such a routine ourselves; we count on its
availability in the scientific computing software that we use. Similar things should happen with particle filters.
Scientific Systems Company Inc. and University of North Carolina at Chapel Hill, under a United States
Army Small Business Technology Transfer (STTR) project Advanced Computational Algorithms for Nonlinear
Filtering for Real Time Environment, have developed a MATLAB toolbox for particle filters that we call PFLib.
Its main features include the following:
The implementation of a filtering algorithm and its use are separated, as a result of an object oriented
design. People who want to know how things work can examine the source code and make modifications
if they choose to, but people who do not can simply use these filtering objects as black boxes.
Major algorithms and options have been implemented: Extended Kalman Filter, Bootstrap Particle Fil-
ter, Particle Filter with an EKF-type proposal distribution, Auxiliary Particle Filter, and Instrumental
Variable Particle Filter. Resampling schemes include None, Simple Resampling, Residual Resam-
pling, Branch-and-Kill, and System Resampling. Other available parameter choices include sampling
frequency, number of particles, and specification of Jacobians. Despite the variety, the Application Pro-
gramming Interface (API) is consistent and easy to use. To switch from one filter to another, one only
needs to change a few lines of code that initialize the filtering objects. The code that performs the actual
filtering need not be changed.
To further facilitate a general user, a Graphical User Interface (GUI) is also included, so that choices
of filters with relevant options can be made in an intuitive fashion. Moreover, the GUI automatically
generates initialization code according a users selection, so that the code can be cut-and-pasted into other
simulation scripts.
We are releasing the software under a GNU GPL license, which means, among other things, that the source
code is available and can be modified and distributed under the same license.
1
2. MATLAB CLASSES
1
According to MATLAB Helpdesk, Classes and objects enable you to add new data types and new operations to
MATLAB. The class of a variable describes the structure of the variable and indicates the kinds of operations and
functions that can apply to the variable. An object is an instance of a particular class. The phrase object-oriented
programming describes an approach to writing programs that emphasizes the use of classes and objects.
For a simple introduction, we will illustrate the concept using a class in PFLib, the class GaussianDistr
that represents a Gaussian distribution. We first describe how such a class is used, and then describe how it is
implemented.
g =
GaussianDistr object: 1-by-1
Now the variable g represents such an object, with both data and methods. Suppose we want to calculate
the probability density of such a distribution at the point [1; 2]. We simply call the density method that has
been implemented for this class:
ans =
0.0341
Here MATLAB examines the class type of g, the first argument to the function density, then looks for the
function file density.m under the class directory @GaussianDistr, and finally executes that method.
If we want to draw 7 samples from such a distribution, we call the drawSamples method:
>> drawSamples(g, 7)
ans =
2
GaussianDistr
Data:
mean
covariance
normalizing constant
inverse of covariance
Methods:
constructor
density
draw samples
gDistr.mean = theMean(:);
gDistr.n = length(gDistr.mean);
gDistr.cov = theCov;
gDistr.const = 1 / sqrt((2 * pi) ^ gDistr.n * det(theCov));
gDistr.invCov = inv(theCov);
where x(k) is the state, y(k) is the measurement, v(k) and w(k) are i.i.d. noises, and k N.
From a simulation point of view, a filter is a dynamical system in the form
where z(k) is the internal state of the filter, x(k) is the estimate of the current state x(k) based on the posterior
distribution obtained by the filter, and the functions () and () are parameterized by the functions f (), h(),
Particle filtering algorithms can be applied to systems of a more general form, but the implementation of a generic
toolbox would be considerably harder, since systems will be specified by users at run time.
3
function d = density(gDistr, x)
d = zeros(1, ncol);
x = x - repmat(gDistr.mean, 1, ncol);
for i = 1:ncol
d(i) = gDistr.const * exp(-x(:, i) * gDistr.invCov * x(:, i) / 2);
end;
and statistics of x(0), v(k) and w(k). In other words, a filtering subsystem, for a given system (1), takes in an
observation y(k) and produces an estimate x(k), based upon its internal state z(k), and the particular filtering
algorithms used. In the case of Extended Kalman Filter, z(k) includes the estimated mean and covariance. In
the case of a Particle Filter, z(k) includes all the particles and their weights, among others.
It therefore follows that we can use a MATLAB object to represent such a filter. An illustration of the
PF Simple class in PFLib is shown in Figure 4.
PF Simple
Data:
functions f() and h()
distributions of v and w
particles, weights
resampling function and related parameters
internal resetting counter
Methods:
constructor
update
get
To use a filtering object, first we create one by choosing a desired class and properly initializing it:
4
Here in each iteration, the filter object is updated with the new measurement. The get function can return
particles, weights, as well as the state estimate.
Resampling scheme choices that we have implemented include (i) None (for pedagogical purpose), (ii) Simple
Resampling, (iii) Residual Resampling, (iv) Branch-and-Kill, and (v) System Resampling.
Other available choices include (i) sampling frequency (e.g., every 5 steps), (ii) number of particles, and (iii)
Jacobians.
A partial list of references is.26
Given the above options, initializing a particle filter object can become tedious. To ease the users burden,
we have created a Graphical User Interface that first lets the user pick and choose and then generates the
initialization code for the user. To illustrate, two screen shots are shown in Figures 5 and 6. In the latter, since
filter type has been chosen in the drop-down box on the upper-left corner, only choices relevant to this filter
type is shown (hence the empty middle-part).
After the user has made all the choices, initialization code will be shown in an editor window, as is displayed
in Figure 7.
5
Figure 6. Collecting information on filter choices.
5. EXTENDING PFLIB
It is easy to extend PFLib to include more or new filtering algorithms. To give interested readers an idea of
the encapsulated implementation, the update method for a simple bootstrap filter is listed in Figure 8. To
implement a new algorithm, its author can use the code for the bootstrap filter as a template, and change the
update function to perform the calculations specified by the new algorithm. As far as the users are concerned,
6
they have one more choice in the library, and can use it the same way as any other algorithm.
N = length(fltr.w);
sum w = sum(fltr.w);
if sum w <= realmin
error(weights are numerically zero; change parameters or method.);
end;
7
ACKNOWLEDGMENTS
This work was supported in part by the US Army STTR contract # W911NF-04-C-0108, under the direction of
Dr. Mou-Hsiung (Harry) Chang.
REFERENCES
1. MATLAB Classes and Objects (Programming and Data Types). http://www.mathworks.com/access/helpdesk r13/
help/techdoc/matlab prog/ch14 oop.html.
2. A. Doucet, J. de Freitas, and N. Gordon, Sequential Monte Carlo Methods in Practice, New York:
SpringerVerlag, 2001.
3. M. Arulampalam, S. Maskell, N. Gordon, and T. Clapp, A tutorial on particle filters for online
nonlinear/non-gaussian bayesian tracking, IEEE Transactions on Signal Processing 50(2), pp. 174188,
2002.
4. J. S. Liu and R. Chen, Sequential Monte Carlo methods for dynamic systems, Journal of the American
Statistical Association 93(443), pp. 10321044, 1998.
5. N. de Freitas, Sequential monte carlo methods homepage. http://www.cs.ubc.ca/nando/smc/index.html.
6. A. Budhiraja, L. Chen, and C. Lee, A survey of numerical methods for nonlinear filtering problems, Physica
D , 2006. doi:10.1016/j.physd.2006.08.015.