您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 卡尔曼滤波算法(C--C++两种实现代码)
卡尔曼滤波算法实现代码C++实现代码如下:============================kalman.h================================//kalman.h:interfaceforthekalmanclass.////////////////////////////////////////////////////////////////////////#if!defined(AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__INCLUDED_)#defineAFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__INCLUDED_#if_MSC_VER1000#pragmaonce#endif//_MSC_VER1000#includemath.h#includecv.hclasskalman{public:voidinit_kalman(intx,intxv,inty,intyv);CvKalman*cvkalman;CvMat*state;CvMat*process_noise;CvMat*measurement;constCvMat*prediction;CvPoint2D32fget_predict(floatx,floaty);kalman(intx=0,intxv=0,inty=0,intyv=0);//virtual~kalman();};#endif//!defined(AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__INCLUDED_)============================kalman.cpp================================#includekalman.h#includestdio.h/*testerdeprintertouteslesvaleursdesvecteurs*//*testerdechangerlesmatricesdunoises*//*replacestatebycvkalman-state_post???*/CvRandStaterng;constdoubleT=0.1;kalman::kalman(intx,intxv,inty,intyv){cvkalman=cvCreateKalman(4,4,0);state=cvCreateMat(4,1,CV_32FC1);process_noise=cvCreateMat(4,1,CV_32FC1);measurement=cvCreateMat(4,1,CV_32FC1);intcode=-1;/*creatematrixdata*/constfloatA[]={1,T,0,0,0,1,0,0,0,0,1,T,0,0,0,1};constfloatH[]={1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0};constfloatP[]={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)};constfloatQ[]={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};constfloatR[]={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));/*chooseinitialstate*/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);}CvPoint2D32fkalman::get_predict(floatx,floaty){/*updatestatewithcurrentposition*/state-data.fl[0]=x;state-data.fl[2]=y;/*predictpointposition*//*x'k=A鈥鈥P'k=A鈥-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);cvKalmanCorrect(cvkalman,measurement);floatmeasured_value_x=measurement-data.fl[0];floatmeasured_value_y=measurement-data.fl[2];constCvMat*prediction=cvKalmanPredict(cvkalman,0);floatpredict_value_x=prediction-data.fl[0];floatpredict_value_y=prediction-data.fl[2];return(cvPoint2D32f(predict_value_x,predict_value_y));}voidkalman::init_kalman(intx,intxv,inty,intyv){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;}c语言实现代码如下:#includestdlib.h#includerinv.cintlman(n,m,k,f,q,r,h,y,x,p,g)intn,m,k;doublef[],q[],r[],h[],y[],x[],p[],g[];{inti,j,kk,ii,l,jj,js;double*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;a[ii]=0.0;for(kk=0;kk=n-1;kk++)a[ii]=a[ii]+p[i*n+kk]*f[j*n+kk];}for(i=0;i=n-1;i++)for(j=0;j=n-1;j++){ii=i*n+j;p[ii]=q[ii];for(kk=0;kk=n-1;kk++)p[ii]=p[ii]+f[i*n+kk]*a[kk*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;a[jj]=0.0;for(kk=0;kk=n-1;kk++)a[jj]=a[jj]+p[i*n+kk]*h[j*n+kk];}for(i=0;i=m-1;i++)for(j=0;j=m-1;j++){jj=i*m+j;e[jj]=r[jj];for(kk=0;kk=n-1;kk++)e[jj]=e[jj]+h[i*n+kk]*a[kk*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;g[jj]=0.0;for(kk=0;kk=m-1;kk++)g[jj]=g[jj]+a[i*l+kk]*e[j*m+kk];}for(i=0;i=n-1;i++){jj=(ii-1)*n+i;x[jj]=0.0;for(j=0;j=n-1;j++)x[jj]=x[jj]+f[i*n+j]*x[(ii-2)*n+j];}for(i=0;i=m-1;i++){jj=i*l;b[jj]=y[(ii-1)*m+i];for(j=0;j=n-1;j++)b[jj]=b[jj]-h[i*n+j]*x[(ii-1)*n+j];}for(i=0;i=n-1;i++){jj=(ii-1)*n+i;for(j=0;j=m-1;j++)x[jj]=x[jj]+g[i*m+j]*b[j*l];}if(iik){for(i=0;i=n-1;i++)for(j=0;j=n-1;j++){jj=i*l+j;a[jj]=0.0;for(kk=0;kk=m-1;kk++)a[jj]=a[jj]-g[i*m+kk]*h[kk*n+j];if(i==j)a[jj]=1.0+a[jj];}for(i=0;i=n-1;i++)for(j=0;j=n-1;j++){j
本文标题:卡尔曼滤波算法(C--C++两种实现代码)
链接地址:https://www.777doc.com/doc-2598780 .html