clear
clc
load scenario_colored
% standard
out_plot = sequence;


%% SSD2
tau = 1;
t_samp = 1;
factor = 4;  % Choose 3-5

n_wind = round(factor*tau/t_samp,0);

sig = [];
no_wind = size(out_plot,2) - n_wind;

REC = [];

for i = 0:no_wind
    sig = out_plot((1+i):(n_wind+i));
    t = 1:n_wind;
    
    % Estimates
    sig_dif = [];
    for j = 2:n_wind
        sig_dif(j-1) = sig(j) - sig(j-1);
    end
    
    m = mean(sig_dif);
    mu =(1/n_wind)*(sum(sig) - m*sum(t));
    pre_var = (sig - m*t - mu).^2;
    variance = sqrt((1/(n_wind - 2))*sum(pre_var));
    
    % Statistics: two-tailed Student's test
    signlev_lb = 0.01/2;
    signlev_ub = 1 - signlev_lb;
    tcrit_lb = tinv(signlev_lb, n_wind);
    tcrit_ub = tinv(signlev_ub, n_wind);
    
    sig = sig - mu;
    prob = [];
    for n = 1:n_wind
        if sig(n) <= tcrit_ub*variance & sig(n) >= tcrit_lb*variance
            prob = [prob, 1];
        else
            prob = [prob, 0];
        end
    end
    SS = sum(prob)/n_wind;
    REC = [REC; 1+i, n_wind+i, SS];
end

%% SSD3
lambda1 = 0.2; lambda2 = 0.1; lambda3 = 0.1;
Xf = out_plot(1); var1 = 0; var2 = 0; 
R = []; R_crit = 2.0;

for i = 2:size(out_plot,2)
    var1 = lambda2*(out_plot(i) - Xf)^2 + (1-lambda2)*var1;
    var2 = lambda3*(out_plot(i) - out_plot(i-1))^2 + (1-lambda3)*var2;
    Xf = lambda1*out_plot(i) + (1-lambda1)*Xf;
    R = [R, (2-lambda1)*var1/var2];
end

%% Accuracy Check and Compare
Time = [30,5,40,20,30,40,80,40,30,20,40,5,30];
Time_cum = [Time(1)];
for i = 2:size(Time,2)
    Time_cum(i) = Time_cum(i-1) + Time(i);
end

wlength_CNN = 20;
wnum_CNN = size(standard,2) - wlength_CNN + 1;
standard = standard(wlength_CNN:end);

% SSD2
start_SSD2 = size(REC,1) - wnum_CNN + 1;
SSD2 = REC(start_SSD2:end,3)';

for i = 1:size(SSD2,2)
    if SSD2(i) >= 0.85
        SSD2(i) = 1;
    else
        SSD2(i) = 0;
    end
end

sum(abs(standard - SSD2))
false_posSSD2 = 0; false_negSSD2 = 0;
position_SSD2 = [];
for j = 1:size(SSD2,2)
     if standard(j) == 0 & SSD2(j) == 1
         false_posSSD2 = false_posSSD2 + 1;
         position_SSD2 = [position_SSD2, j+wlength_CNN-1];
     elseif standard(j) == 1 & SSD2(j) == 0
         false_negSSD2 = false_negSSD2 + 1;
         position_SSD2 = [position_SSD2, j+wlength_CNN-1];
     end
end

% SSD3
start_SSD3 = size(R,2) - wnum_CNN + 1;
R = R(start_SSD3:end);
for i = 1:size(R,2)
     if R(i) <= R_crit
         R(i) = 1;
     else
         R(i) = 0;
     end
end

sum(abs(standard - R))
false_posSSD3 = 0; false_negSSD3 = 0;
position_SSD3 = [];
for j = 1:size(R,2)
     if standard(j) == 0 & R(j) == 1
         false_posSSD3 = false_posSSD3 + 1;
         position_SSD3 = [position_SSD3, j+wlength_CNN-1];
     elseif standard(j) == 1 & R(j) == 0
         false_negSSD3 = false_negSSD3 + 1;
         position_SSD3 = [position_SSD3, j+wlength_CNN-1];
     end
end

% CNN
load pred1 
CNN1 = double(nTEST1);
load pred2 
CNN2 = double(nTEST2);
load pred3
CNN3 = double(nTEST3);

CNN = [CNN1,CNN2,CNN3];

for i = 1:size(CNN,2)
     if CNN(i) == 0
         CNN(i) = 1;
     else
         CNN(i) = 0;
     end
 end
 
sum(abs(standard - CNN))
false_posCNN = 0; false_negCNN = 0;
position_CNN = [];
for j = 1:size(CNN,2)
     if standard(j) == 0 & CNN(j) == 1
         false_posCNN = false_posCNN + 1;
         position_CNN = [position_CNN, j+wlength_CNN-1];
     elseif standard(j) == 1 & CNN(j) == 0
         false_negCNN = false_negCNN + 1;
         position_CNN = [position_CNN, j+wlength_CNN-1];
     end
 end
 
total = [sum(abs(standard - SSD2)), sum(abs(standard - R)), sum(abs(standard - CNN))]
false_pos = [false_posSSD2, false_posSSD3, false_posCNN]
false_neg = [false_negSSD2, false_negSSD3, false_negCNN]

%load sequence_ori

dot_SSD2 = []; 
for i = 1:size(position_SSD2,2)
dot_SSD2 = [dot_SSD2, sequence_ori(position_SSD2(i))];
end
err_SSD2 = zeros(1,length(Time_cum)); err_SSD2(1) = sum(position_SSD2 <= Time_cum(1));
for j = 2:size(err_SSD2,2)
    err_SSD2(j) = sum(position_SSD2 <= Time_cum(j))-sum(position_SSD2 <= Time_cum(j-1));
end

dot_SSD3 = [];
for i = 1:size(position_SSD3,2)
dot_SSD3 = [dot_SSD3, sequence_ori(position_SSD3(i))];
end
err_SSD3 = zeros(1,length(Time_cum)); err_SSD3(1) = sum(position_SSD3 <= Time_cum(1));
for j = 2:size(err_SSD3,2)
    err_SSD3(j) = sum(position_SSD3 <= Time_cum(j))-sum(position_SSD3 <= Time_cum(j-1));
end

dot_CNN = [];
for i = 1:size(position_CNN,2)
 dot_CNN = [dot_CNN, sequence_ori(position_CNN(i))];
end
err_CNN = zeros(1,length(Time_cum)); err_CNN(1) = sum(position_CNN <= Time_cum(1));
for j = 2:size(err_CNN,2)
     err_CNN(j) = sum(position_CNN <= Time_cum(j))-sum(position_CNN <= Time_cum(j-1));
end

subplot(3,1,1)
plot(sequence_ori)
hold on
scatter(position_SSD3,dot_SSD3)
subplot(3,1,2)
plot(sequence_ori)
hold on
scatter(position_SSD2,dot_SSD2)
subplot(3,1,3)
plot(sequence_ori)
hold on
scatter(position_CNN,dot_CNN)

err = [err_SSD2; err_SSD3; err_CNN];
