打印

卡尔曼滤波的实现程序

[复制链接]
3509|11
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
梅花望青竹|  楼主 | 2012-4-8 09:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
===================kalman.h=======================
// kalman.h: interface for the kalman class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__INCLUDED_)
#define AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif
// _MSC_VER > 1000

#include <math.h>
#include "cv.h"



class kalman  
{
public:
void init_kalman(int x,int xv,int y,int yv);
CvKalman* cvkalman;
CvMat* state;
CvMat* process_noise;
CvMat* measurement;
const CvMat* prediction;
CvPoint2D32f get_predict(
float x, float y);
kalman(
int x=0,int xv=0,int y=0,int yv=0);
//virtual ~kalman();


};

#endif
// !defined(AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__INCLUDED_)








==================kalman.cpp=====================
#include "kalman.h"
#include <stdio.h>


/* tester de printer toutes les valeurs des vecteurs */
/* tester de changer les matrices du noises */
/* replace state by cvkalman->state_post ??? */


CvRandState rng;
const
double T = 0.1;
kalman::kalman(
int x,int xv,int y,int yv)
{     
    cvkalman = cvCreateKalman( 4, 4, 0 );
    state = cvCreateMat( 4, 1, CV_32FC1 );
    process_noise = cvCreateMat( 4, 1, CV_32FC1 );
    measurement = cvCreateMat( 4, 1, CV_32FC1 );
   
int code = -1;
   
   
/* create matrix data */
const
float A[] = {
   1, T, 0, 0,
   0, 1, 0, 0,
   0, 0, 1, T,
   0, 0, 0, 1
  };
     
     
const
float H[] = {
    1, 0, 0, 0,
    0, 0, 0, 0,
   0, 0, 1, 0,
   0, 0, 0, 0
  };
      
     
const
float P[] = {
    pow(320,2), pow(320,2)/T, 0, 0,
   pow(320,2)/T, pow(320,2)/pow(T,2), 0, 0,
   0, 0, pow(240,2), pow(240,2)/T,
   0, 0, pow(240,2)/T, pow(240,2)/pow(T,2)
    };

     
const
float Q[] = {
   pow(T,3)/3, pow(T,2)/2, 0, 0,
   pow(T,2)/2, T, 0, 0,
   0, 0, pow(T,3)/3, pow(T,2)/2,
   0, 0, pow(T,2)/2, T
   };
   
     
const
float R[] = {
   1, 0, 0, 0,
   0, 0, 0, 0,
   0, 0, 1, 0,
   0, 0, 0, 0
   };
   
   
    cvRandInit( &rng, 0, 1, -1, CV_RAND_UNI );

    cvZero( measurement );
   
    cvRandSetRange( &rng, 0, 0.1, 0 );
    rng.disttype = CV_RAND_NORMAL;

    cvRand( &rng, state );

    memcpy( cvkalman->transition_matrix->data.fl, A,
sizeof(A));
    memcpy( cvkalman->measurement_matrix->data.fl, H,
sizeof(H));
    memcpy( cvkalman->process_noise_cov->data.fl, Q,
sizeof(Q));
    memcpy( cvkalman->error_cov_post->data.fl, P,
sizeof(P));
    memcpy( cvkalman->measurement_noise_cov->data.fl, R,
sizeof(R));
   
//cvSetIdentity( cvkalman->process_noise_cov, cvRealScalar(1e-5) );   
    //cvSetIdentity( cvkalman->error_cov_post, cvRealScalar(1));
//cvSetIdentity( cvkalman->measurement_noise_cov, cvRealScalar(1e-1) );

/* choose initial state */

    state->data.fl[0]=x;
    state->data.fl[1]=xv;
    state->data.fl[2]=y;
    state->data.fl[3]=yv;
    cvkalman->state_post->data.fl[0]=x;
    cvkalman->state_post->data.fl[1]=xv;
    cvkalman->state_post->data.fl[2]=y;
    cvkalman->state_post->data.fl[3]=yv;

cvRandSetRange( &rng, 0, sqrt(cvkalman->process_noise_cov->data.fl[0]), 0 );
    cvRand( &rng, process_noise );


    }

     
CvPoint2D32f kalman::get_predict(
float x, float y){
   

   
/* update state with current position */
    state->data.fl[0]=x;
    state->data.fl[2]=y;

   
   
/* predict point position */
/* x'k=Ak+Bk
       P'k=A
k-1*AT + Q */
    cvRandSetRange( &rng, 0, sqrt(cvkalman->measurement_noise_cov->data.fl[0]), 0 );
    cvRand( &rng, measurement );
   
     
/* xk=A?xk-1+B?uk+wk */
    cvMatMulAdd( cvkalman->transition_matrix, state, process_noise, cvkalman->state_post );
   
   
/* zk=H?xk+vk */
    cvMatMulAdd( cvkalman->measurement_matrix, cvkalman->state_post, measurement, measurement );
   
   
/* adjust Kalman filter state */
/* Kk=P'kT?H'kT+R)-1
       xk=x'k+Kk
?zk-H'k)
       Pk=(I-Kk
)'k */
    cvKalmanCorrect( cvkalman, measurement );
   
float measured_value_x = measurement->data.fl[0];
   
float measured_value_y = measurement->data.fl[2];

   
const CvMat* prediction = cvKalmanPredict( cvkalman, 0 );
   
float predict_value_x = prediction->data.fl[0];
   
float predict_value_y = prediction->data.fl[2];

   
return(cvPoint2D32f(predict_value_x,predict_value_y));
}

void kalman::init_kalman(int x,int xv,int y,int yv)
{
state->data.fl[0]=x;
    state->data.fl[1]=xv;
    state->data.fl[2]=y;
    state->data.fl[3]=yv;
    cvkalman->state_post->data.fl[0]=x;
    cvkalman->state_post->data.fl[1]=xv;
    cvkalman->state_post->data.fl[2]=y;
    cvkalman->state_post->data.fl[3]=yv;
}

相关帖子

沙发
gaochy1126| | 2012-4-8 10:08 | 只看该作者
我想问一下,卡尔曼滤波的参数是怎么确定的?

使用特权

评论回复
板凳
teet| | 2012-4-8 10:35 | 只看该作者
不太清楚呀

使用特权

评论回复
地板
teet| | 2012-4-8 10:35 | 只看该作者
:(

使用特权

评论回复
5
职场新鲜人| | 2012-4-10 11:55 | 只看该作者
参数是不是需要一点点调试啊

使用特权

评论回复
6
老鱼探戈| | 2012-5-19 10:30 | 只看该作者
哪里COPY来的代码!

使用特权

评论回复
7
shenmulzb1985| | 2012-5-19 12:50 | 只看该作者
代码很强大,不过我看了一遍当然还是看不太明白的啊,我会吃透楼主的代码的啊

使用特权

评论回复
8
fengzhongzhu| | 2012-5-19 17:43 | 只看该作者
好厉害,可是看不明白啊

使用特权

评论回复
9
czdo| | 2012-5-19 18:54 | 只看该作者
怎么也注释一下!!!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

98

主题

2589

帖子

7

粉丝