ba=round(rand);
bb=round(rand);

if ba==1
    ca=1;
elseif ba==0
    ca=-1;
end
if bb==1
    cb=1;
elseif bb==0
    cb=-1;
end

C=[ca;cb]
ga=[1;1];
gb=[1;-1];
G=[ga,gb];
S=G*C;
eta1=0.8+0.2*rand;
eta2=0.8+0.2*rand;
phi1=-pi/4+pi/2*rand;
phi2=-pi/4+pi/2*rand;
ha=eta1*exp(i*phi1);
hb=eta2*exp(i*phi2);
H=[ha,hb;ha,hb]
phi=2*pi*randn(2,1);
W=0.1*exp(i*phi).*randn(2,1);
X=H.*G*C+W;

c00=0;
c01=0;
c10=0;
c11=0;

if abs(real(X(1)))<=1
    c01=c01+1;
    c10=c10+1;
elseif real(X(1))<-1

   c00=c00+1;
else real(X(1))>1
    c11=c11+1;
end

if abs(real(X(2)))<=1
    c00=c00+1;
    c11=c11+1;
elseif real(X(2))<-1
    c01=c01+1;
else real(X(2))>1
    c10=c10+1;
end

if max([c00 c01 c10 c11]) == c00
    c1 = -1
    c2 = -1
    sa1=-1;
    sa2=-1;
    sb1=-1;
    sb2=1;
elseif max([c00 c01 c10 c11]) == c01
    c1 = -1
    c2 = 1
    sa1=-1;
    sa2=-1;
    sb1=1;
    sb2=-1;
elseif max([c00 c01 c10 c11]) == c10
    c1 = 1
    c2 = -1
    sa1=1;
    sa2=1;
    sb1=-1;
    sb2=1;
else max([c00 c01 c10 c11]) == c11
    c1 = 1
    c2 = 1
    sa1=1;
    sa2=1;
    sb1=1;
    sb2=-1;
end

syms p1;
syms p2;
eqn=0.5*abs(X(1))^2*sin(2*angle(X(1))-2*p1)+0.5*abs(X(2))^2*sin(2*angle(X(2))-2*p1)+abs(X(1))*abs(X(2))*sin(angle(X(1))+angle(X(2))-2*p1)==0;
P1=solve(eqn,p1);
phi01=double(P1);

eqn=0.5*abs(X(1))^2*sin(2*angle(X(1))-2*p2)+0.5*abs(X(2))^2*sin(2*angle(X(2))-2*p2)-abs(X(1))*abs(X(2))*sin(angle(X(1))+angle(X(2))-2*p2)==0;
P2=solve(eqn,p2);
phi02=double(P2);

if min(abs(phi01)) == abs(phi01(1))
    phia=real(phi01(1))
elseif min(abs(phi01)) == abs(phi01(2))
    phia=real(phi01(2))
end

if min(abs(phi02)) == abs(phi02(1))
    phib=real(phi02(1))
elseif min(abs(phi02)) == abs(phi02(2))
    phib=real(phi02(2))
end

etaa=0.5*(abs(X(1))*sa1*cos((angle(X(1)))-phia)+abs(X(2))*sa2*cos((angle(X(2))-phia)))
etab=0.5*(abs(X(1))*sb1*cos((angle(X(1)))-phib)+abs(X(2))*sb2*cos((angle(X(2))-phib)))

figure
p1=plot(real(X(1)),imag(X(1)),'bx');
hold on
p2=plot(real(X(2)),imag(X(2)),'rx');
legend('x_1','x_2')
axis([-3 3 -2 2])
line([0,0], ylim, 'Color', 'k', 'LineWidth', 0.5); % Draw line for Y axis.
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 0.5); % Draw line for X axis.
line([1,1], ylim, 'Color', 'r', 'LineWidth', 0.5); 
line([-1,-1], ylim, 'Color', 'r', 'LineWidth', 0.5); % Draw line for decision regions
xlabel('Re');
ylabel('Im');
title('Decision regions')
legend([p1 p2],{'x1','x2'})
hold off