Basic Kalman Filter_SCILAB

4

Click here to load reader

Transcript of Basic Kalman Filter_SCILAB

Page 1: Basic Kalman Filter_SCILAB

8/11/2019 Basic Kalman Filter_SCILAB

http://slidepdf.com/reader/full/basic-kalman-filterscilab 1/4

Basic Kalman filter

Language: Scilab

Processor: Not Relevant

Submitted by Sylvain B on Jan 20 2011

Licensed under a Creative Commons Attribution 3.0 Unported

License

DSP Code Sharing > Basic Kalman filter

Basic Kalman filter

 

This is the most basic implementation of a Kalman filter.

For measured position, velocity and acceleration, the Kalman filter estimates the true datas.

This example works with Scilab.

 

// Clear initialisation

clf()

// Time vector 

t =[0:0.5:100];

len_t = length(t);

dt = 100 / len_t;

// Real values x: position v: velocity a: acceleration

x_vrai = 0 * t;

v_vrai = 0 * t;

a_vrai = 0 * t;

// Meseared values.

x_mes = 0 * t;

v_mes = 0 * t;

a_mes = 0 * t;

// Initialisation

for i = 0.02 * len_t: 0.3 * len_t,

  a_vrai(i) = 1;

end

for i = 0.5 * len_t: 0.7 * len_t,

  a_vrai(i) = 2;

end

// State of kalman filter 

etat_vrai = [ x_vrai(1);  v_vrai(1); a_vrai(1) ];

A = [ 1 dt dt*dt/2; 0 1 dt; 0 0 1];//transition matrice

for i = 2:length(t),

  etat_vrai = [ x_vrai(i-1);  v_vrai(i-1); a_vrai(i) ];

  etat_vrai = A * etat_vrai;

  x_vrai(i) = etat_vrai(1); 

v_vrai(i) = etat_vrai(2);

Sign in

 

Not a member? | Forgot your Password?

Search code

 

Search tips

See Also

Home Forums Code Snippets Blogs Books Jobs Papers Links Contact Us

Kalman filter http://www.dsprelated.com/showcod

8/6/2013

Page 2: Basic Kalman Filter_SCILAB

8/11/2019 Basic Kalman Filter_SCILAB

http://slidepdf.com/reader/full/basic-kalman-filterscilab 2/4

end

max_brt_x = 200;

max_brt_v = 10;

max_brt_a = 0.3;

// Random noise

x_brt = grand(1, len_t, 'unf', -max_brt_x, max_brt_x);

v_brt = grand(1, len_t, 'unf', -max_brt_v, max_brt_v);

a_brt = grand(1, len_t, 'unf', -max_brt_a, max_brt_a);

// Compute meas

x_mes = x_vrai + x_brt;

v_mes = v_vrai + v_brt;

a_mes = a_vrai + a_brt;

// START 

x_est = 0 * t;

v_est = 0 * t;

a_est = 0 * t;

etat_mes = [ 0; 0; 0 ];

etat_est = [ 0; 0; 0 ];

inn = [ 0; 0; 0 ];

// Matrix of covariance of the bruit.

// independant noise -> null except for diagonal,

// and on the diagonal == sigma^2.

// VAR(aX+b) = a^2 * VAR(X);

// VAR(X) = E[X*X] - E[X]*E[X]

R = [ max_brt_x*max_brt_x 0 0; 0 max_brt_v*max_brt_v 0; 0 0

max_brt_a*max_brt_a ];

Q = [1 0 0; 0 1 0; 0 0 1];

P = Q;

for i = 2:length(t),

  // Init : measured state

  etat_mes = [ x_mes(i);  v_mes(i); a_mes(i) ];

 

// Innovation: measured state - estimated state

  inn = etat_mes - etat_est;

 

// Covariance of the innovation

  S = P + R;

 

// Gain

  K = A * inv(S);

 

// Estimated state

  etat_est = A * etat_est + K * inn;

 

// Covariance of prediction error 

  // C = identity 

  P = A * P * A' + Q - A * P * inv(S) * P * A';

 

// End: estimated state

  x_est(i) = etat_est(1);

  v_est(i) = etat_est(2);

  a_est(i) = etat_est(3);

end

// Blue : real

// Gree : noised 

// Red: estimated 

subplot(311)

Kalman filter http://www.dsprelated.com/showcod

8/6/2013

Page 3: Basic Kalman Filter_SCILAB

8/11/2019 Basic Kalman Filter_SCILAB

http://slidepdf.com/reader/full/basic-kalman-filterscilab 3/4

Tweet 1 0 1

deepasaini wrote: 1/9/2012

Sylvain B wrote: 1/9/2012

deepasaini wrote: 1/10/2012

deepasaini wrote: 1/10/2012

Sylvain B wrote: 1/10/2012

deepasaini wrote: 1/10/2012

plot(t, a_vrai, t, a_mes, t, a_est); 

subplot(312)

plot(t, v_vrai, t, v_mes, t, v_est); 

subplot(313)

plot(t, x_vrai, t, x_mes, t, x_est); 

Rate this code snippet:

Rating: 2 | Votes: 3

 submit

 

posted by  Sylvain B

Comments

 

Hi,

i have to use kalman filter for my problem and i am using above code for that but in this code real and measured value both are same

please guide me about the process which have

used in above code. i have little knowledge of scilab and basic about kalman filter. please suggest about the input constraints in above

code.

 

Hello,

The mesured values are computed by adding random noise to the theorical ones :

 // Compute meas

x_mes = x_vrai + x_brt;

v_mes = v_vrai + v_brt;

a_mes = a_vrai + a_brt;

(sorry for the non translated value : vrai == real )

 

Hello,

thanx for your support ...

But i have some query, in this code real values(x_vrai,v_vrai, a_vrai) initialized with help of for loop. but in my prob i have make it general

and to pass real values through form.

 

what type of problem we can solve by t his ?

Can we estimate the true values of position , velocity and acceleration of any vehicle...?

What type of modification are neccessary to make it general....??

 

You should fill "x_mes" with the value you got from acquisition !

Then the algo computes the x_est (estimated values !)

Then you can delete the x_vrai which are the theoritical to make this snapshot !

 

 // Compute meas

x_mes = x_vrai + x_brt;

v_mes = v_vrai + v_brt;

a_mes = a_vrai + a_brt;

in above line measure values obtained by adding random noise in real values.....

are you saying that pass measured noisy values(measured values which have sum noise also) through form... as input?

 

Like   0

Kalman filter http://www.dsprelated.com/showcod

8/6/2013

Page 4: Basic Kalman Filter_SCILAB

8/11/2019 Basic Kalman Filter_SCILAB

http://slidepdf.com/reader/full/basic-kalman-filterscilab 4/4

Sylvain B wrote: 1/10/2012

deepasaini wrote: 1/11/2012

deepasaini wrote: 1/11/2012

Sylvain B wrote: 1/11/2012

UNGY_STARS wrote: 1/12/2012

Sylvain B wrote: 1/12/2012

UNGY_STARS wrote: 1/13/2012

 Yes,

replace the lines, by :

x_mes = your_values_from_acquisition

 

thanks a lot .....for this kind response!!

 

is there any particular range of time duration.........?

as used t =[0:0.5:100]; in this code .....

is there any specific limit..??

 

It depends on the simulation measures, and the errors at the end. If you put an infinite time, it could be that at the end the filter is totally

lost.

You can increase the time to have a longer simulation.

 

what type of problem can be solve by t his code..??

please give me any example..!!

 

This is a filte, to do estimation.

You provide "meseaured" values, and it gives you the "theoritical" ones, with less error.

 

thanks.....

Add a Comment

You need to login before you can post a comment (best way to prevent spam). ( Not a member?)

DSP Related Home Discussion Groups Code Snippets B logs Arti cles DSP Books pr ivacy pol icy Contac t

Kalman filter http://www.dsprelated.com/showcod

8/6/2013