clear all;
close all;
clc;
class_num = 119;
sample_num = 26;
row = 60;
line = 60;
rowline = row * line;
disp('LDA to AR now begin');
right_num = [];
for train_num = 10
load ar_1;
test_num = sample_num - train_num;
train_mark = [ones(1, train_num), zeros(1, test_num)];
train_total = train_num * class_num;
train_total = train_num * class_num;
test_total = test_num * class_num;
ar_train = zeros(rowline, train_total);
ar_test = zeros(rowline, test_total);
ar_m_all = zeros(rowline,1);
ar_m = zeros(rowline,class_num);
kk = 0;
tt = 0;
for ii = 1:class_num
for jj = 1:sample_num
if train_mark(jj) == 1
kk = kk + 1;
ar_train(:, kk) = ar_1(:, ii, jj);
ar_m_all = ar_m_all + ar_train(:, kk);
ar_m(:, ii) = ar_m(:, ii) + ar_train(:, kk);
else
tt = tt + 1;
ar_test(:, tt) = ar_1(:, ii, jj);
end
end
end
ar_m = ar_m / train_num;
ar_m_all = ar_m_all / train_total;
clear ar_1 train_mark ii jj kk tt;
%-------------------- 计算Sb, Sw, St -----------------------
Sb = zeros(rowline, class_num);
Sw = zeros(rowline, train_total);
St = zeros(rowline, train_total);
kk = 0;
for ii = 1:class_num
Sb(:, ii) = ar_m(:, ii) - ar_m_all;
for jj = 1:train_num
kk = kk + 1;
Sw(:, kk) = ar_train(:, kk) - ar_m(:, ii);
St(:, kk) = ar_train(:, kk) - ar_m_all;
end
end
clear ar_m_all ar_m ii jj kk;
% -------------- calculate transform of St --------------
ss = St' * St / train_total;
rr = train_total - class_num;
[V_ss, D_ss] = eig(ss);
dd = abs(eig(ss));
[dd_value, dd_site] = sort(dd);
yy = [];
Dt = zeros(rr);
jj = 0;
for ii = train_total : -1 : train_total - rr + 1
yy = [yy; V_ss(:, dd_site(ii))'];
jj = jj + 1;
Dt(jj,jj) = dd_value(ii)^(-0.5);
end
yy = yy';
zz = St * yy * Dt;
Sbb = zz' * Sb * Sb' / class_num * zz ;
Sww = zz' * Sw * Sw' / train_total * zz;
clear Sb Sw St ss V_ss D_ss dd dd_value dd_site yy Dt ii jj;
% ----------- calculate transform of unified fisher vectors ------------
ss = inv(Sww) * Sbb;
[V_ss, D_ss] = eig(ss);
dd = abs(eig(ss));
[dd_value, dd_site] = sort(dd);
yy = [];
for ii = rr : -1 : rr - (class_num - 1) + 1
v_temp = V_ss(:, dd_site(ii))';
v_temp = v_temp / norm(v_temp);
yy = [yy; v_temp];
end
ww = zz * yy';
clear Sbb Sww ss rr V_ss D_ss dd dd_value dd_site yy v_temp zz ii;
%------------------ 分类 -----------------------
ar_train_new = ww' * ar_train;
ar_test_new = ww' * ar_test;
error = [];
for ii = 1:class_num
for jj = 1:test_num
kk = (ii-1) * test_num + jj;
res_y = ar_test_new(:, kk);
dis = [];
for pp = 1:class_num
for qq = 1:train_num
res_x = ar_train_new(:, (pp-1) * train_num + qq);
temp = (-1) * abs(res_x' * res_y) / (norm(res_x) * norm(res_y));
dis = [dis; temp];
end
end
[value, site] = min(dis);
site_new = ceil(site / train_num);
if site_new ~= ii
error = [error; (ii-1) * test_num + jj];
end
end
end
right_num = [right_num; train_num, (1.0 - length(error) / (class_num * test_num)) * 100];
right_num'
clear ar_train_new ar_test_new error res_x res_y dis temp value site site_new ii jj kk pp qq train_total test_num test_total;
end
right_num'
[m, n] = size(right_num');
result_num = sum(right_num', 2) / n;
result_num(2, :)
clear class_num sample_num row line rowline train_num m n right_num result_num;