gamma = linspace(1.5, 4, 100);

R1 = 1;
R2 = gamma*R1;

% signal energies
Es = R1^2*(1 + 3*gamma.^2)/4;
Eb = Es/4;

% a) there are foour relevant disance between signal points
d1 = sqrt(2)*R1*gamma./gamma;          % distance between points on inner ring
d2 = (gamma-1)*R1;                     % distance between points on diagonal
d3 = sqrt(2-sqrt(3))*gamma*R1;         % dist b/w points on outer ring
d4 = sqrt(1+gamma.^2-sqrt(3)*gamma)*R1;% dist from inner ring to off-diagonal outer

% b) translate to energy efficiency
eta1 = d1.^2./Eb;
eta2 = d2.^2./Eb;
eta3 = d3.^2./Eb;
eta4 = d4.^2./Eb;

eta_min = min([eta1; eta2; eta3; eta4]);

figure(3)
plot(gamma, eta_min,'-', ...
    gamma, eta1,'--', ...
    gamma, eta2,'--', ...
    gamma, eta3,'--', ...
    gamma, eta4,'--');
grid on
xlabel('\gamma')
ylabel('\eta')

legend('\eta_{min}', '\eta_1', '\eta_2', '\eta_3', '\eta_4')

% c) compute conditional error probabilities
EbN0 = 10^1.3;

Pe_inner = 2*Q(sqrt(eta1*EbN0/2)) + Q(sqrt(eta2*EbN0/2)) + 2*Q(sqrt(eta4*EbN0/2));
Pe_diag = Q(sqrt(eta2*EbN0/2)) + 2*Q(sqrt(eta3*EbN0/2));
Pe_off  = 2*Q(sqrt(eta3*EbN0/2)) + Q(sqrt(eta4*EbN0/2));

% d) average the conditional Pe
figure(4)
Pe = Pe_inner/4 + Pe_diag/4 + Pe_off/2;

semilogy(gamma, Pe) 
grid on
xlabel('\gamma')
ylabel('P_e')
figure(4)

