SFWA UKF · Chapter 7 of 7

Filter experiments

A filter which works on a well-behaved simulation teaches you little about the day it stops working. This chapter gives you two ways to make the pendulum tracker go wrong, and the change which puts each one right. In the first the pendulum is pushed by something the model has never heard of. In the second you take the sigma points apart and move them yourself.

A push the model does not know about

Stand a fan beside the pendulum. It pushes the weight sideways with a steady torque of 0.15 N m about the pivot, enough to hold the rod 0.15 rad from hanging if it were still. The tracker of the quickstart knows about gravity and friction and nothing else.

The filter does not fail outright. Each frame pulls its estimate back towards the truth, as it would correct any other error. But the model then predicts the next frame as if the fan were not there, and is wrong again in the same direction. The sign of the trouble is in the innovations. For a filter whose models are right, they scatter evenly about zero. Here they are positive, frame after frame.

No tuning of the noise settings removes a steady error like this. Larger process noise would hide it, at the cost of following every noisy frame. The filter has nowhere to put the push. So give it somewhere: add the unknown torque to the state.

disturbance.cpp

enum PushKey { Push = 2 };
using PushedState = UKF::StateVector<
    UKF::Field<Angle, real_t>, UKF::Field<Rate, real_t>,
    UKF::Field<Push, real_t>>;  // N m, an unknown steady torque
using PushedFilter = UKF::Core<PushedState, Frame, UKF::IntegratorRK4>;

disturbance.cpp

template <>
template <>
PushedState PushedState::derivative<>() const {
    PushedState rate_of_change;
    rate_of_change.set_field<Angle>(get_field<Rate>());
    rate_of_change.set_field<Rate>((get_field<Push>() - kDamping * get_field<Rate>() -
                                    kWeightTorque * std::sin(get_field<Angle>())) / kInertia);
    rate_of_change.set_field<Push>(0.0);  // expected to stay as it is
    return rate_of_change;
}

The process model now includes the push in the angular acceleration, and says that the push itself is expected to stay as it is. The first guess is no push at all, with a standard deviation of 0.2 N m. A little process noise on the third field lets the estimate follow a push which changes slowly.

disturbance.cpp

PushedFilter three;
three.state << 0.0, 0.0, 0.0;  // no push assumed to begin with
three.covariance.setZero();
three.covariance.diagonal() << 0.5 * 0.5, 2.0 * 2.0, 0.2 * 0.2;  // but it could be 0.2 N m
three.process_noise_covariance.setZero();
three.process_noise_covariance.topLeftCorner<2, 2>() = interval_noise(dt, gust_intensity);
three.process_noise_covariance(2, 2) = 1e-4 * dt;  // the push may change slowly
three.measurement_covariance.set_field<BobX>(camera_sigma * camera_sigma);

The measurement model is unchanged. The camera still reports only the sideways position of the weight. The filter works the torque out from how the positions fail to match a pendulum with no push.

bench.torque = 0.15;  // N m, the fan: the simulated pendulum feels it, and the filter is not told
Tracker filter;       // angle and rate
Drawing: the simulated pendulum (solid rod), the estimate (blue rod with a ring, and a fan of two standard deviations) and the camera frame (orange diamond) at the selected instant. Upper plot: the innovation of every frame, the frame minus the reading the filter predicted. The dashed line is their mean after the first second. Lower plot: the true torque of the fan (neutral line) and, for the three-state filter, its estimate of that torque with a band of two standard deviations.

Try it

  1. With Angle and rate selected, look at the innovations. Nearly all the dots are above zero, and their mean is 0.062 m, twice the camera’s error. The filter is being surprised in the same direction at every frame.
  2. Select Angle, rate and push. The dots drop to scatter about zero, with a mean of 0.0026 m. In the lower plot the estimated push rises from nothing and settles near the true value within a second or two.
  3. Set the fan’s torque to zero and compare the two filters. Their innovations now look alike, and the three-state filter’s push estimate settles near zero after its first second. The extra state costs little when there is nothing for it to find.
  4. Raise the torque to 0.3 N m with two states. The innovations grow, and the estimated rate, in the readout, is the worse for it.

disturbance runs both filters on the same frames from the library.

t 1 s  push estimate 0.139 +/- 0.024 N m  innovation: two states  0.081 m, three states -0.001 m
t 2 s  push estimate 0.131 +/- 0.016 N m  innovation: two states  0.044 m, three states -0.011 m
t 3 s  push estimate 0.138 +/- 0.015 N m  innovation: two states  0.100 m, three states  0.042 m
t 4 s  push estimate 0.147 +/- 0.014 N m  innovation: two states  0.118 m, three states  0.058 m
t 5 s  push estimate 0.148 +/- 0.014 N m  innovation: two states  0.061 m, three states  0.001 m
t 6 s  push estimate 0.152 +/- 0.014 N m  innovation: two states  0.093 m, three states  0.025 m
true push 0.150 N m
mean innovation after 1 s: two states 0.0619 m, three states 0.0026 m
rate error after 1 s: two states 0.40 rad/s RMS, three states 0.14 rad/s RMS

After six seconds the three-state filter puts the push at 0.152 N m, within 0.014. The rate estimate improves most, from 0.40 to 0.14 rad/s RMS, because the rate is the field the camera cannot correct directly and so depends most on the model.

If your program already knows the torque, because it commands the motor that applies it, do not estimate it. Pass it to the model as an input, as in State and process models. A state for an unknown is the tool for a quantity which matters to the prediction, which you cannot measure, and which changes slowly enough for the readings to reveal it.

Move the sigma points

The overview pushed an uncertain angle through the camera’s sine curve with three sigma points. Where those points sit and how much each counts are set by three numbers, which the library takes from UKF::Parameters for your state type.

sigma.cpp

// Sigma-point settings belong to a state type. Specialise them before any filter uses it.
namespace UKF::Parameters {
template <> constexpr real_t AlphaSquared<Tuned> = 1.0;
template <> constexpr real_t Beta<Tuned> = 2.0;
template <> constexpr real_t Kappa<Tuned> = 2.0;
}  // Plain keeps the library's defaults: AlphaSquared 1, Beta 0, Kappa 3.

The defaults are α² = 1, β = 0 and κ = 3. In the figure there is one uncertain quantity, the angle, so the default reach is √4 = 2 standard deviations.

namespace UKF::Parameters {
template <> constexpr real_t AlphaSquared<Tuned> = 1.00;
template <> constexpr real_t Beta<Tuned> = 0.00;
template <> constexpr real_t Kappa<Tuned> = 3.00;
}
// Rebuild, then: ./build-examples/sigma 1.20 0.30
Drawing: the estimated angle (blue rod), its spread (blue fan) and the two outer sigma points (thin rods), with dotted lines to where each expects the camera to report. On the image line, the green bell is the reading the sigma points predict and the dashed curve is the reference found by adding up over every possible angle. Plot: the camera model as a curve, with the sigma points on it, the spread of angles as a blue bell on the angle axis and the predicted and reference readings as bells against the reading axis.

Try it

  1. Lower Kappa to 0. The outer points move in to one standard deviation and the centre’s weight for the mean becomes zero: the prediction is now made from the two outer points alone.
  2. Set Kappa to 2 and Beta to 2, the values the example program compiles in. The predicted mean, 0.4455 m, is within a tenth of a millimetre of the reference. The predicted spread, 0.066 m, is further from the reference’s 0.059 m than the default’s 0.062 m was. No one setting is best at everything.
  3. With the defaults restored, lower AlphaSquared to 0.10. The points pull in to 0.63 standard deviations, and the centre weight for the mean turns negative, −1.5. The predicted mean barely changes here. But with a negative weight a covariance can come out invalid when the model bends hard, and the library reports that as a failed step.
  4. Move the estimated angle to 1.5 rad, the top of the curve, and widen its standard deviation. No choice of settings makes three points describe what happens here: readings pile up against 0.5 m, and the true distribution is no longer a bell.

sigma makes the same calculation with the library’s own functions, once with the defaults and once with the settings above. Sigma-point settings are fixed when the program is compiled, so the two cases need two state types.

sigma.cpp

// The mean and standard deviation of the camera reading for an uncertain angle.
template <typename StateType>
void transform(const char* name, real_t angle, real_t angle_sigma, Trace& trace) {
    StateType state;
    state << angle;
    // The points sit at the mean and at the mean plus or minus the scaled root of the covariance.
    typename StateType::CovarianceMatrix spread;
    spread(0, 0) = angle_sigma * std::sqrt(UKF::Parameters::ScaledDimension<StateType>);
    const auto angles = state.calculate_sigma_point_distribution(spread);

    Frame frame;
    const auto readings = frame.template calculate_sigma_point_distribution<StateType>(angles);
    const auto mean = frame.template calculate_sigma_point_mean<StateType>(readings);
    const auto deltas = mean.template calculate_sigma_point_deltas<StateType>(readings);
    const auto covariance = mean.template calculate_sigma_point_covariance<StateType>(deltas);
angle 1.20 +/- 0.30 rad through 0.5 sin(angle):
default  points at 1.200, 1.800, 0.600 rad -> reading 0.4457 +/- 0.0621 m
tuned    points at 1.200, 1.720, 0.680 rad -> reading 0.4455 +/- 0.0662 m
at the mean angle alone: 0.4660 m

For most filters the defaults are a sound choice, and time is better spent on the models and the noise settings. Change the sigma-point settings when you have a reference to compare against, as this figure does, and when a specific symptom points to them. Keep AlphaSquared well above 10−6 in single precision: the outer weights grow as it shrinks and multiply the rounding error of every point.

More to try from the command line

Several of the examples take settings as arguments. Each of these changes one thing from a run described in an earlier chapter.

Further experiments
CommandWhat changesLook for
./build-examples/sensors 20 -0.2A camera frame every 0.2 s, and a gyroscope bias of −0.2 rad/sThe bias is still found, a little more slowly. Two sensors
./build-examples/blur 0.005No blur: the same error along the path as across itThe two filters agree. Errors with a direction
./build-examples/encoder 18.5 1.5 0.4 0.3A poor encoder and a confident predictionThe estimate moves only a small part of the way. One update by hand
./build-examples/calibrate oneEvery calibration reading at one clamp angleA wide prediction at an unseen angle. One angle is not enough
./build-examples/disturbance 0.3A fan twice as strongA larger steady innovation with two states. Above

Check your understanding

Your filter’s innovations average well above zero. Name two things to check before adding a state.

The sensor and the model. A camera whose centre column is miscalibrated adds the same offset to every frame, which looks just like this; calibrate it. And a constant in the process model may be wrong, such as the rod’s length. A state for an unknown push is the answer only when the sensor and the constants are right and something outside the model is acting.

With three states the camera still measures only position. How can the filter separate a push from an error in its rate estimate?

By how each shows up over time. A wrong rate makes the next position wrong once, and is then corrected. A steady push makes every prediction wrong in the same direction, and bends the whole swing towards one side. Over several frames only the push explains that pattern, and the filter’s covariance links the innovations to the third state accordingly.

For two uncertain quantities and the default settings, how far out are the sigma points and what does the centre count towards the mean?

√(1 × (2 + 3)) = √5, about 2.24 standard deviations. The centre counts 1 − 2/5 = 0.6 and each of the four outer points 0.1.

Reference

Used in this chapter
NameWhat it is
filter.innovationThe last reading minus its prediction. Log it: a steady offset points to a missing state or a wrong constant.
UKF::Parameters::AlphaSquared<State>, Kappa<State>The reach of the sigma points and their weights for the mean.
UKF::Parameters::Beta<State>An adjustment to the centre point’s weight for the covariance.
calculate_sigma_point_distribution, calculate_sigma_point_mean, calculate_sigma_point_deltas, calculate_sigma_point_covarianceThe steps of the unscented transform, on a state or a measurement vector, for use outside a filter.

StateVector.h: sigma-point settings and weights · Process models and sigma-point scaling · disturbance.cpp · sigma.cpp