Author Archives: Fredrik Bagge Carlson

How to Tune a Kalman Filter: Step-by-Step Guide | JuliaHub

By: Fredrik Bagge Carlson

Re-posted from: https://info.juliahub.com/blog/tune-kalman-filter

The celebrated Kalman filter finds applications in many fields of engineering and economics. While many are familiar with the basic concepts of the Kalman filter, almost equally many find the “tuning parameters” associated with a Kalman filter nonintuitive and difficult to choose. While there are several parameters that can be tuned in a real-world application of a Kalman filter, we will focus on the most important ones: the process and measurement noise covariance matrices.

The Kalman filter

The Kalman filter is a form of Bayesian estimation algorithm that estimates the state $$x$$ of a linear dynamical system, subject to Gaussian noise $$e$$ acting on the measurements as well as the dynamics, $$w$$. More precisely, let the dynamics of a discrete-time linear dynamical system be given by

$$x_{k+1} = {Ax_k + Bu_k + w_k}$$
$$y_k = {Cx_k + Du_k + e_k}$$

 

where $$x_k \in \Bbb R^{n_x}$$ is the state of the system at time $$k, u_k \in \Bbb R^{n_u}$$ is an external input, $$A$$ and $$B$$ are the state transition and input matrices respectively, $$c$$ an output matrix and $$w_k \backsim N(0, R_1) $$ and $$e_k \backsim N(0, R_1) $$, are normally distributed process noise and measurement noise terms respectively. A state estimator like the Kalman filter allows us to estimate $$x$$ given only noisy measurements $$y \in \Bbb R^{n_y}$$, i.e., without necessarily having measurements of all the components of $$x$$ available.[obs] For this reason, state estimators are sometimes referred to as virtual sensors, i.e., they allows use to estimate what we cannot measure.

The Kalman filter is popular for several important reasons, for one, it is the optimal estimator in the mean-square sense if the system dynamics is linear (can be time varying) and the noise acting on the system is Gaussian. In most practical applications, neither of these conditions hold exactly, but they often hold sufficiently well for the Kalman filter to remain useful.[nonlin] A perhaps even more useful property of the Kalman filter is that the posterior probability distribution over the state remains Gaussian throughout the operation of the filter, making it efficient to compute and store.

obs: Under a technical condition on the observability of the system dynamics.

nonlin: Several nonlinear state estimators exist as well.

What does “tuning the Kalman filter” mean?

To make use of a Kalman filter, we obviously need the dynamical model of the system given by the four matrices $$A,B,C$$ and $$D$$. We furthermore require a choice of the covariance matrices $$R_1$$ and $$R_2$$, and it is here a lot of aspiring Kalman-filter users get stuck. The covariance matrix of the measurement noise is often rather straightforward to estimate, just collect some measurement data when the system is at rest and compute the sample covariance, but we often lack any and all feeling for what the process noise covariance, $$R_1$$, should be.

In this blog post, we will try to give some intuition for how to choose the process noise covariance matrix $$R_1$$. We will come at this problem from a disturbance-modeling perspective, i.e., trying to reason about what disturbances act on the system and how, and what those imply for the structure and value of the covariance matrix $$R_1$$.

State Estimation for High-index DAEs

By: Fredrik Bagge Carlson

Re-posted from: https://info.juliahub.com/blog/state-estimation-high-index-daes

Introduction

In a previous blog post, we spoke about how to tune a Kalman filter, a widely used state estimator for linear dynamical systems. Unfortunately, not all practically occurring systems are represented sufficiently well with linear dynamics affected by Gaussian noise. In fact, such assumptions are almost never satisfied completely, even if they may be good enough in many situations. In this post, we will take a step up in sophistication and consider nonlinear systems expressed as differential-algebraic equations, and explore the challenges and opportunities that arise when performing state estimation and virtual sensing for this richer set of systems.

Differential-Algebraic Equations

Modern equation-based modeling tools, such as ModelingToolkit, allow the user to quickly and in a component-based fashion, model complex real-world systems. Such tools often produce differential-algebraic equation models (DAEs), i.e., the dynamics may include both differential equations and algebraic equations. The standard Kalman filter, and most of its nonlinear extensions, do not deal with algebraic equations, so how do we estimate the state for such a DAE system?

Before we proceed, we define some notion. Let the dynamic equations of the system we want to estimate the state of be written as

$$M\dot x(t) = f(x(t),u(t))$$

where $$M$$ is called a “mass matrix”, $$x$$ is the state (technically, since we are dealing with DAEs, it’s a descriptor) and $$u$$ is the input. If we take $$M=I$$, we have a standard ODE system, if $$M$$ is invertible, we can easily convert this system to an ODE system like $$ẋ=M^-1f(x,u)$$, so the interesting case is when $$M$$ is singular. A commonly occurring special case is when $$M$$ has the form

$$\left\lbrack \matrix{1 & 0\cr 0 & 0} \right\rbrack$$

i.e., the system can be written as

$$\left\lbrack \matrix{\dot x_\delta \cr 0} \right\rbrack = f(x,u)$$

where $$\dot x_\delta$$ includes the derivatives of the differential state variables only. In this form, we have $$f$$ containing a number of ordinary differential equations, followed by one or several algebraic equations $$0=g(x,u)$$

Differential-equation solvers that support DAEs often make use of a nonlinear root finder that continuously solves the equation $$0=g(x,u)$$ in order to simulate the system.

Now, how do we estimate the state descriptor $$x$$ in a DAE system? This question is much more nuanced than when we’re dealing with systems of ordinary differential equations only. How come?

When we design a state estimator, we must come up with some model of the disturbances that affect the dynamics and the measurements. This disturbance model is usually designed to include both unmeasured inputs that affect the system, but also to mitigate the effects of model error. Modeling any physical system perfectly well is impossible, and we must somehow account for the fact that our dynamic equations are an imperfect representation of the true dynamics that govern the system. As detailed in the previous post, the Kalman filter models these disturbances as Gaussian noise, but for nonlinear DAE systems, things can quickly become more challenging.