% The demo.m is from the following paper:

%Anti-Drift in E-nose: A Subspace Projection Approach with Drift Reduction

%leizhang@cqu.edu.cn

clear all
clc
close all;

% load data

% Batch1 is regarded as source domain
% Batch(i) is regarded as target domain, where i=2,3,...,10
% The dataset from Vergara et al. in UCSD, the detailed content can be foud in the following paper:
% Chemical gas sensor drift compensation using classifier ensembles

load batchs

batchS = batch1_p(:,4:2:end);
batchT = batch2_p(:,4:2:end);

batchS_label=batch1_p(:,1);
batchT_label=batch2_p(:,1);

batchS=batchS./repmat(sqrt(sum(batchS.^2,1)),size(batchS,1),1);
batchT=batchT./repmat(sqrt(sum(batchT.^2,1)),size(batchT,1),1);

MbatchS=mean(batchS);
MbatchT=mean(batchT);
Ms=MbatchS';
Mt=MbatchT';

batchS=batchS';
batchT=batchT';

counter=0;
for i=0
    lambda=10^i;
    %% obtain the common subspace
     neweigenvector=DRCA(Ms,Mt,batchS,batchT,lambda);
     
     for d=128
         P=neweigenvector(:,1:d);
         batchS_P=batchS'*P;
         batchT_P=batchT'*P;
         for m=3
             c=10^m;
             for n=0
                 gama=10^n;
                 counter=counter+1;
                 
                 %% SVM:
                 
                 cmd=[' -c ',num2str(c),' -g ',num2str(gama)];
                 model = svmtrain(batchS_label,batchS_P,cmd);  
                 [predict_label_train, accuracy_train] = svmpredict(batchS_label,batchS_P, model); 
                 [predict_label_test, accuracy_test] = svmpredict(batchT_label,batchT_P, model);
                 d1=diff([predict_label_train';batchS_label']);
                 N1 = numel(find(d1==0)); 
                 d2=diff([predict_label_test';batchT_label']);
                 N2 = numel(find(d2==0));
                 accur_train=N1/size(batchS_P,1);
                 accur_test=N2/size(batchT_P,1)
                 
                  result(counter,1)=accur_train;
                  result(counter,2)=accur_test;                
             end
         end
     end
end
% the best recognition accuracy
[max1,index1]=max(result(:,2))