from __future__ import division
from numpy import sqrt, pi, log10
n1=1.47 #refractive index of fiber
n=1 #refractive index of air
r=((n1-n)/(n1+n))**2 #computing fraction of light reflected
loss=-10*log10(1-r) #loss
total_loss=2*loss
print "r = %.3f, which means %.1f percent of the transimitted light is reflected at one interface" %(r,r*100)
print "Total loss is %.3f dB" %(total_loss)
#answer in the book for total loss of fiber is wrong.
from numpy import arccos
n1=1.47 #refractive index of fiber
n=1 #refractive index of air
d=40*10**-6 #core diameter
y=4*10**-6 #lateral dispalcement
a=d/2 #computing core radius
eta_lateral = (16*(n1/n)**2)/(pi*(1+(n1/n))**4)*(2*arccos(y/(2*a))-(y/a)*(1-(y/(2*a))**2)**0.5) #computing eta_lateral with air gap
loss=-10*log10(eta_lateral) #computing loss when air gap is present
eta_lateral1=(2*arccos(y/(2*a))-(y/a)*(1-(y/(2*a))**2)**0.5)/pi #computing eta_lateral without air gap
loss1=-10*log10(eta_lateral1) #computing loss when air gap is not present
print "loss with air gap is %.2f dB.\nloss with no air gap is %.2f dB." %(loss,loss1)
#answer in the book for loss with air gap is wrong
n1=1.48 #refractive index of fiber
n=1 #refractive index of air
theta=10 #angle in degree
NA1=0.3
NA2=0.6
eta_angular1= (16*(n1/n)**2)/((1+(n1/n))**4)*(1-((n*theta*pi/180)/(pi*NA1))) #computing eta angular
eta_angular2= (16*(n1/n)**2)/((1+(n1/n))**4)*(1-((n*theta*pi/180)/(pi*NA2))) #computing eta angular
loss1=-10*log10(eta_angular1) #computing loss
loss2=-10*log10(eta_angular2) #computing loss
print "\nLoss when NA is %.1f is %.2f dB.\nLoss when NA is %.1f is %.2f dB." %(NA1,loss1,NA2,loss2)
from numpy import exp
d=1*10**-6 #lateral displacement
W=4.95*10**-6 #MFD
Lsm_lat= -10*log10(exp(-(d/W)**2)) #computing loss
print "Insertion loss is %.2f dB." %(Lsm_lat)
lamda=1.3*10**-6 #wavelength
theta=1 #angle in degree
n2=1.465 #cladding refractive index
W=4.95*10**-6 #MFD
Lsm_ang= -10*log10(exp(-(pi*n2*W*(theta*pi/180)/lamda)**2)) #computing loss
print "Insertion loss is %.2f dB." %(Lsm_ang)
p1=50*10**-6
p2=0.003*10**-6
p3=25*10**-6
p4=26.5*10**-6
EL=10*log10(p1/(p3+p4)) #computing excess loss
IL13=10*log10(p1/p3) #computing insertion loss
IL14=10*log10(p1/p4) #computing insertion loss
ct=10*log10(p2/p1) #computing cross talk
sr=(p3/(p3+p4))*100 #computing split ratio
print """Excess loss is %.2f dB.
Insertion loss from port 1 to port 3 is %.2f dB.
Insertion loss from port 1 to port 4 is %.2f dB.
cross talk is %.2f dB.\nSplit ratio is %.2f percent""" %(EL,IL13,IL14,ct,sr )
#Printing and calculation error in the book.Minus sign is not printed in the answer of excess loss.
#P1 is taken 25 instead of 50 while calculating cross talk.
N=16 #Number of ports
Pin=1*10**-3 #input power
Pout=12*10**-6 #output power
split_loss=10*log10(N) #computing split loss
excess_loss=10*log10(Pin/(Pout*N)) #computing excess loss
total_loss=split_loss+excess_loss #computing total loss
insertion_loss= 10*log10(Pin/Pout) #computing insertion loss
print "Total loss is %.2f dB.\nInsertion loss is %.2f dB." %(total_loss,insertion_loss)
#answer in the book for Total loss & insertion loss are wrong.