《卡尔曼滤波算法(C--C++两种实现代码)》由会员分享,可在线阅读,更多相关《卡尔曼滤波算法(C--C++两种实现代码)(10页珍藏版)》请在金锄头文库上搜索。
1、卡尔曼滤波算法实现代码C+实现代码如下:=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 #include “cv.h“ cla
2、ss 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_KALM
3、AN_H_ED3D740F_01D2_4616_8B74_8BF57636F2C0_INCLUDED_)=kalman.cpp= #include “kalman.h“ #include /* tester de printer toutes les valeurs des vecteurs*/* tester de changer les matrices du noises */* replace state by cvkalman-state_post ? */CvRandState rng; constdouble T = 0.1; kalman:kalman(int x, int x
4、v, 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 */constfloat A = 1, T , 0, 0, 0, 1, 0, 0, 0, 0, 1, T, 0, 0, 0, 1 ; constfloa
5、t H = 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ; constfloat 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) ; constfloat Q = pow(T,3)/3, pow(T,2)/2, 0, 0, pow(T,2)/2, T, 0, 0, 0, 0, pow(T,3)/3,
6、 pow(T,2)/2, 0, 0, pow(T,2)/2, T ; constfloat R = 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ; cvRandInit( cvZero( measurement ); cvRandSetRange( rng.disttype = CV_RAND_NORMAL; cvRand( memcpy( cvkalman-transition_matrix-data.fl, A, sizeof (A); memcpy( cvkalman-measurement_matrix-data.fl, H, size
7、of (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, cvRealS
8、calar(1); /cvSetIdentity( cvkalman-measurement_noise_cov, cvRealScalar(1e-1) ); /* choose initial state */state-data.fl0=x; state-data.fl1=xv; state-data.fl2=y; state-data.fl3=yv; cvkalman-state_post-data.fl0=x; cvkalman-state_post-data.fl1=xv; cvkalman-state_post-data.fl2=y; cvkalman-state_post-dat
9、a.fl3=yv; cvRandSetRange( cvRand( CvPoint2D32f kalman:get_predict(float x, float y) /* update state with current position */state-data.fl0=x; state-data.fl2=y; /* predict point position */* xk=A鈥k+B鈥k Pk=A鈥k-1*AT + Q */cvRandSetRange( cvRand( /* xk=A?xk-1+B?uk+wk */cvMatMulAdd( cvkalman-transition_m
10、atrix, state, process_noise, cvkalman-state_post ); /* zk=H?xk+vk */cvMatMulAdd( cvkalman-measurement_matrix, cvkalman-state_post, measurement, measurement ); cvKalmanCorrect( cvkalman, measurement ); float measured_value_x = measurement-data.fl0; float measured_value_y = measurement-data.fl2; const
11、 CvMat* prediction = cvKalmanPredict( cvkalman, 0 ); float predict_value_x = prediction-data.fl0; float predict_value_y = prediction-data.fl2; return(cvPoint2D32f(predict_value_x,predict_value_y); void kalman:init_kalman(int x, int xv, int y, int yv) state-data.fl0=x; state-data.fl1=xv; state-data.f
12、l2=y; state-data.fl3=yv; cvkalman-state_post-data.fl0=x; cvkalman-state_post-data.fl1=xv; cvkalman-state_post-data.fl2=y; cvkalman-state_post-data.fl3=yv; c 语言实现代码如下:#include “stdlib.h“ #include “rinv.c“ int lman(n,m,k,f,q,r,h,y,x,p,g) int n,m,k; double f,q,r,h,y,x,p,g; int i,j,kk,ii,l,jj,js; double
13、 *e,*a,*b; e=malloc(m*m*sizeof(double); l=m; if (ln) l=n; a=malloc(l*l*sizeof(double); b=malloc(l*l*sizeof(double); for (i=0; i=n-1; i+) for (j=0; j=n-1; j+) ii=i*l+j; aii=0.0; for (kk=0; kk=n-1; kk+) aii=aii+pi*n+kk*fj*n+kk; for (i=0; i=n-1; i+) for (j=0; j=n-1; j+) ii=i*n+j; pii=qii; for (kk=0; kk
14、=n-1; kk+) pii=pii+fi*n+kk*akk*l+j; for (ii=2; ii=k; ii+) for (i=0; i=n-1; i+) for (j=0; j=m-1; j+) jj=i*l+j; ajj=0.0; for (kk=0; kk=n-1; kk+) ajj=ajj+pi*n+kk*hj*n+kk; for (i=0; i=m-1; i+) for (j=0; j=m-1; j+) jj=i*m+j; ejj=rjj; for (kk=0; kk=n-1; kk+) ejj=ejj+hi*n+kk*akk*l+j; js=rinv(e,m); if (js=0) free(e); free(a); free(b); return(js); for (i=0; i=n-1; i+) for (j=0; j=m-1; j+) jj=i*m+j; gjj=0.0; for (kk=0; kk=m-1; kk+) gjj=gjj+ai*l+kk*ej*m+kk; for (i=0; i=n-1; i+) jj=(ii-1)*n+i; xjj=0.0; for (j=0; j=n-1; j+) xjj=xjj