Is it possible to sample multiple signals at different rates with the extendedKalmanFilter object?

2 views (last 30 days)
I need to implement an extended Kalman filter in a MATLAB for loop and decided to use the extendedKalmanFilter object. This was not too difficult and I have it working on a non-linear system with 2 outputs. However, each output is sampled at a different rate, y(1) every minute and y(2) every ten minutes:
So my measurement data looks like this:
t y1 y2
________ ______ ______
0 740.4 526.58
0.016667 741.23 NaN
0.033333 738.07 NaN
0.05 740.26 NaN
0.066667 739.75 NaN
0.083333 738.43 NaN
0.1 738.98 NaN
0.11667 739.46 NaN
0.13333 741.76 NaN
0.15 741.07 NaN
0.16667 737.93 546.05
0.18333 741.1 NaN
0.2 739.31 NaN
0.21667 738.65 NaN
0.23333 739.16 NaN
When I search for anything on this topic I mostly see references to this capability in Simulink, but not in MATLAB code.
Here is the code I tried, naively, and the error that is raised:
EKF = extendedKalmanFilter(@arom3_StateFcnRodin, @arom3_MeasurementFcnRodin2, InitialState);
EKF.ProcessNoise = diag(sigma_w);
EKF.MeasurementNoise = diag(R);
EKF.StateCovariance = diag(P0);
for i = 1:nT
% Process measurements from simulation
yk_m = Y_m(i, :)';
if isnan(yk_m(2)) % if 2nd measurement is missing
EKF.MeasurementFcn = @arom3_MeasurementFcnRodin1;
EKF.MeasurementNoise = diag(R(1));
yk_m = yk_m(1);
EKF.MeasurementFcn = @arom3_MeasurementFcnRodin2;
EKF.MeasurementNoise = diag(R);
% Update Kalman Filter
[CorrectedState, CorrectedStateCovariance] = correct(EKF, yk_m);
[PredictedState, PredictedStateCovariance] = predict(EKF, dt, params);
Where arom3_MeasurementFcnRodin1 and arom3_MeasurementFcnRodin2 are different measurement functions reflecting the 1 and 2 output system respectively:
function y = arom3_MeasurementFcnRodin1(xa)
y = xa(1);
function y = arom3_MeasurementFcnRodin2(xa)
y = [xa(1);
Error raised:
Error using ExtendedKalmanFilter
Expected z to be an array with number of elements equal to 1.
Error in matlabshared.tracking.internal.validateInputSizeAndType (line 14)
validateattributes(value, ...
Error in matlabshared.tracking.internal.ExtendedKalmanFilter/correct (line 629)
Error in arom3_sim_EKF3 (line 193)
[CorrectedState, CorrectedStateCovariance] = correct(EKF, yk_m);
Note: the code works when the sample rates are both 1.
Is this even possible with extendedKalmanFilter? Are there any other options, or will I have to implement the EKF by hand?
  1 Comment
Bill Tubbs
Bill Tubbs on 30 Sep 2021
Edited: Bill Tubbs on 30 Sep 2021
Further sleuthing has revealed that the error occurred at t=0.16667 when yk_m is [737.93; 546.05]. I.e. the first double sample after the first series of single samples.
Here is line 629 of the function [x_corr, P_corr] = correct(obj, z, varargin)
if coder.internal.is_defined(obj.pN)
('z', 'ExtendedKalmanFilter', z, obj.pN);
Looks like it is checking the size of z (which is yk_m) and comparing it to a private property obj.pN which is presumably still set to 2.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by