% This script is to plot the data set and test vectors in the transformed 
% space

% load the data sets
%
load s1.dat;
load s2.dat;
[m,n] = size(s1);

% Setup files for storing the results
%
FID = fopen('set1_original','w');

% adjust the data so that the required mean is obtained
%
for k=1:m
b(k,1) = s1(k,1) - 1.2299 ;
b(k,2) = s1(k,2) + 0.2579 ;
fprintf(FID,'%7.6f   %7.6f\n', b(k,1),b(k,2));
end;
mean_1 = mean(b);
[m,n] = size(s2);

% Setup files for storing the results
%
FID = fopen('set2_original','w');

% adjust the data so that the required mean is obtained
%
for k=1:m
c(k,1) = s2(k,1) - 0.6351 ;
c(k,2) = s2(k,2) + 0.8489 ;
fprintf(FID,'%7.6f   %7.6f\n', b(k,1),b(k,2));
end;
mean_2 = mean(c);

% compute the eigen values and eigen vectors
%
[ev1,eg1] = eig(cov(s1));
transform_1 = sqrtm(inv(eg1)) * transpose(ev1);
[v,d] = eig(cov(s2));
transform_2 = sqrtm(inv(d)) * transpose(v);

% Compute the new data in the transformed space
%
transformed_mat_1 = transpose(transform_1 * transpose(s1));
transformed_mat_2 = transpose(transform_2 * transpose(s2));

% Compute the mean of the transformed data
%
mean_4 = mean(transformed_mat_1);
mean_5 = mean(transformed_mat_2);

% Transform the test set into first transformed space
%
x1_transf = transpose(transform_1 * transpose(x1));
x2_transf = transpose(transform_1 * transpose(x2));
x3_transf = transpose(transform_1 * transpose(x3));
x4_transf = transpose(transform_1 * transpose(x4));

% Transform the test set into second transormed space
%
x1_2_transf = transpose(transform_2 * transpose(x1));
x2_2_transf = transpose(transform_2 * transpose(x2));
x3_2_transf = transpose(transform_2 * transpose(x3));
x4_2_transf = transpose(transform_2 * transpose(x4));

% shift the data points along the origin
%
for i=1:length(s1)
	shift_set1(i,:)=(s1(i,:) - mean_1(1,:));
end;
shift_set1_transf = transpose(transform_1*transpose(shift_set1));

% plot the tranformed data points in transformed space
%
grid on;
hold on;
box on;
plot(shift_set1_transf(:,1),shift_set1_transf(:,2),'k*');

for i=1:length(s2)
	shift_set2(i,:)=s1(i,:)-mean_2(1,:);
end;
shift_set2_transf = transpose(transform_2*transpose(shift_set2));

% plot the set2 in the transformed space
%
grid on;
zoom on;
hold on;
box on;
plot(shift_set2_transf(:,1),shift_set2_transf(:,2),'k+');

% plot the test vectors in the transformed space
%
plot(x1_transf,x1_2_transf,'kv');
plot(x2_transf,x2_2_transf,'kv');
plot(x3_transf,x3_2_transf,'kv');
plot(x4_transf,x4_2_transf,'kv');
legend('set1','set2','test set');

% end of file
%
