Writing A Touchstone® File

This example shows how to write out the data in `rfckt` objects you create in the MATLAB® workspace into an industry-standard data file: Touchstone®. You can use these files in third-party tools.

This simple example shows how to create and analyze an RLCG transmission line object. It then shows how to write the analyzed result into a Touchstone file, read it back in, and compare the file data to the original result.

Create an RF Circuit Object to Represent an RLCG Transmission Line

Create an `rfckt.rlcgline` object to represent an RLCG transmission line. Create variables to represent the transmission line parameters and use the `set` method to update the `rfckt.rlcgline` object to use these values.

```ckt1 = rfckt.rlcgline; R = 0.002; % ohm/m G = 0.002; % S/m mu_0 = pi*4e-7; % H/m L = mu_0; % H/m c = 299792458; % m/s epsilon_0 = 1/(mu_0*c^2); % F/m C = epsilon_0; % F/m linelen = 10; % m Z_vacuum = sqrt(mu_0/epsilon_0); % ohms ckt1.R = R; ckt1.G = G; ckt1.L = L; ckt1.C = C; ckt1.LineLength = linelen;```

Copy the Circuit Object

Make a copy of the first `rfckt` object. Then change the capacitance of the new object to introduce mismatch that will appear as a finite reflection coefficient on the Smith chart.

`ckt2 = copy(ckt1)`
```ckt2 = rfckt.rlcgline with properties: Freq: 1.0000e+09 R: 0.0020 L: 1.2566e-06 C: 8.8542e-12 G: 0.0020 IntpType: 'Linear' LineLength: 10 StubMode: 'NotAStub' Termination: 'NotApplicable' nPort: 2 AnalyzedResult: [] Name: 'RLCG Transmission Line' ```
`ckt2.C = 0.5*ckt1.C;`

Cascade Two Circuit Objects

Create an `rfckt.cascade` object that cascades the two transmission lines together.

`ckt3 = rfckt.cascade('Ckts',{ckt1,ckt2});`

Analyze the Cascade and Plot S-Parameter Data

Use the `analyze` method of the `rfckt.cascade` object to analyze the cascade in the frequency domain. Then, use the `smithplot` method to plot the object's `S11` on a Smith chart.

```freq = logspace(0,8,20); analyze(ckt3,freq); figure smithplot(ckt3,[1,1],'LegendLabels','S11 Original');```

Write out the Data to an S2P File

Use the `rfwrite` function to write the data to a file.

```workingdir = tempname; mkdir(workingdir) filename = fullfile(workingdir,'myrlcg.s2p'); if exist(filename,'file') delete(filename) end filedata = ckt3.AnalyzedResult.S_Parameters; rfwrite(filedata,freq,filename)```

Inspect the S2P File

Use the `type` function to display the contents of the `.s2p` file in the MATLAB command window to see the Touchstone file format.

`type(filename)`
```# GHz S MA R 50 1.000000000000000e-09 4.9956688299520730e-01 1.7999980458876163e+02 4.9963354343955202e-01 -1.6640558205811934e-04 4.9963354343955202e-01 -1.6640558205811902e-04 4.9956688299521823e-01 1.7999980458844280e+02 2.636650898730358e-09 4.9956688299243807e-01 1.7999948476878276e+02 4.9963354343260874e-01 -4.3875342748465255e-04 4.9963354343260863e-01 -4.3875342748465320e-04 4.9956688299251423e-01 1.7999948476794214e+02 6.951927961775606e-09 4.9956688297319030e-01 1.7999864151514811e+02 4.9963354338433591e-01 -1.1568396188433886e-03 4.9963354338433602e-01 -1.1568396188433908e-03 4.9956688297372076e-01 1.7999864151293158e+02 1.832980710832436e-08 4.9956688283937212e-01 1.7999641814969570e+02 4.9963354304875479e-01 -3.0501822197363916e-03 4.9963354304875474e-01 -3.0501822197363930e-03 4.9956688284306028e-01 1.7999641814385149e+02 4.832930238571753e-08 4.9956688190907761e-01 1.7999055591120035e+02 4.9963354071582089e-01 -8.0422656731208887e-03 4.9963354071582100e-01 -8.0422656731208922e-03 4.9956688193471843e-01 1.7999055589579137e+02 1.274274985703133e-07 4.9956687544174527e-01 1.7997509923522475e+02 4.9963352449742515e-01 -2.1204646687655957e-02 4.9963352449742515e-01 -2.1204646687656002e-02 4.9956687561999885e-01 1.7997509919459898e+02 3.359818286283781e-07 4.9956683048132772e-01 1.7993434538435341e+02 4.9963341174836157e-01 -5.5909244749526403e-02 4.9963341174836151e-01 -5.5909244749526417e-02 4.9956683172053190e-01 1.7993434527727968e+02 8.858667904100824e-07 4.9956651792079748e-01 1.7982689184853271e+02 4.9963262792700741e-01 -1.4741305049577125e-01 4.9963262792700708e-01 -1.4741305049577139e-01 4.9956652653563866e-01 1.7982689156699070e+02 2.335721469090122e-06 4.9956434507311936e-01 1.7954357698415038e+02 4.9962717895522329e-01 -3.8867473728393093e-01 4.9962717895522341e-01 -3.8867473728393065e-01 4.9956440496207766e-01 1.7954357625600892e+02 6.158482110660261e-06 4.9954924203836609e-01 1.7879662219796427e+02 4.9958930282204372e-01 -1.0247626683562452e+00 4.9958930282204367e-01 -1.0247626683562459e+00 4.9954965834354326e-01 1.7879662053812120e+02 1.623776739188721e-05 4.9944436490450694e-01 1.7682803522724353e+02 4.9932621885777728e-01 -2.7012650168069037e+00 4.9932621885777739e-01 -2.7012650168069023e+00 4.9944725716695926e-01 1.7682803561343289e+02 4.281332398719396e-05 4.9872093087973957e-01 1.7165347141326552e+02 4.9750823089356755e-01 -7.1099543855329870e+00 4.9750823089356755e-01 -7.1099543855329799e+00 4.9874094831180443e-01 1.7165355930454223e+02 1.128837891684688e-04 4.9395192774857954e-01 1.5829285403785633e+02 4.8537498738050983e-01 -1.8527617133746109e+01 4.8537498738050971e-01 -1.8527617133746109e+01 4.9408695423932697e-01 1.5829462565930422e+02 2.976351441631320e-04 4.7005276175625949e-01 1.2736060374891667e+02 4.1944100749863300e-01 -4.5568428419474273e+01 4.1944100749863295e-01 -4.5568428419474301e+01 4.7083855558191268e-01 1.2738801395821361e+02 7.847599703514606e-04 4.3246964060503451e-01 8.0964046625780895e+01 2.4583660567079157e-01 -9.3582775189553644e+01 2.4583660567079157e-01 -9.3582775189553686e+01 4.3540960369119625e-01 8.1178776658663935e+01 2.069138081114790e-03 4.8456534514674954e-01 4.7259771019756514e+01 8.6002402769753919e-02 -1.6278818625706072e+02 8.6002402769754224e-02 -1.6278818625706029e+02 4.9196758351450021e-01 4.7787861008000988e+01 5.455594781168516e-03 6.0419813636585018e-01 2.6872620020357260e+01 1.6637174822162825e-02 8.7039349152634500e+01 1.6637174822162825e-02 8.7039349152631246e+01 6.1766382847138612e-01 2.7689212173598698e+01 1.438449888287663e-02 6.9842374918749295e-01 1.4365722237145549e+01 1.8280441246750513e-03 -1.1221107334109674e+02 1.8280441246684008e-03 -1.1221107334064077e+02 7.2277371883520813e-01 1.5713306512214592e+01 3.792690190732246e-02 7.4788451928626010e-01 6.7469821304155495e+00 2.0574647544194854e-04 1.5947439599551970e+02 2.0574647539404445e-04 1.5947439599468592e+02 7.9041650399708774e-01 8.2310537700086570e+00 1.000000000000000e-01 7.6256263726735674e-01 2.7440654158858355e+00 6.0006898721147904e-05 3.1994461726741314e+01 6.0006898665262121e-05 3.1994461726741314e+01 8.1978106471099599e-01 3.6949462491260965e+00 ```

Compare the Data

Read the data from the file `myrlcg.s2p` into a new `rfckt` object and plot `S11` on a Smith chart. Visually compare this Smith chart to the previous one to see that the data is the same.

```ckt4 = read(rfckt.passive,filename); figure smithplot(ckt4,[1,1],'LegendLabels','S11 from S2P');```

Four Steps to Building Smarter RF Systems with MATLAB

Download white paper