clear
close all

%控制变量选择
SamplePoint=10;%样本中每个传感器5000点里提取的点数
flag_gas=1;%哪一种气体  1 HCHO 2 nox 3 co
YB=[];
ress=[];
aress=[];
temp_label=[];
if flag_gas==1
    load HCHOalldata2602.mat
    load HCHOalldata2620.mat
    alldata2602=HCHOalldata2602;
    alldata2620=HCHOalldata2620;
    snum=100;
else
    if flag_gas==2
        load NOXalldata2602.mat
        load NOXalldata2620.mat
        alldata2602=NOXalldata2602;
        alldata2620=NOXalldata2620;
        snum=113;
    else
        load COalldata2602.mat
        load COalldata2620.mat
        alldata2602=COalldata2602;
        alldata2620=COalldata2620;
        snum=96;
    end
    
end


    Space=floor(5000/SamplePoint);
    for i=Space:Space:Space*SamplePoint
        YB=[YB;alldata2620(i,:)];
    end
    Space=floor(5000/SamplePoint);
    for i=Space:Space:Space*SamplePoint
        YB=[YB;alldata2602(i,:)];
    end

    YB=[YB;alldata2602(5001:5003,:)];

%到此为止获得的YB为截取出来的样本，倒数三行为温度湿度和浓度
%标准化
[m n]=size(YB);
YB(1:m-3,:)=YB(1:m-3,:)/5000;
YB(m-2:m-1,:)=YB(m-2:m-1,:)/4095;
%******************************************************************************************************

%*********************************************************************************************
% for SamplePoint=5:15
%for l=4
%     for c=9
%    L=10^l;
%    C=10^c;
%    for cyclenum=1:110
%      %截取样本
%      %ICA
%      %EML                                             此部分为程序伪代码
%      %保存某一折的误差（不断叠加）
%    end
%    %保存110折SVR的均值值，并清除上个变量
%     end
%     %保存对应的SamplePoint L C 110折SVR均值
% end
%end
%
%*****************************************************************************************************
for  tt=1:10
for c=9
    for l=4
        L=10^l;
        C=10^c;
        Weight=rand(SamplePoint*2+2,L);
        Bias1=rand(1,L);
        for cyclenum=1:snum
            %截取样本******************************
            tr_label=[1:cyclenum-1 cyclenum+1:snum];
            te_label=cyclenum;
            if cyclenum==1
                tr_label=(2:snum);
                te_label=1;
            end
            if cyclenum==snum
                tr_label=(1:snum-1);
                te_label=snum;
            end
            train_P=YB(1:m-1,tr_label);
            train_T=YB(m,tr_label);
            test_P=YB(1:m-1,te_label);
            test_T=YB(m,te_label);          
            %Elm****************************************
            train_P=train_P';
            test_P=test_P';
            train_T=train_T';
            test_T=test_T';
            
            [N n]=size(train_P);
%             Weight=rand(n,L);
%             Bias1=rand(1,L);
            x=train_P*Weight+Bias1(ones(N*1,1),:);
            H=1./(1+exp(-x));
           % I=diag(ones(1,N));
            if L>=N
                I=diag(ones(1,N));
            Y=H'*inv(H*H'+I/C)*train_T;
            else
                I=diag(ones(1,L));
             Y=inv(H'*H+I/C)*H'*train_T;    
            end
            ytrain=H*Y;
            [N1 n]=size(test_P);
            x=test_P*Weight+Bias1(ones(N1*1,1),:);
            H=1./(1+exp(-x));
            ytest=H*Y;
            
            errortrain=(ytrain-train_T)./train_T*100;
            errortest=(ytest-test_T)./test_T*100;
            aerrortrain=abs(errortrain);  %相对训练误差的绝对值-->aerrortrain
            aerrortest=abs(errortest);  %相对测试误差的绝对值-->aerrortest
            %相对误差
            aver_errtrain=mean(aerrortrain,1)  %求出训练误差的绝对值平均值-->aver_errtrain
            aver_errtest=mean(aerrortest,1) %求出测试误差的绝对值平均值-->aver_errtest
            %绝对误差
            aberrtrain=mean(abs(ytrain-train_T));
            aberrtest=mean(abs(ytest-test_T));
            %mse
            msetrain=mean((ytrain-train_T).^2);
            msetest=mean((ytest-test_T).^2);
            
            
            
            %保存某一折的误差（不断叠加）
            ress=[ress;cyclenum aver_errtrain aver_errtest aberrtrain aberrtest msetrain msetest];
            
        end
        %保存对应的LC留一法平均误差
        aress=[aress;tt c l mean(ress(:,2)) mean(ress(:,3)) mean(ress(:,4)) mean(ress(:,5)) mean(ress(:,6)) mean(ress(:,7))];
        
        ress=[];
    end    
end
end
aaress=[c l mean(aress(:,5)) std(aress(:,5)) mean(aress(:,7)) std(aress(:,7)) mean(aress(:,9)) std(aress(:,9))];