54 "GHz",
'S',
"MA", 50.0, 1e9, 0, 0, 0 };
57 static const char * touchstone_valid_options[] = {
58 "hz",
"khz",
"mhz",
"ghz",
"s",
"y",
"z",
"g",
"h",
"ma",
"db",
"ri", NULL };
63 static void touchstone_join (
void) {
64 vector * yroot, * xroot, * next = NULL;
66 for (yroot = touchstone_vector; yroot != NULL; yroot = next) {
69 for (xroot = next; xroot != NULL; xroot = next) {
72 if ((xroot->
getSize () & 1) == 0) {
91 static int touchstone_vector_check (
void) {
94 nr_double_t f =
real (root->
get (0));
97 if ((size & 1) == 0) {
104 touchstone_options.
ports = (int)
sqrt ((size - 1) / 2.0);
114 for (root = (
vector *) root->
getNext (); root != NULL; root = next) {
116 nr_double_t freq =
real (root->
get (0));
132 "decreasing frequency value\n",
noise ?
"noise" :
"data",
140 if (!even && root->
getSize () != size) {
142 "%d required\n",
noise ?
"noise" :
"data",
157 static void touchstone_options_eval (
void) {
159 for (
int i = 0;
i < touchstone_idents->
length ();
i++) {
160 char * str = touchstone_idents->
get (
i);
162 if (!strcmp (str,
"hz")) {
163 touchstone_options.
factor = 1.0;
164 touchstone_options.
unit =
"Hz";
166 else if (!strcmp (str,
"khz")) {
167 touchstone_options.
factor = 1e3;
168 touchstone_options.
unit =
"kHz";
170 else if (!strcmp (str,
"mhz")) {
171 touchstone_options.
factor = 1e6;
172 touchstone_options.
unit =
"MHz";
174 else if (!strcmp (str,
"ghz")) {
175 touchstone_options.
factor = 1e9;
176 touchstone_options.
unit =
"GHz";
179 else if (!strcmp (str,
"s")) {
182 else if (!strcmp (str,
"y")) {
185 else if (!strcmp (str,
"z")) {
188 else if (!strcmp (str,
"g")) {
191 else if (!strcmp (str,
"h")) {
195 else if (!strcmp (str,
"ma")) {
196 touchstone_options.
format =
"MA";
198 else if (!strcmp (str,
"db")) {
199 touchstone_options.
format =
"dB";
201 else if (!strcmp (str,
"ri")) {
202 touchstone_options.
format =
"RI";
209 static char * touchstone_create_set (
int r,
int c) {
216 static void touchstone_create (
void) {
217 vector * f, * v, * root, * next, * nf = NULL;
223 touchstone_result =
new dataset ();
224 f =
new vector (
"frequency");
229 for (
int r = 0; r <
ports; r++) {
232 v->
setName (touchstone_create_set (r,
c));
240 if (touchstone_options.
noise) {
241 nf =
new vector (
"nfreq");
259 for (
n = 0, root = touchstone_vector; root != NULL; root = next,
n++) {
262 if (
n < touchstone_options.
lines) {
268 for (
int j = 0; j <
ports; j++) {
269 int pos = 1 + j * 2 +
i * 2 *
ports;
272 if (ports == 2 &&
i != j) {
273 pos = 1 +
i * 2 + j * 2 *
ports;
276 if (!strcmp (touchstone_options.
format,
"RI")) {
280 else if (!strcmp (touchstone_options.
format,
"MA")) {
284 else if (!strcmp (touchstone_options.
format,
"dB")) {
294 else if (touchstone_options.
noise) {
299 val =
pow (10.0,
real (root->
get (1)) / 10.0);
308 val = (val - r) / (1.0 - r * val);
321 static void touchstone_normalize_sp (
void) {
322 int ports = touchstone_options.
ports;
328 for (n = 0; n < len; n++) {
331 for (i = 0; i <
ports; i++) {
332 for (j = 0; j <
ports; j++) {
341 for (i = 0; i <
ports; i++) {
342 for (j = 0; j <
ports; j++) {
352 static void touchstone_normalize (
void) {
354 int ports = touchstone_options.
ports;
357 if (touchstone_options.
parameter ==
'S') {
359 touchstone_normalize_sp ();
363 for (
int i = 1; i <=
ports; i++) {
364 for (
int j = 1; j <=
ports; j++) {
373 if (i == 1 && j == 1)
375 else if (i == 2 && j == 2)
379 if (i == 1 && j == 1)
381 else if (i == 2 && j == 2)
391 static void touchstone_finalize (
void) {
393 for (root = touchstone_vector; root != NULL; root = next) {
397 touchstone_vector = NULL;
398 if (touchstone_idents != NULL) {
400 touchstone_idents = NULL;
404 touchstone_options.
unit =
"GHz";
406 touchstone_options.
format =
"MA";
408 touchstone_options.
factor = 1e9;
409 touchstone_options.
ports = 0;
410 touchstone_options.
noise = 0;
411 touchstone_options.
lines = 0;
420 int i,
n, errors = 0;
423 if (touchstone_idents->
length () > 3) {
425 touchstone_idents->
length ());
429 for (i = 0; i < touchstone_idents->
length (); i++) {
430 for (
char * p = touchstone_idents->
get (i); *p !=
'\0'; p++)
434 for (i = 0; i < touchstone_idents->
length (); i++) {
435 char * str = touchstone_idents->
get (i);
436 if ((n = touchstone_idents->
contains (str)) != 1) {
443 for (i = 0; i < touchstone_idents->
length (); i++) {
444 char * str = touchstone_idents->
get (i);
446 for (
int v = 0; touchstone_valid_options[v] != NULL; v++) {
447 if (!strcmp (touchstone_valid_options[v], str))
458 touchstone_options_eval ();
460 if (touchstone_vector == NULL) {
469 errors += touchstone_vector_check ();
472 if ((touchstone_options.
parameter ==
'G' ||
474 touchstone_options.
ports != 2) {
476 "defined\n", touchstone_options.
parameter,
477 touchstone_options.
ports);
482 if (touchstone_options.
noise && touchstone_options.
ports != 2) {
484 "defined\n", touchstone_options.
ports);
491 touchstone_create ();
492 touchstone_normalize ();
500 touchstone_options.
noise ?
" including noise" :
"");
505 touchstone_finalize ();
507 return errors ? -1 : 0;
512 if (touchstone_result != NULL) {
515 touchstone_result = NULL;
517 if (touchstone_vector != NULL) {
518 touchstone_finalize ();
519 touchstone_vector = NULL;
525 touchstone_result = NULL;
526 touchstone_vector = NULL;
527 touchstone_idents = NULL;