% Standing wave ratio (SWR) for a quarter-wave transformer, 
% as described on page 438 in the textbook by N.N. Rao, 
% and plotted as a function of frequency
% 
% Input:
%  Z0: Characteristic impedance of transmission line
%  Z1: Load impedance

if ~exist('Z0');  Z0 =  50.;  end
if ~exist('Z1');  Z1 = 100.;  end
gamR  = (Z1 - Z0)/(Z1 + Z0)   % Reflection coefficient  
swr1  = (1 - gamR)/(1 + gamR) % Corresponding standing wave ratio
Zq =  Z0.*sqrt(swr1) % Required characteristic impedance of QW stub
%
% Compute impedance and reflection coefficient 
% at right end of quarter wave stub at design frequency
% Since Z1 and gamR are real, the line length d_q has to be 
% lambda/4 or lambda/2.  We choose lambda/4 for largest bandwidth.
f  =  0.9:.002:1.1; % frequency relative to design frekvency
Z2 =  Z0.*(1. + gamR.*exp(-i*pi.*f))./(1. - gamR.*exp(-i*pi.*f));
gam2  = (Z2 - Zq)./(Z2 + Zq);
%
% Compute impedance, reflection coefficient, and SWR
% at left end of quarter wave stub
Z3    =  Zq.*(1. + gam2.*exp(-i*pi.*f))./(1. - gam2.*exp(-i*pi.*f));
gam3  =  abs((Z3 - Z0)./(Z3 + Z0));
swr3  = (1. + gam3)./(1. - gam3); % Desired SWR
plot (f, swr3)
title ('Quarter wave transformer')
xlabel ('Relative frequency')
ylabel ('Standing wave ratio')
%
% 20 dB return loss corresponds to an SWR of 1.22, 
% From plot: corresponds to relative bandwidth of about 0.12
