%maxgauss.m 
function [res]=maxgauss(x,M) 
CX=cov(x'); 
[E,D]=eig(CX);
y=real(E)*x; 
y=y-repmat(mean(y')',1,length(y));
x1=y(1,:)/sqrt(var(y(1,:)));
y1=y(2,:)/sqrt(var(y(2,:)));
%Absolute value of the kurtosis is minimized as follows. %Differentiation of kurtosis of the data with mean=0 %and var=1
W=rand(2,2);
W(:,1)=W(:,1)/sqrt(sum(W(:,1).^2));
W(:,2)=W(:,2)-(W(:,2)'*W(:,1))*W(:,1); W(:,2)=W(:,2)/sqrt(sum(W(:,2).^2));
COL1=[kurt(x1)]; COL2=[kurt(y1)]; WTRACT=[]; x2=x1;y2=y1; 
for iter=1:100 
    W(:,1)=W(:,1)-gradient(x2); 
    W(:,2)=W(:,2)-gradient(y2); 
    W(:,1)=W(:,1)/sqrt(sum(W(:,1).^2)); 
    W(:,2)=W(:,2)-(W(:,2)'*W(:,1))*W(:,1); W(:,2)=W(:,2)/sqrt(sum(W(:,2).^2));
    X=W'*[x1;y1]; 
    x2=X(1,:);
    y2=X(2,:);
    COL1=[COL1 kurt(x2)];
    COL2=[COL2 kurt(y2)];
end
figure
subplot(2,1,1)
plot(COL1)
subplot(2,1,2)
plot(COL2)
title('Convergence graph for maximizing Gaussianity')
CZ=cov(real([x2;y2])')
[E,D]=eig(real(CZ));
res=real(E)*(real(D)^(1/2))*[x2;y2];
res=res+repmat(M',1,length(res)); 
figure
subplot(2,2,1) 
plot(x(1,:),x(2,:),'*') 
subplot(2,2,2) 
plot(x1,y1,'*')
subplot(2,2,3) 
plot(x2,y2,'*')
subplot(2,2,4)
plot(real(res(1,:)),real(res(2,:)),'*')
