from __future__ import division
from math import sqrt
#Given
a=5 #cm
a1=a/100 #m
b=2 #cm
b1=b/100 #m
c=15 #cm
c1=c/100 #m
#(i) Air filled cavity
m=1
n=0
p=1
c=3e8 #for air
fr=(1/2)*c*sqrt((m/a1)**2+(n/b1)**2+(p/c1)**2) #hz
print 'Resonant frequency for an air filled cavity: %0.3f'%(fr/10**9),'Ghz'
#(ii) Dielctric filled cavity
er=2.56
fr1=(1/2)*(c/sqrt(er))*sqrt((m/a1)**2+(n/b1)**2+(p/c1)**2) #hz
print 'Resonant frequency for dielectric cavity: %0.3f'%(fr1/10**9),'Ghz'
#Given
a=0.38 #cm
a1=a/100 #m
b=0.76 #cm
b1=b/100 #m
f=50e9
c=3e8
#Length for TE102
m=1
n=0
p=2
l=1/sqrt((f/c)**2-(1/(4*b1**2))) #m
print 'Length c: %0.3f'%(l*100),'cm'
from math import pi
#Given
c=3e8 #m/s
a=2.286 #cm
a1=a/100 #m
b=1.024 #cm
b1=b/100 #m
f=10e9 #hz
sig=6e7
u=4e-7*pi
w=2*pi*f
eet=377
#Shortest cavity length
lamc=2*a1 #m
fc=c/lamc #hz
lam=c/f #m
lamg=lam/sqrt(1-(fc/f)**2) #m
sc=lamg/2 #m
print 'Shortest cavity length: %0.3f'%(sc*100), 'cm'
#Qw of the resonator operating in TE101 mode
rs=sqrt((w*u)/(2*sig)) #ohm
lamr=c/f
x=(((a1*b1)/(sc**2))+((sc**2+a1**2)/(2*sc*a1))+(b1*sc/a1**2))
qw=(2*pi*eet*a1*b1*sc)/(rs*(lamr**3)*x)
print 'Qw of the resonator operating in TE101 mode %0.3f'%qw
#Given
c=3e8 #m/s
a=4.8 #cm
a1=a/100 #m
b=2.2 #cm
b1=b/100 #m
f=5e9 #hz
er=2.25
tandel=4e-4
sig=5.813e7
oneby=3e8
u=4e-7*pi
w=2*pi*f
eet=377
#Length at p=1
m=1
n=0
p=1
z=(f*2*sqrt(er))/c
cp1=p/sqrt((z**2)-((m/a1)**2)-((n/b1)**2))
print 'Length of resonator at p=1: %0.3f'%(cp1*100), 'cm'
#At p=2
cp2=cp1*2
print 'Length of resonator at p=2: %0.3f'%(cp2*100), 'cm'
#Qw
rs=sqrt((w*u)/(2*sig)) #ohm
lamr=c/(f*sqrt(er))
x=(((a1*b1)/(cp1**2))+((cp1**2+a1**2)/(2*cp1*a1))+(b1*cp1/a1**2))
qw=(2*pi*(eet/sqrt(er))*a1*b1*cp1)/(rs*(lamr**3)*x)
qd=1/tandel
q=(qw*qd)/(qw+qd)
print 'Q for TE101 mode: %0.3f'%q
#Given
c=3e8 #m/s
a=2 #cm
a1=a/100 #m
b=2.5 #cm
b1=b/100 #m
print 'TE modes'
h01=3.832
fr=(c/(2*pi))*sqrt((h01/a1)**2+(pi/b1)**2) #hz
print 'Resonant frequency for mode TE010: %0.3f'%(fr/10**9),'Ghz'
h11=1.841
fr1=(c/(2*pi))*sqrt((h11/a1)**2+(pi/b1)**2) #hz
print 'Resonant frequency for mode TE111: %0.3f'%(fr1/10**9),'Ghz'
h21=3.054
fr2=(c/(2*pi))*sqrt((h21/a1)**2+(pi/b1)**2) #hz
print 'Resonant frequency for mode TE211: %0.3f'%(fr2/10**9),'Ghz'
print 'TM modes:'
l1=0
h011=2.405
fr3=(c/(2*pi))*sqrt((h011/a1)**2+(pi*l1/b1)**2) #hz
print 'Resonant frequency for mode TM010 %0.3f'%(fr3/10**9),'Ghz'
l2=1
fr4=(c/(2*pi))*sqrt((h011/a1)**2+(pi*l2/b1)**2) #hz
print 'resonant frequency for mode TM011: %0.3f'%(fr4/10**9),'Ghz'
l3=1
h111=3.832
fr5=(c/(2*pi))*sqrt((h111/a1)**2+(pi*l3/b1)**2) #hz
print 'Resonant frequency for mode TM111: %0.3f'%(fr5/10**9),'Ghz'
#Given
QTM010=1.202
QTE101=1.11
r=QTM010/QTE101
print 'Ratio of Qs of cylindrical and rectangular resonators: %0.3f'%r
#Given
f=7.07e9 #hz
a=3 #cm
a1=a/100 #m
sig=5.8e7
er=2.25
tandel=4e-4
ur=1
n=377
w=2*pi*f
u=4e-7*pi
#Q of resonantor
rs=sqrt(w*u/(2*sig)) #ohm
qw=(0.7419*n)/(rs*sqrt(2.25))
qd=1/tandel
q=(qw*qd)/(qw+qd)
print 'Q of resonator: %0.3f'%q
#Given
a=5 #cm
a1=a/100 #m
b=4 #cm
b1=b/100 #m
c=10 #cm
c1=c/100 #m
sig=5.8e7
u0=4e-7*pi
er=3
eet=377
ur=1
spl=3e8
tandel=2.5e-4
#TE101 mode
m=1
n=0
p=1
fr=(spl/(2*sqrt(er*ur)))*sqrt((m/a1)**2+(n/b1)**2+(p/c1)**2) #hz
print 'Resonant frequency: %0.3f'%(fr/10**9), 'Ghz'
w=2*pi*fr
rs=sqrt((w*u0)/(2*sig)) #ohm
lamr=spl/(fr*sqrt(er))
x=(((a1*b1)/(c1**2))+((c1**2+a1**2)/(2*c1*a1))+((b1*c1)/a1**2))
qw=(2*pi*(eet/sqrt(er))*a1*b1*c1)/(rs*(lamr**3)*x)
print 'Q for TE101 mode: %0.3f'%qw
qd=1/tandel
q=(qw*qd)/(qw+qd)
print 'Q for lossy dielectric: %0.3f'%q
#Value of qw is calculated wrong in book as lamr comes to be 0.08 not 0.89 m
#Given
c=3e8 #m/s
a=2.286 #cm
a1=a/100 #m
b=1.106 #cm
b1=b/100 #m
#For fr1=9.3e9
fr1=9.3e9 #hz
lamr1=c/fr1 #m
c1=(2*a1)/sqrt((((2*a1)/lamr1)**2)-1)
#For fr2=10.2e9
fr2=10.2e9 #hz
lamr2=c/fr2 #m
c2=(2*a1)/sqrt((((2*a1)/lamr2)**2)-1)
r=c1-c2
print 'Range of piston movement: %0.3f'%(r*100), 'cm'
#Given
a=3 #cm
a1=a/100 #m
d=10 #cm
d1=d/100 #m
df=2.5e6
er=2.25
p11=1.841
c=3e8 #m/s
#Resonant frequency
fr=(c/2)*(sqrt((p11/a1)**2+(pi/d1)**2)) #hz
print 'Resonant frequency: %0.3f'%(fr/10**9),'Ghz'
#Q without dielectric
q0=fr/df
print 'Q wirhout dielectric constant: %0.3f'%q0
# Q with dielectric
fr1=fr/sqrt(er)
qd=1e3
q=(q0*qd)/(q0+qd)
print 'Q with dielectric constant: %0.3f'%q
#Given
f=9.375e9 #hz
sig=5.8e7
eet=377
c=3e8 #m/s
w=2*pi*f
r=1.5
u=4e-7*pi
#Radius
a=c/(f*2.62) #m
print 'Radius of resonantor %0.3f'%(a*100), 'cm'
#O
rs=sqrt((w*u)/(2*sig)) #ohm
x=1.202*eet
y=rs*(1+(1/r))
q=x/y
print 'Q of the resonator: %0.3f'%q
#Answer for Q is calculated as 10875 in book but it is 10763.303
#Given
f=5e9 #hz
sig=5.813e7
er=2.25
tandel=4e-4
c=3e8 #m/s
h01=3.832
u=4e-7*pi
#Length of resonator
lamr=c/(f*sqrt(er))
d=sqrt((((((2*3.832)**2)+(pi*pi))*(lamr*lamr))/(2*2*pi*pi)) )
print 'Length of resonator: %0.3f'%(d*100),'cm'
#Q of resonator
n=(120*pi)/sqrt(er)
Rs=sqrt((f*u)/sig)
a=d/2
Qw1=n*(((h01/a)**2+(pi/d)**2)**(3/2))
Qw2=2*Rs*(((h01*h01)/(a*a*a))+((2*pi*pi)/(d*d*d)))
Qw=Qw1/Qw2
Qd=1/tandel
Q=(Qw*Qd)/(Qw+Qd)
print 'Q of resonator: %0.3f'%Q
#Value of Qw is calculated wrong in the book, it should be 50057.91 instead of 53473.8
#Hence the value of Q also differs
#Given
p=100 #mW
#As 2 and 3 are matched terminals
x=1/2
y=1/sqrt(2)
s=[x,-x, y, -x, 0, y, y, y, 0]
#Power delivered
#Port 1
p1=p*(1-s[1]**2)
print 'Power at port 1:',p1, 'mW'
#Port2
p2=p*s[2]**2
print 'Power at port 2:',p2, 'mW'
#Port 3
p3=p*s[3]**2
print 'Power at port 3:' ,p3,'mW'
#Given
p=40 #mW
#Since port 3 is matched
x=sqrt(2)
s=[1, 1, x, 1, 1, -x, x, -x, 0]
r1=40 #ohm
r2=60 #ohm
w=50 #ohm
#Reflection coefficients
T1=(w-r1)/(w+r1)
T2=(r2-w)/(r2+w)
#As power is fed into 1 and 2 equally
pd=p/2
#Power delivered
#Port 1
p1=pd*(1-T1**2)
print 'Power at port 1: %0.3f'%p1,'mW'
#Port2
p2=pd*(1-T2**2)
print 'Power at port 2: %0.3f'%p2,'mW'
from __future__ import division
#Given
T1=1/2
T2=3/5
T3=0
T4=4/5
p=500e-3 #W
#S matrix for magic Tee
x=1/sqrt(2)
s=[0, 0, x, x, 0, 0, x, -x, x ,x, 0, 0, x, -x, 0, 0]
#Using the input output relation
#[b]=[s]*[a]
b=[0.6565, 0.7576, 0.5536, 0.0892]
#(i) Power transmitted through ports
#Port 1
p1=(1/2)*b[0]**2*(1-T1**2)
print 'Power at port 1: %0.3f'%p1,'W'
#Port2
p2=(1/2)*(b[1]**2)*(1-(T2**2))
print 'Power at port 2: %0.3f'%p2, 'W'
#Port 4
p4=(1/2)*b[3]**2*(1-T4**2)
print 'Power at port 4: %0.3f'%p4,'W'
#(ii) Power reflected at port 3
#Port 3
p3=p*b[2]**2
print 'Power at port 3: %0.3f'%p3,'W'
#(iii) Power absorbed
pabs=p-(p1+p2+p3+p4)
print 'Power absorbed: %0.3f'%pabs,'W'
#Answer for power absorbed is calculated wrong in book
from numpy import mat, set_printoptions
#Given
C=10 #dB
D=30 #dB
#Parameters
bet=10**(-C/20)
x=bet/(10**(D/20))
a=sqrt(1-(bet*bet))
#Scattering matrix
#Assuming symmetery
s=mat([[0, a ,x ,(bet*1J)],[ a, 0, (bet*1J), x],[ x ,(bet*1J), 0 ,a],[ (bet*1J) ,x ,a, 0] ])
set_printoptions(precision=3)
print 'Scattering matrix:\n',s
#Given
vswr=2
D1=8 #mW
D2=2 #mW
#Reflection coefficient at arm 4
T=(vswr-1)/(vswr+1)
#Powwe delivered to D1
P=(D1*100)/(1-T**2)
P1=0.99*P
#Power reflected at D1
W1=(P/100)*T*T
#Power reflected at load
W2=D2-W1
Tt=sqrt((W2*100)/(P1))
pt=(1+Tt)/(1-Tt)
print 'VSWR:%0.3f'%pt
Pl=P1*(1-(Tt*Tt))
print 'Power delivered:' ,Pl,'mW'
#Answer for P1 should be 792 but it is given as 800
from numpy import mat, set_printoptions
#Given
I=30 #dB
Il=0.4 #dB
S12=10**(I/-20)
S21=10**(Il/-20)
s=mat([[0, S12],[ S21, 0] ])
set_printoptions(precision=3)
print 'Scattering matrix:\n' ,s
from numpy import mat, set_printoptions
#Given
I=30 #dB
Il=2 #dB
p=1.3
#Elelments
T=(p-1)/(p+1)
S11=T
S22=T
S33=T
S12=10**(-Il/20)
S13=10**(-I/20)
S21=S13
S32=S13
S23=S12
S31=S23
s=mat([[S11, S21, S31] ,[S12, S22, S32], [S13, S23 ,S33] ])
set_printoptions(precision=3)
print 'Scattering matrix:\n' ,s
#Given
f=10e9 #Hz
u=4e-7*pi
c=3e8 #m/s
a=2.29 #cm
a1=a/100
b=1.02 #cm
b1=b/100
#E/H
w=2*pi*f
EbyH=(w*u)/sqrt(((w/c)**2)+((pi/a1)**2))
lam=c/f
lamc=2*a1
d=(1/4)*(lam/sqrt(1-((lam/lamc)**2)))
print 'Position: %0.3f'%(d*100), 'cm'
#Answer for positon is calculated wrong in book
from numpy import mat, set_printoptions
#Given
#As it is perfectly matched
S12=1/sqrt(2)
S21=S12
s=mat([[0 ,S12] ,[S21, 0] ])
set_printoptions(precision=3)
print 'Scattering matrix:\n' , s