from __future__ import division
from numpy import sqrt, pi, log10
L=10 #fiber length in km
Pin=150*10**-6 #input power
Pout=5*10**-6 #output power
ln=20 #length of optical link
interval=1 #splices after interval of 1 km
l=1.2 #loss due to 1 splice
attenuation=10*log10(Pin/Pout)
alpha=attenuation/L
attenuation_loss=alpha*20
splices_loss=(ln-interval)*l
total_loss=attenuation_loss+splices_loss
power_ratio=10**(total_loss/10)
print "Signal attenuation is %.2f dBs.\nSignal attenuation is %.3f dB/Km.\nTotal loss in 20 Km fiber is %.2f dbs.\nTotal attenuation is %.2f dBs.\ninput/output ratio is %0.e." %(attenuation,alpha,attenuation_loss,total_loss,power_ratio)
# Answer wrong for last part.
from numpy import exp
beta_c=8*10**-11 #isothermal compressibility
n=1.46 #refractive index
P=0.286 #photoelastic constat
k=1.38*10**-23 #Boltzmnn constant
T=1500 #temperature
L=1000 #length
lamda=1000*10**-9 #wavelength
gamma_r = 8*(3.14**3)*(P**2)*(n**8)*beta_c*k*T/(3*(lamda**4)) #computing coefficient
attenuation=exp(-gamma_r*L) #computing attenuation
print "Attenuation due to Rayleigh scattering is %.3f." %(attenuation)
beta_c=7*10**-11 #isothermal compressibility
n=1.46 #refractive index
P=0.29 #photoelastic constat
k=1.38*10**-23 #Boltzmnn constant
T=1400 #temperature
L=1000 #length
lamda=0.7*10**-6 #wavelength
gamma_r = 8*(3.14**3)*(P**2)*(n**8)*beta_c*k*T/(3*(lamda**4)) #computing coefficient
attenuation=exp(-gamma_r*L) #computing attenuation
gamma_r=gamma_r
print "Raleigh Scattering corfficient is %.3e per meter" %(gamma_r)
#Attenuation is not calcualted in textbook"
print "Attenuation due to Rayleigh scattering is %.3f" %(attenuation)
#answer for Raleigh Scattering corfficient in the book is given as 0.804d-3, deviation of 0.003d-3
d=5 #core diameter
alpha=0.4 #attenuation
B=0.5 #Bandwidth
lamda=1.4 #wavelength
PB=4.4*10**-3*d**2*lamda**2*alpha*B #computing threshold power for SBS
PR=5.9*10**-2*d**2*lamda*alpha #computing threshold power for SRS
PB=PB*10**3
PR=PR*10**3
print "Threshold power for SBS is %.1f mW.\nThreshold power for SRS is %.3f mW." %(PB,PR)
#NOTE - Calculation error in the book while calculating threshold for SBS.
#Also, while calculating SRS, formula is taken incorrectly,
#Bandwidth is multiplied in second step, which is not in the formula.")
n1=1.5 #refractive index of core
delta=0.03/100 #relative refractive index
lamda=0.82*10**-6 #wavelength
n2=sqrt(n1**2-2*delta*n1**2) #computing cladding refractive index
Rc=(3*n1**2*lamda)/(4*3.14*(n1**2-n2**2)**1.5) #computing critical radius
Rc=Rc*10**3
print "Critical radius is %.f micrometer." %(Rc)
n1=1.45 #refractive index of core
delta=3.0/100 #relative refractive index
lamda=1.5*10**-6 #wavelength
a=5*10**-6 #core radius
n2=sqrt(n1**2-2*delta*n1**2) #computing cladding refractive index
Rc=(3*n1**2*lamda)/(4*3.14*(n1**2-n2**2)**0.5) #computing critical radius for single mode
Rc=Rc*10**6
print "Critical radius is %.2f micrometer" %(Rc)
lamda_cut_off= 2*3.14*a*n1*sqrt(2*delta)/2.405
RcSM= (20*lamda/(n1-n2)**1.5)*(2.748-0.996*lamda/lamda_cut_off)**-3 #computing critical radius for single mode
RcSM=RcSM*10**6
print "Critical radius for single mode fiber is %.2f micrometer." %(RcSM)
#Calculation error in the book.(2.748-0.996*lamda/lamda_cut_off)**-3 in this term raised to -3 is not taken in the book.
lamda=1550*10**-9
lamda0=1.3*10**-6
s0=0.095
Dt=lamda*s0/4*(1-(lamda0/lamda)**4) #computing material dispersion
Dt=Dt*10**9
print "Material dispersion at 1550 nm is %.1f ps/nm/km" %Dt
#answer in the book is wrong.
tau=0.1*10**-6 #pulse broadning
dist=20*10**3 #distance
Bopt=1/(2*tau) #computing optical bandwidth
Bopt=Bopt*10**-6
dispertion=tau/dist #computing dispersion
dispertion=dispertion*10**12
BLP=Bopt*dist #computing Bandwidth length product
BLP=BLP*10**-3
print "optical bandwidth is %d MHz.\nDispersion per unit length is %d ns/km.\nBandwidth length product is %d MHz.km." %(Bopt,dispertion,BLP)
RSW=0.0012 #relative spectral width
lamda=0.90*10**-6 #wavelength
L=1 #distance in km (assumed)
P=0.025 #material dispersion parameter
c=3*10**5 #speed of light in km/s
M=10**3*P/(c*lamda) #computing material dispersion
sigma_lamda=RSW*lamda
sigmaM=sigma_lamda*L*M*10**7 #computing RMS pulse broadning
sigmaB=25*L*M*10**-3
print "Material dispersion parameter is %.2f ps/nm/km.\nRMS pulsr broadning when sigma_lamda is 25 is %.1f ns/km.\nRMS pulse broadning is %.2f ns/km." %(M,sigmaB,sigmaM)
#answer in the book for RMS pulse broadning is wrong.
L=10 #length of optical link
n1=1.49 #refractive index
c=3*10**8 #speed of light
delta=1.0/100 #relative refractive index
delTS=L*n1*delta/c #computing delay difference
delTS=delTS*10**12
sigmaS=L*n1*delta/(2*sqrt(3)*c) #computing rms pulse broadning
sigmaS=sigmaS*10**12
B=1/(2*delTS) #computing maximum bit rate
B=B*10**3
B_acc=0.2/(sigmaS) #computing accurate bit rate
B_acc=B_acc*10**3
BLP=B_acc*L #computing Bandwidth length product
print "Delay difference is %d ns.\nRMS pulse broadning is %.1f ns.\nBit rate is %.1f Mbit/s.\nAccurate bit rate is %.3f Mbits/s.\nBandwidth length product is %.1f MHz.km" %(delTS,sigmaS,B,B_acc,BLP)
#answer for maximum bit rate is given as 1.008 Mb/s, deviation of 0.008 Mb/s.