clear
clc

%定义变量区
SamplePoint=5;%样本中每个传感器5000点里提取的点数
HCHO=[];%初始化甲醛样本集
NO=[];%初始化no2样本集
CO=[];%初始化co样本集
ress=[];%记录结果变量
aress=[];%记录结果变量
temp_label=[];%标签的暂存变量
count=0;
%%********************************************
%HCHO
%提取tgs2620的点
load HCHOalldata2620.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    HCHO=[HCHO;HCHOalldata2620(i,:)];
end
%提取tgs2602的点
load HCHOalldata2602.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    HCHO=[HCHO;HCHOalldata2602(i,:)];
end
%提取温湿度和浓度
HCHO=[HCHO;HCHOalldata2602(5001:5003,:)];


%NOX
%提取tgs2620的点
load NOX2620alldata.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    NO=[NO;NOX2620alldata(i,:)];
end
%提取tgs2602的点
load NOX2602alldata.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    NO=[NO;NOX2602alldata(i,:)];
end
%提取温湿度和浓度
NO=[NO;NOX2602alldata(5001:5003,:)];

%CO
%提取tgs2620的点
load co2620alldatamatrix.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    CO=[CO;co2620alldatamatrix(i,:)];
end
%提取tgs2602的点
load co2602alldatamatrix.mat
Space=floor(5000/SamplePoint);
for i=Space:Space:Space*SamplePoint
    CO=[CO;co2602alldatamatrix(i,:)];
end
%提取温湿度和浓度
CO=[CO;co2602alldatamatrix(5001:5003,:)];


%组合所有样本  5003*（100+113+96）
sa=[HCHO NO CO];

%标准化
[m n]=size(sa);
sa(1:m-3,:)=sa(1:m-3,:)/5000;
sa(m-2:m-1,:)=sa(m-2:m-1,:)/4095;
a=1;
b=2;
c=3;
label=[a(:,ones(1,100)) b(:,ones(1,113)) c(:,ones(1,96))];
sa(m,:)=[];
sa=[sa;label];
[m n]=size(sa);
load('5折分组.mat')
for tt=1 
    for c=8
        for g=-2
            bestc=2^(c);
            bestg=2^(g);
    ress=[];
        for cyclenum=1:5
            count=0;
            %截取样本******************************
            switch cyclenum
                case 1 
                    tr_label=[gl2,gl3,gl4,gl5];
                    te_label=gl1;
                  
                case 2 
                    tr_label=[gl1,gl3,gl4,gl5];
                    te_label=gl2;
                   
                case 3
                    tr_label=[gl1,gl2,gl4,gl5];
                    te_label=gl3;
                   
                   
                case 4
                    tr_label=[gl1,gl2,gl3,gl5];
                    te_label=gl4;
               
                    
                case 5
                    tr_label=[gl1,gl2,gl3,gl4];
                    te_label=gl5;
         
                
            end
            
            train_P=sa(1:m-1,tr_label);
            train_T=sa(m,tr_label);
            test_P=sa(1:m-1,te_label);
            test_T=sa(m,te_label);
            %SVM****************************************
            train_P=train_P';
            test_P=test_P';
            train_T=train_T';
            test_T=test_T';
            Method_option.plotOriginal = 0;
            Method_option.scale = 0;
            Method_option.plotScale = 0;
            Method_option.pca = 0;
            Method_option.type = 1;
%             bestc=256;
%             bestg=8;
            [predict_label,accuracy] = SVC(train_T,train_P,test_T,test_P,Method_option,bestc,bestg);
           % [predict_Y,mse,r]=SVR(train_T,train_P,test_T,test_P,Method_option);
            ytrain=predict_label{1};
            ytest=predict_label{2};
            
            gas1=0;
            wr1=0;
            gas2=0;
            wr2=0;
            gas3=0;
            wr3=0;
            
            for i=1:size(te_label,2)
                switch test_T(i)
                    case 1
                         gas1=gas1+1;
                        if ytest(i)~=1
                            wr1=wr1+1;
                        end
                    case 2
                         gas2=gas2+1;
                        if ytest(i)~=2
                            wr2=wr2+1;
                        end
                        
                    case 3
                         gas3=gas3+1;
                        if ytest(i)~=3
                            wr3=wr3+1;
                        end
                end
                    
            end
            ge1=(gas1-wr1)/gas1*100;
            ge2=(gas2-wr2)/gas2*100;
            ge3=(gas3-wr3)/gas3*100;

            %保存某一折的误差（不断叠加）
            ress=[ress;cyclenum ge1 ge2 ge3];
        end
        aress=[aress;c g mean(ress(:,2)) mean(ress(:,3)) mean(ress(:,4))];
        end
    end
end
%mean10=[mean(aress(:,2)) mean(aress(:,3)) mean(aress(:,4))]

