from __future__ import division
import cmath
from math import pi
#Given
z0=50 #ohm
zg=50 #ohm
l=0.25 #m
f=4e9 #hz
zl=100 #ohm
vg=10 #V
w=2*pi*f #rad/sec
c=3e8 #m/s
#(i) Voltage and current at any point
tg=(zg-z0)/(zg+z0)
tl=(zl-z0)/(zl+z0)
vi=z0*vg/(z0+zg) #V
print 'Voltage at any point = %0.2f V' %(vi)
ii=vg/(2*z0) #A
print 'Current at any point = %0.2f A'%(ii)
#(ii) Voltage at generator end
#Taking z=1
z=1
bet=w/c
vz=(vg/2)*cmath.exp(-1J*bet*(z+l))*(1+(tl*cmath.exp(2*1J*bet*z))) #V
print 'Voltage at generator end ={:.3f}'.format(vz),'V'
iz=ii*cmath.exp(-1J*bet*(z+l))*(1-(tl*cmath.exp(2*1J*bet*z))) #A
vz1=(vg/2)*cmath.exp(-1J*bet*(z+l))*(1+(tl*cmath.exp(2*1J*bet*z))) #V
#Voltage at load end, z=0
z11=0
vl=(vg/2)*cmath.exp(-1J*bet*l)*(1+(tl*cmath.exp(2*1J*bet*z11))) #V
print 'Voltage at load end ={:.3f}'.format(vl),'V'
#(iii) Reflection coefficient
zx=0.25
tz=tl*cmath.exp(1J*2*bet*zx)
print 'Reflection coefficient:{:.3f}'.format(tz)
#(iv) VSWR
p=(1+tl)/(1-tl)
print 'VSWR:' ,p
#(v) Average power delivered to the load
vl=20/3
pl0=vl**2/(2*zl) #W
print 'Average power delivered to the load = %0.2f W' %pl0
from math import log
#Given
pm=3
pl=4
l=24 #cm
l1=l/100 #m
#Attenuation
tin=(pm-1)/(pm+1)
tl=(pl-1)/(pl+1)
alp=(1/(2*l1))*log(tl/tin) #Np/m
print 'Attenuation in the line = %0.2f'%alp,'Mp/m'
from math import sqrt
#Given
c=3e8 #m/s
z0=200 #ohm
zl=800 #ohm
f=30e6 #hz
#Characterstic impedance
z00=sqrt(z0*zl) #ohm
print 'Characterstic impedance = %0.2f ohm' %z00
#Length of line
lam=c/f #m
l=lam/4 #m
print 'Length of line = %0.2f m' %l
#Given
l=1.2 #mH
r=8 #ohm
c=200e-12 #F
#(i) Resonant frequency
f0=(1/(2*pi))*sqrt(1/(l*c)) #hz
print 'Resonant frequency:%0.2f'%f0,'Hz'
#(ii) Impedance of circuit
print 'Impedance of circuit:',r,'ohm'
#(iii)Q factor of the circuit
q=1/(2*pi*f0*c*r)
print 'Q factor of the circuit:%0.2f'%q
#(iv) Bandwidth
df=f0/q #hz
print 'Bandwidth:%0.2f' %df,'Hz'
#The value of resonant frequency is calculated wrong in book
#Hence Q factor and bandwidth, all these answers dont match
from math import cos, sin
#Given
c=3e8 #m/s
le=25 #m
zl=40+(1J*30) #ohm
f=10e6 #hz
cap=40e-12 #F
l=300e-9 #H/m
#Input impedance
z0=sqrt(l/cap) #ohm
zl1=zl/z0
lam=c/f #m
bet=(2*pi*le)/lam #rad
zin=((zl1*cos(bet))+(1J*sin(bet)))/(cos(bet)+(1J*zl1*sin(bet))) #ohm
print 'Input impedance: {:.3f}'.format(zin), 'ohm'
#Reflection coefficient
t=(zl1-1)/(zl1+1)
print 'Reflection coefficient:{:.3f}'.format(t)
import cmath
#Given
c=3e8 #m/s
R=2.25 #ohm
L=1e-9 #H/m
C=1e-12 #F/m
f=0.5e9 #hz
G=0
w=2*pi*f #rad/sec
#Characterstic impedance
z0=cmath.sqrt((R+(1J*w*L))/(G+(1J*w*C))) #ohm
print 'Characterstic impedance:{:.3f}'.format(z0),'ohm'
#Propagation constant
gam=cmath.sqrt((R+(1J*w*L))*(G+(1J*w*C)))
print 'Propagation constant:{:.3f}'.format(gam)
#Given
c=3e8 #m/s
f=3e9 #Hz
ZL=50-(1J*100) #ohms
Z0=50 #ohm
#Wavelength
lam=c/f
print 'Wavelength:',lam*100, 'cm'
#Normalized load impedance
z=ZL/Z0
print 'Normalized load impedance:' ,z
#From chart
zin=0.45+(1J*1.2)
yin=0.27-(1J*0.73)
ZINN=Z0*zin
print 'Line impedance:' ,ZINN,'ohm'
YINN=yin/Z0
print 'Line admittance:',YINN, 'mho'
#Given
ZL=75+(1J*100) #ohms
Z0=50 #ohm
#Normalized load impedance
z=ZL/Z0
print 'Normalized load impedance:' ,z
#(i) 0.051*lam
#From chart
r=4.6
Zi1=r*Z0
print 'Input impedance at 0.051 lam:' ,Zi1,'ohm'
#(ii) 0.102*lam
r1=1.5-(1J*2)
Zi2=r1*Z0
print 'Input impedance at 0.102 lam:' ,Zi2,'ohm'
#(iii) 0.301*lam
r2=0.22
Zi3=r2*Z0
print 'Input impedance at 0.301 lam:' ,Zi3, 'ohm'
#Given
ZL=15+(1J*20) #ohms
Z0=50 #ohm
#Normalized load impedance
z=ZL/Z0
print 'Normalized load impedance:' ,z
#From chart
T=0.6
print 'Reflection coefficient:' ,T
#VSWR
p=4
print 'VSWR:' ,p
#Given
Z0=50 #ohm
p=2.4
#From chart
zl=1.4+1J
L=Z0*zl
print 'Load:',L, 'ohm'
#Given
Z0=50 #ohm
T=2.23
#From chart
zl=2+1J
ZLd=Z0*zl
print 'Normalized impedance:',ZLd, 'ohm'
#Impedance of device is by negating the real part
imp=-(ZLd.real)+((ZLd.imag)*1J)
print 'Impedance of device:' ,imp,'ohm'
#Given
p=3
m1=54 #cm
m2=204 #cm
#Point A
print 'Point A'
lam=4*(m2-m1)
dA=0.083*lam
L=m1-dA
print 'Location of stub:',L, 'cm'
IA=0.114*lam
print 'Length:' ,IA, 'cm'
#Point B
print 'Point B'
dB=0.083*lam
IB=0.386*lam
Lb=dB+m1
print 'Location of stub:',Lb, 'cm'
#Given
Z0=50 #ohm
ZL=100 #ohms
f=10e9 #Hz
c=0.159e-12 #F
#Normalized load impedance
z=ZL/Z0
print 'Normalized load impedance:' ,z
#From chart
zin=0.4+(1J*0.55)
ZINN=Z0*zin
print 'Normalized impedance:',ZINN, 'ohm'
#From given wave equation we can see
w=1e9 #rad/sec
bet=30 #rad/m
c=3e8 #m/s
u0=1 #let
e0=1/(9e16)
vp=w/bet #m/sec
print 'Phase velocity:%0.2f' %vp,'m/s'
e=1/(vp**2*u0)
er=e/(e0*u0)
print 'Dielectric constant:' ,er
#Given
c=3e8 #m/s
f=10e9 #hz
er=6
tandel=2e-4
vp=c/er #m/sec
print 'Phase velocity: %0.f'%vp,'m/sec'
al=(pi*f*tandel)/vp #Np/m
print 'Attenuation constant: %0.3f'%al,'Np/m'
#Answer for velocity is calculated wrong in book, hence answers dont match for both
#Given
er=2.2
n0=377 #ohm
n2=n0/sqrt(er) #ohm
n1=377 #ohm
#Reflection coefficient
t=(n2-n1)/(n2+n1)
print 'Reflection coefficient: %0.2f'%t
#Vswr
#Taking mod of reflection coefficient
t1=-t
p=(1+t1)/(1-t1)
print 'VSWR: %0.3f'%p
#Given
sig=5 #mohm/m
er=80*8.85e-12
eaz=0.1
u=1.26e-6
az=-log(0.1)
#(i) Range at 25Khz
f=25e3 #Khz
w=2*pi*f #rad/sec
a=w*(sqrt((u*er/2)*(sqrt(sig**2/(w**2*er**2)+1)-1)))
z=az/a #m
print 'Range at 25khz: %0.3f'%z, 'm'
#(ii) Range at 25Mhz
f1=25e6 #Mhz
w1=2*pi*f1 #rad/sec
a1=w1*(sqrt((u*er/2)*(sqrt(sig**2/(w1**2*er**2)+1)-1)))
z1=az/a1 #m
print 'Range at 25Mhz: %0.3f'%z1, 'm'