Révision 453
Ajouté par Clement ROCHE il y a presque 3 ans
branch/ROCHE/sp4a3/sp4a3_kalman.c | ||
---|---|---|
int i,j;
|
||
|
||
for(i=0;i<na;i++) {
|
||
for(j=0;j<ma;j++) {
|
||
R[i][j] = A[i][j]+B[i][j];
|
||
for(j=0;j<ma;j++) { //Les boucles for balayent la matrice
|
||
R[i][j] = A[i][j]+B[i][j]; //Addition de chaque element des 2 matrices
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
void Inverse_Mat_22(int n,int m,double A[n][m],double R[n][m]){
|
||
float det;
|
||
|
||
det = (A[0][0]*A[1][1]) - (A[0][1]*A[1][0]);
|
||
det = (A[0][0]*A[1][1]) - (A[0][1]*A[1][0]); //calcul du det
|
||
|
||
//Stockage de R avec la formule mathematique d inversion de matrice 2x2
|
||
R[0][0]= A[1][1] / det ;
|
||
R[0][1]= - (A[0][1] / det) ;
|
||
R[1][0]= - (A[1][0] / det) ;
|
||
... | ... | |
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;
|
||
|
||
//Meme principe que Add mais avec un -
|
||
for(i=0;i<na;i++) {
|
||
for(j=0;j<ma;j++) {
|
||
R[i][j] = A[i][j]-B[i][j];
|
||
... | ... | |
int i,j,k;
|
||
|
||
for(i=0;i<na;i++) {
|
||
for(j=0;j<mb;j++) {
|
||
for(j=0;j<mb;j++) { //On balaye les lignes de la matrice 1 et les colonnes de la matrice 2
|
||
|
||
R[i][j] = 0;
|
||
R[i][j] = 0; //Mise a zero de l element de la matrice R correspondant aux resultats
|
||
|
||
for(k=0;k<ma;k++) {
|
||
R[i][j] += A[i][k] * B[k][j];
|
||
for(k=0;k<ma;k++) { //Balaye les colonnes de laa matrice 1
|
||
R[i][j] += A[i][k] * B[k][j]; //Somme de tout les produits entre A et B
|
||
}
|
||
}
|
||
}
|
||
... | ... | |
// kalman param
|
||
double sigma_etat = 10.0;
|
||
double sigma_observation = 2.0;
|
||
double X[4][1] = {{0},{0},{0},{0}};
|
||
double X[4][1] = {{0},{0},{0},{0}};
|
||
double X1[4][1];
|
||
double X2[4][1];
|
||
|
||
double P[4][4] = {{sigma_etat*sigma_etat, 0, 0, 0},
|
||
{0, sigma_etat*sigma_etat, 0, 0},
|
||
... | ... | |
double R[2][2] = {{sigma_observation*sigma_observation, 0},
|
||
{0 , sigma_observation*sigma_observation}};
|
||
|
||
double K[4][2];
|
||
double K[4][2];
|
||
double K1[4][2];
|
||
double H[2][4] = {{1, 0, 0, 0},
|
||
{0, 1, 0, 0}};
|
||
double HT[4][2];
|
||
... | ... | |
{0, 0, 1, 0},
|
||
{0, 0, 0, 1}};
|
||
double FT[4][4];
|
||
Transpose_Mat(4,4,F,FT);
|
||
Transpose_Mat(4,4,F,FT);
|
||
|
||
double P1[4][4];
|
||
double P2[4][4];
|
||
double R6[4][4];
|
||
|
||
double R1[2][2];
|
||
double R2[2][4];
|
||
double R3[2][2];
|
||
double R4[4][1];
|
||
double R5[2][1];
|
||
|
||
double DELTA[2][1];
|
||
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
|
||
printf("-------------%04d--------------\n",cpt);
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0) {
|
||
printf("-------------%04d--------------\n",cpt);
|
||
|
||
double Obs[2][1] = {{xobs},{yobs}};
|
||
int i,j;
|
||
if (cpt ==0)
|
||
{
|
||
t0=t;x0=xobs;y0=yobs;
|
||
... | ... | |
{
|
||
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
|
||
double Obs[2][1] = {{xobs},{yobs}};
|
||
// 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;
|
||
|
||
Mul_Mat_Mat(4,4,F,4,4,P,P1);
|
||
Mul_Mat_Mat(4,4,P1,4,4,FT,P);
|
||
Add_Mat_Mat(4,4,P,4,4,Q,P1);
|
||
|
||
// X = F*X
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
Plot_Mat(P1,"P(k+1|k) = F.P(k|k).FT + Q = ");
|
||
|
||
//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);
|
||
|
||
Mul_Mat_Mat(4,4,P1,4,2,HT,K1);
|
||
|
||
Mul_Mat_Mat(2,4,H,4,4,P1,R2);
|
||
Mul_Mat_Mat(2,4,R2,4,2,HT,R1);
|
||
Add_Mat_Mat(2,2,R1,2,2,R,R1);
|
||
|
||
Inverse_Mat_22(2,2,R1,R3);
|
||
|
||
|
||
Mul_Mat_Mat(4,2,K1,2,2,R3,K);
|
||
|
||
|
||
|
||
// K = P*H' / ( H*P*H' + R);
|
||
Plot_Mat(K,"K = ");
|
||
|
||
|
||
//Delta
|
||
|
||
Mul_Mat_Mat(2,4,H,4,1,X1,R5);
|
||
Sub_Mat_Mat(2,1,Obs,2,1,R5,DELTA);
|
||
|
||
Plot_Mat(DELTA,"DELTA = Obs - H.X(k+1|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*delta;
|
||
|
||
Mul_Mat_Mat(4,2,K,2,1,DELTA,R4);
|
||
Add_Mat_Mat(4,1,X1,2,1,R4,X2);
|
||
|
||
|
||
// P = P - K*H*P;
|
||
Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
|
||
|
||
|
||
|
||
Plot_Mat(X2," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
|
||
|
||
|
||
// P = P - K*H*P;
|
||
|
||
Mul_Mat_Mat(4,2,K,2,4,H,P);
|
||
Mul_Mat_Mat(4,4,P,4,4,P1,R6);
|
||
Sub_Mat_Mat(4,4,P1,4,4,R6,P2);
|
||
|
||
Plot_Mat(P2," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
|
||
|
||
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
programme fini le filtre fonctionne