Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
touchstone_producer.cpp
Go to the documentation of this file.
1 /*
2  * touchstone_producer.cpp - the Touchstone data file producer
3  *
4  * Copyright (C) 2007 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id$
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <time.h>
32 #include <ctype.h>
33 #include <string.h>
34 
35 #include "touchstone_producer.h"
36 #include "matrix.h"
37 #include "matvec.h"
38 #include "constants.h"
39 
40 using namespace qucs;
41 
42 /* Global variables. */
43 /* dataset * qucs_data = NULL; -- already defined in CSV producer */
44 /* FILE * touchstone_out = NULL; -- already defined in Touchstone lexer */
45 
47  char parameter; // type of variable
48  int ports; // number of S-parameter ports
49  double resistance; // reference impedance
50  const char * format; // data format
51  qucs::vector * vd; // appropriate dependency vector
52  matvec * mv; // appropriate data matrix vector
53  qucs::vector * fmin; // minimum noise figure
54  qucs::vector * sopt; // optimum input refelction for minimum noise figure
55  qucs::vector * rn; // effective noise resistance
56  qucs::vector * vf; // dependency vector for noise
57 }
59 
60 /* Definition of line separator. */
61 #ifdef __MINGW32__
62 #define touchstone_crlf "\n"
63 #else
64 #define touchstone_crlf "\r\n"
65 #endif
66 
67 /* The Touchstone noise data printer. */
69  if (touchstone_data.vf != NULL && touchstone_data.sopt != NULL &&
70  touchstone_data.rn != NULL && touchstone_data.fmin != NULL) {
71  // blank line separator
73  // noise data
74  for (int i = 0; i < touchstone_data.vf->getSize (); i++) {
75  nr_double_t f = real (touchstone_data.vf->get (i));
76  fprintf (touchstone_out, "%." NR_DECS "e"
77  " %+." NR_DECS "e"
78  " %+." NR_DECS "e" " %+." NR_DECS "e"
79  " %+." NR_DECS "e"
80  touchstone_crlf, f,
81  10.0 * std::log10 (real (touchstone_data.fmin->get (i))),
84  real (touchstone_data.rn->get (i)) /
86  }
87  }
88 }
89 
90 /* The Touchstone data printer. */
91 void touchstone_print (void) {
92  // header line
93  fprintf (touchstone_out, "# %s %c %s R %g" touchstone_crlf,
96  // one-port file
97  if (touchstone_data.ports == 1) {
98  for (int i = 0; i < touchstone_data.vd->getSize (); i++) {
99  matrix S = touchstone_data.mv->get (i);
100  nr_double_t f = real (touchstone_data.vd->get (i));
101  fprintf (touchstone_out, "%." NR_DECS "e"
102  " %+." NR_DECS "e" " %+." NR_DECS "e"
103  touchstone_crlf, f, real (S(0,0)), imag (S(0,0)));
104  }
105  }
106  // two-port file
107  else if (touchstone_data.ports == 2) {
108  for (int i = 0; i < touchstone_data.vd->getSize (); i++) {
109  matrix S = touchstone_data.mv->get (i);
110  nr_double_t f = real (touchstone_data.vd->get (i));
111  fprintf (touchstone_out, "%." NR_DECS "e"
112  " %+." NR_DECS "e" " %+." NR_DECS "e"
113  " %+." NR_DECS "e" " %+." NR_DECS "e"
114  " %+." NR_DECS "e" " %+." NR_DECS "e"
115  " %+." NR_DECS "e" " %+." NR_DECS "e"
116  touchstone_crlf, f,
117  real (S(0,0)), imag (S(0,0)),
118  real (S(1,0)), imag (S(1,0)),
119  real (S(0,1)), imag (S(0,1)),
120  real (S(1,1)), imag (S(1,1)));
121  }
122  }
123  // three-port file
124  else if (touchstone_data.ports == 3) {
125  for (int i = 0; i < touchstone_data.vd->getSize (); i++) {
126  matrix S = touchstone_data.mv->get (i);
127  nr_double_t f = real (touchstone_data.vd->get (i));
128  fprintf (touchstone_out, "%." NR_DECS "e"
129  " %+." NR_DECS "e" " %+." NR_DECS "e"
130  " %+." NR_DECS "e" " %+." NR_DECS "e"
131  " %+." NR_DECS "e" " %+." NR_DECS "e"
132  touchstone_crlf " %" NR_DECS "s"
133  " %+." NR_DECS "e" " %+." NR_DECS "e"
134  " %+." NR_DECS "e" " %+." NR_DECS "e"
135  " %+." NR_DECS "e" " %+." NR_DECS "e"
136  touchstone_crlf " %" NR_DECS "s"
137  " %+." NR_DECS "e" " %+." NR_DECS "e"
138  " %+." NR_DECS "e" " %+." NR_DECS "e"
139  " %+." NR_DECS "e" " %+." NR_DECS "e"
141  f,
142  real (S(0,0)), imag (S(0,0)),
143  real (S(0,1)), imag (S(0,1)),
144  real (S(0,2)), imag (S(0,2)), " ",
145  real (S(1,0)), imag (S(1,0)),
146  real (S(1,1)), imag (S(1,1)),
147  real (S(1,2)), imag (S(1,2)), " ",
148  real (S(2,0)), imag (S(2,0)),
149  real (S(2,1)), imag (S(2,1)),
150  real (S(2,2)), imag (S(2,2)));
151  }
152  }
153  // four-port and above files
154  else if (touchstone_data.ports >= 4) {
155  for (int i = 0; i < touchstone_data.vd->getSize (); i++) {
156  matrix S = touchstone_data.mv->get (i);
157  nr_double_t f = real (touchstone_data.vd->get (i));
158  int cs = S.getCols ();
159  int rs = S.getRows ();
160  fprintf (touchstone_out, "%." NR_DECS "e", f);
161  for (int r = 0; r < rs; r++) {
162  if (r >= 1)
163  fprintf (touchstone_out, " %" NR_DECS "s", " ");
164  for (int c = 0; c < cs; c++) {
165  if (c > 1 && (c & 3) == 0)
166  fprintf (touchstone_out, " %" NR_DECS "s", " ");
167  fprintf (touchstone_out, " %+." NR_DECS "e" " %+." NR_DECS "e",
168  real (S(r,c)), imag (S(r,c)));
169  if ((c > 1 && (c & 3) == 3) || (c == cs - 1))
170  fprintf (touchstone_out, touchstone_crlf);
171  }
172  }
173  }
174  }
175 }
176 
177 /* The function finds an appropriate S-parameters or other parameter
178  type (G, H, Y, Z, etc.) matrix from the given dataset and stores it
179  into the global Touchstone structure. */
180 void touchstone_find_data (dataset * data, const char * name) {
181  qucs::vector * v;
182  char * n, * vn, * vd = NULL, * vf = NULL;
183  strlist * deps;
184  int r, c, rs = -1, cs = -1, s = 0;
185 
186  // find parameter matrix data and its dimensions
187  for (v = data->getVariables (); v != NULL; v = (::vector *) v->getNext ()) {
188  vn = v->getName ();
189  // requested matrix vector name found?
190  if (strstr (vn, name) == vn) {
191  if ((n = matvec::isMatrixVector (vn, r, c)) != NULL) {
192  if (rs < r) rs = r;
193  if (cs < c) cs = c;
194  s = v->getSize ();
195  free (n);
196  if ((deps = v->getDependencies ()) != NULL) {
197  vd = deps->get (0);
198  }
199  }
200  }
201  // minimum noise figure?
202  if (!strcmp (vn, "Fmin")) {
204  if ((deps = v->getDependencies ()) != NULL) vf = deps->get (0);
205  }
206  // optimal input reflection for minimum noise figure?
207  else if (!strcmp (vn, "Sopt")) {
209  if ((deps = v->getDependencies ()) != NULL) vf = deps->get (0);
210  }
211  // effective noise resitance?
212  else if (!strcmp (vn, "Rn")) {
213  touchstone_data.rn = v;
214  if ((deps = v->getDependencies ()) != NULL) vf = deps->get (0);
215  }
216  }
217 
218  // matrix entries found
219  if (rs >= 0 && cs >= 0 && vd != NULL) {
220  int ss = rs > cs ? rs : cs;
221  // fill in number of ports
222  touchstone_data.ports = ss + 1;
223  // create quadratic matrix vector
224  matvec * mv = new matvec (s, ss + 1, ss + 1);
225  mv->setName (name);
226  // fill in matrix vectors
227  for (v = data->getVariables (); v; v = (::vector *) v->getNext ()) {
228  vn = v->getName ();
229  if (strstr (vn, name) == vn) {
230  if ((n = matvec::isMatrixVector (vn, r, c)) != NULL) {
231  mv->set (*v, r, c);
232  free (n);
233  }
234  }
235  }
236  // store in global structure
237  touchstone_data.mv = mv;
238  touchstone_data.parameter = toupper (mv->getName ()[0]);
239  // look for dependency (frequency) vector
240  for (v = data->getDependencies (); v; v = (::vector *) v->getNext ()) {
241  if (vd && !strcmp (v->getName (), vd)) {
242  touchstone_data.vd = v;
243  }
244  if (vf && !strcmp (v->getName (), vf)) {
245  touchstone_data.vf = v;
246  }
247  }
248  }
249 }
250 
251 /* This is the overall Touchstone producer. */
252 void touchstone_producer (const char * variable) {
253 
254  // apply data variable name
255  if (variable == NULL) {
256  variable = "S";
257  }
258 
259  // initialize global Touchstone structure
260  touchstone_data.mv = NULL;
261  touchstone_data.vd = NULL;
262  touchstone_data.format = "RI";
264  touchstone_data.fmin = NULL;
265  touchstone_data.sopt = NULL;
266  touchstone_data.rn = NULL;
267  touchstone_data.vf = NULL;
268 
269  // look for appropriate matrix data
270  touchstone_find_data (qucs_data, variable);
271 
272  // print matrix data if available
273  if (touchstone_data.mv != NULL) {
274  touchstone_print ();
275  delete touchstone_data.mv;
277  }
278  else {
279  fprintf (stderr, "no such data variable `%s' found\n", variable);
280  }
281 }
#define touchstone_crlf
qucs::vector get(int, int)
Definition: matvec.cpp:116
matrix real(matrix a)
Real part matrix.
Definition: matrix.cpp:568
matrix abs(matrix a)
Computes magnitude of each matrix element.
Definition: matrix.cpp:531
int getSize(void) const
Definition: vector.cpp:192
name
Definition: parse_mdl.y:352
Global physical constants header file.
n
Definition: parse_citi.y:147
#define S(con)
Definition: property.cpp:158
r
Definition: parse_mdl.y:515
object * getNext(void)
Definition: object.h:59
i
Definition: parse_mdl.y:516
matrix imag(matrix a)
Imaginary part matrix.
Definition: matrix.cpp:581
void touchstone_print(void)
free($1)
nr_complex_t log10(const nr_complex_t z)
Compute principal value of decimal logarithm of z.
Definition: complex.cpp:225
Dense matrix class header file.
FILE * touchstone_out
v
Definition: parse_zvr.y:141
struct touchstone_data_t touchstone_data
int getCols(void)
Definition: matrix.h:103
#define deg(x)
Convert radian to degree.
Definition: constants.h:123
char * getName(void)
Definition: object.cpp:84
void touchstone_print_noise(void)
void touchstone_find_data(dataset *data, const char *name)
qucs::dataset * qucs_data
vd
Definition: parse_zvr.y:181
strlist * getDependencies(void)
Definition: vector.cpp:138
nr_complex_t get(int)
Definition: vector.cpp:179
void touchstone_producer(const char *variable)
matrix arg(matrix a)
Computes the argument of each matrix element.
Definition: matrix.cpp:555
static char * isMatrixVector(char *, int &, int &)
Definition: matvec.cpp:148
data
Definition: parse_citi.y:117