clear all
close all
clc
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARTIE DEDIEE A LA BASE D'ENTRAINEMENT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Extraction de siganux contenus dans BASE_X_ENTRAINEMENT2 pour former un tableau 25000*80: 
% (chaque colonne représente un signal, chaque ligne représente un échantillons d'un signal)
x=1;
for z=1:80 % représente le nombre de signaux contenus dans BASE_X_ENTRAINEMENT2
    for y=1:25000 % 25000 représente le nombre d'échantillons contenus dans chaque signal
        signal(y,z)= BASE_X_ENTRAINEMENT2(x,1);
        x=x+1;
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Extraction de la flexion et extension de chaque signal :
for z=1:80;
    for y=1:25000;
        if (y>4999 && y<10000);
            flexion1(y,z)=signal(y,z);
        else flexion1(y,z)=0;
        end
         if (y>14999 && y<20000);
            extension1(y,z)=signal(y,z);
        else extension1(y,z)=0;
         end
        if (y>20000 && y<25000);
            bruit1(y,z)=signal(y,z);
        else bruit1(y,z)=0;
        end
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Decalage des signaux flexion1 et extension1 pour former des signaux de 25000 échantillons :
b=5000;
c=15000;
d=20000;
for z=1:80
    for a=1:5000
        flexion2(a,z)=flexion1(b,z);
        b=b+1;
        extension2(a,z)=extension1(c,z);
        c=c+1;
        bruit2(a,z)=bruit1(d,z);
        d=d+1;
    end
    b=5000;
    c=15000;
    d=20000;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Découpage des signaux flexion2 et extension2 en segments de 250 ms: 
b=0;
d=1;
for c=1:320   % *********** 320 = 80 * nombre de segment dans une seconde 
    for a=1:1250 % ********1250 = 5000 / nombre de segment dans une seconde 
         b=b+1;
        flexion3(a,c)=flexion2(b,d);
        extension3(a,c)=extension2(b,d);
    end 
    if b==5000
        b=0;
        d=d+1;   
    end
end 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Extraction des caractéristiques à partor de la base d'entrainement  :

% (IEMG,WL,MAV,RMS,SSI,SSC,ZC,VAR,AMP_MAX,ECART_TYPE)  

WL_flexion3=zeros(1,320); %***************320= 80 * nombre de segment dans une seconde 
WL_flexion3=WL_flexion3';
WL_extension3=zeros(1,320); %***************320= 80 * nombre de segment dans une seconde 
WL_extension3=WL_extension3';

ZC_flex=0;
ZC_ext=0;

c=0;
d=0;

for a=1:320 %***************320= 80 * nombre de segment dans une seconde 
        IEMG_flexion3(a)=sum(abs(flexion3(:,a)));
        IEMG_extension3(a)=sum(abs(extension3(:,a)));
        
        MAV_flexion3(a) = mean(abs(flexion3(:,a)));
        MAV_extension3(a) = mean(abs(extension3(:,a)));
        
        RMS_flexion3(a) =sqrt(mean(flexion3(:,a).^2));
        RMS_extension3(a) =sqrt(mean(extension3(:,a).^2));
        
        SSI_flexion3(a)=sum(abs(flexion3(:,a).^2));
        SSI_extension3(a)=sum(abs(extension3(:,a).^2));
        
    for b=1:1249 %***************** 1249= (5000 / nombre de segment dans une seconde) - 1
        
        WL_flexion3(a)=WL_flexion3(a)+ abs(flexion3(b+1,a)-flexion3(b,a));
        WL_extension3(a)=WL_extension3(a)+abs(extension3(b+1,a)-extension3(b,a));
        
        if((flexion3(b,a)>0 && flexion3(b+1,a)<0) || (flexion3(b,a)<0 && ...
                 flexion3(b+1,a) > 0))
            ZC_flex=ZC_flex+1;
        end
         
         if((extension3(b,a)>0 && extension3(b+1,a)<0) || (extension3(b,a)<0 && ... 
                extension3(b+1,a)> 0))
            ZC_ext=ZC_ext+1;
         end 
        
    end
    
    for b=2:1249 %***************** 1249= (5000 / nombre de segment dans une seconde) - 1
        
         if (flexion3(b,a)<flexion3(b+1,a) && flexion3(b,a)<flexion3(b-1,a)) || ( flexion3(b,a)>flexion3(b+1,a) && flexion3(b,a)>flexion3(b-1,a))
         c=c+1;
        end  
        if (extension3(b,a)<extension3(b+1,a) && extension3(b,a)<extension3(b-1,a) || extension3(b,a)>extension3(b+1,a) && extension3(b,a)>extension3(b-1,a))
         d=d+1;
        end  
    end
        ZC_flexion(a,1)=ZC_flex;
        ZC_flex=0;
        ZC_extension(a,1)=ZC_ext;
        ZC_ext=0; 
        
        SSC_flexion3(a)=c;
        SSC_extension3(a)=d;
        c=0;
        d=0;
end     

IEMG_flexion=IEMG_flexion3';
IEMG_extension=IEMG_extension3';

MAV_flexion =MAV_flexion3'; 
MAV_extension=MAV_extension3';

RMS_flexion=RMS_flexion3';
RMS_extension=RMS_extension3';

WL_flexion= WL_flexion3;
WL_extension= WL_extension3;

AMP_MAX_flexion3=max(abs(flexion3(:,:)));
AMP_MAX_extension3=max(abs(extension3(:,:)));
AMP_MAX_flexion=AMP_MAX_flexion3';
AMP_MAX_extension=AMP_MAX_extension3';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Evaluations de la pertinence des caractéristiques en utilisant le boxplot:

IEMG=[IEMG_extension IEMG_flexion];
MAV=[MAV_extension MAV_flexion];
RMS=[RMS_extension RMS_flexion];
ZC=[ZC_extension ZC_flexion];
WL=[WL_extension WL_flexion];
VAR=[VAR_extension VAR_flexion];
SSC=[SSC_extension SSC_flexion];
AMP_MAX=[AMP_MAX_extension AMP_MAX_flexion]; 
ECART_TYPE=[ECART_TYPE_extension ECART_TYPE_flexion];
SSI=[SSI_extension SSI_flexion];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
boxplot(IEMG)
title('IEMG')
figure
boxplot(MAV)
title('MAV')
figure
boxplot(RMS)
title('RMS')
figure
boxplot(ZC)
title('ZC')
figure
boxplot(WL)
title('WL')
figure
boxplot(VAR)
title('VAR')
figure
title('SSC')
figure
boxplot(AMP_MAX)
title('AMP_MAX')
figure
boxplot(ECART_TYPE)
title('ECART_TYPE')
figure
boxplot(SSI)
title('SSI')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% FIN DE LA PARTIE DEDIEE A LA BASE D'ENTRAINEMENT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARTIE DEDIEE A LA BASE DE TEST %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Extraction des mouvements flexion et extension de la base de donneés BASE_X_TEST2: 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=1;
    for z=1:40 % représente le nombre de signaux contenus dans BASE_X_TEST2
        for y=1:25000 % 25000 représente le nombre d'échantillons contenus dans chaque signal
            signal_test(y,z)=BASE_X_TEST2(x,1);% 
            x=x+1;
        end
    end

% Extraction de la flexion et extension de chaque signal :
for z=1:40;
    for y=1:25000;
        if (y>4999 && y<10000);
            flexion1_test(y,z)=signal_test(y,z);
        else flexion1_test(y,z)=0;
        end
         if (y>14999 && y<20000);
            extension1_test(y,z)=signal_test(y,z);
        else extension1_test(y,z)=0;
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b=5000;
c=15000;
for z=1:40
    for a=1:5000
        flexion2_test(a,z)=flexion1_test(b,z);
        b=b+1;
        extension2_test(a,z)=extension1_test(c,z);
        c=c+1;
    end
    b=5000;
    c=15000;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b=0;
d=1;
for c=1:160 %********* 160 = 40* nombre de segment dans une seconde
    for a=1:1250 %*********1250 = 5000 / nombre de segment dans une seconde 
         b=b+1;
        flexion3_test(a,c)=flexion2_test(b,d);
        extension3_test(a,c)=extension2_test(b,d);
    end 
    if b==5000
        b=0;
        d=d+1;   
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Extraction des caractéristiques à partir de la base dde test :

WL_flexion3_test=zeros(1,160);%*************** 160 = 40* nombre de segment dans une seconde
WL_flexion3_test=WL_flexion3_test';
WL_extension3_test=zeros(1,160);%*************** 160 = 40* nombre de segment dans une seconde
WL_extension3_test=WL_extension3_test';

c=0;
d=0;

for a=1:160%*************** 160 = 40* nombre de segment dans une seconde
    
        IEMG_flexion3_test(a)=sum(abs(flexion3_test(:,a)));
        IEMG_extension3_test(a)=sum(abs(extension3_test(:,a)));  
        MAV_flexion3_test(a) = mean(abs(flexion3_test(:,a)));
        MAV_extension3_test(a) = mean(abs(extension3_test(:,a)));
        
    for b=1:1249%***************** 1250 = (5000 / nombre de segment dans une seconde) - 1
        
        WL_flexion3_test(a)=WL_flexion3_test(a)+ abs(flexion3_test(b+1,a)-flexion3_test(b,a));
        WL_extension3_test(a)=WL_extension3_test(a)+abs(extension3_test(b+1,a)-extension3_test(b,a));
       
    end
    
     for b=2:1249 %***************** 1249= (5000 / nombre de segment dans une seconde) - 1
        
         if (flexion3_test(b,a)<flexion3_test(b+1,a) && flexion3_test(b,a)<flexion3_test(b-1,a)) || ( flexion3_test(b,a)>flexion3_test(b+1,a) && flexion3_test(b,a)>flexion3_test(b-1,a))
         c=c+1;
        end  
        if (extension3_test(b,a)<extension3_test(b+1,a) && extension3_test(b,a)<extension3_test(b-1,a) || extension3_test(b,a)>extension3_test(b+1,a) && extension3_test(b,a)>extension3_test(b-1,a))
         d=d+1;
        end  
    end
        SSC_flexion3_test(a)=c;
        SSC_extension3_test(a)=d;
        c=0;
        d=0;
end    

IEMG_flexion_test=IEMG_flexion3_test';
IEMG_extension_test=IEMG_extension3_test';

MAV_flexion_test =MAV_flexion3_test'; 
MAV_extension_test=MAV_extension3_test';

WL_flexion_test= WL_flexion3_test;
WL_extension_test= WL_extension3_test;

AMP_MAX_flexion3_test=max(abs(flexion3_test(:,:)));
AMP_MAX_extension3_test=max(abs(extension3_test(:,:)));
AMP_MAX_flexion_test=AMP_MAX_flexion3_test';
AMP_MAX_extension_test=AMP_MAX_extension3_test';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% FIN DE LA PARTIE DEDIEE A LA BASE DE TEST %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARTIE DEDIEE A L'ENTRAINEMENT/TEST DU CLASSIFIEUR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Cration des classes :
for i=1:320%**************** 320 = 80 * nombre de segment dans une seconde 
    Y(i,1)=-1; %(-1) représente la classe extension
end
for i=321:640%************** 640 = 80 * nombre de segment dans une seconde * 2
    Y(i,1)=1; %(1) représente la classe flexion
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1) entrainement  :
% WL : 
X1=[WL_extension;WL_flexion]; 
SVM1=fitcsvm(X1,Y);
% MAV : 
X2=[MAV_extension;MAV_flexion];
SVM2=fitcsvm(X2,Y);
% IEMG : 
X3=[IEMG_extension;IEMG_flexion];
SVM3=fitcsvm(X3,Y);
% AMP_MAX : 
X4=[AMP_MAX_extension;AMP_MAX_flexion];
SVM4=fitcsvm(X4,Y);
% WL+IEMG : 
X5=[WL_extension IEMG_extension;WL_flexion IEMG_flexion];
SVM5=fitcsvm(X5,Y);
%WL+MAV : 
X6=[WL_extension MAV_extension;WL_flexion MAV_flexion];
SVM5=fitcsvm(X6,Y);
%WL+AMP_MAX : 
X7=[WL_extension AMP_MAX_extension;WL_flexion WL_flexion];
SVM5=fitcsvm(X7,Y);
% %WL+AMP_MAX +  IEMG : 
X8=[WL_extension AMP_MAX_extension IEMG_extension;WL_flexion WL_flexion IEMG_flexion];
SVM8=fitcsvm(X8,Y);
 %WL+AMP_MAX +MAV : 
X9=[WL_extension AMP_MAX_extension MAV_extension;WL_flexion WL_flexion MAV_flexion];
SVM9=fitcsvm(X9,Y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2) test : 
% WL :
X1_test=[WL_extension_test;WL_flexion_test];
label1= predict(SVM1,X1_test);
% MAV :
X2_test=[MAV_extension_test;MAV_flexion_test];
label2= predict(SVM2,X2_test);
% IEMG :
X3_test=[IEMG_extension_test;IEMG_flexion_test];
label3= predict(SVM3,X3_test);
% AMP_MAX :
X4_test=[AMP_MAX_extension_test;AMP_MAX_flexion_test];
label4= predict(SVM4,X4_test);
% WL+IEMG : 
X5_test=[WL_extension_test IEMG_extension;WL_flexion_test IEMG_flexion_test];
label1= predict(SVM5,X5_test);
%WL+MAV : 
X6_test=[WL_extension_test MAV_extension_test;WL_flexion_test MAV_flexion_test];
label6= predict(SVM6,X6_test);
%WL+ AMP_MAX :
X7_test=[WL_extension_test AMP_MAX_extension_test;WL_flexion_test AMP_MAX_extension_test];
label7= predict(SVM7,X7_test);
% 
X8_test=[WL_extension_test AMP_MAX_extension_test IEMG_extension_test ;WL_flexion_test AMP_MAX_extension_test IEMG_flexion_test];
label8= predict(SVM8,X8_test);
% 
X9_test=[WL_extension_test AMP_MAX_extension_test MAV_extension_test;WL_flexion_test AMP_MAX_extension_test MAV_flexion_test];
label9= predict(SVM9,X9_test);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3) Calcul de la précision :

TP1=0; 
TP2=0; 
TP3=0; 
TP4=0; 
TP5=0; 
TP7=0; 
TP8=0;
TP9=0;

for i=1:160  %**************** 160 = 360 / 2 = (80 * nombre de segment dans une seconde) / 2
    if label1(i,1)==-1
        TP1=TP1+1;
    end
    
    if label2(i,1)==-1
        TP2=TP2+1;
    end
    
    if label3(i,1)==-1
        TP3=TP3+1;
    end
    
    if label4(i,1)==-1
        TP4=TP4+1;
    end
    
    if label5(i,1)==-1
        TP5=TP5+1;
    end
    if label7(i,1)==-1
        TP7=TP7+1;
    end
     if label8(i,1)==-1
        TP8=TP8+1;
     end
     if label9(i,1)==-1
        TP9=TP9+1;
    end
end
for i=161:320 %************* 320 = 80 * nombre de segment dans une seconde 
    
    if label1(i,1)==1
        TP1=TP1+1;
    end
    
     if label2(i,1)==1
        TP2=TP2+1;
     end
     
     if label3(i,1)==1
        TP3=TP3+1;
     end
     
     if label4(i,1)==1
        TP4=TP4+1;
     end
     
     if label5(i,1)==1
        TP5=TP5+1;
     end
     
     if label7(i,1)==1
        TP7=TP7+1;
     end
     if label8(i,1)==1
        TP8=TP8+1;
     end
     if label9(i,1)==1
        TP9=TP9+1;
    end
end

p=[TP1;TP2;TP3;TP4;TP5;TP7];
precision = p*100/320 


