% file: /u0/ngan/course_work/speech/hw2/hw2.m
%
% name: Julie Ngan
% course: EE8993 Speech Recognition
% homework: 2
%
% this project uses principle component analysis on two 2-dimensional
% data sets to find a decision region
%

% load in the data sets
%
load set1.dat;
load set2.dat;

% define the x = y axis for the decision region
%
x_eq_y = [-4,-4; 4,4];

% plot the original data sets and the decision region
%
figure
plot (x_eq_y(:,1), x_eq_y(:,2), 'k-');
hold on 
plot (set1(:,1), set1(:,2), 'k*');
hold on
plot (set2(:,1), set2(:,2), 'ko');

% find the means of the two data sets
%
u1 = mean(set1);
u2 = mean(set2);

% load in the 4 test points
%
x1 = [-1 -1];
x2 = [0 0];
x3 = [0.5 0.5];
x4 = [0.5 -0.5];

% find the Euclidean distances between the test points and the mean of 
% the first data set
%
dist11 = dist (x1, u1');
dist21 = dist (x2, u1');
dist31 = dist (x3, u1');
dist41 = dist (x4, u1');

% find the Euclidean distances between the test points and the mean of 
% the second data set
%
dist12 = dist (x1, u2');
dist22 = dist (x2, u2');
dist32 = dist (x3, u2');
dist42 = dist (x4, u2');

% assign classes to the test points
%

fprintf ('classifying sets 1 & 2\n');

if dist11 < dist12
	fprintf ('x1 belongs to set 1\n');
else 
	fprintf ('x1 belongs to set 2\n');
end;

if dist21 < dist22
	fprintf ('x2 belongs to set 1\n');
else 
	fprintf ('x2 belongs to set 2\n');
end;

if dist31 < dist32
	fprintf ('x3 belongs to set 1\n');
else 
	fprintf ('x3 belongs to set 2\n');
end;

if dist41 < dist42
	fprintf ('x4 belongs to set 1\n\n');
else 
	fprintf ('x4 belongs to set 2\n\n');
end;

% find the eigen vectors and eigen values for the covariances of the
% two data sets for the calculation of the transformation matrix
%
[set1_eig_vec,set1_eig_val] = eig (cov (set1));
[set2_eig_vec,set2_eig_val] = eig (cov (set2));

% calculate lambda values
%
lambda1 = set1_eig_val ^ -0.5;
lambda2 = set2_eig_val ^ -0.5;

% calculate the transformation matrix
%
T1 = lambda1 * set1_eig_vec';
T2 = lambda2 * set2_eig_vec';

% find the transformation of the test points
%
y11 = (T1 * x1')';
y12 = (T1 * x2')';
y13 = (T1 * x3')';
y14 = (T1 * x4')';

y21 = (T2 * x1')';
y22 = (T2 * x2')';
y23 = (T2 * x3')';
y24 = (T2 * x4')';

% find the transformation of the data sets to produce new data sets
%
new_set1 = T1 * set1';
new_set2 = T2 * set2';

% plot the transformed data sets
%
figure
plot (new_set1(1,:), new_set1(2,:), 'k*');
hold
plot (new_set2(1,:), new_set2(2,:), 'ko');

% find the new mean for the new data sets
%
new_u1 = mean (new_set1');
new_u2 = mean (new_set2');

% recalculate the Euclidean distance between the new test points and 
% the new data sets
%
new_dist11 = dist (y11, new_u1');
new_dist21 = dist (y12, new_u1');
new_dist31 = dist (y13, new_u1');
new_dist41 = dist (y14, new_u1');

new_dist12 = dist (y21, new_u2');
new_dist22 = dist (y22, new_u2');
new_dist32 = dist (y23, new_u2');
new_dist42 = dist (y24, new_u2');

% assign classes to the test points
%

fprintf ('classifying new sets 1 & 2\n');

if new_dist11 < new_dist12
	fprintf ('x1 belongs to set 1\n');
else 
	fprintf ('x1 belongs to set 2\n');
end;

if new_dist21 < new_dist22
	fprintf ('x2 belongs to set 1\n');
else 
	fprintf ('x2 belongs to set 2\n');
end;

if new_dist31 < new_dist32
	fprintf ('x3 belongs to set 1\n');
else 
	fprintf ('x3 belongs to set 2\n');
end;

if new_dist41 < new_dist42
	fprintf ('x4 belongs to set 1\n\n');
else 
	fprintf ('x4 belongs to set 2\n\n');
end;

% define the difference threshold
%
DIFF = 0.01;

% define beginning index
%
index = 1;

% loop through (-5,-5) to (5,5) with increment of 0.1 and calculate the
% Euclidean distance of the transformed sample points and the transformed
% means to find the decision region
%
for x = -5:0.1:5,
	for y = -5:0.1:5,
		point = [x,y];
		d1 = dist ((T1 * point')', new_u1');
		d2 = dist ((T2 * point')', new_u2');
		if abs(d2-d1) <= DIFF
			ppoints(index,:) = [x,y];
			index = index + 1;
		end;
	end;
end;

% plot the new decison region with the untransformed data sets
%
figure
plot (ppoints(:,1), ppoints(:,2), 'k-')
hold
plot (set1(:,1), set1(:,2), 'k*')
plot (set2(:,1), set2(:,2), 'ko')
		
% demonstration orthonormalization by showing the covariance matrix
% are identity matrix
%
fprintf ('demonstrate orthonormalization\n');
cov (new_set1')
cov (new_set2')

% find the matrix y = T (x - u)
%
for j = 1:100
	super_set1(:,j) = new_set1(:,j)-new_u1';
	super_set2(:,j) = new_set2(:,j)-new_u2';
end;

% plot the orthonormalized data sets
%
figure
plot (super_set1(1,:), super_set1(2,:), 'k*')
figure
plot (super_set2(1,:), super_set2(2,:), 'ko')

%
% end of file
