# Thread: Kalman filtering application

1. Actually, i am going to use VB.net to build such a model. Before that, i need to understand how to build this mathematics model.

I have tried to use the code you posted before in MatLab, but i was given a different graphic and not similar to yours. Are you plotting the result of function rv=KalmanSim only?
Is Data() a build-in function? I can't excute directly by typing x=data(1) in command window.
What do the S represent for ?

Do you mind to show me the full model you built?

besides, i don't know what really the power consumption is. So i do not sure that it is not dynamic. Should i consider the estimation on state (X(K+1)=FX(K)+u(k)if it is dynamic?

Thank You

2. Originally Posted by atlove
Actually, i am going to use VB.net to build such a model. Before that, i need to understand how to build this mathematics model.

I have tried to use the code you posted before in MatLab, but i was given a different graphic and not similar to yours. Are you plotting the result of function rv=KalmanSim only?
Is Data() a build-in function? I can't excute directly by typing x=data(1) in command window.
What do the S represent for ?

Do you mind to show me the full model you built?

besides, i don't know what really the power consumption is. So i do not sure that it is not dynamic. Should i consider the estimation on state (X(K+1)=FX(K)+u(k)if it is dynamic?

Thank You
A generator of random data vectors:

Code:
function RV=DataGen(aa,nlines)
%
%  Function to generate simulated data matrix
%
% aa the row vector of coefficients
ll=length(aa);
RV=[];
for idx=1:nlines
x=rand(1,ll).*20;  % generate a random independednt variable vector
% these are uniform of (0,20) in this case
y=x*aa'+randn(1,1); % generate the noisey depended variable
rv=[x,y];          % pack x and y into a data vactor
RV=[RV;rv];
end
The Kalman Inovation process:

Code:
function [X,P]=KalmanInov(Data,R,X,P)
%
% Kalman Filter Inovation Processing
%
% data is a row vector with the independent vars and the depended var
%
ll=length(Data)
H=Data(1:ll-1);          %extract the H matrix or vector from the data
z=Data(ll);                %extract the measurement from the data

% Below is the standard Kalman inovation equations in matrix form

S=H*P*H'+R; %note this is a scalar
K=P*H'*inv(S);
X=X+K*(z-H*X);
P=(eye(ll-1,ll-1)-K*H)*P;
The main program which is passed an array containing the data -either real data or simulated:

Code:
function rv=KalmanRun(data)
%
%  function to run simulation of the Kalman filter for
%  MHF question
%
%  data is a matrix each row of which contains the vector of data
%
%  The system model is:
%
%  z=sum(x(i)*aa(i)) + w
%
%  where z is the measurement to be explained, the x's are the
%  independent variables corresponding to z and w is a scalar zero
%  mean gaussian noise term.
%
%  Initial values, the state estimate is set to zero, and the covariance
%  to a diagonal matrix of the right dimension with 10^2 down the diagonal
%
%  the measurement noise variance R (of w in the model) is set to 1.
%

sz=size(data);
niter=sz(1);    %number of rows of data
ll=sz(2);       %length of each row of data
P=eye(ll-1,ll-1).*100; %initial covariance of state estimate
X=zeros(ll-1,1);            %initial state estimate
R=1;                 %measurement variance
rv=X;
%
% loop
%
for idx=1:niter
D=data(idx,:);     % load the nest line of data into D consists of [x(1),..,x(n),z]
[X,P]=KalmanInov(D,R,X,P);  % inovate the filter with the data vactor D
rv=[rv,X];        %accumulate the state estimate
end

plot([0:niter],rv');
If you post some of your data I could run this code over it to see what we get.

CB

3. Originally Posted by atlove

besides, i don't know what really the power consumption is. So i do not sure that it is not dynamic. Should i consider the estimation on state (X(K+1)=FX(K)+u(k)if it is dynamic?

Thank You
If we have a dynamic model it will be necessary to extent the state to include additional terms to represent the variation over time. Another posibility is to just include some plant noise to represent random drift with time. But I would suggest we try the static model first.

CB

4. Originally Posted by atlove
What do the S represent for ?
S is the (co)variance of the prediction error (also know as the innnovation, or measurement residual):z-H*X, in this case it is a scalar as the dependent variable (aka the measurement) is a scalar.

It is an working variable used in the computation of the Kalman gain, but it can be used to monitor how well the filter is predicting the measurements as (z-H*X)/sqrt(S) should be a standard normal random variable.

CB

5. CaptainBlack

can you show me all the equations you have used in the code?
what data you need to know to run the latest code?

Thank You

6. Originally Posted by atlove
CaptainBlack

can you show me all the equations you have used in the code?
what data you need to know to run the latest code?

Thank You
These lines are the equations for the Kalman innovation process:

S=H*P*H'+R; %S=HPH'+R this is the variance of the innovation z-HX

K=P*H'*inv(S); %K=PH'S^(-1) is the Kalman gain

X=X+K*(z-H*X); %X=X+K(z-HX) is the state update equation

P=(eye(ll-1,ll-1)-K*H)*P; %P=(I-KH)P is the covariance update

The rest or the code is just data formating and initialization.

The data that I would like is a few lines (>6) of what in your original post you denotes as:

x1(ti), x2(ti), x3(ti),... x6(ti)

Preferably some tens of lines of this data.

================================================== ==========

Hopefully the initialization I use at present will be adequate, but there is a possibility that I will need to set the initial values from the first 5-10 lines of data.

CB

7. Originally Posted by CaptainBlack
These lines are the equations for the Kalman innovation process:

S=H*P*H'+R; %S=HPH'+R this is the variance of the innovation z-HX

K=P*H'*inv(S); %K=PH'S^(-1) is the Kalman gain

X=X+K*(z-H*X); %X=X+K(z-HX) is the state update equation

P=(eye(ll-1,ll-1)-K*H)*P; %P=(I-KH)P is the covariance update

The rest or the code is just data formating and initialization.

The data that I would like is a few lines (>6) of what in your original post you denotes as:

x1(ti), x2(ti), x3(ti),... x6(ti)

Preferably some tens of lines of this data.

================================================== ==========

Hopefully the initialization I use at present will be adequate, but there is a possibility that I will need to set the initial values from the first 5-10 lines of data.

CB
Why 5-10 lines of example data? are you going to work out the covariance P ,R and the initiation of estimation of X with this data?

8. Originally Posted by atlove
Why 5-10 lines of example data? are you going to work out the covariance P ,R and the initiation of estimation of X with this data?
It may be neccessary to generate the initial estimate and covariance and estimate the measurement variance from the data, so a minimum of 5 lines of data are needed to get an initial state estimate by solving the set of 5 simultaneous equations for the five state elements. Then from the measurement residuals to estimate the measurement variance, for this it would be better to use different data from that used to generate the initial state.

This would be necessary if we know nothing about the system, it is normal to know the measurement variance and limits of the state beforehand, and if this is the case the filter can be started using such data.

CB

9. Originally Posted by CaptainBlack
It may be neccessary to generate the initial estimate and covariance and estimate the measurement variance from the data, so a minimum of 5 lines of data are needed to get an initial state estimate by solving the set of 5 simultaneous equations for the five state elements. Then from the measurement residuals to estimate the measurement variance, for this it would be better to use different data from that used to generate the initial state.

This would be necessary if we know nothing about the system, it is normal to know the measurement variance and limits of the state beforehand, and if this is the case the filter can be started using such data.

CB
But the state elements (a,b,c,d,e) are changing in different time(mentioned in the 1st post). If you solve the 5 equations, you will get the same "a,b,c,d,e" value for those 5 equations and violate the changing value of state elements. So i think that it should be a dynamic model and extra measurement will be necessary(X(k+)=F*X(k)+w(k)).

I can't give the data since the project consultant do not prepare this for me.

Besides, what is the result of E[X(k)]? E means the expected value, X(k)-state vector.
Is it [E(a) E(b) E(c) E(d) E(e)] ? i do not have the state value, how can i find E[X(k)]?

10. Originally Posted by atlove
But the state elements (a,b,c,d,e) are changing in different time(mentioned in the 1st post).
Not by my reading of you first post.

But if they are we need a dynamic model for their time evolution.

CB

11. Originally Posted by atlove
I can't give the data since the project consultant do not prepare this for me.
There seems to be insufficient information to proceed further.

Besides, what is the result of E[X(k)]? E means the expected value, X(k)-state vector.
Is it [E(a) E(b) E(c) E(d) E(e)] ? i do not have the state value, how can i find E[X(k)]?
Yes, and the filter will estimate this for you once it is statrted up with appropriate initial values for the state and covariance.

CB

12. Originally Posted by CaptainBlack
There seems to be insufficient information to proceed further.
Can the model you built be simulate with some random value?

Originally Posted by CaptainBlack
Yes, and the filter will estimate this for you once it is statrted up with appropriate initial values for the state and covariance.
CB
How to choose or calculated appropriate initial values for state and covariance?

13. I found a book which summarize the equations of kalman filter. I think it is applicable for my project. But i don't know how to find F, the part of initialization, Q and R. Is it neccessary to have sample data in order to find those variables (x0, F, P0, R, Q)? If it did, can you show me the equations for them?

14. Originally Posted by atlove
I found a book which summarize the equations of kalman filter. I think it is applicable for my project. But i don't know how to find F, the part of initialization, Q and R. Is it neccessary to have sample data in order to find those variables (x0, F, P0, R, Q)? If it did, can you show me the equations for them?
In the usual applications because we know about the process/system we have some idea of the possible range of in state so we would set x0 to the middle of the range and set P0 as diagonal with diagonal elements of the order of the square of the possible range of the corresponding state element. F and Q we can write down because we know what an adequate model of the system dynamics is, and we generally have a good idea what R is also as it is a characteristic of our measurement process. These all come from knowledge of the system and there is no set of equations within the KF that tell you what they should be.

If we do not have that system knowledge we have to try to deduce what are suitable values from the data, or worse still design an adaptive KF which will do the equivalent of estimating them from the data. But now we are outside the realm of the KF and in the territory of case by case analysis and design.

CB

15. Originally Posted by CaptainBlack
In the usual applications because we know about the process/system we have some idea of the possible range of in state so we would set x0 to the middle of the range and set P0 as diagonal with diagonal elements of the order of the square of the possible range of the corresponding state element.

F and Q we can write down because we know what an adequate model of the system dynamics is, and we generally have a good idea what R is also as it is a characteristic of our measurement process. These all come from knowledge of the system and there is no set of equations within the KF that tell you what they should be.

If we do not have that system knowledge we have to try to deduce what are suitable values from the data, or worse still design an adaptive KF which will do the equivalent of estimating them from the data. But now we are outside the realm of the KF and in the territory of case by case analysis and design.

CB
OK, i got what you mean. What we can do now is to learn the knowledge of the system. Otherwise, we need to try and test with the given data to make such a model. Can you show me an example that how to found those variable with enough information?

Is the E(x) purely the mean of state ?
If i have a matrix
....|x11 x12 x13|
x= |x21 x22 x23|
....|x31 x32 x33|

how to find the E(x)?
Thank You

Page 2 of 4 First 1234 Last