Révision 473
Ajouté par Abdou FAYE il y a presque 3 ans
sp4a3_kalman.c | ||
---|---|---|
#include <math.h>
|
||
|
||
#include "sp4a3_kalman_extra.h"
|
||
|
||
|
||
void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double R[na][ma]){
|
||
|
||
int i, j; // variables permettant à parcourir l'ensembles des deux matrices en lignes et colonnes
|
||
void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
|
||
int i,j;
|
||
for (i=0;i<na;i++)
|
||
{
|
||
for (j=0;j<ma;j++)
|
||
{
|
||
out [i][j] = A[i][j] + b[i][j];
|
||
}
|
||
}
|
||
}
|
||
void Init_Mat(int na,int ma,double A[na][ma], double out[na][ma]){
|
||
int i=0, j=0;
|
||
for (i=0; i<na; i++)
|
||
for (j=0; j<ma; j++) //Boucle imbriquées nous permettant d'initialiser nos matrices( tableau) en lignes et en colonnes.
|
||
R[i][j] = A[i][j]+B[i][j]; // Addition de matrices se fait termes à termes
|
||
|
||
}
|
||
|
||
for (j=0; j<ma; j++){
|
||
out[i][j]=A[i][j];
|
||
}
|
||
}
|
||
|
||
|
||
void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
|
||
int i,j;
|
||
double det=0;
|
||
double B1[n][m];
|
||
det = A[0][0]*A[1][1] - A[0][1]*A[1][0];
|
||
B1[0][0] = A[1][1];
|
||
B1[0][1] = A[0][1]*(-1);
|
||
B1[1][0] = A[1][0]*(-1);
|
||
B1[1][1] = A[0][0];
|
||
if (det !=0){
|
||
for (i=0;i<n;i++)
|
||
{
|
||
for (j=0;j<m;j++)
|
||
{
|
||
B[i][j]= (1/det)*B1[i][j];
|
||
}
|
||
}
|
||
}
|
||
|
||
// Le determiannt est une valeur importante pour le calcul d'inverse de matrices 2*2
|
||
double det_A; // déclaration de la variable nous permettant le calcul du determinant de la matrice A.
|
||
|
||
det_A= A[0][0]*A[1][1]-A[1][0]*A[0][1]; // calcul du determinant de A, det = 1/ ad-cb pour une matrice carre 2x2 A= [a b , c d ]
|
||
}
|
||
|
||
// ici nous calculons l'inverse de A qui est égale à: A^-1= 1/(det A) * [d -c , -b a]
|
||
B[0][0]= A[1][1]*(1/(det_A));
|
||
B[0][1]= -A[0][1]*(1/(det_A));
|
||
B[1][0]= -A[1][0]*(1/(det_A));
|
||
B[1][1]= A[0][0]*(1/(det_A));
|
||
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
|
||
int i,j;
|
||
for (i=0;i<n;i++)
|
||
for (j=0;j<m;j++)
|
||
R[j][i]=A[i][j];
|
||
}
|
||
|
||
|
||
|
||
void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
|
||
int i,j;
|
||
for (i=0;i<na;i++)
|
||
{
|
||
for (j=0;j<ma;j++)
|
||
{
|
||
out [i][j] = A[i][j] - b[i][j];
|
||
}
|
||
}
|
||
}
|
||
|
||
void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double out[na][mb]){
|
||
int i,j,k,i1,j1;
|
||
double somme[4][4];
|
||
for (i1=0;i1<na;i1++)
|
||
{
|
||
for (j1=0;j1<mb;j1++)
|
||
{
|
||
somme[i1][j1]=0;
|
||
}
|
||
}
|
||
for (i=0;i<na;i++)
|
||
{
|
||
for (j=0;j<mb;j++)
|
||
{
|
||
for (k=0;k<nb;k++)
|
||
{
|
||
somme[i][j] += A[i][k]*B[k][j];
|
||
out[i][j] = somme [i][j];
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
|
||
int i,j; // Deux oéprandes qui serviront à parcourir l'ensembles des deux matrices en lignes et colonne
|
||
for (i=0;i<n;i++)
|
||
for (j=0;j<m;j++) //Boucle imbriquées nous permettant d'initialisée nos matrices( tableau) en lignes et en colonnes.
|
||
R[j][i]=A[i][j]; // Inverse les lignes en colennes et inverssement.
|
||
}
|
||
|
||
void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double R[na][ma]){
|
||
int i, j; // Deux oéprandes qui serviront à parcourir l'ensembles des deux matrices en lignes et colonne
|
||
for (i=0; i<na; i++)
|
||
for (j=0; j<ma; j++) //Boucle imbriquées nous permettant d'initialisée nos matrices( tableau) en lignes et en colonnes.
|
||
R[i][j] = A[i][j]-B[i][j];
|
||
|
||
}
|
||
|
||
void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double R[na][mb]){
|
||
|
||
}
|
||
|
||
|
||
|
||
void tests_unitaires(void){
|
||
//Matrices d'entrée
|
||
double T21a[2][1]={{7},{-5}};
|
||
... | ... | |
printf("Kalman\n");
|
||
double t = 0;
|
||
double t0,x0,y0;
|
||
double xobs,yobs;
|
||
double x,y;
|
||
double oldx,oldy;
|
||
double dx=0,dy=0,dt=0.1;
|
||
int cpt = 0;
|
||
... | ... | |
{0, 0, 1, 0},
|
||
{0, 0, 0, 1}};
|
||
double FT[4][4];
|
||
Transpose_Mat(4,4,F,FT);
|
||
Transpose_Mat(4,4,F,FT);
|
||
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &x, &y)>0){
|
||
printf("-------------%04d--------------\n",cpt);
|
||
|
||
if (cpt ==0)
|
||
{
|
||
t0=t;x0=xobs;y0=yobs;
|
||
xobs=xobs-x0;yobs=yobs-y0;
|
||
Plot_Mat(F,"F = ");
|
||
Plot_Mat(H,"H = ");
|
||
Plot_Mat(R,"R = ");
|
||
}
|
||
else
|
||
{
|
||
t -= t0;xobs -= x0;yobs -= y0;
|
||
|
||
debug=0; ///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
|
||
// X = F*X
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
|
||
//P = F*P*F'+Q;
|
||
Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
|
||
|
||
// K = P*H' / ( H*P*H' + R);
|
||
Plot_Mat(K,"K = ");
|
||
|
||
//X = X + K*([xobs(i);yobs(i)]-H*X);
|
||
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
|
||
|
||
// P = P - K*H*P;
|
||
Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
|
||
|
||
/// La matrice X doit contenir la position filtrée ///
|
||
}
|
||
t = cpt * dt;
|
||
dx = (xobs - oldx)/dt;
|
||
dy = (yobs - oldy)/dt;
|
||
fprintf(Fout,"%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",t,xobs,yobs,sqrt(dx*dx+dy*dy)*dt,X[0][0],X[1][0],X[2][0],X[3][0],sqrt(X[2][0]*X[2][0]+X[3][0]*X[3][0])*dt);
|
||
oldx = xobs;
|
||
oldy = yobs;
|
||
cpt ++;
|
||
}
|
||
fclose(Fout);
|
||
fclose(fichier);
|
||
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 5:6 w l, '' u 2:3 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 1:9 w l, '' u 1:4 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 9 w l , 'vitesse_reelle.dat' u 2 w l\";");
|
||
return 0;
|
||
}
|
||
if (cpt ==0)
|
||
{
|
||
t0=t;x0=x;y0=y;
|
||
x=x-x0;y=y-y0;
|
||
Plot_Mat(F,"F = ");
|
||
Plot_Mat(H,"H = ");
|
||
Plot_Mat(R,"R = ");
|
||
}
|
||
else
|
||
{
|
||
t -= t0;x -= x0;y -= y0;
|
||
|
||
debug=1; ///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
|
||
// X = F*X
|
||
double matx[4][1];
|
||
Init_Mat(4, 1, X, matx);
|
||
Mul_Mat_Mat(4, 4, F, 4, 1, matx, X);
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
|
||
//P = F*P*F'+Q;
|
||
double out[4][4], out1[4][4];
|
||
Mul_Mat_Mat(4, 4, F, 4, 4, P, out);
|
||
Mul_Mat_Mat(4, 4, out, 4, 4, FT, out1);
|
||
Add_Mat_Mat(4, 4, out1, 4, 4, Q, P);
|
||
Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
|
||
|
||
// K = P*H' / ( H*P*H' + R);
|
||
|
||
double mat[4][2];
|
||
Mul_Mat_Mat(4, 4, P, 4, 2, HT, mat);
|
||
Plot_Mat(mat,"P(k+1|k).HT = ");
|
||
|
||
double mat1[2][2], mat2[2][2];
|
||
Mul_Mat_Mat(2, 4, H, 4, 2, mat, mat1);
|
||
Add_Mat_Mat(2, 2, mat1, 2, 2, R, mat2);
|
||
Plot_Mat(mat2, "H.P(k+1|k).HT + R = ");
|
||
double mat3[2][2];
|
||
Inverse_Mat_22(2,2,mat2,mat3);
|
||
Mul_Mat_Mat(4,2,mat,2,2,mat3,K);
|
||
Plot_Mat(K,"K= ");
|
||
|
||
//X = X + K*([xb(i);yb(i)]-H*X);
|
||
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
//X = X + K*([xb(i);yb(i)]-H*X);
|
||
double Obs[2][1]={{x}, {y}}, Delta[2][1], mat4[2][1];
|
||
Mul_Mat_Mat(2, 4, H, 4, 1, X, mat4);
|
||
Sub_Mat_Mat(2, 1, Obs, 2, 1, mat4, Delta);
|
||
Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
|
||
double mat5[4][1];
|
||
Mul_Mat_Mat(4, 2, K, 2, 1, Delta, mat5);
|
||
Add_Mat_Mat(4, 1, X, 4, 1, mat5, X);
|
||
Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
|
||
|
||
// P = P - K*H*P;
|
||
double mat6 [4][4];
|
||
Mul_Mat_Mat(4,2,K,2,4,H,mat6);
|
||
double mat7[4][4];
|
||
Mul_Mat_Mat(4,4,mat6,4,4,P,mat7);
|
||
Sub_Mat_Mat(4,4,P,4,4,mat7,P);
|
||
Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
|
||
|
||
/// La matrice X doit contenir la position filtrée ///
|
||
}
|
||
t = cpt * dt;
|
||
dx = (x - oldx)/dt;
|
||
dy = (y - oldy)/dt;
|
||
fprintf(Fout,"%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",t,x,y,sqrt(dx*dx+dy*dy)*dt,X[0][0],X[1][0],X[2][0],X[3][0],sqrt(X[2][0]*X[2][0]+X[3][0]*X[3][0])*dt);
|
||
oldx = x;
|
||
oldy = y;
|
||
cpt ++;
|
||
}
|
||
fclose(Fout);
|
||
fclose(fichier);
|
||
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 5:6 w l, '' u 2:3 w l\";");
|
||
system ("gnuplot -p -e \"plot 'output.dat' u 1:9 w l, '' u 1:4 w l\";");
|
||
system ("gnuplot -p -e \"plot 'vitesse_reelle.dat' u 2 w l, 'output.dat' u 9 w l\";");
|
||
return 0;
|
||
}
|
Formats disponibles : Unified diff
fonctionnement du filtre kalman