Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# dsp.KalmanFilter System object

Package: dsp

Estimate system measurements and states using Kalman filter

## Description

The KalmanFilter is an estimator used to recursively obtain a solution for linear optimal filtering. This estimation is made without precise knowledge of the underlying dynamic system. The Kalman filter implements the following linear discrete-time process with state, x, at the kth time-step: (state equation). This measurement, z, is given as: (measurement equation).

The Kalman filter algorithm computes the following two steps recursively:

• Prediction: Process parameters x (state) and P (state error covariance) are estimated using the previous state,

• Correction: The state and error covariance are corrected using the current measurement,

To filter each channel of the input:

1. Define and set up your Kalman filter. See Construction.

2. Call step to filter each channel of the input according to the properties of dsp.KalmanFilter. The behavior of step is specific to each object in the toolbox.

## Construction

H = dsp.KalmanFilter returns the Kalman filter System object™, H, with default values for the parameters.

H = dsp.KalmanFilter('PropertyName',PropertyValue, ...) returns an Kalman filter System object, H, with each property set to the specified value.

H = dsp.KalmanFilter(STMatrix, MMatrix, PNCovariance, MNCovariance, CIMatrix, 'PropertyName', PropertyValue, ...) returns a Kalman filter System object, H. The StateTransitionMatrix property is set to STMatrix, and MeasurementMatrix property is set to MMatrix. In addition, the ProcessNoiseCovariance property set to PNCovariance, MeasurementNoiseCovariance property set to MNCovariance, ControlInputMatrix property set to CIMatrix. Other specified properties are set to the specified values.

## Properties

 StateTransitionMatrix Model of state transition Specify A in the state equation that relates the state at the previous time step to the state at current time step. A is a square matrix with each dimension equal to the number of states. The default value is 1. ControlInputMatrix Model of relation between control input and states Specify B in the state equation that relates the control input to the state. B is a matrix with a number of rows equal to the number of states. This property is activated only when the ControlInputPort property value is true. The default value is 1. MeasurementMatrix Model of relation between states and measurement output Specify H in the measurement equation that relates the states to the measurements. H is a matrix with the number of columns equal to the number of measurements. The default value is 1. ProcessNoiseCovariance Covariance of process noise Specify Q as a square matrix with each dimension equal to the number of states. This matrix, Q, is the covariance of the white Gaussian process noise, w, in the state equation. The default value is 0.1. MeasurementNoiseCovariance Covariance of measurement noise Specify R as a square matrix with each dimension equal to the number of states. This matrix, R, is the covariance of the white Gaussian process noise, v, in the measurement equation. The default value is 0.1. InitialStateEstimate Initial value for states Specify an initial estimate of the states of the model as a column vector with length equal to the number of states. The default value is 0. InitialErrorCovarianceEstimate Initial value for state error covariance Specify an initial estimate for covariance of the state error, as a square matrix with each dimension equal to the number of states. The default value is 0.1. DisableCorrection Disable port for filters Specify as a scalar logical value, disabling System object filters from performing the correction step after the prediction step in the Kalman filter algorithm. The default value is false. ControlInputPort Presence of a control input Specify if the control input is present, using a scalar logical value. The default value is true.

## Methods

 clone Create Kalman Filter System object with same property values isLocked Locked status for input attributes and nontunable properties release Allow property value and input characteristics changes reset Reset internal states of Kalman filter step Filter input with Kalman filter object

## Examples

Estimate a changing scalar

Create the System objects for the changing scalar input, the Kalman filter, and the scope (for plotting):

```numSamples = 4000;
R = 0.02;
hSig = dsp.SignalSource;
hSig.Signal = [ones(numSamples/4,1);   -3*ones(numSamples/4,1);...
4*ones(numSamples/4,1); -0.5*ones(numSamples/4,1)];
hTScope = dsp.TimeScope('NumInputPorts', 3, 'TimeSpan', numSamples, ...
'TimeUnits', 'Seconds', 'YLimits',[-5 5], ...
'ShowLegend', true); % Create the Time Scope
hKalman = dsp.KalmanFilter('ProcessNoiseCovariance', 0.0001,...
'MeasurementNoiseCovariance', R,...
'InitialStateEstimate', 5,...
'InitialErrorCovarianceEstimate', 1,...
'ControlInputPort',false); %Create Kalman filter
```

Add noise to the scalar, and pass the result to the Kalman filter. Step through the System objects to obtain the data streams, and plot the filtered signal:

```while(~isDone(hSig))
trueVal = step(hSig);
noisyVal = trueVal + sqrt(R)*randn;
estVal = step(hKalman, noisyVal);
step(hTScope,noisyVal,trueVal,estVal);
end```

Disable the correction step in the Kalman Filter algorithm

Create the signal, Kalman Filter, and Time Scope System objects:

```numSamples = 4000;
R = 0.02;
hSig = dsp.SignalSource;
hSig.Signal = [  ones(numSamples/4,1);   -3*ones(numSamples/4,1);...
4*ones(numSamples/4,1); -0.5*ones(numSamples/4,1)];
hTScope = dsp.TimeScope('NumInputPorts', 3, 'TimeSpan', numSamples, ...
'TimeUnits', 'Seconds', 'YLimits',[-5 5], ...
'Title', ['True(channel 1), noisy(channel 2) and ',...
'estimated(channel 3) values'], ...
'ShowLegend', true);
hKalman = dsp.KalmanFilter('ProcessNoiseCovariance', 0.0001,...
'MeasurementNoiseCovariance', R,...
'InitialStateEstimate', 5,...
'InitialErrorCovarianceEstimate', 1,...
'ControlInputPort',false);
ctr = 0;
```

Add noise to the signal. Step through the System objects to obtain the data streams, and plot the filtered signal:

```while(~isDone(hSig))
trueVal = step(hSig);
noisyVal = trueVal + sqrt(R)*randn;
estVal = step(hKalman, noisyVal);
step(hTScope,trueVal,noisyVal,estVal);

% Disabling the correction step of second filter for the middle
% one-third of the simulation
if ctr == floor(numSamples/3)
hKalman.DisableCorrection = true;
end
if ctr == floor(2*numSamples/3)
hKalman.DisableCorrection = false;
end
ctr = ctr + 1;
end
```

Use a single System object to track multiple scalar values

Create the signal where the columns are the two scalar values to be tracked. Also create the Kalman Filter, and the Time Scopes:

```numSamples = 4000;
R = 0.02;
hSig = dsp.SignalSource;
sig1 = [  ones(numSamples/4,1);   -3*ones(numSamples/4,1);...
4*ones(numSamples/4,1); -0.5*ones(numSamples/4,1)];
sig2 = [-2*ones(numSamples/4,1);   4*ones(numSamples/4,1);...
-3*ones(numSamples/4,1); 1.5*ones(numSamples/4,1)];

hSig.Signal = [sig1, sig2];

hTScope1 = dsp.TimeScope('NumInputPorts', 3, 'TimeSpan', numSamples, ...
'TimeUnits', 'Seconds', 'YLimits',[-5 5], ...
'Title', ['True(channel 1), noisy(channel 2) and ',...
'estimated(channel 3) values'], ...
'ShowLegend', true);
hTScope2 = clone(hTScope1);
hKalman = dsp.KalmanFilter('ProcessNoiseCovariance', 0.0001,...
'MeasurementNoiseCovariance', R,...
'InitialStateEstimate', -3,...
'InitialErrorCovarianceEstimate', 1,...
'ControlInputPort',false);
```

Add noise to the signal. Step through the System objects to obtain the data streams, and plot the filtered signal:

```while(~isDone(hSig))
trueVal = step(hSig);
noisyVal = trueVal + sqrt(R)*randn(1,2);
estVal = step(hKalman, noisyVal);
% Plot results of first channel on Time Scope
step(hTScope1,trueVal(:,1),noisyVal(:,1),estVal(:,1));
% Plot results of second channel on Time Scope
step(hTScope2,trueVal(:,2),noisyVal(:,2),estVal(:,2));
end
```

Use a unit step as the control input to track a ramp signal

Create the ramp signal to be tracked, the control input, the Time Scope, and the Kalman Filter:

```numSamples = 200;
R = 100;
hSig = dsp.SignalSource;
hSig.Signal = (1:numSamples)';
hControl = dsp.SignalSource;
hControl.Signal = ones(numSamples,1);

hTScope = dsp.TimeScope('NumInputPorts', 3, 'TimeSpan', numSamples, ...
'TimeUnits', 'Seconds', 'YLimits',[-5 205], ...
'Title', ['True(channel 1), Noisy(channel 2) and ',...
'estimated(channel 3) values'], ...
'ShowLegend', true);
hKalman = dsp.KalmanFilter('ProcessNoiseCovariance', 0.0001,...
'MeasurementNoiseCovariance', R,...
'InitialStateEstimate', 3,...
'InitialErrorCovarianceEstimate', 1);
```

## Algorithms

This object implements the algorithm, inputs, and outputs described on the Kalman Filter block reference page. The object properties correspond to the block parameters.

## References

[1] Greg Welch and Gary Bishop, An Introduction to the Kalman Filter, Technical Report TR95 041. University of North Carolina at Chapel Hill: Chepel Hill, NC., 1995.