如何读取和绘制多端口 touchstone 文件 (s1p、s2p、s3p 等) 中的 S 参数

如何读取和绘制多端口 touchstone 文件 (s1p、s2p、s3p 等) 中的 S 参数

pgfplots可以轻松创建精美的图表,并将其无缝集成到您的 TikZ 图形中。

作为高频工程师,您不可避免地会使用网络分析仪来测量 S 参数。保存测量结果的实际标准是 Touchstone 文件。对于双端口系统,您会得到一个*.s2p如下所示的文件:

! TOUCHSTONE file generated by XXXX
! Date and time: Wed Jun 04 19:21:59 2014
! Project name: transition
# GHZ S MA R 50
0                              1             180     4.05226e-006             -45       0.00214819            -135        0.0326957               0  
0.0749999955            0.999678         178.746        0.0001547         44.2365     4.40317e-005        -137.093        0.0312474         -13.366  
0.149999991             0.998953         177.518      0.000154895         156.805     6.23325e-005        -137.592        0.0280867        -24.2334  
0.224999994             0.998173         176.312      0.000191275        -138.125     7.47758e-005        -137.862        0.0245106        -31.5581  
0.299999982             0.997505         175.117      0.000132603        -89.9357     8.48586e-005        -139.217        0.0214594        -36.0664  
0.375                   0.996964         173.926     6.99054e-006         -136.62     9.55566e-005        -140.334        
[...]

您可以通过以下方式直接解析该文件:

\begin{tikzpicture}
    \begin{axis}[width=0.8\textwidth,height=7.5cm,xmin=0,xmax=75,ymin=-80,xlabel={frequency [GHz]},ylabel={S [dB]},legend pos=outer north east]
        \addplot[color=red] table[mark=none,comment chars=!, x index=0, skip first n=5, y expr={20*log10{\thisrowno{1}}}] {transition.s2p};
        \addlegendentry{$s_{11}$}
        \addplot[color=blue] table[mark=none,comment chars=!, x index=0, skip first n=6, y expr={20*log10{\thisrowno{3}}}] {transition.s2p};
        \addlegendentry{$s_{21}$}
        \node[coordinate, pin={below:$\small{s_{21}=\SI{-0,30}{dB}}$}] at (axis cs:61.25,-0.3) {};
    \end{axis}
\end{tikzpicture}

结果是:

解析 s2p 文件的示例

但是s4p文件有 16 个元素,以矩阵形式写入。例如:

! TOUCHSTONE file generated by xxx
! Date and time: Fri Jun 06 12:06:41 2014
! Project name: coupler
# GHZ S MA R 50
0                      0.0419805               0     9.42573e-005               0      0.000111583               0     8.80383e-005             180  
                    9.42259e-005               0        0.0419806               0     8.79926e-005             180      0.000111677               0  
                     0.000111607               0     8.80789e-005             180        0.0419801               0     9.42701e-005               0  
                    8.80392e-005             180      0.000111656               0     9.42518e-005               0        0.0419793               0  
0.0749999955           0.0394465        -13.0429      0.000193207        -28.6791     8.60595e-005         70.0056      0.000106558        -80.3171  
                     0.000193175        -28.6827         0.039446        -13.0437      0.000106588        -80.2888     8.60951e-005         69.9513  
                    8.60757e-005         69.9988      0.000106582        -80.3272        0.0394461        -13.0419      0.000193195        -28.6752  
                     0.000106596        -80.3031      8.6086e-005         69.9705       0.00019319        -28.6779        0.0394467        -13.0423  
0.149999991            0.0343958        -22.6939      0.000239256        -3.93789     5.10449e-005        -177.505      0.000121496        0.965584  
                     0.000239219        -3.93766        0.0343964         -22.694       0.00012157        0.961136     5.09615e-005        -177.497  
                    5.10392e-005        -177.503      0.000121511        0.965866        0.0343962        -22.6947      0.000239235        -3.93823  
                     0.000121562         0.96405     5.09984e-005        -177.493      0.000239232        -3.93833        0.0343972         -22.694  
0.224999994            0.0299796        -27.6324      0.000161031         22.3635     8.78685e-005        -68.1676       0.00010533         82.8833  
                     0.000161004         22.3691        0.0299786        -27.6333      0.000105341         82.8535     8.79148e-005        -68.1169  
                    8.78884e-005        -68.1626      0.000105358         82.8922        0.0299787        -27.6337      0.000161021         22.3588  
                     0.000105368          82.869     8.79103e-005        -68.1415      0.000161014         22.3621        0.0299785        -27.6333  
[...]

我无法想象你如何读取该文件,例如绘制 s21(第二行,每个矩阵的第一行)。你可以尝试作为一种选择

each nth point=4

但随后 x 轴的值(频率)就消失了。之前有人解决过我的问题吗?提前谢谢!

答案1

lua如果文档是用 编译的,则可以借助脚本重写输入标准文件。可以从文件内部调用代码lualatex。由于此脚本写入了新的数据文件,因此需要启用此解决方案。lua.texlua-shell-escape

*.s1p正如问题中提到的,单端口和双端口文件(和)不需要采取任何措施*.s2p。对于从三端口开始的所有内容,S 矩阵分为几行。我目前没有检查那么多真实文件,但我尝试考虑试金石规格在本文档中,我们发现以下示例:

3-port network description
<frequency value> <N11> <N12> <N13>
<N21> <N22> <N23>
<N31> <N32> <N33>

4-port network description
<frequency value> <N11> <N12> <N13> <N14>
<N21> <N22> <N23> <N24>
<N31> <N32> <N33> <N34>
<N41> <N42> <N43> <N44>

6-port network format (single frequency point)
<frequency value> <N11> <N12> <N13> <N14> !row 1
<N15> <N16>
<N21> <N22> <N23> <N24> !row 2
<N25> <N26>
<N31> <N32> <N33> <N34> !row 3
<N35> <N36>
<N41> <N42> <N43> <N44> !row 4
<N45> <N46>
<N51> <N52> <N53> <N54> !row 5
<N55> <N56>
<N61> <N62> <N63> <N64> !row 6
<N65> <N66> 

目前,代码无法处理行尾注释,因为!row n。但变化矩阵分裂应该可以很好地处理。<frequency value>是一个(实值)数字,而是<Nxx>一个复数值(幅度-角度、dB-角度或实部-虚部)。因此,包括频率的行具有奇数个值,要么是 7,要么是 9,而其他行始终具有偶数个条目。

首先,我们需要一个包含 lua 代码的单独文件。我将其命名为snp_process.lua。在其中,我们定义一个函数rewrite_snp(infilename, outfilename),该函数读取输入文件(*.snp标准文件)并相应地写入输出文件,其中一行包含每个频率的完整 S 矩阵。

\begin{luacode}

function rewrite_snp(infilename, outfilename)
  infile  = io.open( infilename, "r")
  outfile = io.open(outfilename, "w")

  filetype = infilename:byte(-2)-48      -- 48 == '0' in decimal
  if filetype == 3 or filetype == 4 then
    typical_length= 2 * filetype         -- s3p -> 6 entries, s4p -> 8 entries
    other_length  = 2 * filetype +1      -- lines with frequencies -> one more entry, start of new line for outfile
  else
    typical_length = 0                    -- can be arbitrary?
    other_length   = 2 * 4 +1             -- this line is limited to freq + 4 complex data values
  end

  flag_start = 0                         -- this is a flag to write newlines only after first line

  for line in infile:lines() do
    first_char = string.sub(line,1,1)    -- extract first char of line to identify commented lines
    if not (( first_char == "!" ) or (first_char == "#") ) then
      t={}                               -- make a table from the line (whitespace-delimited)
      length=0
      for k,v in string.gmatch(line, "%S+") do
        t[k]=v
        length = length + 1
      end

      if length == other_length then     -- first value is a frequency, insert newline before
        if flag_start then
          outfile:write('\string\n' .. line)
        else
          outfile:write(line)
        end
        flag_start=1
      elseif length == typical_length or filetype > 4 then 
        outfile:write(line)               -- just write this line into the file
      else
        print("Now that is strange!")
      end
    end
  end

  infile:close()
  outfile:close()
end

\end{luacode}

我们只需要在主文档中调用 lua 函数即可,例如针对示例*.s4p文件。我们可以在此处为处理后的文件选择一个输出文件名out.txt

%!Tex program = lualatex
\documentclass{article} 

\usepackage{luacode,pgfplots}
\input{snp_process.lua}
\directlua{rewrite_snp("in.s4p","out.txt")}

\begin{document}


\begin{tikzpicture}
\begin{axis}[xlabel={frequency $f$ in GHz},ylabel={S-parameters in dB},legend pos=outer north east]
  \addplot[color=red]  table[x index=0,  y expr={20*log10{\thisrowno{1}}}] {out.txt};
  \addplot[color=blue] table[x index=0,  y expr={20*log10{\thisrowno{15}}}] {out.txt};
  \addplot[color=black,dashed] table[x index=0,  y expr={20*log10{\thisrowno{31}}}] {out.txt};
  \addlegendentry{$S_{11}$}
  \addlegendentry{$S_{23}$}
  \addlegendentry{$S_{44}$}
\end{axis}
\end{tikzpicture}

\end{document}

处理给定示例时的输出如下

0                      0.0419805               0     9.42573e-005               0      0.000111583               0     8.80383e-005             180                    9.42259e-005               0        0.0419806               0     8.79926e-005             180      0.000111677               0                     0.000111607               0     8.80789e-005             180        0.0419801               0     9.42701e-005               0                    8.80392e-005             180      0.000111656               0     9.42518e-005               0        0.0419793               0
0.0749999955           0.0394465        -13.0429      0.000193207        -28.6791     8.60595e-005         70.0056      0.000106558        -80.3171                     0.000193175        -28.6827         0.039446        -13.0437      0.000106588        -80.2888     8.60951e-005         69.9513                    8.60757e-005         69.9988      0.000106582        -80.3272        0.0394461        -13.0419      0.000193195        -28.6752                     0.000106596        -80.3031      8.6086e-005         69.9705       0.00019319        -28.6779        0.0394467        -13.0423
0.149999991            0.0343958        -22.6939      0.000239256        -3.93789     5.10449e-005        -177.505      0.000121496        0.965584                     0.000239219        -3.93766        0.0343964         -22.694       0.00012157        0.961136     5.09615e-005        -177.497                    5.10392e-005        -177.503      0.000121511        0.965866        0.0343962        -22.6947      0.000239235        -3.93823                     0.000121562         0.96405     5.09984e-005        -177.493      0.000239232        -3.93833        0.0343972         -22.694
0.224999994            0.0299796        -27.6324      0.000161031         22.3635     8.78685e-005        -68.1676       0.00010533         82.8833                     0.000161004         22.3691        0.0299786        -27.6333      0.000105341         82.8535     8.79148e-005        -68.1169                    8.78884e-005        -68.1626      0.000105358         82.8922        0.0299787        -27.6337      0.000161021         22.3588                     0.000105368          82.869     8.79103e-005        -68.1415      0.000161014         22.3621        0.0299785        -27.6333

此示例的输出图如下所示。

s4p 图

相关内容