from __future__ import division
from numpy import log, sqrt, pi, log10
P=10**-9 #probability of error
eta=1 #ideal detector
h=6.626*10**-34 #plank's constant
c=3*10**8 #speed of light
lamda=1*10**-6 #wavelength
B=10**7 #bit rate
Mn = -log(P)
print "The quantum limit at the receiver to maintain bit error rate 10**-9 is (%.1f*h*f)/eta." %Mn
f=c/lamda
Popt= 0.5*Mn*h*f*B/eta #computing optical power
Popt_dB = 10 * log10(Popt) + 30 #optical power in dbm
Popt=Popt*10**12
print "Minimum incident optical power is %.1f W or %.1f dBm." %(Popt,Popt_dB)
SN_dB=60 #signal to noise ratio
h=6.626*10**-34 #plank's constant
c=3*10**8 #speed of light
lamda=1.3*10**-6 #wavelength
eta=1
B=6.5*10**6 #Bandwidth
SN=10**(SN_dB/10)
f=c/lamda
Popt= 2*SN*h*f*B/eta #computing optical power
Popt_dB = 10 * log10(Popt) + 30 #optical power in dbm
Popt=Popt*10**6
print "Incident power required to get an SNR of 60 dB at the receiver is %.4f microWatt or %.3f dBm" %(Popt,Popt_dB)
#Calculation error in the book.They have take SN as 10**5 while calculating, which has lead to an error in final answer
#answer in the book 198.1nW and -37.71 dBm
lamda=0.85*10**-6
h=6.626*10**-34 #plank's constant
c=3*10**8 #speed of light
q=1.6*10**-19 #charge of electron
eta=65/100 #quantum efficiency
P0=300*10**-9 #optical power
Id=3.5 #dark current
B=6.5*10**6 #bandwidth
K=1.39*10**-23 #Boltzman constant
T=293 #temperature
R=5*10**3 #load resister
Ip= 10**9*eta*P0*q*lamda/(h*c)
Its=10**9*(2*q*B*(Ip+Id))
Its=sqrt(Its)
print "rms shot noise current is %.2f nA." %(Its)
It= 4*K*T*B/R
It=sqrt(It)
It=It*10**9
print "Thermal noise is %.2f nA." %(It)
#answer given in book for Thermal noise is wrong.
lamda=0.85*10**-6
h=6.626*10**-34 #plank's constant
c=3*10**8 #speed of light
q=1.6*10**-19 #charge of electron
eta=65/100 #quantum efficiency
P0=300*10**-9 #optical power
Id=3.5 #dark current
B=6.5*10**6 #bandwidth
K=1.39*10**-23 #Boltzman constant
T=293 #temperature
R=5*10**3 #load resister
F_dB=3 #noise figure
F=10**(F_dB/10)
Ip=10**9*eta*P0*q*lamda/(h*c)
Its=10**9*(2*q*B*(Ip+Id))
It1= 4*K*T*B*F/R
SN= Ip**2/(Its+It1)
SN_dB=10*log10(SN)
SN=SN
print "SNR is %.2e or %.2f dB." %(SN,SN_dB)
#answer given in the book is wrong.
Cd=7*10**-12
B=9*10**6
Ca=7*10**-12
R=(2*3.14*Cd*B)**-1
B1=(2*3.14*R*(Cd+Ca))**-1
R=R/1000
B1=B1/10**6
print """Thus for 9MHz bandwidth maximum load resistance is %.2f Kohm
Now if we consider input capacitance of following amplifier Ca then Bandwidth is %.2fMHz
Maximum post detection bandwidth is half."""%(R,B1)
#answer for resistance in the book is wrong
w=25*10**-6 #width
v=3*10**4 #velocity
t=w/v #computing drift time
BW=(2*pi*t)**-1 #computing bandwidth
rt=1/BW #response time
rt=rt*10**9
print "Maximum response time is %.2f ns." %(rt)
#Answer in the book is wrong.