function [accuracy] = linear_svm(c2Fea,label,param)
% This function is to classify object images using linear svm, including
% training phase and test phase. 
% Input:  c2Fea    -- feature matrix, each column represents a feature
%                     vector
%         label    -- class label of each feature
%         param    -- see default settings below
% Output: accuracy -- classification accurary of each trials
% Written by Jimei Yang
% Oct. 31, 2009
if ~isfield(param,'tr_num')
    param.tr_num = 30;
end
if ~isfield(param,'nRounds')
    param.nRounds = 5;
end

classes = unique(label);
nClass = length(classes);

accuracy = zeros(param.nRounds, nClass);

for ii = 1:param.nRounds
    fprintf('Round: %d...', ii);
    tic;
    tr_idx = [];
    ts_idx = [];
    
    for jj = 1:nClass
        idx_label = find(label == classes(jj));
        num = length(idx_label);
        
        idx_rand = randperm(num);
        
        tr_idx = [tr_idx; idx_label(idx_rand(1:param.tr_num))];
        ts_idx = [ts_idx; idx_label(idx_rand(param.tr_num+1:end))];
    end
    
    tr_fea = c2Fea(:, tr_idx)';
    tr_label = label(tr_idx);
    
    ts_fea = c2Fea(:, ts_idx)';
    ts_label = label(ts_idx);
    
%     clear c2Fea;
    
    model = train( tr_label, tr_fea, '-s 4' );
    [ pred_label, a ] = predict( ts_label, ts_fea, model );
    acc = zeros(length(classes), 1);
    
    for jj = 1 : nClass
        c = classes(jj);
        if c == 68
            c = 68;
        end
        idx = find(ts_label == c);
        curr_pred_label = pred_label(idx);
        curr_gnd_label = ts_label(idx);
        acc(jj) = length(find(curr_pred_label == curr_gnd_label))/length(idx);
    end
    
    accuracy(ii,:) = acc;
    disp(['--accuracy:',num2str(mean(accuracy(ii,:))),' in ',num2str(toc),' secconds.']);
end