StoNorm.nb
  This code takes the measured radiation 2x2 S data ((Subscript[S, Rad])) and 2x2 cavity S data ((Subscript[S, Cav])) and finds the eigenvalues of the normalized impedance (z) and scattering (s) matrices. This code is written for two-dimensional cavities. Example input files are Srad.txt and Scav.txt. This cavity data set is just one rendition of the cavity. Ordinarily one wants to analyze a large number of renditions (~100) to compile statistics for the resulting normalized s and z matrices.  The user must specify the directories for the input and output files.

<< Graphics`Graphics`

<<Statistics`DescriptiveStatistics`

<< Statistics`NonlinearFit`

<<Statistics`MultinormalDistribution`

<<Statistics`MultiDescriptiveStatistics`

<< LinearAlgebra`MatrixManipulation`

<<Statistics`DataManipulation`

<<Graphics`Graphics3D`

$HistoryLength[10];

**********************************************************************************

**********************************************************************************

[Set the Directory of Cavity Scattering Matrix file]

SetDirectory["xxxx"];

**********************************************************************************

[Set  Cavity Scattering Matrix file name]

s22temp1=OpenRead["Scav.txt"];

s22temp = ReadList[s22temp1, Number];

Close[s22temp1];

**********************************************************************************

[Define Frequency Range , Number of Frequency Samples , Number of Traces (By Default =9 : {freq, Re[S11], Im[S11], Re[S12], Im[S12], Re[S21], Im[S21], Re[S22], Im[S22]}.  These values are for the example data file Scav.txt ]

freqmin= 3*10^9;
freqmax= 20*10^9;
freqsample=16001;
numtraces = 9;

sdata = Partition[s22temp, freqsample*numtraces];

Clear[s22temp];

**********************************************************************************

[Generating Cavity Impedance Matrix from Source Cavity Scattering Matrix]

freq = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, 1, 
            freqsample}];

res11mat = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, freqsample + 1, 
            2*freqsample}];
ims11mat = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, 2*freqsample + 1, 
            3*freqsample}];
S11 = 
        Transpose[
          Table[res11mat[[i, j]] + I*ims11mat[[i, j]], {i, 1, 
              Length[res11mat]}, {j, 1, 
              Length[res11mat[[1]]]}]];
Clear[res11mat, ims11mat];

res12mat = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, 3*freqsample + 1, 
            4*freqsample}];
ims12mat = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, 4*freqsample + 1, 
            5*freqsample}];
S12 = 
        Transpose[
          Table[res12mat[[i, j]] + I*ims12mat[[i, j]], {i, 1, 
              Length[res12mat]}, {j, 1, 
              Length[res12mat[[1]]]}]];
\(\(Clear[res12mat, ims12mat];\)
\)
res21mat = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, 5*freqsample + 1, 
            6*freqsample}];
ims21mat = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, 6*freqsample + 1, 
            7*freqsample}];
S21 = 
        Transpose[
          Table[res21mat[[i, j]] + I*ims21mat[[i, j]], {i, 1, 
              Length[res21mat]}, {j, 1, 
              Length[res21mat[[1]]]}]];
Clear[res21mat, ims21mat];

res22mat = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, 7*freqsample + 1, 
            8*freqsample}];
ims22mat = 
        Table[sdata[[i, j]], {i, 1, Length[sdata]}, {j, 8*freqsample + 1, 
            9*freqsample}];
S22 = 
        Transpose[
          Table[res22mat[[i, j]] + I*ims22mat[[i, j]], {i, 1, 
              Length[res22mat]}, {j, 1, 
              Length[res22mat[[1]]]}]];
Clear[res22mat, ims22mat];

Scav = 
      Table[{{S11[[i, j]], S12[[i, j]]}, {S21[[i, j]], 
            S22[[i, j]]}}, {i, 1, Length[S11]}, {j, 1, 
          Length[S11[[1]]]}]; 
    Clear[S11, S12, S21, S22];

Zcav = 
        Table[50*(IdentityMatrix[2] + Scav[[i, j]]) . 
              Inverse[(IdentityMatrix[2] - Scav[[i, j]])], {i, 1, 
            Length[Scav]}, {j, 1, Length[Scav[[1]]]}];

**********************************************************************************

**********************************************************************************

[Set the Directory of Radiation Scattering Matrix file]

SetDirectory["xxxx"];

**********************************************************************************

[Set Radiation Scattering Matrix file name]

s22temp1=OpenRead["Srad.txt"];

sdata = ReadList[s22temp1, Number];

Close[s22temp1];

**********************************************************************************

res11rad = 
        Table[sdata[[i]], {i, freqsample + 1, 
            2*freqsample}];
ims11rad = 
      Table[sdata[[i]], {i, 2*freqsample + 1, 
          3*freqsample}]; 
S11rad = 
      Table[res11rad[[i]] + I*ims11rad[[i]], {i, 1, 
          Length[res11rad]}]; 
\(Clear[res11rad, ims11rad]; 
\)
res12rad = 
      Table[sdata[[i]], {i, 3*freqsample + 1, 
          4*freqsample}]; 
ims12rad = 
      Table[sdata[[i]], {i, 4*freqsample + 1, 
          5*freqsample}]; 
S12rad = 
      Table[res12rad[[i]] + I*ims12rad[[i]], {i, 1, 
          Length[res12rad]}]; 
\(Clear[res12rad, ims12rad]; 
\)
res21rad = 
      Table[sdata[[i]], {i, 5*freqsample + 1, 
          6*freqsample}]; 
ims21rad = 
      Table[sdata[[i]], {i, 6*freqsample + 1, 
          7*freqsample}]; 
S21rad = 
      Table[res21rad[[i]] + I*ims21rad[[i]], {i, 1, 
          Length[res21rad]}]; 
\(Clear[res21rad, ims21rad]; 
\)
res22rad = 
      Table[sdata[[i]], {i, 7*freqsample + 1, 
          8*freqsample}]; 
ims22rad = 
      Table[sdata[[i]], {i, 8*freqsample + 1, 
          9*freqsample}]; 
S22rad = 
      Table[res22rad[[i]] + I*ims22rad[[i]], {i, 1, 
          Length[res21rad]}]; 
\(Clear[res22rad, ims22rad]; 
\)
Srad = 
      Table[{{S11rad[[i]], 
            S12rad[[i]]}, {S21rad[[i]], 
            S22rad[[i]]}}, {i, 1, Length[S11rad]}]; 
\(\(Clear[S11rad, S12rad, S21rad, S22rad];\)
\)
Zrad = 
        Table[50*(IdentityMatrix[2] + Srad[[i]]) . 
              Inverse[(IdentityMatrix[2] - Srad[[i]])], {i, 
            1, Length[Srad]}];

**********************************************************************************
**********************************************************************************
**********************************************************************************

[Defining Normalized Impedance and Scattering Matrices]

**********************************************************************************
**********************************************************************************
**********************************************************************************

Znorm = 
        Table[MatrixPower[
              Re[Zrad[[i]]], -0.5] . (Zcav[[i, j]] - 
                I*Im[Zrad[[i]]]) . 
            MatrixPower[Re[Zrad[[i]]], -0.5], {i, 1, 
            Length[Zcav]}, {j, 1, 
            Length[Zcav[[1]]]}];
Snorm = 
        Table[(Znorm[[i, j]] - IdentityMatrix[2]) . 
            Inverse[(IdentityMatrix[2] + Znorm[[i, j]])], {i, 1, 
            Length[Znorm]}, {j, 1, Length[Znorm[[1]]]}];

eigZ = 
        Flatten[Table[
            Eigenvalues[Znorm[[i, j]]], {i, 1, Length[Znorm]}, {j, 1, 
              Length[Znorm[[1]]]}]];

eigS = 
        Flatten[Table[
            Eigenvalues[Snorm[[i, j]]], {i, 1, Length[Znorm]}, {j, 1, 
              Length[Znorm[[1]]]}]];

**********************************************************************************
**********************************************************************************
**********************************************************************************

[Defining Universal Loss Parameter]

**********************************************************************************
**********************************************************************************
**********************************************************************************

reeigZ = Sort[Re[eigZ]];

imeigZ = Sort[Im[eigZ]];

Ktwidle = 
        0.32/(0.5*(Variance[
                    Table[reeigZ[[i]], {i, 1, 
                        Round[0.99*Length[reeigZ]]}]] + 
                  Variance[
                    Table[imeigZ[[i]], {i, Round[0.005*Length[imeigZ]], 
                        Round[0.995*Length[imeigZ]]}]]));

**********************************************************************************
**********************************************************************************
**********************************************************************************

[Exporting Generated Files]

**********************************************************************************
**********************************************************************************
**********************************************************************************

[Set the Output Directory for Normalized Impedance and Scattering  Eigenvalues file]

SetDirectory["xxxx"];

Export["EigZnorm.txt", 
        Table[{Re[eigZ[[i]]], Im[eigZ[[i]]]}, {i, 1, 
            Length[eigZ]}], "Table"];

Export["EigSnorm.txt", 
        Table[{Re[eigS[[i]]], Im[eigS[[i]]]}, {i, 1, 
            Length[eigS]}], "Table"];

**********************************************************************************
**********************************************************************************
**********************************************************************************