Révision 275
Ajouté par gasacco il y a presque 4 ans
branch/sacco/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 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]){
|
||
if (na ==nb && ma ==mb)
|
||
{
|
||
int i,j;
|
||
for (i=0;i<=na-1;i++)
|
||
{
|
||
for (j=0;j<=ma-1;j++)
|
||
{
|
||
out[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 B[n][m]){
|
||
if (n==2 && m ==2)
|
||
{
|
||
double det = A[0][0]*A[1][1]- A[0][1]*A[1][0];
|
||
if (det !=0)
|
||
{
|
||
B[0][0] = (double) A[1][1]/det;
|
||
B[1][1] = (double) A[0][0]/det;
|
||
B[0][1] = (double) -A[0][1]/det;
|
||
B[1][0] = (double) -A[1][0]/det;
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
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 out[na][ma]){
|
||
|
||
void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double out[na][ma]){
|
||
if (na ==nb && ma ==mb)
|
||
{
|
||
int i,j;
|
||
for (i=0;i<=na-1;i++)
|
||
{
|
||
for (j=0;j<=ma-1;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]){
|
||
void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double out[na][mb]){
|
||
if (ma == nb)
|
||
{
|
||
for (int i=0;i<na;i++)
|
||
{
|
||
for (int j=0;j<mb;j++)
|
||
{
|
||
out[i][j] = 0;
|
||
for (int k=0; k<ma;k++)
|
||
{
|
||
out[i][j] += A[i][k]*B[k][j];
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
... | ... | |
double RMT44T42[4][2]={{-308,-268},{-611,-99},{-816,118},{-432,122}};
|
||
double RAT44[4][4]={{13,15,21,19},{27,27,30,48},{87,53,19,9},{59,14,15,49}};
|
||
double RST44[4][4]={{-11,-11,-7,-11},{-15,-17,-16,-32},{-69,-37,-5,3},{-49,-6,-9,-45}};
|
||
double RMT44T44[4][4]={{816,412,192,304},{1155,583,379,687},{1146,668,466,758},{486,308,222,338}};
|
||
double RMT44T44[4][4]={{816,412,192,304},{1155,583,379,687},{1146,668,466,758},{486,308,222,338}};
|
||
|
||
double Y1[2][2] = {{5,2},{3,8}};
|
||
|
||
|
||
printf("Execution des tests unitaires.\n");
|
||
Transpose_Mat(2,4,T24,R42); if (!Equal_Mat_Mat(RTT24,R42)) error("Erreur calcul Transposition 2x4");
|
||
Transpose_Mat(4,4,T44a,R44); if (!Equal_Mat_Mat(RTT44,R44)) error("Erreur calcul Transposition 4x4");
|
||
Inverse_Mat_22(2,2,T22a,R22); if (!Equal_Mat_Mat(RInvT22,R22)) error("Erreur calcul Inversion 2x2");
|
||
Inverse_Mat_22(2,2,T22a,R22); if (!Equal_Mat_Mat(RInvT22,R22)) error("Erreur calcul Inversion 2x2");
|
||
|
||
//Add_Mat_Mat(2,2,T22b, 4,2, R22, R42); if (Equal_Mat_Mat(RAT22, R42) error("Erreur calcul Y1");
|
||
Add_Mat_Mat(2,2,T22a,2,2,T22b,R22); if (!Equal_Mat_Mat(RAT22,R22)) error("Erreur calcul Addition 2x2");
|
||
Add_Mat_Mat(4,4,T44a,4,4,T44b,R44); if (!Equal_Mat_Mat(RAT44,R44)) error("Erreur calcul Addition 4x4");
|
||
Add_Mat_Mat(4,1,T41a,4,1,T41b,R41); if (!Equal_Mat_Mat(RAT41,R41)) error("Erreur calcul Addition 4x1");
|
||
... | ... | |
Mul_Mat_Mat(2,4,T24,4,1,T41a,R21); if (!Equal_Mat_Mat(RMT24T41,R21)) error("Erreur calcul Multiplication 2x4 4x1");
|
||
Mul_Mat_Mat(2,4,T24,4,2,T42a,R22); if (!Equal_Mat_Mat(RMT24T42,R22)) error("Erreur calcul Multiplication 2x4 4x2");
|
||
Mul_Mat_Mat(2,4,T24,4,4,T44a,R24); if (!Equal_Mat_Mat(RMT24T44,R24)) error("Erreur calcul Multiplication 2x4 4x4");
|
||
printf("Test unitaires OK.\n");
|
||
printf("Test unitaires OK.\n");
|
||
|
||
}
|
||
|
||
int main(int argc,char **argv){
|
||
... | ... | |
{0, 0, 0, 1}};
|
||
double FT[4][4];
|
||
Transpose_Mat(4,4,F,FT);
|
||
|
||
double tampon_1[4][4];
|
||
double tampon_2[2][4];
|
||
|
||
|
||
while(fscanf(fichier, "%lf %lf %lf", &t, &x, &y)>0){
|
||
printf("-------------%04d--------------\n",cpt);
|
||
... | ... | |
///Ajouter votre code ci-dessous///
|
||
// Kalman
|
||
|
||
// X = F*X
|
||
// X = F*X
|
||
Mul_Mat_Mat(4,4,F, 4,1, X, X);
|
||
Plot_Mat(X," X(k+1|k) = ");
|
||
|
||
//P = F*P*F'+Q;
|
||
//P = F*P*F'+Q;
|
||
Mul_Mat_Mat(4,4,F,4,4,P,tampon_1);
|
||
Mul_Mat_Mat(4,4,tampon_1,4,4, FT, tampon_1);
|
||
Add_Mat_Mat(4,4,tampon_1,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);
|
||
|
||
Mul_Mat_Mat(4,4,P,4,2, HT, K);
|
||
Mul_Mat_Mat(2,4,H,4,4,P, tampon_2);
|
||
Mul_Mat_Mat(2,4,tampon_2, 4,2,HT, tampon_2);
|
||
Add_Mat_Mat(2,2,tampon_2,2,2,R, tampon_2);
|
||
Inverse_Mat_22(2,2,tampon_2, tampon_2);
|
||
Mul_Mat_Mat(4,2,K, 2,2, tampon_2, K);
|
||
|
||
Plot_Mat(K,"K = ");
|
||
|
||
//X = X + K*([xb(i);yb(i)]-H*X);
|
Formats disponibles : Unified diff
Codage des opérations matricielles