Index: branch/DEMAGALHAES/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/DEMAGALHAES/sp4a3/sp4a3_kalman.c	(revision 427)
+++ branch/DEMAGALHAES/sp4a3/sp4a3_kalman.c	(revision 428)
@@ -6,92 +6,126 @@
 
 #include "sp4a3_kalman_extra.h"
 
+void Add_Mat_Mat(int Ligne_MatA, int Colonne_MatA, double A[Ligne_MatA][Colonne_MatA],int Ligne_MatB, int Colonne_MatB, double B[Ligne_MatB][Colonne_MatB], double Res[Ligne_MatA][Colonne_MatA])
+{
+      for (int i = 0; i < Ligne_MatA; i++)
+      {
+            for (int j = 0; j < Colonne_MatA; j++)
+            {
+                  Res[i][j] = A[i][j]+B[i][j];
+            }
+      }
+}
+
+void Sub_Mat_Mat(int Ligne_MatA, int Colonne_MatA, double A[Ligne_MatA][Colonne_MatA],int Ligne_MatB, int Colonne_MatB, double B[Ligne_MatB][Colonne_MatB], double Res[Ligne_MatA][Colonne_MatA])
+{
+      for (int i = 0; i < Ligne_MatA; i++)
+      {
+            for (int j = 0; j < Colonne_MatA; j++)
+            {
+                  Res[i][j] = A[i][j]-B[i][j];
+            }
+      }
+}
+
+void Mul_Mat_Mat(int Ligne_MatA, int Colonne_MatA, double A[Ligne_MatA][Colonne_MatA],int Ligne_MatB, int Colonne_MatB, double B[Ligne_MatB][Colonne_MatB], double Res[Ligne_MatA][Colonne_MatB])
+{
+      for (int i = 0; i < Ligne_MatA; i++)
+      {
+          for (int j = 0; j < Colonne_MatB; j++)
+            {
+                Res[i][j]=0;
+                for(int k=0; k < Ligne_MatB; k++)
+                {
+                    Res[i][j] += A[i][k]*B[k][j];
+                }
+            }
+      }
+}
+
+void Inverse_Mat_Mat(int Ligne_MatA, int Colonne_MatA, double A[Ligne_MatA][Colonne_MatA],double Res[Ligne_MatA][Colonne_MatA])
+//OBLIGATOIREMENT POUR DES MATRICES DE TAILLES 2x2
+{
+    double unSurDet;
+    unSurDet = 1/(A[0][0]*A[1][1]-A[0][1]*A[1][0]);
+    Res[0][0] = A[1][1]*unSurDet;
+    Res[0][1] = -unSurDet*A[0][1];
+    Res[1][0] = -unSurDet*A[1][0];
+    Res[1][1] = A[0][0]*unSurDet;
+}
+
+void Transpose_Mat(int Ligne_MatA, int Colonne_MatA, double A[Ligne_MatA][Colonne_MatA],double Res[Colonne_MatA][Ligne_MatA])
+{
+	int i,j;
+	for (i=0;i<Colonne_MatA;i++)
+    {
+        for (j=0;j<Ligne_MatA;j++)
+        {
+            Res[i][j]=A[j][i];
+        }
+    }
+}
 
-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 tests_unitaires(void){
+	//Matrices d'entrée
+	double T21a[2][1]={{7},{-5}};
+	double T21b[2][1]={{-3},{46}};
+	double T22a[2][2]={{12,78},{-5,13}};
+	double T22b[2][2]={{-25,36},{7,42}};
+	double T24[2][4]={{7,-71,-12,3},{41,123,-5,10}};
+	double T41a[4][1]={{45},{-123},{-78},{-410}};
+	double T41b[4][1]={{-10},{45},{27},{-9}};
+	double T42a[4][2]={{-73,45},{10,12},{-41,-35},{8,-23}};
+	double T44a[4][4]={{1,2,7,4},{6,5,7,8},{9,8,7,6},{5,4,3,2}};
+	double T44b[4][4]={{12,13,14,15},{21,22,23,40},{78,45,12,3},{54,10,12,47}};
+
+	//Matrices résultat
+	double R21[2][1],R22[2][2],R24[2][4],R41[4][1],R42[4][2],R44[4][4];
+
+	//Matrices de validation
+	double RST21[2][1]={{10},{-51}};
+	double RInvT22[2][2]={{0.02380952380952381,-0.1428571428571428},{0.009157509157509158,0.02197802197802198}};
+	double RAT22[2][2]={{-13,114},{2,55}};
+	double RTT24[4][2]={{7,41},{-71,123},{-12,-5},{3,10}};
+	double RMT24T41[2][1]={{8754},{-16994}};
+	double RMT24T42[2][2]={{-705,-186},{-1478,3266}};
+	double RMT24T44[2][4]={{-512,-425,-523,-606},{784,697,1143,1138}};
+	double RAT41[4][1]={{35},{-78},{-51},{-419}};
+	double RMT42T21[4][1]={{-736},{10},{-112},{171}};
+	double RMT42T22[4][2]={{-1101,-5109},{60,936},{-317,-3653},{211,325}};
+	double RMT42T24[4][4]={{1334,10718,651,231},{562,766,-180,150},{-1722,-1394,667,-473},{-887,-3397,19,-206}};
+	double RTT44[4][4]={{1,6,9,5},{2,5,8,4},{7,7,7,3},{4,8,6,2}};
+	double RMT44T41[4][1]={{-2387},{-4171},{-3585},{-1321}};
+	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}};
+
+	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_Mat(2,2,T22a,R22);	if (!Equal_Mat_Mat(RInvT22,R22)) 	error("Erreur calcul Inversion 2x2");
+	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");
+	Sub_Mat_Mat(2,1,T21a,2,1,T21b,R21);	if (!Equal_Mat_Mat(RST21,R21)) 		error("Erreur calcul Soustraction 2x1");
+	Sub_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RST44,R44)) 		error("Erreur calcul Soustraction 4x4");
+	Mul_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RMT44T44,R44)) 	error("Erreur calcul Multiplication 4x4 4x4");
+	Mul_Mat_Mat(4,4,T44a,4,1,T41a,R41);	if (!Equal_Mat_Mat(RMT44T41,R41)) 	error("Erreur calcul Multiplication 4x4 4x1");
+	Mul_Mat_Mat(4,4,T44a,4,2,T42a,R42);	if (!Equal_Mat_Mat(RMT44T42,R42)) 	error("Erreur calcul Multiplication 4x4 4x2");
+	Mul_Mat_Mat(4,2,T42a,2,1,T21a,R41);	if (!Equal_Mat_Mat(RMT42T21,R41)) 	error("Erreur calcul Multiplication 4x2 2x1");
+	Mul_Mat_Mat(4,2,T42a,2,2,T22a,R42);	if (!Equal_Mat_Mat(RMT42T22,R42)) 	error("Erreur calcul Multiplication 4x2 2x2");
+	Mul_Mat_Mat(4,2,T42a,2,4,T24,R44);	if (!Equal_Mat_Mat(RMT42T24,R44)) 	error("Erreur calcul Multiplication 4x2 2x4");
+	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");
 }
 
-void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
+int main(int argc,char **argv){
+
+    tests_unitaires();
 
-}
-
-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]){
-
-}
-
-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 tests_unitaires(void){
-	//Matrices d'entrée
-	double T21a[2][1]={{7},{-5}};
-	double T21b[2][1]={{-3},{46}};
-	double T22a[2][2]={{12,78},{-5,13}};
-	double T22b[2][2]={{-25,36},{7,42}};
-	double T24[2][4]={{7,-71,-12,3},{41,123,-5,10}};
-	double T41a[4][1]={{45},{-123},{-78},{-410}};
-	double T41b[4][1]={{-10},{45},{27},{-9}};
-	double T42a[4][2]={{-73,45},{10,12},{-41,-35},{8,-23}};
-	double T44a[4][4]={{1,2,7,4},{6,5,7,8},{9,8,7,6},{5,4,3,2}};
-	double T44b[4][4]={{12,13,14,15},{21,22,23,40},{78,45,12,3},{54,10,12,47}};
-
-	//Matrices résultat
-	double R21[2][1],R22[2][2],R24[2][4],R41[4][1],R42[4][2],R44[4][4];
-
-	//Matrices de validation
-	double RST21[2][1]={{10},{-51}};
-	double RInvT22[2][2]={{0.02380952380952381,-0.1428571428571428},{0.009157509157509158,0.02197802197802198}};
-	double RAT22[2][2]={{-13,114},{2,55}};
-	double RTT24[4][2]={{7,41},{-71,123},{-12,-5},{3,10}};
-	double RMT24T41[2][1]={{8754},{-16994}};
-	double RMT24T42[2][2]={{-705,-186},{-1478,3266}};
-	double RMT24T44[2][4]={{-512,-425,-523,-606},{784,697,1143,1138}};
-	double RAT41[4][1]={{35},{-78},{-51},{-419}};
-	double RMT42T21[4][1]={{-736},{10},{-112},{171}};
-	double RMT42T22[4][2]={{-1101,-5109},{60,936},{-317,-3653},{211,325}};
-	double RMT42T24[4][4]={{1334,10718,651,231},{562,766,-180,150},{-1722,-1394,667,-473},{-887,-3397,19,-206}};
-	double RTT44[4][4]={{1,6,9,5},{2,5,8,4},{7,7,7,3},{4,8,6,2}};
-	double RMT44T41[4][1]={{-2387},{-4171},{-3585},{-1321}};
-	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}};
-
-	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");
-	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");
-	Sub_Mat_Mat(2,1,T21a,2,1,T21b,R21);	if (!Equal_Mat_Mat(RST21,R21)) 		error("Erreur calcul Soustraction 2x1");
-	Sub_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RST44,R44)) 		error("Erreur calcul Soustraction 4x4");
-	Mul_Mat_Mat(4,4,T44a,4,4,T44b,R44);	if (!Equal_Mat_Mat(RMT44T44,R44)) 	error("Erreur calcul Multiplication 4x4 4x4");
-	Mul_Mat_Mat(4,4,T44a,4,1,T41a,R41);	if (!Equal_Mat_Mat(RMT44T41,R41)) 	error("Erreur calcul Multiplication 4x4 4x1");
-	Mul_Mat_Mat(4,4,T44a,4,2,T42a,R42);	if (!Equal_Mat_Mat(RMT44T42,R42)) 	error("Erreur calcul Multiplication 4x4 4x2");
-	Mul_Mat_Mat(4,2,T42a,2,1,T21a,R41);	if (!Equal_Mat_Mat(RMT42T21,R41)) 	error("Erreur calcul Multiplication 4x2 2x1");
-	Mul_Mat_Mat(4,2,T42a,2,2,T22a,R42);	if (!Equal_Mat_Mat(RMT42T22,R42)) 	error("Erreur calcul Multiplication 4x2 2x2");
-	Mul_Mat_Mat(4,2,T42a,2,4,T24,R44);	if (!Equal_Mat_Mat(RMT42T24,R44)) 	error("Erreur calcul Multiplication 4x2 2x4");
-	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");
-}
-
-int main(int argc,char **argv){
-
-	tests_unitaires();
-
 	FILE* fichier = fopen("pos_t_x_y.dat","r");
 	if (fichier == NULL)
         error("Impossible d'ouvrir le fichier GPGGA_data.dat");
@@ -129,15 +163,11 @@
 	double K[4][2];
 	double H[2][4] = 	{{1, 0, 0, 0},
 						 {0, 1, 0, 0}};
-	double HT[4][2];
-	Transpose_Mat(2,4,H,HT);
 
 	double F[4][4] =	{{1, 0, dt, 0},
 						 {0, 1, 0, dt},
 						 {0, 0, 1, 0},
 						 {0, 0, 0, 1}};
-	double FT[4][4];
-	Transpose_Mat(4,4,F,FT);
 
 	while(fscanf(fichier, "%lf %lf %lf", &t, &x, &y)>0){
 		printf("-------------%04d--------------\n",cpt);
@@ -154,26 +184,72 @@
 		{
 			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
-
-			// X = F*X
+			// Kalmans
+			// 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;
+
+			double A[4][4];
+			double B[4][4];
+			// A = F*P
+			Mul_Mat_Mat(4,4,F,4,4,P,A);
+			// B = A*FT
+            double FT[4][4];
+            Transpose_Mat(4,4,F,FT);
+            Mul_Mat_Mat(4,4,A,4,4,FT,B);
+			// P = B+Q
+			Add_Mat_Mat(4,4,B,4,4,Q,P);
+
 			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*([xb(i);yb(i)]-H*X);
-			//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+			// K = P*H' / ( H*P*H' + R);
+			// K = C /E
+            double C[4][2];
+            double HT[4][2];
+            Transpose_Mat(2,4,H,HT);
+            Mul_Mat_Mat(4,4,P,4,2,HT,C);
+
+            double D[2][4];
+            Mul_Mat_Mat(2,4,H,4,4,P,D);
+            double E[2][2];
+            Mul_Mat_Mat(2,4,D,4,2,HT,E);
+            Add_Mat_Mat(2,2,E,2,2,R,E);
+            double E_inv[2][2];
+            Inverse_Mat_Mat(2,2,E,E_inv);
+
+            Mul_Mat_Mat(4,2,C,2,2,E,K);
+            Plot_Mat(K,"K = ");
+
+
+			// DELTA = obs - H*X
+			double DELTA[2][1];
+			double obs[2][1]={{x},{y}};
+			double Z[2][1];
+			Mul_Mat_Mat(2,4,H,4,1,X,Z);
+            Sub_Mat_Mat(2,1,obs,2,1,Z,DELTA);
+			Plot_Mat(DELTA,"DELTA = Obs - H.X(k+1|k)");
+
+			// X = X + K*DELTA
+			double KD[4][1];
+			Mul_Mat_Mat(4,2,K,2,1,DELTA,KD);
+			Add_Mat_Mat(4,1,X,4,1,KD,X);
             Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
 
-			// P = P - K*H*P;
+			// P = P - K*H*P;
+			// W = H*P
+			double W[2][4];
+			Mul_Mat_Mat(2,4,H,4,4,P,W);
+			// Y = K*W
+			double Y[4][4];
+			Mul_Mat_Mat(4,2,K,2,4,W,Y);
+			// P = P-Y
+			Sub_Mat_Mat(4,4,P,4,4,Y,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;
