from __future__ import division
from numpy import array, linalg, mat, set_printoptions
#Given
z=mat('[4 2 ;2 4]')
I=mat('[1 0;0 1]')
#Scattering matrix
s=(z-I)*linalg.inv(z+I)
set_printoptions(precision=3)
print 'Scattering Matrix:\n' ,s
from math import sqrt, pi
from cmath import exp
#Given
P=12.8e-3 #W
l=3 #cm
lamb=4.2 #cm
vswr=2.2
jfi=1J*4.49
#ap
ap=sqrt(2*P)
#Phase shift
bl=(2*pi*l)/lamb
#bp
bp=(ap*(vswr-1))/(vswr+1)
a=ap*exp(jfi)
b=bp*exp(jfi)
print 'Required Waves:\n{:.3}'.format(a),'\n{:.3}'.format(b)
from math import log10
#Given
S11=0.10
S12=0.90
A12=-45
S21=0.90
A21=45
S22=0.3
#(i) Network is reciprocal
if(A12==A21):
print 'Network is reciprocal'
else:
print 'Network is not reciprocal'
#(ii) Network is lossles
x=(S11**2)+(S12**2)
if(x==1):
print 'Network is lossless'
else:
print 'Network is not lossless'
#(iii)Return loss
T=S11-((S12*S21)/(1+S22))
Tm=-T #mod of T
L=-20*log10(Tm)
print 'Return Loss: %0.3f'%L,'dB'
#Given
S11=0.6
S12=0.045
S21=2.5
S22=0.50
TS=0.5
TL=0.4
Z0=50 #ohm
Vrms=10 #V
#(i) Gain Parameters
#(i)Reflection coefficients of input and output
Tin=S11+((S12*S21*TL)/(1-(S22*TL)))
Tout=S22+((S12*S21*TS)/(1-(S22*TS)))
#Transducer Gain
x=(1-(TS)**2)/((1-(S11*TS))**2)
y=(S21*S21)
z=(1-(TL)**2)/((1-(Tout*TL))**2)
GT=x*y*z
print 'Transducer Gain: %0.3f'%GT
#Available Power Gain
z1=1-(Tout)**2
GA=(x*y)/z1
print 'Available power Gain: %0.3f'%GA
#Power Gain
z2=1-(Tin)**2
GP=(x*y)/z2
print 'Power Gain: %0.3f'%GP
#(ii) Power levels
#Power available at source
Pavs=(sqrt(2)*Vrms)**2/(8*Z0)
print 'Power available at source: %0.3f'%Pavs,'W'
Pl=9.4*Pavs
#Power available at input
Pin=Pl/13.5
print 'Power available at input: %0.3f'%Pin,'W'
#(iii) VSWRs
M1=Pin/Pavs
M2=Pl/(9.6*Pavs)
Tin1=sqrt(1-M1)
Tout1=sqrt(1-M2)
vswrin=(1+Tin1)/(1-Tin1)
print 'Input VSWR: %0.3f'%vswrin
vswrout=(1+Tout1)/(1-Tout1)
print 'Output VSWR: %0.3f'%vswrout
#Calculations for gain are done wrong in book, hence answers dont match