% file: build_transforms.m
%
% this script requires no arguments
%
% this script builds the transform matrices using the whitening transformation
% as given in:
%     K. Fukunaga, "Introduction to Statistical Pattern Recognition," Academic
%     Press, San Diego, Ca, 1990, pp. 24-30.
% a separate transform is computed for each set and the set is then transformed
% into the new space. once in the new space, the means and distances are
% recomputed and the sample points are reclassified.
%

% get the covariance matrices of the euclidean sets
%
cov_euclid_set_1 = cov(euclid_set_1)
cov_euclid_set_2 = cov(euclid_set_2)

% get the eigenvalues and eigenvectors of each of the covariance matrices
%
[eig_vec_1, eig_val_1] = eig(cov_euclid_set_1)
[eig_vec_2, eig_val_2] = eig(cov_euclid_set_2)

% create the unnormalized transform
%
transform_1 = inv(sqrtm(eig_val_1))*transpose(eig_vec_1)
transform_2 = inv(sqrtm(eig_val_2))*transpose(eig_vec_2)

% transform set1 and set 2 to the new space
%
set_1_xform = transpose(transform_1*transpose(euclid_set_1));
set_2_xform = transpose(transform_2*transpose(euclid_set_2));
sample_set_1_xform = transpose(transform_1*transpose(euclid_sample_set));
sample_set_2_xform = transpose(transform_2*transpose(euclid_sample_set));

% compute the new covariance matrices
%
cov_xform_set_1 = cov(set_1_xform)
cov_xform_set_2 = cov(set_2_xform)

% calculate the new means of the distribution
%
mean_xform_set_1 = mean(set_1_xform)
mean_xform_set_2 = mean(set_2_xform)

% determine the distance between each set and the sample points and classify
%
for samp_num = 1 : num_sample_points
	
	% compute the distance for each sample point
	%
	sample_distance_xform_set_1 = dist(mean_xform_set_1, transpose(sample_set_1_xform(samp_num,:)));
	sample_distance_xform_set_2 = dist(mean_xform_set_2, transpose(sample_set_2_xform(samp_num,:)));

	% display the distances to the user
	%
	fprintf('The dist from sample point %d (%.4f, %.4f) to set 1 in the xformed space is: %.4f\n', samp_num,  euclid_sample_set(samp_num,1), euclid_sample_set(samp_num,2), sample_distance_xform_set_1);
	fprintf('The dist from sample point %d (%.4f, %.4f) to set 2 in the xformed space is: %.4f\n', samp_num,  euclid_sample_set(samp_num,1), euclid_sample_set(samp_num,2), sample_distance_xform_set_2);
	
	% assign the sample vector to a class if distance 2 is less than 
	% distance 1 then classify into set 2 otherwise classify into set 1
	%
	if ((sample_distance_xform_set_2 - sample_distance_xform_set_1) < -1E-4) 
		fprintf('Sample point %d (%.4f, %.4f) belongs to set 2\n', samp_num,  euclid_sample_set(samp_num,1), euclid_sample_set(samp_num,2));
	else
		fprintf('Sample point %d (%.4f, %.4f) belongs to set 1\n', samp_num,  euclid_sample_set(samp_num,1), euclid_sample_set(samp_num,2));
	end

	% print a blank line to separate the samples
	%
	fprintf('\n');
end

% compute y = T(x-u)
%
for i=1:1:100
	x_minus_mean_1(i,:) = euclid_set_1(i,:) - mean_set_1;
	x_minus_mean_2(i,:) = euclid_set_2(i,:) - mean_set_2;
end

% transformed the mean-subtracted set into the new space
%
xform_x_sub_mean_1 = transpose(transform_1 * transpose(x_minus_mean_1));
xform_x_sub_mean_2 = transpose(transform_2 * transpose(x_minus_mean_2));

% compute the means of each transformed set and display
%
xform_moved_mean_1 = mean(xform_x_sub_mean_1);
fprintf('mean of T(x-u) for set 1 = [%.3f, %.3f]\n', xform_moved_mean_1(:,1), xform_moved_mean_1(:,2));
xform_moved_mean_2 = mean(xform_x_sub_mean_2);
fprintf('mean of T(x-u) for set 2 = [%.3f, %.3f]\n', xform_moved_mean_2(:,1), xform_moved_mean_2(:,2));

% end of script
%
