{JZ}卡爾曼濾波算法C~~C++兩種實(shí)現(xiàn)代碼231_第1頁
{JZ}卡爾曼濾波算法C~~C++兩種實(shí)現(xiàn)代碼231_第2頁
{JZ}卡爾曼濾波算法C~~C++兩種實(shí)現(xiàn)代碼231_第3頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、卡爾曼濾波算法實(shí)現(xiàn)代碼C+實(shí)現(xiàn)代碼如下:=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#include#includecv.hclasskalmanpublic:voidinit_kalm

2、an(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);/virtualkalman();#endif/!defined(AFX_KALMAN_H_ED3D740F_01D2_4616_8B74_8BF57636F2C0_INCLUDED_)=kalman.cpp=#i

3、ncludekalman.h#include/*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

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,

5、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;cvRandlnit(&rng,0,1,-1,CV_RAND_UNI);cvZero(measurement);cvRandSetRange(&rng,0,0.1,0);rng.disttype=CV_RAND_NORMAL;cvRand(&rng,state);memcpy(cvkalma

6、n-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,Psizeof(P);memcpy(cvkalman-measurement_noise_cov-data.fl,R,sizeof(R);/cvSetldentity(cvkalman-process_noise_cov,c

7、vRealScalar(1e-5);/cvSetldentity(cvkalman-error_cov_post,cvRealScalar(1);/cvSetIdentity(cvkalman-measurement_noise_cov,cvRealScalar(1e-1);/*chooseinitialstate*/state-data.flO=x;state-data.fl1=xv;state-data.fl2=y;state-data.fl3=yv;cvkalman-state_post-data.flO=x;cvkalman-state_post-data.fl1=xv;cvkalma

8、n-state_post-data.fl2=y;cvkalman-state_post-data.fl3=yv;cvRandSetRange(&rng,0,sqrt(cvkalman-process_noise_cov-data.flO),0);cvRand(&rng,process_noise);CvPoint2D32fkalman:get_predict(floatx,floaty)/*updatestatewithcurrentposition*/state-data.flO=x;state-data.fl2=y;/*predictpointposition*/*xk=A欽k+B欽kPk

9、=A欽k-1*AT+Q*/cvRandSetRange(&rng,0,sqrt(cvkalman-measurement_noise_cov-data.flO),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,meas

10、urement);cvKalmanCorrect(cvkalman,measurement);floatmeasured_value_x=measurement-data.fl0;floatmeasured_value_y=measurement-data.fl2;constCvMat*prediction=cvKalmanPredict(cvkalman,0);floatpredict_value_x=prediction-data.fl0;floatpredict_value_y=prediction-data.fl2;return(cvPoint2D32f(predict_value_x

11、,predict_value_y);voidkalman:init_kalman(intx,intxv,inty,intyv)state-data.flO=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-data.fl3=yv;c語言實(shí)現(xiàn)代碼如下#includestdlib.h#includerinv.cin

12、tlman(n,m,k,f,q,r,h,y,x,p,g)intn,m,k;doublef,q,r,h,y,x,p,g;double*e,*a,*b;e=malloc(m*m*sizeof(double);l=m;if(ln)匸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+)

13、ii=i*n+j;pii=qii;for(kk=0;kk=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+)滬i*l+j;ajj=0.0;for(kk=0;kk=n-1;kk+)ajj=ajj+pi*n+kk*hn+kk;for(i=0;i=m-1;i+)for(j=0;j=m-1;j+)jj=i*m+j;ejj=rjj;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

14、=0)戊市if(js=0)戊市free(e);free(a);free(b);return(js);Ifor(i=0;i=n-1;i+)for(j=0;j=m-1;j+)for(j=0;j=m-1;j+)滬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+)滬(ii-1)*n+i;xjj=0.0;for(j=0;j=n-1;j+)xjj=xjj+fi*n+j*x(ii-2)*n+j;for(i=0;i=m-1;i+)jj=i*l;bjj=y(ii-1)*m+i;for(j=0;j=n-1;j+)

15、bjj=bjj-hi*n+j*x(ii-1)*n+j;for(i=0;i=n-1;i+)rlHEILI滬(ii-1)*n+i;for(j=0;j=m-1;j+)xjj=xjj+gi*m+j*bI;if(iik)for(i=0;i=n-1;i+)for(j=0;j=n-1;j+)jj=i*l+j;ajj=0.0;for(kk=0;kk=m-1;kk+)ajj=ajj-gi*m+kk*hkk*n+j;if(i=j)ajj=1.0+ajj;for(i=0;i=n-1;i+)for(j=0;j=n-1;j+)jj=i*l+j;bjj=0.0;for(kk=0;kk=n-1;kk+)bjj=bjj+ai*l

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論