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 -1 -1]';
b=[-1 1 -1]';
c=[-1 -1 1]';
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:10  
    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-3,tr_label);
            train_T=sa(m-2:m,tr_label);
            test_P=sa(1:m-3,te_label);
            test_T=sa(m-2:m,te_label);
            %NN****************************************
             HIDDEN=ceil(sqrt(0.43*(2*SamplePoint+2)+0.12+2.54*SamplePoint+0.77+0.35+0.51));
            net=newff(minmax(train_P),[HIDDEN,3],{'logsig','purelin'},'trainlm');  %创建BP网络
            %设置BP网络训练参数
            net.trainParam.show=1;
            net.trainParam.lr=1;
            net.trainParam.epochs=20000;
            net.trainParam.goal=0.01;
            net.trainParam.showWindow=true;
            
            %训练BP网络
            net=train(net,train_P,train_T);  %XX是训练数据，YY是训练目标
            
            ytrain=sim(net,train_P);
            ytest=sim(net,test_P);
            
            tr_l=[];
            ytr_l=[];
            te_l=[];
            yte_l=[];
            %计算出训练目标的标志
            for i=1:size(tr_label,2)
                if train_T(1,i)==1
                    tr_l=[tr_l 1];
                else
                    if train_T(2,i)==1
                        tr_l=[tr_l 2];
                    else 
                        tr_l=[tr_l 3];
                    end
                end
            end
            %计算出得出的训练结果的标志
            for i=1:size(tr_label,2)
                if ytrain(1,i)>ytrain(2,i)
                   if ytrain(1,i)>ytrain(3,i)
                       ytr_l=[ytr_l 1];
                   else
                       ytr_l=[ytr_l 3];
                   end
                else
                    if ytrain(2,i)>ytrain(3,i)
                        ytr_l=[ytr_l 2];
                    else
                        ytr_l=[ytr_l 3];
                    end
                end
            end
            %计算出测试目标的标志
             for i=1:size(te_label,2)
                if test_T(1,i)==1
                    te_l=[te_l 1];
                else
                    if test_T(2,i)==1
                        te_l=[te_l 2];
                    else 
                        te_l=[te_l 3];
                    end
                end
            end
             %计算出得出的测试结果的标志
            for i=1:size(te_label,2)
                if ytest(1,i)>ytest(2,i)
                   if ytest(1,i)>ytest(3,i)
                       yte_l=[yte_l 1];
                   else
                       yte_l=[yte_l 3];
                   end
                else
                    if ytest(2,i)>ytest(3,i)
                        yte_l=[yte_l 2];
                    else
                        yte_l=[yte_l 3];
                    end
                end
            end
            
            
            gas1=0;
            wr1=0;
            gas2=0;
            wr2=0;
            gas3=0;
            wr3=0;
            
            for i=1:size(te_label,2)
                switch te_l(i)
                    case 1
                         gas1=gas1+1;
                        if yte_l(i)~=1
                            wr1=wr1+1;
                        end
                    case 2
                         gas2=gas2+1;
                        if yte_l(i)~=2
                            wr2=wr2+1;
                        end
                        
                    case 3
                         gas3=gas3+1;
                        if yte_l(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;tt mean(ress(:,2)) mean(ress(:,3)) mean(ress(:,4))];
        
end
mean10=[mean(aress(:,2)) mean(aress(:,3)) mean(aress(:,4))]
