clc
%%%% input data feeding

load('Sz6.mat')
x6=EEG;

load('Sz7.mat')
x7=EEG;

load('Sz8.mat')
x8=EEG;

load('Sz9.mat')
x9=EEG;

load('Sz10.mat')
x10=EEG;



 


Fs=512; %%% sampling frequency
WW=1; %%% Window length
num_channel_select=27;



%%%%% Separating the input data of each channel

for i=1:num_channel
   
    g6{i}=x6(:,i);
    g7{i}=x7(:,i);
    g8{i}=x8(:,i);
    g9{i}=x9(:,i);
    g10{i}=x10(:,i);
       
end



for i=1:num_channel
    G_tot{i}=[g6{i};g7{i};g8{i};g9{i};g10{i}];
end

%%%%% Defining the length of signal and labeling

num6_window=length(g6{1})/512;
num7_window=length(g7{1})/512;
num8_window=length(g8{1})/512;
num9_window=length(g9{1})/512;
num10_window=length(g10{1})/512;


num6_seizure=num6_window-360;
num7_seizure=num7_window-360;
num8_seizure=num8_window-360;
num9_seizure=num9_window-360;
num10_seizure=num10_window-360;



Num_sample_tot=size(G_tot{1},1);
Num_window_tot=Num_sample_tot/(Fs*WW);

Num_seizure_tot=num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure;

sz6_start=181;
sz6_end=num6_window-180;

sz7_start=num6_window+181;
sz7_end=num6_window+num7_window-180;

sz8_start=num6_window+num7_window+181;
sz8_end=num6_window+num7_window+num8_window-180;

sz9_start=num6_window+num7_window+num8_window+181;
sz9_end=num6_window+num7_window+num8_window+num9_window-180;

sz10_start=num6_window+num7_window+num8_window+num9_window+181;
sz10_end=num6_window+num7_window+num8_window+num9_window+num10_window-180;


BasicVector=[zeros(1,180), ones(1,num6_seizure), zeros(1,360), ones(1,num7_seizure), zeros(1,360), ones(1,num8_seizure), zeros(1,360), ones(1,num9_seizure), zeros(1,360),ones(1,num10_seizure), zeros(1,180)];


%%%%% picking the selected channels


for i=1:num_channel_select
    Z{i}=G_tot{channel_rank_index_ascend(i)};
end


%%%% Averaging the value of the selected channels

Z_concat=cat(3,Z{:});
Input_data_tot=mean(Z_concat,3);

%%%% FPGA input generation

VHDL_in=dec2bin(Input_data_tot);

%%%%% writing input to a txt file to be used in VHDL

dlmwrite('data_in.txt',VHDL_in,'delimiter','');   


%%%% Windowing the input signal with channel selection

k=1;
for nn=1:Fs*WW:Num_sample_tot-Fs*WW+1

  Y_final{k}=Input_data_tot(nn:nn+Fs*WW-1);
  k=k+1;
end



T_window_tot=1:Num_window_tot;

t6_seizure=180+WW:WW:num6_window-180/WW;
t7_seizure=num6_window+180+WW:WW:num6_window+num7_window-180/WW;
t8_seizure=num6_window+num7_window+180+WW:WW:num6_window+num7_window+num8_window-180/WW;
t9_seizure=num6_window+num7_window+num8_window+180+WW:WW:num6_window+num7_window+num8_window+num9_window-180/WW;
t10_seizure=num6_window+num7_window+num8_window+num9_window+180+WW:WW:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Coastline feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:Num_window_tot
    for o=2:Fs*WW
        Y_subtract{i}(o)=abs(Y_final{i}(o)-Y_final{i}(o-1));
        Y_subtract{i}(1)=0;
    end 
end

for i=1:Num_window_tot
    F_coastline_test(i)=sum(Y_subtract{i},'all');
end



FF_coastline_seizure=[F_coastline_test(1,180/WW+1:num6_window-180/WW) , F_coastline_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_coastline_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_coastline_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_coastline_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];



F6_coastline_seizure=FF_coastline_seizure(1,1:num6_seizure);
F7_coastline_seizure=FF_coastline_seizure(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_coastline_seizure=FF_coastline_seizure(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_coastline_seizure=FF_coastline_seizure(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_coastline_seizure=FF_coastline_seizure(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);



%%% plotting the Coastline feature

figure
plot(T_window_tot,F_coastline_test,'linewidth',3)
title(' Coastline feature extraction')
xlabel('Time (sec)')
ylabel('Coastline Amplitude')


hold on

plot(t6_seizure,F6_coastline_seizure,t7_seizure,F7_coastline_seizure,t8_seizure,F8_coastline_seizure,t9_seizure,F9_coastline_seizure,t10_seizure,F10_coastline_seizure,'linewidth',3,'color','r')

hold off

%%%% Coastline detection before post-processing

for i=1:Num_window_tot
    if F_coastline_test(i)>=threshold_coastline
        Det_coastline(i)=1;
    else
        Det_coastline(i)=0;
    end
end

%%%% Applying a 3 sec post-processing on coastline feature (2 out of 3)

for i=3:Num_window_tot
    
    if ((Det_coastline(i)==1 && Det_coastline(i-1)==1) || (Det_coastline(i)==1 && Det_coastline(i-2)==1) || (Det_coastline(i-1)==1 && Det_coastline(i-2)==1)) 
        
        Dett_coastline(i)=1;
        
    else
        Dett_coastline(i)=0;
    end
end

Dett_coastline(1)=Det_coastline(1); Dett_coastline(2)=Det_coastline(2);

    

Coastline_num_seizure=sum(Dett_coastline==1);
Coastline_num_baseline=sum(Dett_coastline==0);

FP_coastline_test=sum(BasicVector-Dett_coastline==-1);
FN_coastline_test=sum(BasicVector-Dett_coastline==1);

TP_coastline_test=Coastline_num_seizure-FP_coastline_test;
TN_coastline_test=Coastline_num_baseline-FN_coastline_test;

Sensitivity_coastline= TP_coastline_test/(TP_coastline_test+FN_coastline_test)
Specificity_coastline=TN_coastline_test/(TN_coastline_test+FP_coastline_test)
Accuracy_coastline=(Sensitivity_coastline+Specificity_coastline)/2


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Energy feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


for i=1:Num_window_tot
    Y_square{i}=(Y_final{i}).^2;
    F_energy_test(i)=mean(Y_square{i});
end


F_energy_seizure_test=[F_energy_test(1,180/WW+1:num6_window-180/WW) , F_energy_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_energy_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_energy_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_energy_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];
%%% Seperating the energy feature for each seizure occurence                  
                 
F6_energy_seizure=F_energy_seizure_test(1,1:num6_seizure);
F7_energy_seizure=F_energy_seizure_test(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_energy_seizure=F_energy_seizure_test(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_energy_seizure=F_energy_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_energy_seizure=F_energy_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);

%%% plotting results of Energy feature

figure
plot(T_window_tot,F_energy_test,'linewidth',3)
title(' Energy feature extraction')
xlabel('Time (sec)')
ylabel('Eenrgy Feature result')


hold on

plot(t6_seizure,F6_energy_seizure,t7_seizure,F7_energy_seizure,t8_seizure,F8_energy_seizure,t9_seizure,F9_energy_seizure,t10_seizure,F10_energy_seizure,'linewidth',3,'color','r')

hold off

%%%% energy detection before post-processing

for i=1:Num_window_tot
    if F_energy_test(i)>=threshold_energy
        Det_energy(i)=1;
    else
        Det_energy(i)=0;
    end
end


%%%% Applying a 3 sec post-processing on Energy feature

for i=3:Num_window_tot
    
    if ((Det_energy(i)==1 && Det_energy(i-1)==1) || (Det_energy(i)==1 && Det_energy(i-2)==1) || (Det_energy(i-1)==1 && Det_energy(i-2)==1)) 
        
        Dett_energy(i)=1;
        
    else
        Dett_energy(i)=0;
    end
end

Dett_energy(1)=Det_energy(1); Dett_energy(2)=Det_energy(2);




Energy_num_seizure=sum(Dett_energy==1);
Energy_num_baseline=sum(Dett_energy==0);

FP_energy_test=sum(BasicVector-Dett_energy==-1);
FN_energy_test=sum(BasicVector-Dett_energy==1);

TP_energy_test=Energy_num_seizure-FP_energy_test;
TN_energy_test=Energy_num_baseline-FN_energy_test;

Sensitivity_energy= TP_energy_test/(TP_energy_test+FN_energy_test)
Specificity_energy=TN_energy_test/(TN_energy_test+FP_energy_test)
Accuracy_energy=(Sensitivity_energy+Specificity_energy)/2


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Nonlinear Energy feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:Num_window_tot
    for o=2:Fs*WW-1
    
    NE_test{i}(o)=(Y_final{i}(o).^2)-Y_final{i}(o-1)*Y_final{i}(o+1);
    NE_test{i}(1)=0;
    NE_test{i}(Fs*WW)=0;
    end
end
 
for i=1:Num_window_tot
    F_NE_test(i)=mean(NE_test{i});
end


%%%% Determining baseline and seizure states for nonlinear Energy feature

F_NE_seizure_test=[F_NE_test(1,180/WW+1:num6_window-180/WW) , F_NE_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_NE_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_NE_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_NE_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];

%%%%%%%%%%%%% Determining baseline and seizure states for nonlinear Energy

F6_NE_seizure=F_NE_seizure_test(1,1:num6_seizure);
F7_NE_seizure=F_NE_seizure_test(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_NE_seizure=F_NE_seizure_test(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_NE_seizure=F_NE_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_NE_seizure=F_NE_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);


%%% plotting results of nonlinear Energy feature

figure
plot(T_window_tot,F_NE_test,'linewidth',3)
title(' Nonlinear feature extraction')
xlabel('Time (sec)')
ylabel('Nonlinear Feature result')


hold on

plot(t6_seizure,F6_NE_seizure,t7_seizure,F7_NE_seizure,t8_seizure,F8_NE_seizure,t9_seizure,F9_NE_seizure,t10_seizure,F10_NE_seizure,'linewidth',3,'color','r')

hold off

%%%% Nonlinear energy detection before processing

for i=1:Num_window_tot
    if abs(F_NE_test(i))>=threshold_NE
        Det_NE(i)=1;
    else
        Det_NE(i)=0;
    end
end

%%% Applying a 3-sec post-processing

for i=3:Num_window_tot
    
    if ((Det_NE(i)==1 && Det_NE(i-1)==1) || (Det_NE(i)==1 && Det_NE(i-2)==1) || (Det_NE(i-1)==1 && Det_NE(i-2)==1)) 
        
        Dett_NE(i)=1;
        
    else
        Dett_NE(i)=0;
    end
end

Dett_NE(1)=Det_NE(1); Dett_NE(2)=Det_NE(2);



NE_num_seizure_test=sum(Dett_NE==1);
NE_num_baseline_test=sum(Dett_NE==0);

FP_NE_test=sum(BasicVector-Dett_NE==-1);
FN_NE_test=sum(BasicVector-Dett_NE==1);

TP_NE_test=NE_num_seizure_test-FP_NE_test;
TN_NE_test=NE_num_baseline_test-FN_NE_test;

Sensitivity_NE= TP_NE_test/(TP_NE_test+FN_NE_test)
Specificity_NE=TN_NE_test/(TN_NE_test+FP_NE_test)
Accuracy_NE=(Sensitivity_NE+Specificity_NE)/2



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Variance feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:Num_window_tot
    
    F_var_test(i)=var(Y_final{i});
end


F_var_seizure_test=[F_var_test(1,180/WW+1:num6_window-180/WW) , F_var_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_var_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_var_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_var_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];

%%%%%%%%%%%%% Picking up the seizure states of each period

F6_var_seizure=F_var_seizure_test(1,1:num6_seizure);
F7_var_seizure=F_var_seizure_test(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_var_seizure=F_var_seizure_test(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_var_seizure=F_var_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_var_seizure=F_var_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);

%%% plotting results of Variance feature

figure
plot(T_window_tot,F_var_test,'linewidth',3)
title(' Variance feature extraction')
xlabel('Time (sec)')
ylabel('Variance Feature result')

hold on

plot(t6_seizure,F6_var_seizure,t7_seizure,F7_var_seizure,t8_seizure,F8_var_seizure,t9_seizure,F9_var_seizure,t10_seizure,F10_var_seizure,'linewidth',3,'color','r')

hold off

%%%% Variance detection before post processing

for i=1:Num_window_tot
    if F_var_test(i)>=threshold_var
        Det_var(i)=1;
    else
        Det_var(i)=0;
    end
end

%%% Applying a 3-sec post-processing

for i=3:Num_window_tot
    
    if ((Det_var(i)==1 && Det_var(i-1)==1) || (Det_var(i)==1 && Det_var(i-2)==1) || (Det_var(i-1)==1 && Det_var(i-2)==1)) 
        
        Dett_var(i)=1;
        
    else
        Dett_var(i)=0;
    end
end

Dett_var(1)=Det_var(1); Dett_var(2)=Det_var(2);


Var_num_seizure=sum(Dett_var==1);
Var_num_baseline=sum(Dett_var==0);

FP_var_test=sum(BasicVector-Dett_var==-1);
FN_var_test=sum(BasicVector-Dett_var==1);

TP_var_test=Var_num_seizure-FP_var_test;
TN_var_test=Var_num_baseline-FN_var_test;

Sensitivity_var= TP_var_test/(TP_var_test+FN_var_test)
Specificity_var=TN_var_test/(TN_var_test+FP_var_test)
Accuracy_var=(Sensitivity_var+Specificity_var)/2



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Minimum feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:Num_window_tot
    F_min_test(i)=min(Y_final{i});
    
end

%%%% Determining baseline and seizure states of Minimum feature

F_min_seizure_test=[F_min_test(1,180/WW+1:num6_window-180/WW) , F_min_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_min_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_min_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_min_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];

%%%%%%%%%%%%% Picking up the seizure states of each period

F6_min_seizure=F_min_seizure_test(1,1:num6_seizure);
F7_min_seizure=F_min_seizure_test(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_min_seizure=F_min_seizure_test(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_min_seizure=F_min_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_min_seizure=F_min_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);


%%% plotting results of Minimum feature

figure
plot(T_window_tot,F_min_test,'linewidth',3)
title(' Minimum feature extraction')
xlabel('Time (sec)')
ylabel('Minimum Feature result')

hold on

plot(t6_seizure,F6_min_seizure,t7_seizure,F7_min_seizure,t8_seizure,F8_min_seizure,t9_seizure,F9_min_seizure,t10_seizure,F10_min_seizure,'linewidth',3,'color','r')

hold off

%%%% Minimum detection before post-processing

for i=1:Num_window_tot
    if abs(F_min_test(i))>=threshold_min
        Det_min(i)=1;
    else
        Det_min(i)=0;
    end
end

%%% Applying a 3-sec post-processing

for i=3:Num_window_tot
    
    if ((Det_min(i)==1 && Det_min(i-1)==1) || (Det_min(i)==1 && Det_min(i-2)==1) || (Det_min(i-1)==1 && Det_min(i-2)==1)) 
        
        Dett_min(i)=1;
        
    else
        Dett_min(i)=0;
    end
end

Dett_min(1)=Det_min(1); Dett_min(2)=Det_min(2);


Min_num_seizure=sum(Dett_min==1);
Min_num_baseline=sum(Dett_min==0);

FP_min_test=sum(BasicVector-Dett_min==-1);
FN_min_test=sum(BasicVector-Dett_min==1);

TP_min_test=Min_num_seizure-FP_min_test;
TN_min_test=Min_num_baseline-FN_min_test;

Sensitivity_min= TP_min_test/(TP_min_test+FN_min_test)
Specificity_min=TN_min_test/(TN_min_test+FP_min_test)
Accuracy_min=(Sensitivity_min+Specificity_min)/2



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Maximum feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:Num_window_tot
    
    F_max_test(i)=max(abs(Y_final{i}));
    
end

%%%% Determining baseline and seizure states of Maximum feature

F_max_seizure_test=[F_max_test(1,180/WW+1:num6_window-180/WW) , F_max_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_max_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_max_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_max_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];

%%%%%%%%%%%%% Picking up the seizure states of each period

F6_max_seizure=F_max_seizure_test(1,1:num6_seizure);
F7_max_seizure=F_max_seizure_test(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_max_seizure=F_max_seizure_test(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_max_seizure=F_max_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_max_seizure=F_max_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);

%%% plotting results of Maximum feature

figure
plot(T_window_tot,F_max_test,'linewidth',3)
title(' Maximum feature result')
xlabel('Time (sec)')
ylabel('Minimum Feature magnitude')

hold on

plot(t6_seizure,F6_max_seizure,t7_seizure,F7_max_seizure,t8_seizure,F8_max_seizure,t9_seizure,F9_max_seizure,t10_seizure,F10_max_seizure,'linewidth',3,'color','r')

hold off

%%%% Max detection before post-processing

for i=1:Num_window_tot
    if abs(F_max_test(i))>=threshold_max
        Det_max(i)=1;
    else
        Det_max(i)=0;
    end
end

%%% Applying a 3-sec post-processing

for i=3:Num_window_tot
    
    if ((Det_max(i)==1 && Det_max(i-1)==1) || (Det_max(i)==1 && Det_max(i-2)==1) || (Det_max(i-1)==1 && Det_max(i-2)==1)) 
        
        Dett_max(i)=1;
        
    else
        Dett_max(i)=0;
    end
end

Dett_max(1)=Det_max(1); Dett_max(2)=Det_max(2);


Max_num_seizure=sum(Dett_max==1);
Max_num_baseline=sum(Dett_max==0);

FP_max_test=sum(BasicVector-Dett_max==-1);
FN_max_test=sum(BasicVector-Dett_max==1); 

TP_max_test=Max_num_seizure-FP_max_test;
TN_max_test=Max_num_baseline-FN_max_test;

Sensitivity_max= TP_max_test/(TP_max_test+FN_max_test)
Specificity_max=TN_max_test/(TN_max_test+FP_max_test)
Accuracy_max=(Sensitivity_max+Specificity_max)/2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Range feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:Num_window_tot
    
    F_range_test(i)=F_max_test(i)-F_min_test(i);
    
end

%%%% Determining baseline and seizure states of Range feature

F_range_seizure_test=[F_range_test(1,180/WW+1:num6_window-180/WW) , F_range_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_range_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_range_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_range_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];

%%%%%%%%%%%%% Picking up the seizure states of each period

F6_range_seizure=F_range_seizure_test(1,1:num6_seizure);
F7_range_seizure=F_range_seizure_test(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_range_seizure=F_range_seizure_test(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_range_seizure=F_range_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_range_seizure=F_range_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);


%%% plotting results of Range feature 

figure
plot(T_window_tot,F_range_test,'linewidth',3)
title(' Range feature result')
xlabel('Time (sec)')
ylabel('Range magnitude')

hold on

plot(t6_seizure,F6_range_seizure,t7_seizure,F7_range_seizure,t8_seizure,F8_range_seizure,t9_seizure,F9_range_seizure,t10_seizure,F10_range_seizure,'linewidth',3,'color','r')

hold off

%%%% Range detection before post-processing

for i=1:Num_window_tot
    if abs(F_range_test(i))>=threshold_range
        Det_range(i)=1;
    else
        Det_range(i)=0;
    end
end

%%% Applying a 3-sec post-processing

for i=3:Num_window_tot
    
    if ((Det_range(i)==1 && Det_range(i-1)==1) || (Det_range(i)==1 && Det_range(i-2)==1) || (Det_range(i-1)==1 && Det_range(i-2)==1)) 
        
        Dett_range(i)=1;
        
    else
        Dett_range(i)=0;
    end
end

Dett_range(1)=Det_range(1); Dett_range(2)=Det_range(2);

Range_num_seizure=sum(Dett_range==1);
Range_num_baseline=sum(Dett_range==0);

FP_range_test=sum(BasicVector-Dett_range==-1);
FN_range_test=sum(BasicVector-Dett_range==1);

TP_range_test=Range_num_seizure-FP_range_test;
TN_range_test=Range_num_baseline-FN_range_test;

Sensitivity_range= TP_range_test/(TP_range_test+FN_range_test)
Specificity_range=TN_range_test/(TN_range_test+FP_range_test)
Accuracy_range=(Sensitivity_range+Specificity_range)/2


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Average feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


for i=1:Num_window_tot
    
F_mean_test(i)=mean(Y_final{i});

end

F_mean_seizure_test=[F_mean_test(1,180/WW+1:num6_window-180/WW) , F_mean_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_mean_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_mean_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_mean_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];

%%%%%%%%%%%%% Picking up the seizure states of each period

F6_mean_seizure=F_mean_seizure_test(1,1:num6_seizure);
F7_mean_seizure=F_mean_seizure_test(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_mean_seizure=F_mean_seizure_test(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_mean_seizure=F_mean_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_mean_seizure=F_mean_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);

%%% plotting results of Average feature

figure
plot(T_window_tot,F_mean_test,'linewidth',3)
title(' Avg feature result')
xlabel('Time (sec)')
ylabel('Avg magnitude')

hold on

plot(t6_seizure,F6_mean_seizure,t7_seizure,F7_mean_seizure,t8_seizure,F8_mean_seizure,t9_seizure,F9_mean_seizure,t10_seizure,F10_mean_seizure,'linewidth',3,'color','r')

hold off

%%% Average detection before post-processing

for i=1:Num_window_tot
    if abs(F_mean_test(i))>=abs(threshold_mean)
        Det_mean(i)=1;
    else
        Det_mean(i)=0;
    end
end

%%% Applying a 3-sec post-processing

for i=3:Num_window_tot
    
    if ((Det_mean(i)==1 && Det_mean(i-1)==1) || (Det_mean(i)==1 && Det_mean(i-2)==1) || (Det_mean(i-1)==1 && Det_mean(i-2)==1)) 
        
        Dett_mean(i)=1;
        
    else
        Dett_mean(i)=0;
    end
end

Dett_mean(1)=Det_mean(1); Dett_mean(2)=Det_mean(2);

Mean_num_seizure=sum(Dett_mean==1);
Mean_num_baseline=sum(Dett_mean==0);

FP_mean_test=sum(BasicVector-Dett_mean==-1);
FN_mean_test=sum(BasicVector-Dett_mean==1);

TP_mean_test=Mean_num_seizure-FP_mean_test;
TN_mean_test=Mean_num_baseline-FN_mean_test;

Sensitivity_mean= TP_mean_test/(TP_mean_test+FN_mean_test)
Specificity_mean=TN_mean_test/(TN_mean_test+FP_mean_test)
Accuracy_mean=(Sensitivity_mean+Specificity_mean)/2


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Correlation feature extraction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for  i=3:Num_window_tot
    
    HV_cor_test(i)= min([F_max_test(i),F_max_test(i-1),F_max_test(i-2)]);
    HV_cor_test(1)=F_max_test(1);
    HV_cor_test(2)=F_max_test(2);
    
    LV_cor_test(i)=max([F_min_test(i) , F_min_test(i-1) , F_min_test(i-2)]);
    LV_cor_test(1)=min(F_min_test(1)); 
    LV_cor_test(2)=min(F_min_test(2));
    
    F_cor_test(i)=HV_cor_test(i)-LV_cor_test(i);
    
end


F_cor_seizure_test=[F_cor_test(1,180/WW+1:num6_window-180/WW) , F_cor_test(1,num6_window+180/WW+1:num6_window+num7_window-180/WW) , F_cor_test(1,num6_window+num7_window+180/WW+1:num6_window+num7_window+num8_window-180/WW) , F_cor_test(1,num6_window+num7_window+num8_window+180/WW+1:num6_window+num7_window+num8_window+num9_window-180/WW),...
                     F_cor_test(1,num6_window+num7_window+num8_window+num9_window+180/WW+1:num6_window+num7_window+num8_window+num9_window+num10_window-180/WW)];

%%%%%%%%%%%%% Picking up the seizure states of each period

F6_cor_seizure=F_cor_seizure_test(1,1:num6_seizure);
F7_cor_seizure=F_cor_seizure_test(1,num6_seizure+1:num6_seizure+num7_seizure);
F8_cor_seizure=F_cor_seizure_test(1,num6_seizure+num7_seizure+1:num6_seizure+num7_seizure+num8_seizure);
F9_cor_seizure=F_cor_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure);
F10_cor_seizure=F_cor_seizure_test(1,num6_seizure+num7_seizure+num8_seizure+num9_seizure+1:num6_seizure+num7_seizure+num8_seizure+num9_seizure+num10_seizure);


%%% plotting results of Correlation feature

figure
plot(T_window_tot,F_cor_test,'linewidth',3)
title(' Correlation feature result')
xlabel('Time (sec)')
ylabel('Correlation magnitude')

hold on

plot(t6_seizure,F6_cor_seizure,t7_seizure,F7_cor_seizure,t8_seizure,F8_cor_seizure,t9_seizure,F9_cor_seizure,t10_seizure,F10_cor_seizure,'linewidth',3,'color','r')

hold off

%%% Correlation detection before post-processing

for i=1:Num_window_tot
    if abs(F_cor_test(i))>=threshold_cor
        Det_cor(i)=1;
    else
        Det_cor(i)=0;
    end
end

%%% Applying a 3-sec post-processing

for i=3:Num_window_tot
    
    if ((Det_cor(i)==1 && Det_cor(i-1)==1) || (Det_cor(i)==1 && Det_cor(i-2)==1) || (Det_cor(i-1)==1 && Det_cor(i-2)==1)) 
        
        Dett_cor(i)=1;
        
    else
        Dett_cor(i)=0;
    end
end

Dett_cor(1)=Det_cor(1); Dett_cor(2)=Det_cor(2);

Cor_num_seizure=sum(Dett_cor==1);
Cor_num_baseline=sum(Dett_cor==0);

FP_cor_test=sum(BasicVector-Dett_cor==-1);
FN_cor_test=sum(BasicVector-Dett_cor==1);

TP_cor_test=Cor_num_seizure-FP_cor_test;
TN_cor_test=Cor_num_baseline-FN_cor_test;

Sensitivity_cor= TP_cor_test/(TP_cor_test+FN_cor_test)
Specificity_cor=TN_cor_test/(TN_cor_test+FP_cor_test)
Accuracy_cor=(Sensitivity_cor+Specificity_cor)/2


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Final seizure detection %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Dett_tot={Dett_coastline, Dett_energy, Dett_NE, Dett_var, Dett_min, Dett_max, Dett_range, Dett_mean, Dett_cor};

for i=1:4
    
    Dett_tot_rank{i}=Dett_tot{rank_FDP_tot(i)};
    
end

for i=1:Num_window_tot
    
    if (Dett_tot_rank{1}(i) + Dett_tot_rank{2}(i) + Dett_tot_rank{3}(i) + Dett_tot_rank{4}(i)) >=3
    
    Final_detection(i)=1;
    
    else
        
    Final_detection(i)=0;
    
    end
    
end


        

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Final results %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Final_num_seizure=sum(Final_detection==1);
Final_num_baseline=sum(Final_detection==0);

FP_Final=sum(BasicVector-Final_detection==-1);
FN_Final=sum(BasicVector-Final_detection==1);

TP_Final=Final_num_seizure-FP_Final;
TN_Final=Final_num_baseline-FN_Final;

sensitivity_Final= TP_Final/(TP_Final+FN_Final)
specificity_Final=TN_Final/(TN_Final+FP_Final)
accuracy_Final=(sensitivity_Final+specificity_Final)/2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Delay calculation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Final_detection6=Final_detection(sz6_start:sz6_end);
delay6=find(Final_detection6==1,1)-1


Final_detection7=Final_detection(sz7_start:sz7_end);
delay7=find(Final_detection7==1,1)-1

Final_detection8=Final_detection(sz8_start:sz8_end);
delay8=find(Final_detection8==1,1)-1


Final_detection9=Final_detection(sz9_start:sz9_end);
delay9=find(Final_detection9==1,1)-1

Final_detection10=Final_detection(sz10_start:sz10_end);
delay10=find(Final_detection10==1,1)-1





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Final results of FPGA implementation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% load('data_out_final_detection.txt');
% final_fpga=data_out_final_detection;
% final_fpga=transpose(final_fpga);
% 
% 
% Final_fpga_num_seizure=sum(final_fpga==1);
% Final_fpga_num_baseline=sum(final_fpga==0);
% 
% FP_Final_fpga=sum(BasicVector-final_fpga==-1);
% FN_Final_fpga=sum(BasicVector-final_fpga==1);
% 
% TP_Final_fpga=Final_fpga_num_seizure-FP_Final_fpga;
% TN_Final_fpga=Final_fpga_num_baseline-FN_Final_fpga;
% 
% sensitivity_Final_fpga= TP_Final_fpga/(TP_Final_fpga+FN_Final_fpga)
% specificity_Final_fpga=TN_Final_fpga/(TN_Final_fpga+FP_Final_fpga)
% data_reduction_ratio=num_channel_select/num_channel
% 
% %%% plotting FPGA output signal
% figure
% plot(T_window_tot,final_fpga,'linewidth',2);
% xlabel('Time (sec)')
% ylabel('Final detection')
% ylim([0 10]);
% 
% 
% load('data_out_active.txt');
% active_fpga=data_out_active;
% 
% active_stage2_fpga=sum(active_fpga==1);
% activation_ratio_fpga=active_stage2_fpga/Num_window_tot




















