# Example 8.1
# Compuatation of error probability if the receiver is (a) a balanced homodyne or (b) a balanced heterodyne
# Page no. 354
import math
# Given data
Po=5.0; # Lunch peak power
fl=50.0; # Fiber loss
G=30.0; # Preamplifier Gain
f=10*10.0**9;
n=1.5;
h=6.63*10**-34; # Planck constant
c=3*10**8.0; # Velocity of light
lambdaa=1550.0*10**-9;
q=1.6*10**-19; # Electron charge
R=0.9;
# Signal calculation
Pr=Po-fl+G;
Pr=10**(Pr/10)*10**-3;
Tb=1/(f);
E=Pr*Tb;
f1=c/lambdaa;
G=10**(G/10);
r=n*h*f1*(G-1);
#rs=q*I;
N=r+(q/(2*R));
Nh=r/2+(q/(2*R));
# Error probability
# (a) For a balanced homodyne receiver with PSK signal
Ps=1/2.0*math.erfc(math.sqrt(E/N));
E1=E/2;
# If the signal is OOK
Pso=1/2.0*math.erfc(math.sqrt(E1/(2*N)));
#(b) For a balanced heterodyne receiver with PSK signal
Pb=1/2.0*math.erfc(math.sqrt(E/(2*Nh)));
#E1=E/2;
# If the signal is OOK
Pbo=1/2.0*math.erfc(math.sqrt(E1/(4*Nh)));
#Displaying the result in command window
print "\n For a balanced homodyne receiver with PSK signal = ",round(Ps*10**9,2)," X 10**-9 "
print "\n For a balanced homodyne receiver with PSK signal If the signal is OOK, = ",round(Pso*10**3,2)," X 10**-3"
print "\n For a balanced heterodyne receiver with PSK signal = ",round(Pb*10**9,3)," X 10**-9"
print "\n For a balanced heterodyne receiver with PSK signal If the signal is OOK,= ",round(Pbo*10**3,2)," X 10**-3"
# The answer vary due to round off error
# Example 8.3
# Calculation of the maximum transmission distance.
# Page no 394
import math
#Given data
p=3.0; # Peak power
tb=40.0*10**9; # Bit rate
c=3.0*10**8; # Velocity of light
lambdaa=1550.0*10**-9; # Operating frequency
l=0.2; # Loss
d=80; # Distance
G=16 # Gain
h=6.626*10**-34 # Planck constant
n=1.0;
pb=10.0**-5; # Error probability
l1=80*10**3; # N spans
# The maximum transmission distance
p=p+10*math.log10(1/2.0);
p=10**(p/10.0)*10**-3;
t=1/(tb);
E=p*t;
f=c/lambdaa;
fl=l*d;
G=10**(G/10.0);
r=n*h*f*(G-1); # Calculation is wrong in book.
#pb=1/2*(exp(-(E/r)));
N=-(E/(math.log(2*pb)*r));
L=N*l1;
# Displaying results in the command window
print "\n The maximum transmission distance = ",round(L*10**-3,2)," km"
# In the book PSD per amplifier calcualation is wrong, therefore final answer is wrong.
# Example 8.6
# To find the mean number of signal photons required in a shot noise-limited coherent communication system based on OOK for the following cases: (i) balanced homodyne receiver; (ii)balanced heterodyne receiver (a) a balanced homodyne or (b) a balanced heterodyne
# Page no. 384
import math
from scipy.special import erfinv
# Given data
Pb=1*10**-9; #Error probability
neta=1; #quantum efficiency
#a)for balanced homodyne receiver
Ns=(erfinv(1-(2*neta*Pb)))**2;
#(b)for balanced heterodyne receiver
Ns1=(erfinv(1-(2*neta*Pb))*math.sqrt(2))**2;
#Displaying the result in command window
print "\n For a balanced homodyne receiver with PSK signal =",round(Ns)
print "\n For a balanced heterodyne receiver with PSK signal = ",round(Ns1)