#Chapter-6, Example 6.2, Page 234
#=============================================================================
#Input parameters
#[s]=[0,(0.3+(%i)*(0.4));(0.3+(%i)*(0.4)),0];#scattering matrix of a two port
#Calculations
#to find l such that S12 and S21 will be real when port1 is shifted lm to the left
#let port 1 be shifted by phi1 degree to the left and port2 position be remained unchanged i.e.,phi2=delta
#Then [phi]=[e**-(j*phi1),0;0,1]
#[S']=[phi]*[s]*[phi]
#for S12 and S21 to be real
import math
phi1=53.13;#in degrees
phi1=phi1*(math.pi/180);#phi in radians
b=34.3;#measured in rad/m
l=(phi1)/b;#distance of shift in m
#Output
print "distance that the position of part1 should be shifted to the left so that S21 and S12 will be real numbers is (m) = ",round(l,3)
#Chapter-6, Example 6.3, Page 236
#=============================================================================
import math
import numpy
from math import sqrt
#Input parameters
D=30.;#directivity in dB
VSWR=1.;#VSWR at each port under matched conditions
C=10.;#coupling factor
#Calculations
S41=sqrt(0.1);
S14=S41;#under matched and lossless conditions
S31=sqrt(((S41)**2)/(10)**(D/10));
S13=S31;
S11=(VSWR-1)/(VSWR+1);
S22=S11;
S33=S22;
S44=S33;
#let input power is given at port1
#p1=p2+P3+p4
S21=sqrt(1-(S41)**2-(S31)**2);
S12=S21;
S34=sqrt((0.5)*(1+(S12)**2-0.1-0.0001));
S43=S34
S23=sqrt(1-10**-4-(S34)**2)
S32=S23;
S24=sqrt(1-0.1-(S34)**2)
S42=S24;
S=numpy.matrix([[S11,S12,S13,S14],[S21,S22,S23,S24],[S31,S32,S33,S34],[S41,S42,S43,S44]]);
#Output
print "The scattering matrix is"
print S
#Chapter-6, Example 6.4, Page 238
#=============================================================================
import numpy
#Input parameters
a1=32*10**-3;#power in watts
a2=0;
a3=0;
#Calculations
S=numpy.array([[0.5,-0.5,0.707],[-0.5,0.5,0.707],[0.707,0.707,0]]);#S-matrix for H-plane tee
X=numpy.array([[a1,0,0],[0,0,0],[0,0,0]]);
#[B]=[b1,b2,b3]
B =S*X
b1=(0.5)**2*a1;#power at port 1
b2=(-0.5)**2*a1;#power at port 2
b3=(0.707)**2*a1;#power at port 3
#Output
print "Thus b1,b2,b3 are",b1,"W,",b2,"W,",round(b3,5),"W respectively"
#Chapter-6, Example 6.5, Page 239
#=============================================================================
#Input parameters
S=([[0.5,-0.5,0.707],[-0.5,0.5,0.707],[0.707,0.707,0]]);
R1=60.;#load at port1 in ohms
R2=75.;#load at port2 in ohms
R3=50.;#characteristic impedance in ohms
P3=20*10**-3;#power at port 3 in Watts
#calculations
p1=(R1-R3)/(R1+R3);
p2=(R2-R3)/(R2+R3);
P1=0.5*P3*(1-(p1)**2);#power delivered to the port1 in Watts
P2=0.5*P3*(1-(p2)**2);#power delivered to the port2 in Watts
#Output
print "Thus power delivered to the port1 and port2 are",round(P1,5), "W,",P2," W respectively"
#Calculate
from numpy import array
#Variable declaration
Il=0.5 #inserion loss(dB)
Is = 30 #isolation loss(dB)
#Calculations
#Il = -20log(S21)
S21 = 10**(-Il/20)
#Is = -20log(S12)
S12 = 10**(-Is/20)
#Perfectly matched ports
S11=0
S22=0
S = array([[S11,S12],[S21,S22]])
#Result
print "The scattering matrix is:\n",S
#Chapter-6, Example 6.9, Page 241
#=============================================================================
#Input parameters
ins=0.5;#insertion loss in db
iso=20;#isolation loss in db
S=2;#VSWR
#Calculations
S21=10**-(ins/20.);#insertion loss=0.5=-20*log[S21]
S13=S21;
S32=S13;
S12=10**-(iso/20.);#isolation loss=30=-20*log[s12]
S23=S12;
S31=S23;
p=(S-1)/(S+1);
S11=p;
S22=p;
S33=p;
S=([[S11,S12,S13],[S21,S22,S23],[S31,S32,S33]]);
print S
#for a perfectly matched,non-reciprocal,lossless 3-port circulator,[S] is given by
#[S]=[0,0,S13;S21,0,0;,0,S32,0]
#i.e.,S13=S21=S32=1
#[S]=[0,0,1;1,0,0;0,1,0]
#Calculate The output power at the port
import math
#Variable declaration
Pi = 90. #power source(W)
C = 20 #dB
D = 35 #dB
Is = 0.5 #insertion loss(dB)
#Calculations
#C = 20=10log(Pi/Pf)
Pf = Pi/(10**(20./10.))
#D=350=10log(Pf/Pb)
Pb = Pf/(10**(35./10.))
Pr = Pi-Pf-Pb #received power
Pr_db = 10*math.log10(Pi/Pr)
Pr_dash=Pr_db-Is
#Result
print "The output power at the port is",round(Pr_dash,3),"dB"
#Chapter-6, Example 6.11, Page 242
#=============================================================================
import math
import cmath
from math import sin
from math import cos,log10
#Calculations
S13=0.1*(cos(90*math.pi/180.)+(1j)*sin(90*math.pi/180.));#conversion from polar to rectangular
S13=abs(S13);
C=-20*log10(S13);#coupling coefficient in dB
S14=0.05*(cos(90*math.pi/180.)+(1j)*sin(90*math.pi/180.));#conversion from polar to rectangular
S14=abs(S14);
D=20*log10(S13/S14);#directivity in dB
I=-20*log10(S14);#isolation in dB
print "Thus coupling,directivity and isolation are",C," dB",round(D,1),"dB and",round(I,0),"dB respetively "
#Calculate VSWR
import math
#Variable declaration
lamda2 = 3.5 #distance between 2 minimas(cm)
lamda_g = 7 #guided wavelength(cm)
d2_1 = 2.5*10**-1 #distance between minimum power points(cm)
#Calculation
S = lamda_g/(math.pi*d2_1)
#Result
print "VSWR =",round(S,4)
#calculate Phase shift introduced
#chapter-6 page 244 example 6.13
import math
wg=7.2##guide wavelength in cm
x=10.5##Position of reference null without the waveguide component in cm
y=9.3##Position of reference null with the waveguide component in cm
#CALCULATION
z=x-y##Path difference introduced due to the component in cm
p=(2.*(math.pi)*(z/wg))##Phase difference introduced in rad
Pd=(p*180.)/(math.pi)##Phase shift introduced in deg
#OUTPUT
print '%s %.2f %s' %('\nPhase shift introduced is Pd=',Pd,'deg')#