Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 55

Math Help - Kalman filtering application

  1. #16
    Junior Member
    Joined
    Feb 2009
    Posts
    33
    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
    Attached Thumbnails Attached Thumbnails Kalman filtering application-1.jpg   Kalman filtering application-2.jpg   Kalman filtering application-3.jpg  
    Last edited by atlove; February 23rd 2009 at 05:56 AM.
    Follow Math Help Forum on Facebook and Google+

  2. #17
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by atlove View Post
    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
    Follow Math Help Forum on Facebook and Google+

  3. #18
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by atlove View Post

    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
    Follow Math Help Forum on Facebook and Google+

  4. #19
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by atlove View Post
    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
    Follow Math Help Forum on Facebook and Google+

  5. #20
    Junior Member
    Joined
    Feb 2009
    Posts
    33
    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
    Last edited by atlove; February 24th 2009 at 06:00 AM.
    Follow Math Help Forum on Facebook and Google+

  6. #21
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by atlove View Post
    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
    Last edited by CaptainBlack; February 24th 2009 at 11:31 AM. Reason: spelling
    Follow Math Help Forum on Facebook and Google+

  7. #22
    Junior Member
    Joined
    Feb 2009
    Posts
    33
    Quote Originally Posted by CaptainBlack View Post
    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?
    Follow Math Help Forum on Facebook and Google+

  8. #23
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by atlove View Post
    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
    Follow Math Help Forum on Facebook and Google+

  9. #24
    Junior Member
    Joined
    Feb 2009
    Posts
    33
    Quote Originally Posted by CaptainBlack View Post
    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)]?

    Thank you for your replying.
    Last edited by atlove; February 25th 2009 at 02:08 AM.
    Follow Math Help Forum on Facebook and Google+

  10. #25
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by atlove View Post
    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
    Follow Math Help Forum on Facebook and Google+

  11. #26
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by atlove View Post
    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
    Follow Math Help Forum on Facebook and Google+

  12. #27
    Junior Member
    Joined
    Feb 2009
    Posts
    33
    Quote Originally Posted by CaptainBlack View Post
    There seems to be insufficient information to proceed further.
    Can the model you built be simulate with some random value?

    Quote Originally Posted by CaptainBlack View Post
    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?
    Follow Math Help Forum on Facebook and Google+

  13. #28
    Junior Member
    Joined
    Feb 2009
    Posts
    33
    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?
    Attached Thumbnails Attached Thumbnails Kalman filtering application-z111111111111111.gif  
    Last edited by atlove; February 25th 2009 at 05:51 AM.
    Follow Math Help Forum on Facebook and Google+

  14. #29
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by atlove View Post
    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
    Follow Math Help Forum on Facebook and Google+

  15. #30
    Junior Member
    Joined
    Feb 2009
    Posts
    33
    Quote Originally Posted by CaptainBlack View Post
    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
    Last edited by atlove; February 26th 2009 at 12:29 AM.
    Follow Math Help Forum on Facebook and Google+

Page 2 of 4 FirstFirst 1234 LastLast

Similar Math Help Forum Discussions

  1. particle filtering
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: December 9th 2009, 10:41 PM
  2. Kalman Gain
    Posted in the Advanced Statistics Forum
    Replies: 6
    Last Post: June 5th 2009, 07:31 AM
  3. Account for latency in Kalman Filter
    Posted in the Advanced Statistics Forum
    Replies: 2
    Last Post: March 16th 2009, 02:56 AM
  4. Mathematica - 2 Questions: Filtering & Headers
    Posted in the Math Software Forum
    Replies: 2
    Last Post: October 24th 2008, 01:29 PM
  5. Filtering the top 5% in real-time
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: July 9th 2008, 06:42 AM

Search Tags


/mathhelpforum @mathhelpforum