filestr='train_data_1.asc';
%filestr='test_data_1.asc';
dims=9;
xdims = 9;
[A]=readata(dims,filestr);
filestr='train_labels_1.asc';
%filestr='test_labels_1.asc';
X=A(:,1:dims);
%[pv1,pv2,Y]= absorp(pv1,pv2,Y,W,X);
fprintf('Subtracting local mean...\n');
%X(winsize^2,:)=(X(winsize^2-2,:)+X(winsize^2-1,:))/2+rand(1,size(X,2));
%Z = X';
%testplot1;
%----------------------------------------------------------------------
% Reduce the dimension and whiten at the same time!
%----------------------------------------------------------------------

% Calculate the eigenvalues and eigenvectors of covariance matrix.
fprintf ('Calculating covariance...\n');
rdim=5;
[n1 n2]=size(A);
X=A(:,1:n2)';
X = X-ones(size(X,1),1)*mean(X);
%X = X./(ones(size(X,1),1)*max(abs(X)));
covarianceMatrix = X*X'/size(X,2);
[E, D] = eig(covarianceMatrix);
% Sort the eigenvalues and select subset, and whiten
fprintf('Reducing dimensionality and whitening...\n');
[dummy,order] = sort(diag(-D));
E = E(:,order(1:rdim));
d = diag(D); 
d = real(d.^(-0.5));
D = diag(d(order(1:rdim)));
%X = D*E'*X;
X=E'*X;
whiteningMatrix = D*E';
dewhiteningMatrix = E*D^(-1);
A=X';
[B]=readata(1,filestr);
A=[A B];
filename='breast5.dat';
%filename='breast3v.dat';
fid=fopen(filename,'w');
[xx yy]=size(A);
stringp = '';
for i = 1:rdim+1
  stringp = [stringp '%8.7f '];
end
stringp = [stringp '\n'];
for i =1:xx
   fprintf(fid,stringp,A(i,:));
end
fclose(fid);

%for testing set
filestr='test_data_1.asc';
dims=9;
xdims = 9;
[A]=readata(dims,filestr);
X=A(:,1:dims);
%[pv1,pv2,Y]= absorp(pv1,pv2,Y,W,X);
fprintf('Subtracting local mean...\n');
%X(winsize^2,:)=(X(winsize^2-2,:)+X(winsize^2-1,:))/2+rand(1,size(X,2));
%Z = X';
%testplot1;
%----------------------------------------------------------------------
% Reduce the dimension and whiten at the same time!
%----------------------------------------------------------------------

% Calculate the eigenvalues and eigenvectors of covariance matrix.
fprintf ('Calculating covariance...\n');
[n1 n2]=size(A);
X=A(:,1:n2)';
X = X-ones(size(X,1),1)*mean(X);
%X = X./(ones(size(X,1),1)*max(abs(X)));
%covarianceMatrix = X*X'/size(X,2);
%[E, D] = eig(covarianceMatrix);
% Sort the eigenvalues and select subset, and whiten
%fprintf('Reducing dimensionality and whitening...\n');
%[dummy,order] = sort(diag(-D));
%E = E(:,order(1:rdim));
%d = diag(D); 
%d = real(d.^(-0.5));
%D = diag(d(order(1:rdim)));
X = D*E'*X;
%X=E'*X;
%whiteningMatrix = D*E';
%dewhiteningMatrix = E*D^(-1);
A=X';
filestr='test_labels_1.asc';
[B]=readata(1,filestr);
A=[A B];
filename='breast5v.dat';
fid=fopen(filename,'w');
[xx yy]=size(A);
stringp = '';
for i = 1:rdim+1
  stringp = [stringp '%8.7f '];
end
stringp = [stringp '\n'];
for i =1:xx
   fprintf(fid,stringp,A(i,:));
end
fclose(fid);
