卡尔曼滤波简介与C——C++算法实现代码 下载本文

内容发布更新时间 : 2024/12/26 3:47:05星期一 下面是文章的全部内容请认真阅读。

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; }