Projet

Général

Profil

« Précédent | Suivant » 

Révision 454

Ajouté par Damien REGENT il y a presque 3 ans

TP3 terminé, il fonctionne

Voir les différences:

branch/Regent/sp4a3/sp4a3_kalman.c
#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]){
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;
for(i=0;i<na;i++)
{
for (j=0;j<ma;j++)
{
R[i][j]=A[i][j]+B[i][j];
}
}
}
void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
void Inverse_Mat_22(int n,int m,double A[n][m],double R[n][m])
{
double det=1/((A[0][0]*A[1][1])-(A[0][1]*A[1][0]));
R[0][0]=A[1][1]*det;
R[1][1]=A[0][0]*det;
R[0][1]=-A[0][1]*det;
R[1][0]=-A[1][0]*det;
//printf("\n %f %f\n %f %f\n",R[0][0] ,R[0][1] ,R[1][0] ,R[1][1]);
}
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
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];
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 R[na][ma]){
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;
for(i=0;i<na;i++)
{
for (j=0;j<ma;j++)
{
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 Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double R[na][mb])
{
int i,j,k;
for(i=0;i<na;i++)
{
for (j=0;j<mb;j++)
{
R[i][j]=0;
for (k=0;k<ma;k++)
{
R[i][j]+=A[i][k]*B[k][j];
}
}
}
}
void tests_unitaires(void){
void tests_unitaires(void)
{
//Matrices d'entrée
double T21a[2][1]={{7},{-5}};
double T21b[2][1]={{-3},{46}};
......
double xobs,yobs;
double oldx,oldy;
double dx=0,dy=0,dt=0.1;
int cpt = 0;
int cpt = 0,i,j;
// kalman param
double sigma_etat = 10.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);
//création de matrices intermédiaires
double X1[4][1];
double P1[4][4] = {{sigma_etat*sigma_etat, 0, 0, 0},
{0, sigma_etat*sigma_etat, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
double X2[4][1];
double P2[4][4] = {{sigma_etat*sigma_etat, 0, 0, 0},
{0, sigma_etat*sigma_etat, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
double Mint[4][4];
double Mint2[4][4];
double Mint3[4][4];
double Mint4[4][4];
double Mint5[4][4];
double Mint6[4][4];
double Mint7[4][4];
double Mint8[4][4];
double Mint9[4][4];
double Delta[2][1];
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
printf("-------------%04d--------------\n",cpt);
......
{
t -= t0;xobs -= x0;yobs -= y0;
debug=0; ///Mettre à 1 pour afficher les matrices.
debug=1; ///Mettre à 1 pour afficher les matrices.
///Ajouter votre code ci-dessous///
// Kalman
// Kalman
double XY[2][1]={{xobs},{yobs}};
// X = F*X
Plot_Mat(X," X(k+1|k) = ");
// X = F*X
Mul_Mat_Mat(4,4,F,4,1,X,X1);
Plot_Mat(X1," X(k+1|k) = ");
//P = F*P*F'+Q;
Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
//P = F*P*F'+Q;
Mul_Mat_Mat(4,4,F,4,4,P,Mint);
Mul_Mat_Mat(4,4,Mint,4,4,FT,Mint2);
Add_Mat_Mat(4,4,Mint2,4,4,Q,P1);
Plot_Mat(P1,"P(k+1|k) = F.P(k|k).FT + Q = ");
// K = P*H' / ( H*P*H' + R);
// K = P*H' /( H*P*H' + R);
Mul_Mat_Mat(4,4,P1,4,2,HT,Mint);
Mul_Mat_Mat(2,4,H,4,4,P1,Mint2);
Mul_Mat_Mat(2,4,Mint2,4,2,HT,Mint3);
Add_Mat_Mat(2,2,Mint3,2,2,R,Mint6);
Inverse_Mat_22(2,2,Mint6,Mint5);
Mul_Mat_Mat(4,2,Mint,2,2,Mint5,K);
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 = ");
//X = X + K*([xobs(i);yobs(i)]-H*X);
Mul_Mat_Mat(2,4,H,4,1,X1,Mint2);
Sub_Mat_Mat(2,1,XY,2,1,Mint2,Delta);
Mul_Mat_Mat(4,2,K,2,1,Delta,Mint7);
Add_Mat_Mat(4,1,X1,4,1,Mint7,X2);
Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
Plot_Mat(X2," 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) = ");
// P = P - K*H*P;
Mul_Mat_Mat(4,2,K,2,4,H,Mint8);
Mul_Mat_Mat(4,4,Mint8,4,4,P1,Mint9);
Sub_Mat_Mat(4,4,P1,4,4,Mint9,P2);
Plot_Mat(P2," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
//X(k+1|k) devient X(k+1|k+1) et X(k|k) devient X(k+1|k) ( X=X1; X1=X2 );
for (i=0;i<4;i++)
{
for (j=0;j<1;j++)
{
X[i][j]=X2[i][j];
}
}
for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
P[i][j]=P2[i][j];
}
}
/// La matrice X doit contenir la position filtrée ///
}
t = cpt * dt;

Formats disponibles : Unified diff