% Non-target disturbance counteraction
% T3SRC
clear;clc;close all;
load ('D:\Matlab_Programs\Drift_program\E-nose-data\data_cho_new.mat');data_cho_new(3,:)=[];%data_cho_new=con_rmv(data_cho_new);
load ('D:\Matlab_Programs\Drift_program\E-nose-data\data_ben_new.mat');data_ben_new(3,:)=[];%data_ben_new=con_rmv(data_ben_new);
load ('D:\Matlab_Programs\Drift_program\E-nose-data\data_jiaben_new.mat');data_jiaben_new(3,:)=[];%data_jiaben_new=con_rmv(data_jiaben_new);
load ('D:\Matlab_Programs\Drift_program\E-nose-data\data_co_new.mat');data_co_new(3,:)=[];%data_co_new=con_rmv(data_co_new);
load ('D:\Matlab_Programs\Drift_program\E-nose-data\data_an_new.mat');data_an_new(3,:)=[];%data_an_new=con_rmv(data_an_new);
load ('D:\Matlab_Programs\Drift_program\E-nose-data\data_no2_new.mat');data_no2_new(3,:)=[];%data_no2_new=con_rmv(data_no2_new);
load ('D:\Matlab_Programs\Drift_program\E-nose-data\jiujing.mat');jiujing(3,:)=[];%jiujing=con_rmv(jiujing);

ind_cho=randperm(size(data_cho_new,2)); ind_cho_tr=ind_cho(1:10);ind_cho_te=ind_cho(11:end);
ind_ben=randperm(size(data_ben_new,2));ind_ben_tr=ind_ben(1:10);ind_ben_te=ind_ben(11:end);
ind_jiaben=randperm(size(data_jiaben_new,2));ind_jiaben_tr=ind_jiaben(1:10);ind_jiaben_te=ind_jiaben(11:end);
ind_co=randperm(size(data_co_new,2));ind_co_tr=ind_co(1:10);ind_co_te=ind_co(11:end);
ind_an=randperm(size(data_an_new,2));ind_an_tr=ind_an(1:10);ind_an_te=ind_an(11:end);
ind_no2=randperm(size(data_no2_new,2));ind_no2_tr=ind_no2(1:10);ind_no2_te=ind_no2(11:end);

yita=100;
Xtr=[data_cho_new(:,ind_cho_tr),data_ben_new(:,ind_ben_tr),data_jiaben_new(:,ind_jiaben_tr),data_co_new(:,ind_co_tr),data_an_new(:,ind_an_tr),data_no2_new(:,ind_no2_tr)];
Xte=[data_cho_new(:,ind_cho_te),data_ben_new(:,ind_ben_te),data_jiaben_new(:,ind_jiaben_te),data_co_new(:,ind_co_te),data_an_new(:,ind_an_te),data_no2_new(:,ind_no2_te)];

alpha=inv(Xtr'*Xtr+yita*eye(size(Xtr,2)))*Xtr'*Xtr;
Error_train=norm(Xtr-Xtr*alpha,'fro')
for i=1:size(Xtr,2)
    err=0;
    for j=1:size(alpha,2)
        err=err+norm(Xtr(:,i)-Xtr*alpha(:,j))^2;
    end
    Error_train_i(i)=sqrt(err/size(alpha,2));
end

% targets test
for i=1:size(Xte,2)
    err=0;
    for j=1:size(alpha,2)
        err=err+norm(Xte(:,i)-Xtr*alpha(:,j))^2;
    end
    Error_test_i(i)=sqrt(err/size(alpha,2));
end
plot(1:length(Error_train_i),Error_train_i,'g-.');hold on
plot(1+length(Error_train_i):length(Error_train_i)+length(Error_test_i),Error_test_i,'k-.');hold on

% non-targets test
for i=1:size(jiujing,2)
    err=0;
    for j=1:size(alpha,2)
        err=err+norm(jiujing(:,i)-Xtr*alpha(:,j))^2;
    end
    Error_alcohol_i(i)=sqrt(err/size(alpha,2));
end
plot(length(Error_train_i)+length(Error_test_i)+1:length(Error_train_i)+length(Error_test_i)+length(Error_alcohol_i),Error_alcohol_i,'r-.');hold on

legend('Target Training','Target Testing','Non-Target Disturbance (alcohol)')
xlabel('Sample Index');
ylabel('Total Representation Error (TRE)')

error_by=min(Error_test_i):0.0001:max(Error_test_i);
for i=1:length(error_by)
E_boundary=error_by(i);

Error=[Error_test_i,Error_alcohol_i];
label=[zeros(1,length(Error_test_i)),ones(1,length(Error_alcohol_i))]; % 0-target; 1-nontargets
pred_label=ones(1,length(label));
pred_label(find(Error<=E_boundary))=0;
total_rate(i)=length(find(pred_label==label))/length(label)*100;
target_rate(i)=length(find(pred_label(1:length(Error_test_i))==label(1:length(Error_test_i))))/length((Error_test_i))*100;
nontarget_rate(i)=length(find(pred_label(1+length(Error_test_i):end)==label(1+length(Error_test_i):end)))/length((Error_alcohol_i))*100;

% ROC: Receive Operating Characteristic Curve
% TPR (true positive rate): positive class has been correctly recognized as positive class
TPR(i)=nontarget_rate(i)/100;
% FPR (false positive rate): negitive class has been wrongly recongized as positive class
FPR(i)=1-target_rate(i)/100;
end
figure
subplot(1,2,1)
plot(error_by,total_rate,'g-.');hold on
plot(error_by,target_rate,'k-.');hold on
plot(error_by,nontarget_rate,'r-.');
legend('total recognition rate','target recognition rate','nontarget recognition rate')
xlabel('Error Boundary')
ylabel('Recognition rate (%)')
title ('Recognition Rate Curve')
subplot(1,2,2)
plot(FPR,TPR)
xlabel('False Positive Rate (FPR)');
ylabel('True Positive Rate (TPR)');
title ('Receive Operating Characteristic Curve (ROC-curve)')

load('A32.txt')
A32(:,10)=[];A32(:,9)=[];A32(:,3)=[];
A32=A32/4095;
for i=1:size(A32,2)
    A32_n(:,i)=filtering(A32(:,i));
end
figure
subplot(1,2,1)
plot(A32_n(:,3),'r');hold on;
plot(A32_n(:,4),'k');hold on;
plot(A32_n(:,6),'b');hold on;
plot(A32(:,7),'g');hold on;
xlabel('Observation points')
ylabel('Normalized sensor response')

load('Inter_11.txt')
Inter_11(:,10)=[];Inter_11(:,9)=[];Inter_11(:,3)=[];
Inter_11=Inter_11/4095;
for i=1:size(Inter_11,2)
    Inter_11_n(:,i)=filtering(Inter_11(:,i));
end

subplot(1,2,2)
plot(Inter_11_n(:,3),'r');hold on;
plot(Inter_11_n(:,4),'k');hold on;
plot(Inter_11_n(:,6),'b');hold on;
plot(Inter_11(:,7),'g');hold on;
xlabel('Observation points')
ylabel('Normalized sensor response')

load('b11.txt')
b11(:,10)=[];b11(:,9)=[];b11(:,3)=[];
b11=b11/4095;
for i=1:size(b11,2)
    b11_n(:,i)=filtering(b11(:,i));
end


A32_n=A32_n';
for i=1:size(A32_n,2)
    err=0;
    for j=1:size(alpha,2)
        err=err+norm(A32_n(:,i)-Xtr*alpha(:,j))^2;
    end
    Error_smoke_i(i)=sqrt(err/size(alpha,2));
end

Inter_11_n=Inter_11_n';
for i=1:size(Inter_11_n,2)
    err=0;
    for j=1:size(alpha,2)
        err=err+norm(Inter_11_n(:,i)-Xtr*alpha(:,j))^2;
    end
    Error_perfume_i(i)=sqrt(err/size(alpha,2));
end

b11_n=b11_n';
for i=1:size(b11_n,2)
    err=0;
    for j=1:size(alpha,2)
        err=err+norm(b11_n(:,i)-Xtr*alpha(:,j))^2;
    end
    Error_mixture_i(i)=sqrt(err/size(alpha,2));
end

figure
subplot(231)
response_smoke=mean([A32_n(3,:);A32_n(4,:);A32_n(6,:);A32_n(7,:)]);
plot(response_smoke,'k')
subplot(234)
plot(Error_smoke_i,'g');hold on
subplot(232)
response_perfume=mean([Inter_11_n(3,:);Inter_11_n(4,:);Inter_11_n(6,:);Inter_11_n(7,:)]);
plot(response_perfume,'k')
subplot(235)
plot(Error_perfume_i,'g');hold on
subplot(233)
response_mixture=mean([b11_n(3,:);b11_n(4,:);b11_n(6,:);b11_n(7,:)]);
plot(response_mixture,'k')
subplot(236)
plot(Error_mixture_i,'g')

