转载自http://www.matlabsky.com/thread-30399-1-1.html
opencv2+中也有卡尔曼的例子,不过是对一维点的预测,通过这篇文章对卡尔曼又重新认识了,强大啊|||
卡尔曼滤波器简介
五、MATLAB实现代码
%%%% Constant Velocity Model Kalman Filter Simulation %%%%
%==========================================================================
clear all; close all; clc;
%% Initial condition
ts = 1; % Sampling time
t = [0:ts:100];
T = length(t);
%% Initial state
x = [0 40 0 20]';
x_hat = [0 0 0 0]';
%% Process noise covariance
q = 5
Q = q*eye(2);
%% Measurement noise covariance
r = 5
R = r*eye(2);
%% Process and measurement noise
w = sqrt(Q)*randn(2,T); % Process noise
v = sqrt(R)*randn(2,T); % Measurement noise
%% Estimate error covariance initialization
p = 5;
P(:,:,1) = p*eye(4);
%==========================================================================
%% Continuous-time state space model
%{
x_dot(t) = Ax(t)+Bu(t)
z(t) = Cx(t)+Dn(t)
%}
A = [0 1 0 0;
0 0 0 0;
0 0 0 1;
0 0 0 0];
B = [0 0;
1 0;
0 0;
0 1];
C = [1 0 0 0;
0 0 1 0];
D = [1 0;
0 1];
%% Discrete-time state space model
%{
x(k+1) = Fx(k)+Gw(k)
z(k) = Hx(k)+Iv(k)
Continuous to discrete form by zoh
%}
sysc = ss(A,B,C,D);
sysd = c2d(sysc, ts, 'zoh');
[F G H I] = ssdata(sysd);
%% Practice state of target
for i = 1:T-1
x(:,i+1) = F*x(:,i);
end
x = x+G*w; % State variable with noise
z = H*x+I*v; % Measurement value with noise
%==========================================================================
%%% Kalman Filter
for i = 1:T-1
%% Prediction phase
x_hat(:,i+1) = F*x_hat(:,i);
% State estimate predict
P(:,:,i+1) = F*P(:,:,i)*F'+G*Q*G';
% Tracking error covariance predict
P_predicted(:,:,i+1) = P(:,:,i+1);
%% Kalman gain
K = P(:,:,i+1)*H'*inv(H*P(:,:,i+1)*H'+R);
%% Updata step
x_hat(:,i+1) = x_hat(:,i+1)+K*(z(:,i+1)-H*x_hat(:,i+1));
% State estimate update
P(:,:,i+1) = P(:,:,i+1)-K*H*P(:,:,i+1);
% Tracking error covariance update
P_updated(:,:,i+1) = P(:,:,i+1);
end
%==========================================================================
%% Estimate error
x_error = x-x_hat;
%% Graph 1 practical and tracking position
figure(1)
plot(x(1,:),x(3,:),'r');
hold on;
plot(x_hat(1,:),x_hat(3,:),'g.');
title('2D Target Position')
legend('Practical Position','Tracking Position')
xlabel('X axis [m]')
ylabel('Y axis [m]')
hold off;
%% Graph 2
figure(2)
plot(t,x(1,:)),grid on;
hold on;
plot(t,x_hat(1,:),'r'),grid on;
title('Practical and Tracking Position on X axis')
legend('Practical Position','Tracking Position')
xlabel('Time [sec]')
ylabel('Position [m]')
hold off;
%% Graph 3
figure(3)
plot(t,x_error(1,:)),grid on;
title('Position Error on X axis')
xlabel('Time [sec]')
ylabel('Position RMSE [m]')
hold off;
%% Graph 4
figure(4)
plot(t,x(2,:)),grid on;
hold on;
plot(t,x_hat(2,:),'r'),grid on;
title('Practical and Tracking Velocity on X axis')
legend('Practical Velocity','Tracking Velocity')
xlabel('Time [sec]')
ylabel('Velocity [m/sec]')
hold off;
%% Graph 5
figure(5)
plot(t,x_error(2,:)),grid on;
title('Velocity Error on X axis')
xlabel('Time [sec]')
ylabel('Velocity RMSE [m/sec]')
hold off;
%==========================================================================
六、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
七、C++语言实现代码
//
//////////////////////////////////////////////////////////////////////
#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"
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_)
#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;
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, 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;
}