Révision 520
Ajouté par lefraisse il y a presque 4 ans
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 out[na][ma]){
|
||
|
||
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 k,j;
|
||
if ((na!=nb)||(ma!=mb))
|
||
printf("Addition impossible : Les matrices doivent avoir la même taille \n");
|
||
else{
|
||
for (j=0;j<na;j++){
|
||
for (k=0;k<ma;k++){
|
||
out[j][k]=A[j][k]+b[j][k];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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 B[n][m]){
|
||
float fac=1/((A[0][0]*A[1][1])-(A[0][1]*A[1][0]));
|
||
if ((n!=2)||(m!=2))
|
||
printf("Le calcul se fait pour des matrice 2x2 \n");
|
||
B[0][0]=fac*A[1][1];
|
||
B[0][1]=-fac*A[0][1];
|
||
B[1][0]=-fac*A[1][0];
|
||
B[1][1]=fac*A[0][0];
|
||
}
|
||
|
||
void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
|
||
... | ... | |
}
|
||
|
||
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 k,j;
|
||
if ((na!=nb)||(ma!=mb))
|
||
printf("Soustraction impossible : Les matrices doivent être de même taille \n");
|
||
else{
|
||
for (j=0;j<na;j++){
|
||
for (k=0;k<ma;k++){
|
||
out[j][k]=A[j][k]-b[j][k];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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;
|
||
double C[na][mb];
|
||
//for(i=0;i<na;i++){
|
||
// for(j=0;j<mb;j++){
|
||
// C[i][j]=0;
|
||
// }
|
||
//}
|
||
|
||
if (ma==nb){
|
||
for (i=0;i<na;i++){
|
||
for (j=0;j<mb;j++){
|
||
C[i][j]=0;
|
||
for (k=0;k<nb;k++){
|
||
C[i][j]+= A[i][k]*B[k][j];
|
||
}
|
||
out[i][j]=C[i][j];
|
||
}
|
||
}
|
||
//for (i=0;i<naa;i++){
|
||
// for (j=0;j<mb;j++){
|
||
// C[i][j]=out[i][j]
|
||
//}
|
||
//}
|
||
}
|
||
}
|
||
|
||
|
||
... | ... | |
{
|
||
t -= t0;x -= x0;y -= y0;
|
||
|
||
debug=0; ///Mettre à 1 pour afficher les matrices.
|
||
debug=1; ///Mettre à 1 pour afficher les matrices.
|
||
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
Plot_Mat(X," X(k|k) = ");
|
||
|
||
// X = F*X
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
// X = F*X
|
||
Mul_Mat_Mat(4,4,F,4,1,X,X);
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
Plot_Mat(P,"P(k|k) = ");
|
||
|
||
//P = F*P*F'+Q;
|
||
//P = F*P*F'+Q;
|
||
double FP[4][4];
|
||
double FPFT[4][4];
|
||
Mul_Mat_Mat(4,4,F,4,4,P,FP);
|
||
Mul_Mat_Mat(4,4,FP,4,4,FT,FPFT);
|
||
Add_Mat_Mat(4,4,FPFT,4,4,Q,P);
|
||
Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
|
||
|
||
// K = P*H' / ( H*P*H' + R);
|
||
// K = P*H' / ( H*P*H' + R);
|
||
double PHT[4][2];
|
||
double HP[2][4];
|
||
double HPHT[2][2];
|
||
double S[2][2];
|
||
double IS[2][2];
|
||
Mul_Mat_Mat(4,4,P,4,2,HT,PHT);
|
||
Mul_Mat_Mat(2,4,H,4,4,P,HP);
|
||
Mul_Mat_Mat(2,4,HP,4,2,HT,HPHT);
|
||
Add_Mat_Mat(2,2,HPHT,2,2,R,S);
|
||
Inverse_Mat_22(2,2,S,IS);
|
||
Mul_Mat_Mat(4,2,PHT,2,2,IS,K);
|
||
Plot_Mat(PHT,"P(k+1|k).HT = ");
|
||
Plot_Mat(S,"H.P(k+1|k).HT + R =");
|
||
Plot_Mat(IS,"INV(H.P(k+1|k).HT + R) =");
|
||
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 HX[2][1];
|
||
double obs[2][1];
|
||
double DELTA[2][1];
|
||
double KD[4][1];
|
||
obs[0][0]=x;
|
||
obs[1][0]=y;
|
||
Mul_Mat_Mat(2,4,H,4,1,X,HX);
|
||
Sub_Mat_Mat(2,1,obs,2,1,HX,DELTA);
|
||
Mul_Mat_Mat(4,2,K,2,1,DELTA,KD);
|
||
Add_Mat_Mat(4,1,X,4,1,KD,X);
|
||
Plot_Mat(obs,"Obs = ");
|
||
//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
|
||
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;
|
||
// P = P - K*H*P;
|
||
double KH[4][4];
|
||
double KHP[4][4];
|
||
Mul_Mat_Mat(4,2,K,2,4,H,KH);
|
||
Mul_Mat_Mat(4,4,KH,4,4,P,KHP);
|
||
Sub_Mat_Mat(4,4,P,4,4,KHP,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 ///
|
Formats disponibles : Unified diff
Correction fonction Mul_Mat_Mat