Index: branch/papa_ndiaye/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/papa_ndiaye/sp4a3/sp4a3_kalman.c	(revision 547)
+++ branch/papa_ndiaye/sp4a3/sp4a3_kalman.c	(revision 548)
@@ -3,36 +3,63 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <math.h>
-
 #include "sp4a3_kalman_extra.h"
 
+void Transpose_Mat(int n,int m,double A[n][m],double R[m][n])
+{
+	for (int i=0;i<n;i++)
+		for (int j=0;j<m;j++)
+        {
+            R[j][i]=A[i][j];
+        }
+
+}
 
-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])
+{
+    for(int i=0;i<na;i++)
+        for(int j=0;j<ma;j++)
+        {
+            R[i][j]=0;
+            R[i][j]=A[i][j]+B[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])
+{
+    for(int i=0;i<na;i++)
+        for(int j=0;j<ma;j++)
+        {
+            R[i][j]=0;
+            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 B[n][m])
+{
+    double invdet,det;
+    det=((A[0][0])*(A[1][1])) - (A[1][0])*(A[0][1]);
+    invdet=1/det;
+    B[0][0]=invdet*A[1][1];
+    B[1][1]=invdet*A[0][0];
+    B[0][1]=-(invdet*A[0][1]);
+    B[1][0]=-(invdet*A[1][0]);
 
 }
 
-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 Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double R[na][mb])
+{
+    for(int i=0;i<na;i++)
+        for(int j=0;j<mb;j++)
+        {
+            R[i][j]=0;
+            for(int k=0;k<ma;k++)
+                R[i][j]=R[i][j]+(A[i][k])*(B[k][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 Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double R[na][mb]){
-
-}
-
-
-
-void tests_unitaires(void){
+void tests_unitaires(void)
+{
 	//Matrices d'entrée
 	double T21a[2][1]={{7},{-5}};
 	double T21b[2][1]={{-3},{46}};
@@ -52,7 +79,6 @@
 	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}};
@@ -68,7 +94,6 @@
 	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");
@@ -84,11 +109,13 @@
 	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");
+	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){
+int main(int argc,char **argv)
+{
 
 	tests_unitaires();
 
@@ -109,10 +136,19 @@
 	int cpt = 0;
 
 	// kalman param
-	double sigma_etat = 10.0;
-	double sigma_observation = 2.0;
-	double X[4][1] = {{0},{0},{0},{0}};
+	double sigma_etat = 10.0;
 
+	double sigma_observation = 2.0;
+
+	double X[4][1] = {{0},{0},{0},{0}};
+	double Xa[4][1] = {{0},{0},{0},{0}};
+
+	double P1[4][4];
+
+	double Pa[4][4];
+
+
+
 	double P[4][4] = 	{{sigma_etat*sigma_etat, 0, 0, 0},
 						 {0, sigma_etat*sigma_etat, 0, 0},
 						 {0, 0, 0, 0},
@@ -124,13 +160,22 @@
 						 {0, 0, 0, 0.1}};
 
 	double R[2][2] = 	{{sigma_observation*sigma_observation, 0},
-						 {0 , sigma_observation*sigma_observation}};
+						 {0 , sigma_observation*sigma_observation}};
+
+
+    double R1[2][2];
 
-	double K[4][2];
+	double K[4][2];
+
+	double zero[4][4];
+
+	double K1[4][2];
+
 	double H[2][4] = 	{{1, 0, 0, 0},
-						 {0, 1, 0, 0}};
+						 {0, 1, 0, 0}};
+    double H1[2][4];
+
 	double HT[4][2];
-	Transpose_Mat(2,4,H,HT);
 
 	double F[4][4] =	{{1, 0, dt, 0},
 						 {0, 1, 0, dt},
@@ -137,9 +182,24 @@
 						 {0, 0, 1, 0},
 						 {0, 0, 0, 1}};
 	double FT[4][4];
-	Transpose_Mat(4,4,F,FT);
+
+	double PH[4][4];
+
+	double PHt[4][4];
+
+	double RPHt[4][4];
+
+	double PF[4][4];
+
+	double PFt[4][4];
+
+	double Delta[2][1];
+
+	double KDelta[4][1];
 
-	while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0){
+	while(fscanf(fichier, "%lf %lf %lf", &t, &xobs, &yobs)>0)
+    {
+
 		printf("-------------%04d--------------\n",cpt);
 
 		if (cpt ==0)
@@ -152,43 +212,93 @@
 		}
 		else
 		{
-			t -= t0;xobs -= x0;yobs -= y0;
+			t -= t0;xobs -= x0;yobs -= y0;
 
+            double obs[2][1]={{xobs},{yobs}};
+            	Transpose_Mat(2,4,H,HT);
+                Transpose_Mat(4,4,F,FT);
+
+
 			debug=0; ///Mettre à 1 pour afficher les matrices.
 			///Ajouter votre code ci-dessous///
-			// Kalman
+			// Kalman
 
-			// X = F*X
-			Plot_Mat(X," X(k+1|k) = ");
+            Mul_Mat_Mat(4,4,F,4,1,X,Xa);// Xa = F*X
 
-			//P = F*P*F'+Q;
-			Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
+			Plot_Mat(Xa," X(k+1|k) = ");
+
+            Mul_Mat_Mat(4,4,P,4,4,FT,PFt);//PFt= P*F'
+
+            Mul_Mat_Mat(4,4,F,4,4,PFt,PF);//PF= F*P*F'
+
+            Add_Mat_Mat(4,4,Q,4,4,PF,P);//Pa = F*P*F'+Q;
 
-			// K = P*H' / ( H*P*H' + R);
-			Plot_Mat(K,"K = ");
+			Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
+
+            Mul_Mat_Mat(4,4,P,4,2,HT,PHt);//PHt= P*H'
+
+            Mul_Mat_Mat(2,4,H,4,2,PHt,PH);//PH= H*P*H'
+
+            Add_Mat_Mat(2,2,R,2,2,PH,R1);//R1= H*P*H' + R
+
+            Inverse_Mat_22(2,2,R1,RPHt);//RPHt= 1/ ( H*P*H' + R)
+
+            Mul_Mat_Mat(4,2,PHt,2,2,RPHt,K);// K = P*H' / ( H*P*H' + R);
 
-			//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 = ");
+            for(int i=0;i<4;i++)
+                for(int j=0;j<4;j++)
+                    zero[i][j]=0;
+			Plot_Mat(K,"K = ");
+
+            Mul_Mat_Mat(2,4,H,4,1,Xa,H1);//H1=H*X
+
+            Sub_Mat_Mat(2,1,obs,2,1,H1,Delta);// Delta=[xobs(i);yobs(i)]-H*X
+
+            Mul_Mat_Mat(4,2,K,2,1,Delta,KDelta);//KDelta=K*(obs -H*X)
+
+            Add_Mat_Mat(4,1,Xa,4,1,KDelta,X);//X = X + K*([xobs(i);yobs(i)]-H*X);
 
-			// P = P - K*H*P;
+            Plot_Mat(obs,"obs=");
+
+			Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+
+            Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
+
+            Mul_Mat_Mat(4,2,K,2,4,H,K1);//K1=K*H
+
+            Mul_Mat_Mat(4,4,K1,4,4,P,P1);// P1= K1*P
+
+            Sub_Mat_Mat(4,4,P,4,4,P1,Pa);// Pa = P - K*H*P;
+
+            Add_Mat_Mat(4,4,Pa,4,4,zero,P);// 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;
-		dx = (xobs - oldx)/dt;
-		dy = (yobs - oldy)/dt;
-		fprintf(Fout,"%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",t,xobs,yobs,sqrt(dx*dx+dy*dy)*dt,X[0][0],X[1][0],X[2][0],X[3][0],sqrt(X[2][0]*X[2][0]+X[3][0]*X[3][0])*dt);
-		oldx = xobs;
-		oldy = yobs;
-		cpt ++;
+          }
+		t = cpt * dt;
+
+		dx = (xobs - oldx)/dt;
+
+		dy = (yobs - oldy)/dt;
+
+		fprintf(Fout,"%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",t,xobs,yobs,sqrt(dx*dx+dy*dy)*dt,X[0][0],X[1][0],X[2][0],X[3][0],sqrt(X[2][0]*X[2][0]+X[3][0]*X[3][0])*dt);
+
+		oldx = xobs;
+
+		oldy = yobs;
+
+		cpt ++;
+
 	}
 	fclose(Fout);
 	fclose(fichier);
 
-	system ("gnuplot -p -e \"plot 'output.dat' u 5:6 w l, '' u 2:3 w l\";");
-	system ("gnuplot -p -e \"plot 'output.dat' u 1:9 w l, '' u 1:4 w l\";");
-	system ("gnuplot -p -e \"plot 'output.dat' u 9 w l  , 'vitesse_reelle.dat' u 2 w l\";");
+	system ("gnuplot -p -e \"plot 'output.dat' u 5:6 w l, '' u 2:3 w l\";");
+
+	system ("gnuplot -p -e \"plot 'output.dat' u 1:9 w l, '' u 1:4 w l\";");
+
+	system ("gnuplot -p -e \"plot 'output.dat' u 9 w l  , 'vitesse_reelle.dat' u 2 w l\";");
+
 	return 0;
 }
