Debian Codegraph @

doc/source/examples/networks/Export Network as Touchstone File.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Export Network as Touchstone File"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "Frequently a `Network` is required to be saved as a [Touchstone file](https://en.wikipedia.org/wiki/Touchstone_file) so that it can be imported into another program such as [Keysight ADS](https://www.keysight.com/zz/en/products/software/pathwave-design-software/pathwave-advanced-design-system.html) or [Microwave Office](https://www.cadence.com/en_US/home/tools/system-analysis/rf-microwave-design/awr-design-environment-platform.html). The `Network.write_touchstone()` method allows `Network` objects to be exported in the appropriate format."
15 ]
16 },
17 {
18 "cell_type": "code",
19 "execution_count": 5,
20 "metadata": {},
21 "outputs": [],
22 "source": [
23 "from pathlib import Path\n",
24 "\n",
25 "import numpy as np\n",
26 "\n",
27 "import skrf as rf"
28 ]
29 },
30 {
31 "cell_type": "markdown",
32 "metadata": {
33 "collapsed": false,
34 "jupyter": {
35 "outputs_hidden": false
36 }
37 },
38 "source": [
39 "In this example, we are going to first generate a random 2-port `Network` and export the data as a Touchstone file using all the default arguments"
40 ]
41 },
42 {
43 "cell_type": "code",
44 "execution_count": 6,
45 "metadata": {
46 "collapsed": false,
47 "jupyter": {
48 "outputs_hidden": false
49 },
50 "pycharm": {
51 "name": "#%%\n"
52 }
53 },
54 "outputs": [
55 {
56 "name": "stdout",
57 "output_type": "stream",
58 "text": [
59 "!Default output\n",
60 "! Created with skrf (http://scikit-rf.org).\n",
61 "# GHz S RI R 50.0 \n",
62 "!freq ReS11 ImS11 ReS21 ImS21 ReS12 ImS12 ReS22 ImS22\n",
63 "1.0 -0.464865151852476 0.4502139349896557 -0.946087157816933 -0.774157144858624 0.22978320604401903 -0.015382277631633245 -0.5143243821327415 0.5414888218758449\n",
64 "5.9 -0.7714731941571016 0.5035362767937888 -0.13676846127948927 -0.28407348971702495 0.22574007371745886 0.7585739185331353 -0.10651147209906475 0.8017532911041758\n",
65 "10.8 0.9283594792475798 -0.949741920331475 0.0013830072200151644 0.5869045175861258 -0.7303982230754289 0.4182901609474474 0.5005946980290608 -0.7192362826596026\n",
66 "15.7 0.05196345220484089 0.9250542904689592 -0.9756169835079893 -0.6532005774023946 0.29678736213869095 -0.2743856183602864 0.2781155976701659 0.36062628422326837\n",
67 "20.6 -0.07332329228960388 0.6134822411048317 -0.5177662945200454 0.41284206902464593 -0.2849256858620177 -0.5010232817863058 0.10007939815281519 -0.7011091912436347\n",
68 "25.5 0.30846454726129147 -0.2866491167667229 -0.8623359516384592 0.6450662462360961 -0.43036099270200423 -0.342207252231161 0.39369055828649846 0.2474543341740585\n",
69 "30.4 0.9145795203794396 0.44609982646636714 0.4880768496247685 0.22450675141896115 0.32412739749113073 0.8792809268192112 -0.30411660385369754 -0.9493881130068704\n",
70 "35.3 -0.24284888674338645 0.5629931583833381 -0.25560444449996966 -0.47942310384730313 -0.5503294379329955 0.5260341997912912 0.22181324903183786 -0.27354385834163275\n",
71 "40.2 -0.15445158678019144 0.1402545231555048 0.05233201867297299 0.9262610033960388 0.08012592129752671 0.6362204417635848 0.9853804482302841 0.924566084120912\n",
72 "45.1 -0.24518734266073183 -0.30977957882926366 -0.5973235149690603 -0.9127315640504685 0.7676585384502421 0.613791431633252 0.9912077531024381 -0.17175649696500606\n",
73 "50.0 0.9023961597916488 0.8155000860198645 -0.2374313674378361 -0.8139274742596938 0.5362539112613807 -0.8023396937590226 0.5812260184801696 -0.8008646540965754\n",
74 "\n"
75 ]
76 }
77 ],
78 "source": [
79 "# Handle for random generator\n",
80 "rand_gen = np.random.default_rng()\n",
81 "\n",
82 "# Set the number of points\n",
83 "ports = 2\n",
84 "\n",
85 "# Generate the frequency object\n",
86 "points = 11\n",
87 "freq = rf.Frequency(1, 50, points, \"GHz\")\n",
88 "\n",
89 "# Create random data to fill the Network\n",
90 "s_random = rand_gen.uniform(-1, 1, (freq.npoints, ports, ports)) + 1j * rand_gen.uniform(\n",
91 " -1, 1, (freq.npoints, ports, ports)\n",
92 ")\n",
93 "\n",
94 "# Generate the Network object\n",
95 "ntwk = rf.Network(s=s_random, frequency=freq, name=\"test_ntwk\")\n",
96 "ntwk.comments = \"Default output\"\n",
97 "\n",
98 "# Define the save location\n",
99 "filepath = Path(f\"test_network.s{ports}p\")\n",
100 "\n",
101 "# Save the Network as a touchstone file using the default arguments\n",
102 "print(ntwk.write_touchstone(filepath, return_string=True))"
103 ]
104 },
105 {
106 "cell_type": "markdown",
107 "metadata": {
108 "collapsed": false,
109 "jupyter": {
110 "outputs_hidden": false
111 }
112 },
113 "source": [
114 "In this example, we are going to export the same 2-port `Network` as a Touchstone file, but use the formatting strings to format the columns of the resulting file."
115 ]
116 },
117 {
118 "cell_type": "code",
119 "execution_count": 7,
120 "metadata": {
121 "collapsed": false,
122 "jupyter": {
123 "outputs_hidden": false
124 },
125 "pycharm": {
126 "name": "#%%\n"
127 }
128 },
129 "outputs": [
130 {
131 "name": "stdout",
132 "output_type": "stream",
133 "text": [
134 "!Formatted output\n",
135 "! Created with skrf (http://scikit-rf.org).\n",
136 "# GHz S DB R 50.0 \n",
137 "!freq dBS11 angS11 dBS21 angS21 dBS12 angS12 dBS22 angS22\n",
138 "1.00000 \t -3.78001 \t 135.91728 \t 1.74467 \t-140.70748 \t -12.75422 \t -3.82981 \t -2.53568 \t 133.52619\n",
139 "5.90000 \t -0.71236 \t 146.86774 \t -10.02599 \t-115.70870 \t -2.03153 \t 73.42779 \t -1.84321 \t 97.56733\n",
140 "10.80000 \t 2.46464 \t -45.65229 \t -4.62863 \t 89.86499 \t -1.49692 \t 150.20076 \t -1.14698 \t -55.16175\n",
141 "15.70000 \t -0.66297 \t 86.78488 \t 1.39407 \t-146.19668 \t -7.86827 \t -42.75398 \t -6.83192 \t 52.36053\n",
142 "20.60000 \t -4.18236 \t 96.81565 \t -3.58010 \t 141.43282 \t -4.78591 \t-119.62635 \t -2.99668 \t -81.87623\n",
143 "25.50000 \t -7.51247 \t -42.90062 \t 0.64358 \t 143.20185 \t -5.19538 \t-141.50957 \t -6.65092 \t 32.15143\n",
144 "30.40000 \t 0.15134 \t 26.00150 \t -5.39670 \t 24.70160 \t -0.56410 \t 69.76471 \t -0.02690 \t-107.76177\n",
145 "35.30000 \t -4.24885 \t 113.33311 \t -5.29913 \t-118.06431 \t -2.36891 \t 136.29304 \t -9.06483 \t -50.96184\n",
146 "40.20000 \t -13.61245 \t 137.75801 \t -0.65149 \t 86.76633 \t -3.85950 \t 82.82193 \t 2.61453 \t 43.17627\n",
147 "45.10000 \t -8.06652 \t-128.36125 \t 0.75501 \t-123.20214 \t -0.15005 \t 38.64454 \t 0.05178 \t -9.83060\n",
148 "50.00000 \t 1.70074 \t 42.10428 \t -1.43361 \t-106.26253 \t -0.30902 \t -56.24271 \t -0.09125 \t -54.02980\n",
149 "\n"
150 ]
151 }
152 ],
153 "source": [
154 "# Get a copy of the network\n",
155 "ntwk_formatted = ntwk.copy()\n",
156 "ntwk_formatted.comments = \"Formatted output\"\n",
157 "\n",
158 "# Define the save location\n",
159 "formatted_filepath = Path(f\"test_network_formatted.s{ports}p\")\n",
160 "\n",
161 "# Save the Network as a touchstone file using the string formatting options\n",
162 "freq_fmt = \"{:<8.3f}\"\n",
163 "data_fmt = \"\\t{:>8.3f}\"\n",
164 "print(\n",
165 " ntwk_formatted.write_touchstone(\n",
166 " formatted_filepath,\n",
167 " form=\"db\",\n",
168 " return_string=True,\n",
169 " format_spec_freq=freq_fmt,\n",
170 " format_spec_A=data_fmt,\n",
171 " format_spec_B=data_fmt,\n",
172 " )\n",
173 ")"
174 ]
175 },
176 {
177 "cell_type": "markdown",
178 "metadata": {},
179 "source": [
180 "In this example, we are going to export a new 2-port `Network` as a Touchstone file, but we're going to include random noise data and format those output columns as well."
181 ]
182 },
183 {
184 "cell_type": "code",
185 "execution_count": 8,
186 "metadata": {
187 "pycharm": {
188 "name": "#%%\n"
189 }
190 },
191 "outputs": [
192 {
193 "name": "stdout",
194 "output_type": "stream",
195 "text": [
196 "!Formatted output with noise data\n",
197 "! Created with skrf (http://scikit-rf.org).\n",
198 "# GHz S DB R 50.0 \n",
199 "!freq dBS11 angS11 dBS21 angS21 dBS12 angS12 dBS22 angS22\n",
200 "1.00000 \t -0.91328 \t-130.97542 \t -1.29884 \t 130.51454 \t -4.26300 \t-173.56091 \t -1.87356 \t-113.17161\n",
201 "5.90000 \t -1.46165 \t 34.91404 \t -5.12067 \t 43.04405 \t -0.70823 \t-141.59246 \t -2.14551 \t 47.87232\n",
202 "10.80000 \t -7.68630 \t 31.44550 \t 1.37477 \t -49.54463 \t -39.56968 \t -16.58557 \t -5.65258 \t -8.49307\n",
203 "15.70000 \t -0.90550 \t-136.21873 \t -1.18593 \t 15.14162 \t -1.69316 \t-161.16082 \t -23.72235 \t-165.91385\n",
204 "20.60000 \t -9.51858 \t 89.43189 \t -4.74431 \t 25.90892 \t 1.38186 \t 140.40619 \t -3.82659 \t 6.48215\n",
205 "25.50000 \t -0.03824 \t -91.89634 \t 1.75464 \t 39.54287 \t -2.64012 \t-148.02961 \t -6.09412 \t 177.52691\n",
206 "30.40000 \t -1.85579 \t 11.21887 \t 0.24374 \t-156.97826 \t 1.38966 \t 134.87892 \t -4.55095 \t-136.33491\n",
207 "35.30000 \t -18.78104 \t 17.95128 \t 1.71843 \t -54.35505 \t -12.79699 \t-123.19957 \t -3.37591 \t 121.12044\n",
208 "40.20000 \t -3.73726 \t -41.69093 \t -0.48082 \t-113.51842 \t -1.53413 \t 132.51577 \t -23.70471 \t 172.04825\n",
209 "45.10000 \t -11.28388 \t-116.27157 \t -9.69757 \t 104.96040 \t -4.38896 \t-160.06974 \t 0.49972 \t -50.23027\n",
210 "50.00000 \t -4.56446 \t 115.81996 \t -15.72494 \t 114.38783 \t -2.05009 \t 81.27382 \t 1.60838 \t -54.51620\n",
211 "! Noise Data\n",
212 "! freq\tnf_min_db\tmagGOpt\tdegGOpt\tRn_eff\n",
213 "1.00000 \t 3.83480 \t 0.30042 \t 24.54642 \t 0.01791 \n",
214 "5.90000 \t 4.89486 \t 0.90514 \t 55.03904 \t 0.01656 \n",
215 "10.80000 \t 5.62803 \t 0.99515 \t -69.60951 \t 0.00183 \n",
216 "15.70000 \t 0.14024 \t 0.67099 \t-110.38925 \t 0.01086 \n",
217 "20.60000 \t 2.69752 \t 0.16396 \t 80.84681 \t 0.00130 \n",
218 "25.50000 \t 1.47690 \t 0.95158 \t 143.39918 \t 0.00701 \n",
219 "30.40000 \t 3.60385 \t 0.16757 \t 95.22127 \t 0.00777 \n",
220 "35.30000 \t 2.89029 \t 0.74515 \t-102.91751 \t 0.00525 \n",
221 "40.20000 \t 8.65540 \t 0.74206 \t -46.60443 \t 0.00098 \n",
222 "45.10000 \t 7.21090 \t 0.51109 \t -38.36113 \t 0.01287 \n",
223 "50.00000 \t 6.95480 \t 0.67592 \t -29.05023 \t 0.01013 \n",
224 "\n"
225 ]
226 }
227 ],
228 "source": [
229 "# Create random data to fill the Network\n",
230 "s_random = rand_gen.uniform(-1, 1, (freq.npoints, ports, ports)) + 1j * rand_gen.uniform(\n",
231 " -1, 1, (freq.npoints, ports, ports)\n",
232 ")\n",
233 "\n",
234 "# Create random noise data to fill the Network\n",
235 "nfmin_db_random = rand_gen.uniform(0, 10, freq.npoints)\n",
236 "g_opt_random = rand_gen.uniform(-1, 1, freq.npoints) + 1j * rand_gen.uniform(-1, 1, freq.npoints)\n",
237 "rn_random = rand_gen.uniform(size=freq.npoints)\n",
238 "\n",
239 "# Generate the Network object\n",
240 "noisy_ntwk = rf.Network(s=s_random, frequency=freq, name=\"test_noisy_ntwk\")\n",
241 "noisy_ntwk.comments = \"Formatted output with noise data\"\n",
242 "\n",
243 "# Add the noise data\n",
244 "noisy_ntwk.set_noise_a(noise_freq=freq, nfmin_db=nfmin_db_random, gamma_opt=g_opt_random, rn=rn_random)\n",
245 "\n",
246 "# Define the save location\n",
247 "formatted_filepath = Path(f\"test_noisy_network_formatted.s{ports}p\")\n",
248 "\n",
249 "# Save the Network as a touchstone file using the string formatting options\n",
250 "print(\n",
251 " noisy_ntwk.write_touchstone(\n",
252 " formatted_filepath,\n",
253 " form=\"db\",\n",
254 " return_string=True,\n",
255 " format_spec_freq=freq_fmt,\n",
256 " format_spec_A=data_fmt,\n",
257 " format_spec_B=data_fmt,\n",
258 " format_spec_nf_freq=freq_fmt,\n",
259 " format_spec_nf_min=data_fmt,\n",
260 " format_spec_g_opt_mag=data_fmt,\n",
261 " format_spec_g_opt_phase=data_fmt,\n",
262 " format_spec_rn=data_fmt,\n",
263 " )\n",
264 ")"
265 ]
266 },
267 {
268 "cell_type": "code",
269 "execution_count": null,
270 "outputs": [],
271 "source": [],
272 "metadata": {
273 "collapsed": false,
274 "pycharm": {
275 "name": "#%%\n"
276 }
277 }
278 }
279 ],
280 "metadata": {
281 "interpreter": {
282 "hash": "f9c1ad921755d07784f600fc7c2c11a8933ade33898d5981f5e318f268234d48"
283 },
284 "kernelspec": {
285 "display_name": "Python 3 (ipykernel)",
286 "language": "python",
287 "name": "python3"
288 },
289 "language_info": {
290 "codemirror_mode": {
291 "name": "ipython",
292 "version": 3
293 },
294 "file_extension": ".py",
295 "mimetype": "text/x-python",
296 "name": "python",
297 "nbconvert_exporter": "python",
298 "pygments_lexer": "ipython3",
299 "version": "3.9.13"
300 }
301 },
302 "nbformat": 4,
303 "nbformat_minor": 4
304 }