卡尔曼滤波c语言实现

合集下载

卡尔曼滤波算法(C--C++两种实现代码)(可编辑修改word版)

卡尔曼滤波算法(C--C++两种实现代码)(可编辑修改word版)

卡尔曼滤波算法实现代码C++实现代码如下:kalman(int x=0,int xv=0,int y=0,int yv=0);//virtual ~kalman();};#endif // !defined(AFX_KALMAN_H ED3D740F_01D2_4616_8B74_8BF57636F2C 0 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 鈥kP'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, meas urement, measurement );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;}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 *e,*a,*b;e=malloc(m*m*sizeof(double));l=m;if (l<n) 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 (ii<k){ 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++){ jj=i*l+j; b[jj]=0.0;for (kk=0; kk<=n-1; kk++)b[jj]=b[jj]+a[i*l+kk]*p[kk*n+j];}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<=n-1; kk++)a[jj]=a[jj]+b[i*l+kk]*f[j*n+kk];}for (i=0; i<=n-1; i++)for (j=0; j<=n-1; j++){ jj=i*n+j; p[jj]=q[jj];for (kk=0; kk<=n-1; kk++)p[jj]=p[jj]+f[i*n+kk]*a[j*l+kk];}}}free(e); free(a); free(b); return(js);}。

卡尔曼滤波c语言实现

卡尔曼滤波c语言实现

卡尔曼滤波c语言实现
卡尔曼滤波(Kalman filter)是一种著名的非线性估计方法,它由美国工程师R.E.KALMAN于1960年提出。

卡尔曼滤波基本思想是将系统状态采用隐马尔可夫模型来描述,将系统状态的不确定性表示为有限的概率分布,由此建立一个状态估计问题。

c语言实现卡尔曼滤波的步骤如下:
1、初始化:设置初始条件,包括状态变量x的初始值和协方差矩阵P的初始值。

2、预测:根据当前的状态变量x估计下一时刻的状态变量x_prediction。

3、更新:根据观测值z和预测状态变量
x_prediction更新当前状态变量x_update。

4、循环:重复以上步骤,直到达到目标精度或者停止条件。

卡尔曼滤波代码实现c++

卡尔曼滤波代码实现c++

卡尔曼滤波代码实现c++卡尔曼滤波(Kalman Filter)是一种用于估计线性系统状态的算法。

下面是一个简单的卡尔曼滤波代码实现示例,使用 C++编写:```cpp#include <iostream>#include <vector>class KalmanFilter {public:// 初始化卡尔曼滤波器KalmanFilter(double initial_state, double initial_error_variance, double process_variance) {S = initial_error_variance;X = initial_state;P = initial_error_variance;}// 预测下一时刻的状态void predict(double measurement) {X = X + measurement;P = P + process_variance;}// 修正当前时刻的状态void correct(double measurement) {S = S + P;K = P / S;X = X + K * (measurement - X);P = (1 - K) * P;}private:double X; // 当前时刻的状态估计double P; // 状态估计的误差协方差double S; // 测量噪声的协方差double K; // 卡尔曼增益double process_variance; // 过程噪声的方差};int main() {// 初始状态和误差协方差double initial_state = 0.0;double initial_error_variance = 1.0;double process_variance = 0.1;// 创建卡尔曼滤波器对象KalmanFilter filter(initial_state, initial_error_variance, process_variance);// 进行状态预测和修正std::vector<double> measurements = {1.0, 2.0, 3.0};for (double measurement : measurements) {filter.predict(measurement);filter.correct(measurement);std::cout << "当前状态估计: " << filter.X << std::endl;}return 0;}```上述代码实现了一个简单的卡尔曼滤波器,用于对线性系统的状态进行估计。

卡尔曼滤波算法及C语言代码

卡尔曼滤波算法及C语言代码

到现在为止,我们已经得到了 k 状态下最优的估算值 X(k|k)。但是为了要另卡尔曼滤波器不断 的运行下去直到系统过程结束,我们还要更新 k 状态下 X(k|k)的 covariance: P(k|k)=(I-Kg(k) H)P(k|k-1) ……… (5) 其中 I 为 1 的矩阵,对于单模型单测量,I=1。当系统进入 k+1 状态时,P(k|k)就是式子(2)的 P(k-1|k-1)。这样,算法就可以自回归的运算下去。
卡尔曼滤波器 – Kalman Filter
1.
什么是卡尔曼滤波器
(What is the Kalman Filter?)
在学习卡尔曼滤波器之前,首先看看为什么叫“卡尔曼”。跟其他著名的理论(例如傅立叶变换, 泰勒级数等等)一样,卡尔曼也是一个人的名字,而跟他们Rudolf Emil Kalman,匈牙利数学家,1930 年出生于匈牙利首都布达佩斯。1953, 1954 年于麻省理工学院分别获得电机工程学士及硕士学位。1957 年于哥伦比亚大学获得博士学 位。我们现在要学习的卡尔曼滤波器,正是源于他的博士论文和 1960 年发表的论文《A New Approach to Linear Filtering and Prediction Problems》(线性滤波与预测问题的新方法)。 如果对这编论文有兴趣,可以到这里的地址下载: /~welch/media/pdf/Kalman1960.pdf。
现在我们有了现在状态的预测结果,然后我们再收集现在状态的测量值。结合预测值和测量值, 我们可以得到现在状态(k)的最优化估算值 X(k|k): X(k|k)= X(k|k-1)+Kg(k) (Z(k)-H X(k|k-1)) ……… (3) 其中 Kg 为卡尔曼增益(Kalman Gain): Kg(k)= P(k|k-1) H’ / (H P(k|k-1) H’ + R) ……… (4)

卡尔曼滤波c语言实例

卡尔曼滤波c语言实例

卡尔曼滤波c语言实例Kalman filtering is a widely used method for estimating the state of a dynamic system based on noisy measurements. It combines information from multiple sources to provide a more accurate and reliable estimate of the system's state. Kalman filtering has applications in a variety of fields, including aerospace, robotics, and finance. Its effectiveness lies in its ability to account for uncertainty in both the system dynamics and the sensor measurements.卡尔曼滤波是一种广泛使用的方法,用于基于嘈杂的测量来估计动态系统的状态。

它结合了来自多个源的信息,以提供对系统状态更准确和可靠的估计。

卡尔曼滤波在各个领域都有应用,包括航空航天、机器人技术和金融领域。

其有效性在于能够考虑系统动态和传感器测量中的不确定性。

In a Kalman filter, the system's state is represented as a vector, and the filter continuously updates this vector based on incoming measurements. The filter uses a model of the system dynamics to predict how the state will evolve over time, and then combines this prediction with the measurement to produce an updated estimate.This process is iterated at each time step, resulting in a sequence of state estimates that converge towards the true state of the system.在卡尔曼滤波器中,系统状态被表示为一个向量,并根据传入的测量持续更新这个向量。

卡尔曼滤波算法(C--C++两种实现代码)

卡尔曼滤波算法(C--C++两种实现代码)

卡尔曼滤波算法实现代码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 <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__INCLUD ED_)============================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鈥kP'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, cvk alman->state_post );/* zk=H?xk+vk */cvMatMulAdd( cvkalman->measurement_matrix, cvkalman->state_post, mea surement, measurement );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;}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*e,*a,*b;e=malloc(m*m*sizeof(double));l=m;if(l<n) 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(ii<k){ 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++){ jj=i*l+j; b[jj]=0.0;for(kk=0; kk<=n-1; kk++)b[jj]=b[jj]+a[i*l+kk]*p[kk*n+j];}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<=n-1; kk++)a[jj]=a[jj]+b[i*l+kk]*f[j*n+kk];}for(i=0; i<=n-1; i++)for(j=0; j<=n-1; j++){ jj=i*n+j; p[jj]=q[jj];for(kk=0; kk<=n-1; kk++)p[jj]=p[jj]+f[i*n+kk]*a[j*l+kk];}}}free(e); free(a); free(b); return(js);}。

自适应卡尔曼滤波c语言实现

自适应卡尔曼滤波c语言实现

自适应卡尔曼滤波C语言实现1. 什么是卡尔曼滤波?卡尔曼滤波是一种用于估计系统状态的算法,它基于线性动态系统模型和高斯噪声假设。

卡尔曼滤波器通过不断地更新状态估计值,将测量结果和系统动态进行融合,提供更准确的状态估计。

在实际应用中,传感器测量值常常包含噪声或者不完全准确。

卡尔曼滤波通过对测量值进行加权平均,同时考虑系统的动态模型和测量噪声的特性,可以有效地抑制噪声并提高状态估计的精度。

2. 卡尔曼滤波的基本原理卡尔曼滤波器由两个步骤组成:预测步骤和更新步骤。

预测步骤预测步骤用于根据当前时刻的状态估计值和系统动态模型,预测下一时刻的状态估计值。

假设我们有一个状态向量x表示系统的状态,在每个时刻t,我们可以使用状态转移矩阵A将当前时刻的状态向量x(t)预测到下一时刻的状态向量x(t+1):x(t+1) = A * x(t)同时,我们还需要考虑过程噪声的影响。

假设过程噪声服从均值为0、协方差矩阵为Q的高斯分布,我们可以使用协方差矩阵Q来描述过程噪声的特性。

预测步骤可以表示为:P(t+1|t) = A * P(t|t) * A' + Q其中,P(t|t)表示当前时刻的状态估计误差协方差矩阵,P(t+1|t)表示预测步骤中的状态估计误差协方差矩阵。

更新步骤更新步骤用于根据当前时刻的测量值和预测步骤得到的状态估计值,更新系统状态的估计。

假设我们有一个观测向量z表示系统的观测值,在每个时刻t,我们可以使用观测模型C将当前时刻的状态向量x(t)映射到观测空间中:z(t) = C * x(t)同时,我们还需要考虑观测噪声的影响。

假设观测噪声服从均值为0、协方差矩阵为R的高斯分布,我们可以使用协方差矩阵R来描述观测噪声的特性。

更新步骤可以表示为:K(t+1) = P(t+1|t) * C' * (C * P(t+1|t) * C' + R)^(-1)x(t+1|t+1) = x(t+1|t) + K(t+1) * (z(t+1) - C * x(t+1|t))P(t+1|t+1) = (I - K(t+1) * C) * P(t+1|t)其中,K(t+1)表示卡尔曼增益,x(t+1|t+1)表示更新步骤中的状态估计值,P(t+1|t+1)表示更新步骤中的状态估计误差协方差矩阵。

卡尔曼滤波简介及其实现(附C代码)

卡尔曼滤波简介及其实现(附C代码)

卡尔曼滤波简介及其算法实现代码(C++/C/MATLAB)卡尔曼滤波器简介近来发现有些问题很多人都很感兴趣。

所以在这里希望能尽自己能力跟大家讨论一些力所能及的算法。

现在先讨论一下卡尔曼滤波器,如果时间和能力允许,我还希望能够写写其他的算法,例如遗传算法,傅立叶变换,数字滤波,神经网络,图像处理等等。

因为这里不能写复杂的数学公式,所以也只能形象的描述。

希望如果哪位是这方面的专家,欢迎讨论更正。

卡尔曼滤波器– Kalman Filter1.什么是卡尔曼滤波器(What is the Kalman Filter?)在学习卡尔曼滤波器之前,首先看看为什么叫“卡尔曼”。

跟其他著名的理论(例如傅立叶变换,泰勒级数等等)一样,卡尔曼也是一个人的名字,而跟他们不同的是,他是个现代人!卡尔曼全名Rudolf Emil Kalman,匈牙利数学家,1930年出生于匈牙利首都布达佩斯。

1953,1954年于麻省理工学院分别获得电机工程学士及硕士学位。

1957年于哥伦比亚大学获得博士学位。

我们现在要学习的卡尔曼滤波器,正是源于他的博士论文和1960年发表的论文《A New Approach to Linear Filtering and Prediction Problems》(线性滤波与预测问题的新方法)。

如果对这编论文有兴趣,可以到这里的地址下载:/~welch/media/pdf/Kalman1960.pdf。

简单来说,卡尔曼滤波器是一个“optimal recursive data processing algorithm(最优化自回归数据处理算法)”。

对于解决很大部分的问题,他是最优,效率最高甚至是最有用的。

他的广泛应用已经超过30年,包括机器人导航,控制,传感器数据融合甚至在军事方面的雷达系统以及导弹追踪等等。

近年来更被应用于计算机图像处理,例如头脸识别,图像分割,图像边缘检测等等。

2.卡尔曼滤波器的介绍(Introduction to the Kalman Filter)为了可以更加容易的理解卡尔曼滤波器,这里会应用形象的描述方法来讲解,而不是像大多数参考书那样罗列一大堆的数学公式和数学符号。

卡尔曼滤波算法C语言实现

卡尔曼滤波算法C语言实现

卡尔曼滤波算法及C 语言实现摘要:本文着重讨论了卡尔曼滤波器的原理,典型算法以及应用领域。

清晰地阐述了kalman filter 在信息估计方面的最优性能。

着重介绍简单kalman filter algorithm 的编程,使用kalman filter 的经典5个体现最优化递归公式来编程。

通过c 语言编写程序实现kalman filter 的最优估计能力。

关键词:kalman filter ;最优估计;C 语言1 引言Kalman Filter 是一个高效的递归滤波器,它可以实现从一系列的噪声测量中,估计动态系统的状态。

起源于Rudolf Emil Kalman 在1960年的博士论文和发表的论文《A New Approach to Linear Eiltering and Prediction Problems 》(《线性滤波与预测问题的新方法》)。

并且最先在阿波罗登月计划轨迹预测上应用成功,此后kalman filter 取得重大发展和完善。

它的广泛应用已经超过30年,包括机器人导航,控制。

传感器数据融合甚至在军事方面的雷达系统以及导弹追踪等等,近年来更被广泛应用于计算机图像处理,例如头脸识别,图像分割,图像边缘检测等等。

2 kalman filter 最优化递归估计Kalman filter 是一个“optimal recursive data processing algorithm (最优化递归数据处理方法)”。

对于解决很大部分的问题,他是最优,效率最高甚至是最有用的方法。

而kalman filter 最为核心的内容是体现它最优化估计和递归特点的5条公式。

举一个例子来详细说明5条公式的物理意义。

假设我们要研究的对象是某一个房间的温度信号。

对于室温来说,一分钟内或一小段时间内的值是基本上不变的或者变化范围很小。

也就是说1t 时刻的温度1T 和2t 时刻的温度2T 基本不变,即12T T =。

卡尔曼滤波算法及C语言代码

卡尔曼滤波算法及C语言代码

卡尔曼滤波简介及其算法实现代码卡尔曼滤波算法实现代码(C,C++分别实现)卡尔曼滤波器简介近来发现有些问题很多人都很感兴趣。

所以在这里希望能尽自己能力跟大家讨论一些力所能及的算法。

现在先讨论一下卡尔曼滤波器,如果时间和能力允许,我还希望能够写写其他的算法,例如遗传算法,傅立叶变换,数字滤波,神经网络,图像处理等等。

因为这里不能写复杂的数学公式,所以也只能形象的描述。

希望如果哪位是这方面的专家,欢迎讨论更正。

卡尔曼滤波器– Kalman Filter1.什么是卡尔曼滤波器(What is the Kalman Filter?)在学习卡尔曼滤波器之前,首先看看为什么叫“卡尔曼”。

跟其他著名的理论(例如傅立叶变换,泰勒级数等等)一样,卡尔曼也是一个人的名字,而跟他们不同的是,他是个现代人!卡尔曼全名Rudolf Emil Kalman,匈牙利数学家,1930年出生于匈牙利首都布达佩斯。

1953,1954年于麻省理工学院分别获得电机工程学士及硕士学位。

1957年于哥伦比亚大学获得博士学位。

我们现在要学习的卡尔曼滤波器,正是源于他的博士论文和1960年发表的论文《A New Approach to Linear Filtering and Prediction Problems》(线性滤波与预测问题的新方法)。

如果对这编论文有兴趣,可以到这里的地址下载:/~welch/media/pdf/Kalman1960.pdf。

简单来说,卡尔曼滤波器是一个“optimal recursive data processing algorithm(最优化自回归数据处理算法)”。

对于解决很大部分的问题,他是最优,效率最高甚至是最有用的。

他的广泛应用已经超过30年,包括机器人导航,控制,传感器数据融合甚至在军事方面的雷达系统以及导弹追踪等等。

近年来更被应用于计算机图像处理,例如头脸识别,图像分割,图像边缘检测等等。

2.卡尔曼滤波器的介绍(Introduction to the Kalman Filter)为了可以更加容易的理解卡尔曼滤波器,这里会应用形象的描述方法来讲解,而不是像大多数参考书那样罗列一大堆的数学公式和数学符号。

(完整word版)卡尔曼滤波算法(C--C++两种实现代码)

(完整word版)卡尔曼滤波算法(C--C++两种实现代码)

卡尔曼滤波算法实现代码C++实现代码如下: ============================kalman.h================= =============== // kalman.h: interface for the kalman class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__IN CLUDED_) #define AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__INCLU DED_#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_8BF57636F2C 0__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 鈥 kP'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, meas urement, measurement );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; }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 *e,*a,*b; e=malloc(m*m*sizeof(double)); l=m; if (l<n) 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 (ii<k){ 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++) { jj=i*l+j; b[jj]=0.0; for (kk=0; kk<=n-1; kk++) b[jj]=b[jj]+a[i*l+kk]*p[kk*n+j]; } 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<=n-1; kk++) a[jj]=a[jj]+b[i*l+kk]*f[j*n+kk]; } for (i=0; i<=n-1; i++) for (j=0; j<=n-1; j++) { jj=i*n+j; p[jj]=q[jj]; for (kk=0; kk<=n-1; kk++) p[jj]=p[jj]+f[i*n+kk]*a[j*l+kk]; } }} free(e); free(a); free(b); return(js);}。

卡尔曼滤波c语言,经纬度

卡尔曼滤波c语言,经纬度

卡尔曼滤波c语言,经纬度卡尔曼滤波(Kalman Filtering)是一个广泛应用于导航和位置估计等领域的信号处理技术。

在这篇文章中,我们将重点讨论使用C语言实现卡尔曼滤波来处理经纬度数据。

第一步:了解卡尔曼滤波的原理和应用卡尔曼滤波是一种概率估计方法,通过从多个测量中提取和融合信息,来估计一个对象在时间上的状态。

它通过先验估计和测量更新,不断调整状态估计值。

在处理经纬度数据时,卡尔曼滤波可以用于减少噪声和提高定位精度。

它结合了历史观测值和传感器测量值,利用统计学原理进行动态调整,从而提供更准确的位置估计。

第二步:理解经纬度数据的特点和问题经纬度是用于描述地球表面位置的坐标系统,由纬度和经度两个值组成。

然而,由于测量误差、信号衰减和多路径干扰等因素的存在,经纬度数据会受到很多噪声的影响。

因此,在处理经纬度数据时,我们需要考虑如何降低这些噪声的影响。

第三步:实现卡尔曼滤波算法卡尔曼滤波算法主要由两个步骤组成:预测和更新。

在预测步骤中,根据系统的动态模型和先验信息,估计下一个状态的预测值和协方差。

在更新步骤中,将测量值与预测值进行比较,计算卡尔曼增益和更新后的估计值和协方差。

在C语言中实现卡尔曼滤波算法,需要定义一些变量和函数。

首先,我们需要定义状态向量、状态协方差矩阵、系统的动态模型和测量矩阵。

然后,我们可以编写预测和更新的函数来执行卡尔曼滤波算法。

例如,我们可以定义状态向量为:float state[2]; 经度和纬度状态协方差矩阵为:float covariance[2][2]; 协方差矩阵系统动态模型可以由加速度和速度模型来确定:float acceleration; 加速度float velocity; 速度测量矩阵可以由GPS测量值来确定:float gpsMeasurement[2]; GPS测量值然后,我们可以编写预测函数来计算下一个状态的预测值和协方差:void predict(){state[0] += velocity * deltaT + 0.5 * acceleration * deltaT * deltaT;state[1] += velocity * deltaT + 0.5 * acceleration * deltaT * deltaT;covariance[0][0] += deltaT * deltaT;covariance[1][1] += deltaT * deltaT;}接下来,我们可以编写更新函数来计算卡尔曼增益并更新估计值和协方差:void update(){float innovation[2];innovation[0] = gpsMeasurement[0] - state[0];innovation[1] = gpsMeasurement[1] - state[1];float innovationCovariance[2][2];innovationCovariance[0][0] = covariance[0][0] + gpsAccuracy[0];innovationCovariance[1][1] = covariance[1][1] + gpsAccuracy[1];float kalmanGain[2][2];kalmanGain[0][0] = covariance[0][0] / innovationCovariance[0][0];kalmanGain[1][1] = covariance[1][1] / innovationCovariance[1][1];state[0] += kalmanGain[0][0] * innovation[0];state[1] += kalmanGain[1][1] * innovation[1];covariance[0][0] *= (1 - kalmanGain[0][0]);covariance[1][1] *= (1 - kalmanGain[1][1]);}最后,我们可以在主函数中循环调用预测和更新函数,以实现连续的经纬度数据处理。

卡尔曼滤波 c语言源代码

卡尔曼滤波  c语言源代码

最佳线性滤波理论起源于40年代美国科学家Wiener和前苏联科学家Kолмогоров等人的研究工作,后人统称为维纳滤波理论。

从理论上说,维纳滤波的最大缺点是必须用到无限过去的数据,不适用于实时处理。

为了克服这一缺点,60年代Kalman把状态空间模型引入滤波理论,并导出了一套递推估计算法,后人称之为卡尔曼滤波理论。

卡尔曼滤波是以最小均方误差为估计的最佳准则,来寻求一套递推估计的算法,其基本思想是:采用信号与噪声的状态空间模型,利用前一时刻地估计值和现时刻的观测值来更新对状态变量的估计,求出现时刻的估计值。

它适合于实时处理和计算机运算。

现设线性时变系统的离散状态防城和观测方程为:X(k) = F(k,k-1)·X(k-1)+T(k,k-1)·U(k-1)Y(k) = H(k)·X(k)+N(k)其中X(k)和Y(k)分别是k时刻的状态矢量和观测矢量F(k,k-1)为状态转移矩阵U(k)为k时刻动态噪声T(k,k-1)为系统控制矩阵H(k)为k时刻观测矩阵N(k)为k时刻观测噪声则卡尔曼滤波的算法流程为:1.预估计X(k)^= F(k,k-1)·X(k-1)2.计算预估计协方差矩阵C(k)^=F(k,k-1)×C(k)×F(k,k-1)'+T(k,k-1)×Q(k)×T(k,k-1)'Q(k) = U(k)×U(k)'3.计算卡尔曼增益矩阵K(k) = C(k)^×H(k)'×[H(k)×C(k)^×H(k)'+R(k)]^(-1)R(k) = N(k)×N(k)'4.更新估计X(k)~=X(k)^+K(k)×[Y(k)-H(k)×X(k)^]5.计算更新后估计协防差矩阵C(k)~ = [I-K(k)×H(k)]×C(k)^×[I-K(k)×H(k)]'+K(k)×R(k)×K(k)'6. X(k+1) = X(k)~C(k+1) = C(k)~重复以上步骤其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 *e,*a,*b;e=malloc(m*m*sizeof(double));l=m;if (l<n) 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 (ii<k){ 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++){ jj=i*l+j; b[jj]=0.0;for (kk=0; kk<=n-1; kk++)b[jj]=b[jj]+a[i*l+kk]*p[kk*n+j]; }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<=n-1; kk++)a[jj]=a[jj]+b[i*l+kk]*f[j*n+kk]; }for (i=0; i<=n-1; i++)for (j=0; j<=n-1; j++){ jj=i*n+j; p[jj]=q[jj];for (kk=0; kk<=n-1; kk++)p[jj]=p[jj]+f[i*n+kk]*a[j*l+kk]; }}}free(e); free(a); free(b);return(js);}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 <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鈥 kP'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)-1xk=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;}。

{JZ}卡尔曼滤波算法C~~C++两种实现代码231

{JZ}卡尔曼滤波算法C~~C++两种实现代码231

卡尔曼滤波算法实现代码C++实现代码如下:============================kalman.h========================== ======// kalman.h: interface for the kalman class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_KALMAN_H__ED 3D740F_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 nois es *//* 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, meas urement );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; }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 *e,*a,*b;e=malloc(m*m*sizeof(double));l=m;if (l<n) 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 (ii<k){ 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++){ jj=i*l+j; b[jj]=0.0;for (kk=0; kk<=n-1; kk++)b[jj]=b[jj]+a[i*l+kk]*p[kk*n+j];}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<=n-1; kk++)a[jj]=a[jj]+b[i*l+kk]*f[j*n+kk];}for (i=0; i<=n-1; i++)for (j=0; j<=n-1; j++){ jj=i*n+j; p[jj]=q[jj];for (kk=0; kk<=n-1; kk++)p[jj]=p[jj]+f[i*n+kk]*a[j*l+kk];} }}free(e); free(a); free(b);return(js);。

卡尔曼滤波算法(C--C++两种实现代码)

卡尔曼滤波算法(C--C++两种实现代码)

卡尔曼滤波算法实现代码C++实现代码如下:============================kalman.h================= ===============// kalman.h: interface for the kalman class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__IN CLUDED_)#define AFX_KALMAN_H__ED3D740F_01D2_4616_8B74_8BF57636F2C0__INCLU DED_#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_8BF57636F2C 0__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鈥鈥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, meas urement, measurement );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;}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 *e,*a,*b;e=malloc(m*m*sizeof(double));l=m;if (l<n) 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 (ii<k){ 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++){ jj=i*l+j; b[jj]=0.0;for (kk=0; kk<=n-1; kk++)b[jj]=b[jj]+a[i*l+kk]*p[kk*n+j];}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<=n-1; kk++)a[jj]=a[jj]+b[i*l+kk]*f[j*n+kk];}for (i=0; i<=n-1; i++)for (j=0; j<=n-1; j++){ jj=i*n+j; p[jj]=q[jj];for (kk=0; kk<=n-1; kk++)p[jj]=p[jj]+f[i*n+kk]*a[j*l+kk];}}}free(e); free(a); free(b);return(js);}。

卡尔曼滤波C程序

卡尔曼滤波C程序

#include <avr/io.h>//#include <avr/delay.h>#include <avr/pgmspace.h>#include <avr/interrupt.h>#include <util/twi.h>#include <math.h>#include <stdlib.h>#include"crc16.c"#include"uart.c"#include"kalman.c"#include"twimaster.c"unsigned char Array[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; unsigned char Rcvbuf[2];static int i;float y1,y2;int y3=0;//输出端口初始化void PORT_initial(void){DDRB=0B00110000;PINB=0X00;PORTB=0X00;DDRD=0B01100000;PIND=0X00;PORTD=0X00;}//Timer1初始化void Timer1Init(void){TCCR1A=0;TCCR1B=_BV(WGM12)|_BV(CS11);OCR1A=0x9C40; //20ms //0x4E20; //周期是:10毫秒TIMSK1|=_BV(OCIE1A);//Timer1 CTC中断,此中断发生周期是:10毫秒ISR(TIMER1_COMPA_vect){//gyro,acc为外部变量stateUpdate(gyro);//外部函数y1=yyh1+(gyro*0.02);y2=(int)kalmanUpdate(acc);//外部函数Array[0]=acc;Array[1]=acc>>8;Array[2]=(int)y1;Array[3]=(int)y1>>8;Array[4]=y2;Array[5]=y>>8;Array[6]=y3;//Array[7]=y3>>8;CRC16(Array, Rcvbuf,8);//外部函数for(i=0;i<8;i++) USART_Transmit(Array);//外部函数 USART_Transmit(Rcvbuf[1]);//外部函数USART_Transmit(Rcvbuf[0]);//外部函数}int main(void){PORT_initial();Timer1Init();I2C_Init();USART_initial();I2C_Start(); //启动twi的中断模式。

卡尔曼滤波算法CC++两种实现代码

卡尔曼滤波算法CC++两种实现代码

卡尔曼滤波算法实现代码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_VER>1000#pragmaonce#endif//_MSC_VER>1000#include<math.h>#include"cv.h"classkalman{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_8BF57636F2C 0__INCLUDED_)============================kalman.cpp===============#include"kalman.h"#include<stdio.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敏k+B敏kP'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);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语言实现代码如下#include"stdlib.h"#include"rinv.c"intlman(n,m,k,f,q,r,h,y,x,p,g)intn,m,k;doublef[],q口,r[],h[],y口刈,p[],g口;日日{inti,j,kk,ii,l,jj,js;double*e,*a,*b;e=malloc(m*m*sizeof(double));l=m;if(l<n)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皿=0.0;for(kk=0;kk<=n-1;kk++)I a[jj]=a[jj]+p[i*n+kk]*h[j*n+kk];)for(i=0;i<=m-1;i++)I for(j=0;j<=m-1;j++)yl{jj=i*m+j;e[jj]=r[jj];I for(kk=0;kk<=n-1;kk++)I e[jj]=e[jj]+h[i*n+kk]*a[kk*l+j];)js=rinv(e,m);{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++)I x[jj]=x[jj]+g[i*m+j]*b[j*l];)I if(ii<k)5¥{for(i=0;i<=n-1;i++)I for(j=0;j<=n-1;j++)有己{jj=i*l+j;a皿=0.0;I for(kk=0;kk<=m-1;kk++)I a[jj]=a[jj]-g[i*m+kk]*h[kk*n+j];I if(i==j)a[jj]=1.0+a[jj];□)I for(i=0;i<=n-1;i++)I for(j=0;j<=n-1;j++){jj=i*l+j;b[jj]=0.0;I for(kk=0;kk<=n-1;kk++)I b[jj]=b[jj]+a[i*l+kk]*p[kk*n+j];)I for(i=0;i<=n-1;i++)I for(j=0;j<=n-1;j++)▼I{jj=i*l+j;a[jj]=0.0;I for(kk=0;kk<=n-1;kk++)I a[jj]=a[jj]+b[i*l+kk]*f[j*n+kk];)I for(i=0;i<=n-1;i++)for(j=0;j<=n-1;j++){jj=i*n+j;p[jj]=q[jj];I for(kk=0;kk<=n-1;kk++)I P[jj]=P[jj]+f[i*n+kk]*a[j*l+kk];}}free(e);free(a);free(b);return(js);。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
x_now=x_mid+kg*(z_measure-x_mid);//估计出的最优值
p_now=(1-kg)*p_mid;//最优值对应的covariance
printf("Real position: %6.3f \n",z_real); //显示真值
x_last=z_real+frand()*0.03;
x_mid=x_last;
for(i=0;i<20;i++)
{ x_mid=x_last; //x_last=x(k-1|k-1),x_mid=x(k|k-1)
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#define ADDataCnt 10
double ADData[ADDataCnt];
void InitADData(double RealValue, unsigned int Cnt)
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
double frand()
{ return 2*((rand()/(double)RAND_MAX) - 0.5); //随机噪声}
void main()
{ float x_last=0;
printf("\n Kalman Output : %lf\n",KalmanFilterReturn);
while(1)
;
}
p_mid=p_last+Q; //p_mid=p(k|k-1),p_last=p(k-1|k-1),Q=噪声
kg=p_mid/(p_mid+R); //kg为kalman filter,R为噪声
z_measure=z_real+frand()*0.03;//测量值
double p_last = InitialPrediction;
double p_mid ;
double p_now;
double kg;
for(i=0;i<DataCnt;i++)
{
x_mid=x_last; //x_last=x(k-1|k-1),x_mid=x(k|k-1)
printf("卡尔曼误差所占比例: %d%% \n",100-(int)((sumerror_kalman/sumerror_measure)*100));
}
我现在是希望读取MMA7361加速度计的输出,串口过来的数据跳动很大。
我把帖子的代码稍微改动了一下,好像还不错。
下面的代码没做仔细的验证,风格也不好,抛砖引玉吧。
double R = MeasureNoise_R;
double Q = ProcessNiose_Q;
double x_last = *Data;
double x_mid = x_last;
double x_now;
{
unsigned int i;
for( i =0; i < Cnt; i++)
{
ADData = RealValue + 0.2*rand()/(double)RAND_MAX - 0.1;
}
x_now=x_mid+kg*(*(Data+i)-x_mid);//估计出的最优值
p_now=(1-kg)*p_mid;//最优值对应的covariance
p_last = p_now; //更新covariance值
sumerror_kalman += fabs(z_real - x_now); //kalman估计值的累积误差
sumerror_measure += fabs(z_real-z_measure); //真值与测量值的累积误差
p_last = p_now; //更新covariance值
float p_now;
float z_real=0.56;//0.56
float z_measure;
float sumerror_kalman=0;
float sumerror_measure=0;
int i;
x_last = x_now; //更新系统状态值
}
printf("总体测量误差 : %f\n",sumerror_measure); //输出测量累积误差
printf("总体卡尔曼滤波误差: %f\n",sumerror_kalman); //输出kalman累积误差
}
double KalmanFi来自ter(unsigned int DataCnt,double *Data,double ProcessNiose_Q,double MeasureNoise_R,double InitialPrediction)
{
unsigned int i;
printf("Mesaured position: %6.3f [diff:%.3f]\n",z_measure,fabs(z_real-z_measure)); //显示测量值以及真值与测量值之间的误差
printf("Kalman position: %6.3f [diff:%.3f]\n",x_now,fabs(z_real - x_now)); //显示kalman估计值以及真值和卡尔曼估计值的误差
x_last = x_now; //更新系统状态值
}
return x_now;
}
void main()
{
unsigned int i;
double KalmanFilterReturn;
float p_last=0.02;
float Q=0.018;
float R=0.542;
float kg;
float x_mid;
float x_now;
float p_mid;
p_mid=p_last+Q; //p_mid=p(k|k-1),p_last=p(k-1|k-1),Q=噪声
kg=p_mid/(p_mid+R); //kg为kalman filter,R为噪声
// z_measure=z_real+frand()*0.03;//测量值
InitADData(2.1,ADDataCnt);
for(i = 0;i < ADDataCnt;i++)
printf("ADData[%d] Is : %lf\n",i,ADData);
KalmanFilterReturn = KalmanFilter(ADDataCnt,ADData,0,0.005,10);
相关文档
最新文档