% $Id: test-smithchart.tex 5 2024-07-26 18:00:40Z reinhard $

% Copyright (C) 2019 Reinhard Kotucha <reinhard.kotucha@web.de>
% 
% You may freely use, modify, and/or distribute this file.

%\pdfvariable pageattr {/Group << /S /Transparency /I true /CS /DeviceRGB>>}

\font\sf=cmss10

\def\uncompressed{
  \pdfvariable compresslevel 0
  \pdfvariable objcompresslevel 0
}

\uncompressed

\input luagraphlib
\input mpmarkers

\luagraphicfile{smithchart.lua}  % Smithcharts
\luagraphicfile{antpattern.lua}  % Antenna radiation patterns

\advance\hoffset by 1.5cm

\canvasrule=0pt

\pdfextension info {
  /Author (Reinhard Kotucha)
  /Title (Smith Chart)
  /Creator (LuaTeX with luagraphlib)
  /Subject (Smith Chart)
}

\hoffset=1.5cm
\voffset=1.5cm

\parskip=12pt
\parindent=0pt

\hsize=12cm

\def\section#1{\vskip 10mm\leavevmode{\bf #1}\par}

\begindocument

\directlua{default = {
    antpattern = {
      gainref = 'dipole',
      dB_ref = 0,
      lambda = function (a) return a[2], a[1] end,
      bgcolor = {1, 1, .9},
      fillcolor = {0.9, 0.9, 1},
      strokecolor = {0.561, 0.078, 0.008},
    },
    smithchart = {
      radius = 40,
      reso = 'simple',
      arclinewidth = pt(.4),
      circlelinewidth = pt(.6),
      circlecolor = {0},
      forbiddencolor = {1, .9, .9},
      forbiddenlinewidth = 0,
      arrowlinewidth = pt(2),
      plotcolor = {0, .3, 0},
      plotlinewidth = pt(1),
      bgcolor = {1},
      layer = 'fg',
      lambda = function (a) return a[2], a[3] end,
      Z = {
        fg = {
          arccolor = {.5, 0, 0},
        },
        bg = {
          arccolor = {1, .8, .8},
        },
      },
      Y = {
        fg = {
          arccolor = {0, 0, .5},
        },
        bg = {
          arccolor = {.8, .8, 1},
        },
      },
      labels = {
        framed=true,
        fillcolor = {1},
        strokecolor = {1},
      },
      Gamma = {
        strokecolor = {0},
        linewidth = pt(0.4),
      },
    },
  } % no comma here!
}


\section{Impedance Chart}

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z()}}
\end{canvas}

\section{Addmittance Chart}

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithchart_Z{layer='bg'}}}
  \at\center {\luagraphic{smithchart_Y()}}
  \at\center {\luagraphic{smithlabels_Y()}}
\end{canvas}

\newpage

\section{Arcs of constant $Q$}

In order to determine the center and radius of a circle if three
points on its perimeter are given we use an algorithm Professor David
Wright of Oklahoma State University implemented in Matlab.  It's based
on complex numbers.  The three coordinate pairs may not be collinear.
This algorithm is less complex and more efficient than solving a
system of linear equations.

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithchart_Z{layer='bg'}}}
  \at\center {\luagraphic{smithQarcs{Q={.2,.5,1,2,5}, strokecolor = {0,0.4,0}}}}
  \at\center {\luagraphic{smithlabels_Z{framed=false}}}
\end{canvas}

\section{Forbidden Region if $Q < 2$ is required} 

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithQarea(2, {strokecolor = {0,0.4,0}})}}
  \at\center {\luagraphic{smithchart_Z{}}}
  \at\center {\luagraphic{smithlabels_Z{framed=false}}}
\end{canvas}

\newpage

\section{Impdances which cannot be matched by various L-Networks} 

\hsize=170mm

\leavevmode\hbox to 170mm{\hsize=80mm
  \vbox{
    \begin{canvas}(80mm, 80mm)
      \at(0mm, 77mm){\hbox{zC--yL}}
      \at\center {\luagraphic{smith_zC_yL ()}}
      \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
      \at\center {\luagraphic{smithchart_Z()}}
      \at\center {\luagraphic{smithlabels_Z{framed=false}}}
    \end{canvas}}
  \hfill
  \vbox{
    \begin{canvas}(80mm, 80mm)
      \at(0mm, 77mm){\hbox{zL--yC}}
      \at\center {\luagraphic{smith_zL_yC ()}}
      \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
      \at\center {\luagraphic{smithchart_Z()}}
      \at\center {\luagraphic{smithlabels_Z{framed=false}}}
    \end{canvas}}}
\par
\leavevmode\hbox to 170mm{\hsize=80mm
  \vbox{
    \begin{canvas}(80mm, 80mm)
      \at(0mm, 77mm){\hbox{yC--zL}}
      \at\center {\luagraphic{smith_yC_zL ()}}
      \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
      \at\center {\luagraphic{smithchart_Z()}}
      \at\center {\luagraphic{smithlabels_Z{framed=false}}}
    \end{canvas}}
  \hfill
  \vbox{
    \begin{canvas}(80mm, 80mm)
      \at(0mm, 77mm){\hbox{yL--zC}}
      \at\center {\luagraphic{smith_yL_zC ()}}
      \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
      \at\center {\luagraphic{smithchart_Z()}}
      \at\center {\luagraphic{smithlabels_Z{framed=false}}}
    \end{canvas}}
}
\par
\leavevmode\hbox to 170mm{\hsize=80mm
  \vbox{
    \begin{canvas}(80mm, 80mm)
      \at(0mm, 77mm){\hbox{yL--zL, zL--yL}}
      \at\center {\luagraphic{smith_yL_zL_zL_yL()}}
      \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
      \at\center {\luagraphic{smithchart_Z()}}
      \at\center {\luagraphic{smithlabels_Z{framed=false}}}
    \end{canvas}}
  \hfill
  \vbox{
    \begin{canvas}(80mm, 80mm)
      \at(0mm, 77mm){\hbox{yC--zC, zC--yC}}
      \at\center {\luagraphic{smith_yC_zC_zC_yC()}}
      \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
      \at\center {\luagraphic{smithchart_Z()}}
      \at\center {\luagraphic{smithlabels_Z{framed=false}}}
    \end{canvas}}
}
\newpage

\section{Reflection Coefficient and Return Loss Circles} 

The argument can be either a number or a list of real numbers.  Numbers $< 1$
denote reflection coefficients $|\Gamma|$, numbers $\ge 1$ denote return
loss $|\varrho|$ in dB.

$|\Gamma|$ circles ($|\Gamma|$ = 0.2, 0.4, 0.6, 0.8).

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithgamma{Gamma={.2, .4, .6, .8}, strokecolor={0,.3,0}}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z{framed=true}}}
\end{canvas}

Return loss circles ($\varrho = 10, 20, 30$) dB.

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithgamma{Gamma={-10, -20, -30}, strokecolor={0,.3,0}}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z{framed=true}}}
\end{canvas}

\newpage


\section{Mark Circles of constant $|\Gamma|$ or Return Loss with
  Background Colors}
  
\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithbackground{
        inner=0.3,
        outer=1,
        bgcolor={.9}}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z{framed=false}}}
\end{canvas}

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithbackground{
        inner=0.3,
        outer=1,
        bgcolor={1, .92, .92}}}}
  \at\center {\luagraphic{smithbackground{
        inner=0,
        outer=0.3,
        bgcolor={.9, 1, .9}}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z{framed=false}}}
\end{canvas}


\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithbackground{
        outer=0,
        inner=-10,
        bgcolor={1, 1, .9}}}}
  \at\center {\luagraphic{smithbackground{
        outer=-10,
        inner=-20,
        bgcolor={1, .9, .8}}}}
  \at\center {\luagraphic{smithbackground{
        outer=-20,
        inner=-30,
        bgcolor={1, .8, .7}}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z{framed=false}}}
\end{canvas}

\newpage

\section{Plotting Data}

\def\meta#1{$\langle$#1$\rangle$}

When reading the data file, all lines which do not begin with a number
are ignored, column separators like comma, semicolon, or \meta{TAB}
are replaced by a \meta{SPACE} character.  Multiple \meta{SPACE}
characters are collapsed into one.  Finally the \meta{SPACE} character
is used as a column separator.

Within a [La]TeX input file a lambda expression can be passed to Lua
which is evaluated for each line of the data file.

If no such lambda expression is given,\hfill\break {\hbox to 2em{}\tt
  function (a) return a[1], a[2] end}\hfill\break is assumed.
{\tt a} is a list of columns, {\tt a[}$n${\tt ]} is the $n$th column.

By default the first and second column is selected from each line of
the data file.  However, the expression can contain any valid Lua code
which makes this concept extremely powerful.

There has been no need so far to pre-process any data file, regardless
which program created it.  Tested with data from a Rohde und Schwarz
network analyzer, GNU Octave ASCII data files, and LTSpice.  Even CSV
should work if the data file is compliant with the non-existent file
format specification.

\section{Example 1: Impedance of an Antenna}

Plotting $s_{11}$ of an antenna.  Frequency range 865$\,$MHz $\ldots$ 965$\,$MHz
(green), 902$\,$MHz $\ldots$ 928$\,$MHz (red):

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z{framed=false}}}
  \at\center {\luagraphic{smithdata('data/ant/mobmk.data')}}
  \at\center {\luagraphic{smithdata('data/ant/mobmk.data',
      function (a)
        if a[1] >= 902e6 and a[1] <= 928e6 then;
          return a[2], a[3];
        end;
      end,
      {plotcolor = {.9, 0, 0}})}}
\end{canvas}

{\it Data from {\rm Rohde und Schwarz ZVL} network analyzer.}

\newpage

The return loss shall not exceed $-10$ dB within the band of interest:

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithbackground{
        outer=-0.0001,
        inner=-10,
        bgcolor={1, .92, .92}}}}
  \at\center {\luagraphic{smithbackground{
        inner=-50,
        outer=-10,
        bgcolor={.9, 1, .9}}}}
  \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z{framed=false}}}
  \at\center {\luagraphic{smithdata('data/ant/mobmk.data')}}
  \at\center {\luagraphic{smithdata('data/ant/mobmk.data',
      function (a)
        if a[1] >= 902e6 and a[1] <= 928e6 then;
          return a[2], a[3];
        end;
      end,
      {plotcolor = {.9, 0, 0}})}}
\end{canvas}

\section{Example 2: Third order Chebyshev Highpass Filter}

% This filter was simulated with LTSpice.  Frequency range
% 15$\,$MHz~$\ldots$~4015$\,$MHz.

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Y()}}
  \at\center {\luagraphic{smithdata('data/HP-915/hipass-4G.data',
      function (a) return Z_to_Gamma(a[2], a[3], 50)end)}}
  \at\center {\luagraphic{smithdata('data/HP-915/hipass.data',
      function (a) if a[1] >= 902e6 and a[1] <= 928e6 then;
      return Z_to_Gamma(a[2], a[3], 50) end end,
      {plotcolor = {.9, 0, 0}})}}
\end{canvas}

{\it Data from {\rm LTspice.}}

\bye
\newpage

\section{Highpass filter 415$\,$MHz $\ldots$ 1415$\,$MHz}

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithchart_Y{layer='bg'}}}
  \at\center {\luagraphic{smithchart_Z()}}
  \at\center {\luagraphic{smithlabels_Z()}}
  \at\center {\luagraphic{smithdata('data/HP-915/hipass.data',
      function (a) return Z_to_Gamma(a[2], a[3], 50) end)}}

  \at\center {\luagraphic{smithdata('data/HP-915/hipass.data',
      function (a) if a[1] >= 902e6 and a[1] <= 928e6 then;
      return Z_to_Gamma(a[2], a[3], 50) end end,
      {plotcolor = {.9, 0, 0}})}}
\end{canvas}

{\it Data from {\rm LTspice.}}


\section{Highpass filter 15$\,$MHz $\ldots$ 4015$\,$MHz}

\begin{canvas}(80mm, 80mm)
  \at\center {\luagraphic{smithchart_Z{layer='bg'}}}
  \at\center {\luagraphic{smithchart_Y()}}
  \at\center {\luagraphic{smithlabels_Y()}}
  \at\center {\luagraphic{smithdata('data/HP-915/hipass-4G.data',
      function (a) return Z_to_Gamma(a[2], a[3], 50)end)}}
  \at\center {\luagraphic{smithdata('data/HP-915/hipass.data',
      function (a) if a[1] >= 902e6 and a[1] <= 928e6 then;
      return Z_to_Gamma(a[2], a[3], 50) end end,
      {plotcolor = {.9, 0, 0}})}}
\end{canvas}

{\it Data from {\rm LTspice.}}

\bye

% Local Variables:
%  mode: TeX
%  TeX-master: t
%  TeX-engine: luatex
%  indent-tabs-mode: nil
%  coding: utf-8-unix
% End:
% vim:set tabstop=2 expandtab: