Hello everyone!!

Im struggling on a simple assignment. I have to filter specific data
(which are in .xls format) using the "optimal" kalman filter in both
ARMAX and ARX models. the struggle is on how to connect the data from
the xls file to the filter...

here i give u my kalman filter with the comments

---------------------------------------------------------------------
% s = kalmanf(s)
%
% "s" is a "system" struct containing various fields used as input
% and output. The state estimate "x" and its covariance "P" are
% updated by the function. The other fields describe the mechanics
% of the system and are left unchanged. A calling routine may change
% these other fields as needed if state dynamics are time-dependent;
% otherwise, they should be left alone after initial values are set.
% The exceptions are the observation vectro "z" and the input control
% (or forcing function) "u." If there is an input function, then
% "u" should be set to some nonzero value by the calling routine.
%
% SYSTEM DYNAMICS:
%
% The system evolves according to the following difference equations,
% where quantities are further defined below:
%
% x = Ax + Bu + w meaning the state vector x evolves during one time
% step by premultiplying by the "state transition
% matrix" A. There is optionally (if nonzero) an input
% vector u which affects the state linearly, and this
% linear effect on the state is represented by
% premultiplying by the "input matrix" B. There is also
% gaussian process noise w.
% z = Hx + v meaning the observation vector z is a linear function
% of the state vector, and this linear relationship is
% represented by premultiplication by "observation
% matrix" H. There is also gaussian measurement
% noise v.
% where w ~ N(0,Q) meaning w is gaussian noise with covariance Q
% v ~ N(0,R) meaning v is gaussian noise with covariance R
%
% VECTOR VARIABLES:
%
% s.x = state vector estimate. In the input struct, this is the
% "a priori" state estimate (prior to the addition of the
% information from the new observation). In the output struct,
% this is the "a posteriori" state estimate (after the new
% measurement information is included).
% s.z = observation vector
% s.u = input control vector, optional (defaults to zero).
%
% MATRIX VARIABLES:
%
% s.A = state transition matrix (defaults to identity).
% s.P = covariance of the state vector estimate. In the input struct,
% this is "a priori," and in the output it is "a posteriori."
% (required unless autoinitializing as described below).
% s.B = input matrix, optional (defaults to zero).
% s.Q = process noise covariance (defaults to zero).
% s.R = measurement noise covariance (required).
% s.H = observation matrix (defaults to identity).
%
% NORMAL OPERATION:
%
% (1) define all state definition fields: A,B,H,Q,R
% (2) define intial state estimate: x,P
% (3) obtain observation and control vectors: z,u
% (4) call the filter to obtain updated state estimate: x,P
% (5) return to step (3) and repeat
%
% INITIALIZATION:
%
% If an initial state estimate is unavailable, it can be obtained
% from the first observation as follows, provided that there are the
% same number of observable variables as state variables. This "auto-
% intitialization" is done automatically if s.x is absent or NaN.
%
% x = inv(H)*z
% P = inv(H)*R*inv(H')
%
% This is mathematically equivalent to setting the initial state estimate
% covariance to infinity.


function s = kalmanf(s)

% set defaults for absent fields:
if ~isfield(s,'x'); s.x=nan*z; end
if ~isfield(s,'P'); s.P=nan; end
if ~isfield(s,'z'); error('Observation vector missing'); end
if ~isfield(s,'u'); s.u=0; end
if ~isfield(s,'A'); s.A=eye(length(x)); end
if ~isfield(s,'B'); s.B=0; end
if ~isfield(s,'Q'); s.Q=zeros(length(x)); end
if ~isfield(s,'R'); error('Observation covariance missing'); end
if ~isfield(s,'H'); s.H=eye(length(x)); end

if isnan(s.x)
% initialize state estimate from first observation
if diff(size(s.H))
error('Observation matrix must be square and invertible for
state autointialization.');
end
s.x = inv(s.H)*s.z;
s.P = inv(s.H)*s.R*inv(s.H');
else

% This is the code which implements the discrete Kalman filter:

% Prediction for state vector and covariance:
s.x = s.A*s.x + s.B*s.u;
s.P = s.A * s.P * s.A' + s.Q;

% Compute Kalman gain factor:
K = s.P*s.H'*inv(s.H*s.P*s.H'+s.R);

% Correction based on observation:
s.x = s.x + K*(s.z-s.H*s.x);
s.P = s.P - K*s.H*s.P;
---------------------------------------------------------------------

Thanks for your time and help
nick

Dani AI

Generated

— your kalmanf already implements the predict/correct loop. The missing piece is packaging the Excel columns into a uniformly sampled input/output object, estimating an ARX/ARMAX model from that object, converting (or fitting) a state‑space model, and then designing/applying the Kalman estimator. The minimal workflow and example commands below show that end-to-end path.

Read the spreadsheet and make an iddata object (check that your time vector is uniformly sampled):

T = readtable('mydata.xlsx');        % or use readmatrix/readcell
t = T.Time;                          % adjust to your column names
u = T.Input; y = T.Output;
Ts = mean(diff(t));                  % verify uniform sampling (std(diff(t)) ~= 0)
data = iddata(y, u, Ts);             % package for System Identification

Estimate ARX or ARMAX from the iddata (pick orders with cross‑validation / arxstruc):

na = 2; nb = 2; nk = 1;
sys_arx   = arx(data, [na nb nk]);           % fast LS estimate
sys_armax = armax(data, [na nb 1 nk]);       % includes noise model (C polynomial)

Convert to state‑space (or fit directly with ssest), build a plant that has an explicit process‑noise input, design the Kalman filter, then simulate the estimator on the recorded data:

[A,B,C,D,K] = idssdata(sys_armax);           % converts idpoly/idss to matrices
Plant = ss(A,[B B],C,[D D],Ts);              % second input is the process-noise w
Q = 1*eye(size(B,2));                        % tune process-noise covariance
R = cov(data.OutputData);                    % estimate sensor noise from residuals
[kalmf,L,P] = kalman(Plant,Q,R);
out = lsim(kalmf,[u y],(0:size(y,1)-1)'*Ts); % kalmf inputs: known u and measured y
y_est = out(:,1);                            % first output is estimated y

Troubleshooting tips: ensure uniform sampling and remove NaNs; detrend or de‑mean signals if needed; use resid(data,sys) to check residual whiteness and to estimate measurement noise for R; select model orders via arxstruc or validation and prefer ssest/n4sid for state‑space needs. For reference on the commands and their options, see the MATLAB docs for readtable and iddata, arx/armax, idss/ssest, kalman, and resid. (mathworks.com)

and me too, I need a code with MATLAB describe the ARX model.
All my best
Adil.

commented: me too me too me too - too many of you to count -6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.