% 目录:E:\other\matlab 2007a\work\DCT\DCT_original\network.m and DCTForClassification.m
%此处代码相当于网格法来搜索这两个参数,Cvalue取10个值分别是100,200到1000.Gamma取60个值,分别是0.00002
%到0.00009;0.0002到0.0009;0.002到0.009;依次类推
% This code has been checked by Libing Wang. It should not have any problem.
Cvalue = 0;
Fold = 4; % 4 fold cross validation
C=0;
G=0;
Loocv=10;
for i = 1:10
    Cvalue=Cvalue+100;
    Gamma=0.00001;
    
    for j =1:60
        if Gamma>=1
            Gamma=Gamma+1;
        elseif Gamma>=0.1 
            Gamma=Gamma+0.1;
        elseif Gamma>=0.01
            Gamma=Gamma+0.01;
        elseif Gamma>=0.001 
            Gamma=Gamma+0.001;
        elseif Gamma>=0.0001
            Gamma=Gamma+0.0001;     
        elseif Gamma>=0.00001;
            Gamma=Gamma+0.00001;
        end
              
        SVMParameter=sprintf('-c %f -g %f -v %d',Cvalue,Gamma,Fold);
        LoocvTemp = svmtrain(gnd_Train, fea_Train, SVMParameter);        
        if LoocvTemp > Loocv
           Loocv=LoocvTemp;
           C=Cvalue;
           G=Gamma;
        end
    end
end
SVMParameter = sprintf('-c %f -g %f',C,G);
model = svmtrain(gnd_Train, fea_Train,SVMParameter);
[predict_label, rate, dec_values] = svmpredict(gnd_Test, fea_Test, model); 
accuracy = rate(1) %recognition rate