Debian Codegraph @

skrf/calibration/tests/test_calibration.py

raw symbols SCIP
1 import copy
2 import unittest
3 import warnings
4
5 import numpy as np
6 import pytest
7
8 import skrf as rf
9 from skrf import two_port_reflect
10 from skrf.calibration import (
11 PHN,
12 SOLT,
13 MultiportCal,
14 MultiportSOLT,
15 NISTMultilineTRL,
16 TUGMultilineTRL,
17 TwelveTerm,
18 TwoPortOnePath,
19 UnknownThru,
20 compute_switch_terms,
21 determine_line,
22 determine_reflect,
23 terminate,
24 terminate_nport,
25 )
26 from skrf.media import Coaxial, DistributedCircuit
27 from skrf.networkSet import NetworkSet
28 from skrf.util import suppress_warning_decorator
29
30 # number of frequency points to test calibration at .
31 # i choose 1 for speed, but given that many tests employ *random*
32 # networks values >100 are better for initial verification
33 global NPTS
34 NPTS = 1
35
36 # WR10/WG27/R900 75 to 110 GHz, 0.1x0.05 inch (2.54x1.27 mm)
37 # z0 from 610 to 446 ohm
38 WG_lossless = rf.RectangularWaveguide(rf.F(75, 100, NPTS, unit='GHz'),
39 a=100*rf.mil, z0_override=50)
40 WG = rf.RectangularWaveguide(rf.F(75, 100, NPTS, unit='GHz'), a=100*rf.mil,
41 rho='gold', z0_override=50)
42
43 def _compare_dicts_allclose(first: dict, second: dict) -> None:
44 assert first.keys() == second.keys()
45 for k in first.keys():
46 np.testing.assert_allclose(first[k], second[k], err_msg=f"Values from key '{k}' not equal!")
47
48
49 class DetermineTest(unittest.TestCase):
50 def setUp(self):
51 self.wg = WG
52 wg = self.wg
53 self.X = wg.random(n_ports =2, name = 'X')
54 self.Y = wg.random(n_ports =2, name='Y')
55
56
57 #thru
58 self.T = wg.thru()
59 self.T_m = self.embed(self.T)
60
61 #line
62 self.L = wg.line(80,'deg')
63 self.L_approx = self.wg.line(90,'deg')
64 self.L_m = self.embed(self.L)
65
66 # reflect
67 self.r = [ wg.delay_load(p,k,'deg') \
68 for k in [-10,10,88,92] \
69 for p in [-.9,-1]]
70
71
72 self.R = [rf.two_port_reflect(k,k) for k in self.r]
73
74 short= wg.short()
75 open = wg.open()
76 self.r_estimate = [short, short, short, short, open, open, open ,open]
77 self.R_m = [self.embed(k) for k in self.R]
78
79 def embed(self,x):
80 return self.X**x**self.Y
81
82 def test_determine_line(self):
83 L_found = determine_line(self.T_m, self.L_m,
84 line_approx=self.L_approx)
85 self.assertEqual(L_found,self.L)
86
87 def test_determine_reflect(self):
88 r_found = [determine_reflect(self.T_m,k,self.L_m, l, self.L_approx) \
89 for k,l in zip(self.R_m, self.r_estimate)]
90
91 [ self.assertEqual(k,l) for k,l in zip(self.r, r_found)]
92
93 def test_determine_reflect_matched_thru_and_line_ideal_reflect(self):
94 freq = rf.F(.25, .7, 21, unit = 'GHz')
95 medium = Coaxial.from_attenuation_VF(freq, att = 3.0, VF = .69)
96
97 thru = medium.line(0, 'm')
98 line = medium.line(0.12, 'm')
99
100 short = rf.two_port_reflect(medium.short(), medium.short())
101 r = determine_reflect(thru, short, line)
102 np.testing.assert_array_almost_equal( r.s, -np.ones_like(r.s))
103
104 reflect = rf.two_port_reflect(medium.open(), medium.open())
105 r = determine_reflect(thru, reflect, line, reflect_approx = medium.open())
106 np.testing.assert_array_almost_equal(r.s, np.ones_like(r.s))
107
108 def test_determine_reflect_matched_thru_and_line(self):
109 freq = rf.F(.25, .7, 40, unit = 'GHz')
110 medium = Coaxial.from_attenuation_VF(freq, att = 3.0, VF = .69)
111
112 thru = medium.line(0, 'm')
113 line = medium.line(0.12, 'm')
114 short = medium.short()
115
116 rng = np.random.default_rng(12)
117 short.s[:, 0, 0] += rng.uniform(-.02, 0.02, freq.f.size) + rng.uniform(-.02, 0.02, freq.f.size)*1j
118
119 r = determine_reflect(thru, rf.two_port_reflect(short, short), line)
120 np.testing.assert_array_almost_equal( r.s, short.s)
121
122
123 def test_determine_reflect_regression(self):
124 # this test case fails with only regularization on the t-parameters of thru ** line.inv, see gh-870
125 freq = rf.F(434615384.6153846e-9, .7, 1, unit = 'GHz')
126 medium = Coaxial.from_attenuation_VF(freq, att = 3.0, VF = .69)
127 thru= medium.line(0, 'm')
128 line = medium.line(0.12, 'm')
129 short = rf.Network(frequency=freq, s=[[[(-1.+0.017870117714376983j)] ] ])
130
131 r = determine_reflect(thru, rf.two_port_reflect(short, short), line)
132 np.testing.assert_array_almost_equal( r.s, short.s)
133
134 class ComputeSwitchTermsTest(unittest.TestCase):
135 '''
136 test the indirect method of computing the switch terms
137 with at least three reciprocal devices
138 '''
139 def setUp(self):
140 self.n_ports = 2
141 self.wg = WG
142 wg = self.wg
143
144 self.X = wg.random(n_ports =2, name = 'X')
145 self.Y = wg.random(n_ports =2, name='Y')
146
147 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
148 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
149
150 # Reciprocal devices: asymmetric and both transmissive and reflective devices
151 Rs = [25, 50, 100, ]
152 stands = [wg.resistor(R)**wg.shunt_resistor(R) for R in Rs]
153 stands_meas = [terminate(self.X**k**self.Y, self.gamma_f, self.gamma_r) for k in stands]
154
155 self.gamma_f_indirect, self.gamma_r_indirect = compute_switch_terms(stands_meas)
156
157 def test_gamma_f(self):
158 self.assertTrue(all(np.abs(self.gamma_f_indirect.s - self.gamma_f.s) < 1e-9))
159
160 def test_gamma_r(self):
161 self.assertTrue(all(np.abs(self.gamma_r_indirect.s - self.gamma_r.s) < 1e-9))
162
163 class CalibrationTest:
164 """
165 This is the generic Calibration test case which all Calibration
166 Subclasses should be able to pass. They must implement
167 """
168
169 def test_accuracy_of_dut_correction(self):
170 a = self.wg.random(n_ports=self.n_ports, name = 'actual')
171 m = self.measure(a)
172 c = self.cal.apply_cal(m)
173 c.name = 'corrected'
174 self.assertEqual(c,a)
175
176 def test_error_ntwk(self):
177 a = self.cal.error_ntwk
178
179 def test_coefs_ntwks(self):
180 a = self.cal.coefs_ntwks
181
182 @suppress_warning_decorator("only gave a single measurement orientation")
183 def test_caled_ntwks(self):
184 a = self.cal.caled_ntwks
185
186 @suppress_warning_decorator("only gave a single measurement orientation")
187 def test_residual_ntwks(self):
188 a = self.cal.residual_ntwks
189
190 def test_embed_then_apply_cal(self):
191 a = self.wg.random(n_ports=self.n_ports)
192 self.assertEqual(self.cal.apply_cal(self.cal.embed(a)),a)
193
194 def test_embed_equal_measure(self):
195 a = self.wg.random(n_ports=self.n_ports)
196 self.assertEqual(self.cal.embed(a),self.measure(a))
197
198 @suppress_warning_decorator("n_thrus is None")
199 def test_from_coefs(self):
200 cal_from_coefs = self.cal.from_coefs(self.cal.frequency, self.cal.coefs)
201 ntwk = self.wg.random(n_ports=self.n_ports)
202 self.assertEqual(cal_from_coefs.apply_cal(self.cal.embed(ntwk)),ntwk)
203
204 @suppress_warning_decorator("n_thrus is None")
205 def test_from_coefs_ntwks(self):
206 cal_from_coefs = self.cal.from_coefs_ntwks(self.cal.coefs_ntwks)
207 ntwk = self.wg.random(n_ports=self.n_ports)
208 self.assertEqual(cal_from_coefs.apply_cal(self.cal.embed(ntwk)),ntwk)
209
210
211 class CalibrationInputsTest(unittest.TestCase):
212 """Test Calibration class input validation"""
213 def setUp(self):
214 self.n_ports = 2
215 self.wg = WG
216 wg = self.wg
217
218 self.ideals = [
219 wg.short(nports=2, name='short'),
220 wg.open(nports=2, name='open'),
221 wg.match(nports=2, name='load'),
222 wg.thru(name='thru'),
223 ]
224
225 self.measured = [k.copy() for k in self.ideals]
226
227 def init(self):
228 self.cal = rf.Calibration(
229 ideals = self.ideals,
230 measured = self.measured
231 )
232
233 def test_meas_z0(self):
234 self.measured[0].z0 *= 0.5
235 with pytest.raises(ValueError):
236 self.init()
237
238 def test_ideals_z0(self):
239 self.ideals[0].z0 *= 0.5
240 with pytest.raises(ValueError):
241 self.init()
242
243 def test_ideals_frequency(self):
244 freqs = rf.F(1, 10, NPTS+1, unit="GHz")
245 self.ideals[-1] = rf.Network(s=np.zeros([NPTS + 1, 2, 2]), frequency=freqs)
246 with pytest.raises(IndexError):
247 self.init()
248
249 def test_measured_frequency(self):
250 freqs = rf.F(1, 10, NPTS+1, unit="GHz")
251 self.measured[-1] = rf.Network(s=np.zeros([NPTS + 1, 2, 2]), frequency=freqs)
252 with pytest.raises(ValueError):
253 self.init()
254
255 def test_ideals_frequency2(self):
256 freqs = self.wg.frequency.copy()
257 freqs._f += 1e9
258 self.ideals[-1].frequency = freqs
259 with pytest.raises(IndexError):
260 self.init()
261
262 def test_measured_frequency2(self):
263 freqs = self.wg.frequency.copy()
264 freqs._f += 1e9
265 self.measured[-1].frequency = freqs
266 with pytest.raises(ValueError):
267 self.init()
268
269 class OnePortTest(unittest.TestCase, CalibrationTest):
270 """
271 One-port calibration test.
272
273
274 """
275 def setUp(self):
276 self.n_ports = 1
277 self.wg = WG
278 wg = self.wg
279
280 self.E = wg.random(n_ports =2, name = 'E')
281
282 ideals = [
283 wg.short( name='short'),
284 wg.delay_short( 45.,'deg',name='ew'),
285 wg.delay_short( 90.,'deg',name='qw'),
286 wg.match( name='load'),
287 ]
288 measured = [self.measure(k) for k in ideals]
289
290 self.cal = rf.OnePort(
291 is_reciprocal = True,
292 ideals = ideals,
293 measured = measured,
294 )
295
296 def test_input_networks_1port(self):
297 # test users do not enter 2-port networks by accident
298 with self.assertRaises(ValueError):
299 wg = self.wg
300 ideals = [
301 two_port_reflect(wg.short( name='short'), wg.short( name='short')),
302 wg.delay_short( 45.,'deg',name='ew'),
303 wg.delay_short( 90.,'deg',name='qw'),
304 wg.match( name='load'),
305 ]
306 measured = [self.measure(k) for k in ideals]
307 # create another calibration instance of the same class
308 cal_class = type(self.cal)
309 cal = cal_class(
310 is_reciprocal = True,
311 ideals = ideals,
312 measured = measured,
313 )
314 cal.run()
315
316 def measure(self, ntwk):
317 out = self.E**ntwk
318 out.name = ntwk.name
319 return out
320
321 def test_accuracy_of_directivity(self):
322 self.assertEqual(
323 self.E.s11,
324 self.cal.coefs_ntwks['directivity'],
325 )
326
327 def test_accuracy_of_source_match(self):
328 self.assertEqual(
329 self.E.s22,
330 self.cal.coefs_ntwks['source match'],
331 )
332
333 def test_accuracy_of_reflection_tracking(self):
334 self.assertEqual(
335 self.E.s21*self.E.s12,
336 self.cal.coefs_ntwks['reflection tracking'],
337 )
338
339
340 class SDDLTest(OnePortTest):
341 def setUp(self):
342 self.n_ports = 1
343 #Exact only with a lossless waveguide
344 self.wg = WG_lossless
345 wg = self.wg
346
347 self.E = wg.random(n_ports =2, name = 'E')
348 #self.E.s[0,:,:] = np.array([[.1j,1],[1j,1j+2]])
349 #print self.E.s[0]
350
351 ideals = [
352 wg.short( name='short'),
353 wg.delay_short( 45.,'deg',name='ew'),
354 wg.delay_short( 90.,'deg',name='qw'),
355 wg.load(.2+.2j, name='load'),
356 ]
357 actuals = [
358 wg.short( name='short'),
359 wg.delay_short( 10.,'deg',name='ew'),
360 wg.delay_short( 33.,'deg',name='qw'),
361 wg.load(.2+.2j, name='load'),
362 ]
363 measured = [self.measure(k) for k in actuals]
364
365 self.cal = rf.SDDL(
366 is_reciprocal = True,
367 ideals = ideals,
368 measured = measured,
369 )
370
371 def test_init_with_nones(self):
372 wg=self.wg
373 wg.frequency = rf.F.from_f([100], unit='GHz')
374
375 self.E = wg.random(n_ports =2, name = 'E')
376
377 ideals = [
378 wg.short( name='short'),
379 None,
380 None,
381 wg.load(.2+.2j, name='load'),
382 ]
383 actuals = [
384 wg.short( name='short'),
385 wg.delay_short( 10.,'deg',name='ew'),
386 wg.delay_short( 33.,'deg',name='qw'),
387 wg.load(.2+.2j, name='load'),
388 ]
389 measured = [self.measure(k) for k in actuals]
390
391 self.cal = rf.SDDL(
392 is_reciprocal = True,
393 ideals = ideals,
394 measured = measured,
395 )
396 self.cal.run()
397
398 @pytest.mark.skip(reason='not applicable ')
399 def test_from_coefs(self):
400 pass
401
402 @pytest.mark.skip(reason='not applicable ')
403 def test_from_coefs_ntwks(self):
404 pass
405
406
407 class SDDLWeikle(OnePortTest):
408 def setUp(self):
409 self.n_ports = 1
410 #Exact only with a lossless waveguide
411 self.wg = WG_lossless
412 wg = self.wg
413 self.E = wg.random(n_ports =2, name = 'E')
414 #self.E.s[0,:,:] = np.array([[.1j,1],[1j,1j+2]])
415 #print self.E.s[0]
416
417 ideals = [
418 wg.short( name='short'),
419 wg.delay_short( 45.,'deg',name='ew'),
420 wg.delay_short( 90.,'deg',name='qw'),
421 wg.load(.2+.2j, name='load'),
422 ]
423 actuals = [
424 wg.short( name='short'),
425 wg.delay_short( 10.,'deg',name='ew'),
426 wg.delay_short( 80.,'deg',name='qw'),
427 wg.load(.2+.2j, name='load'),
428 ]
429 measured = [self.measure(k) for k in actuals]
430
431 self.cal = rf.SDDLWeikle(
432 is_reciprocal = True,
433 ideals = ideals,
434 measured = measured,
435 )
436
437 @pytest.mark.skip(reason='not applicable ')
438 def test_from_coefs(self):
439 pass
440
441 @pytest.mark.skip(reason='not applicable ')
442 def test_from_coefs_ntwks(self):
443 pass
444
445
446 class SDDMTest(OnePortTest):
447 """
448 This is a specific test of SDDL to verify it works when the load is
449 a matched load. This test has been used to show that the SDDLWeikle
450 variant fails, with a perfect matched load.
451 """
452 def setUp(self):
453
454 self.n_ports = 1
455 #Exact only with a lossless waveguide
456 self.wg = WG_lossless
457 wg = self.wg
458
459 self.E = wg.random(n_ports =2, name = 'E')
460
461 ideals = [
462 wg.short( name='short'),
463 wg.delay_short( 45.,'deg',name='ew'),
464 wg.delay_short( 90.,'deg',name='qw'),
465 wg.match( name='load'),
466 ]
467 actuals = [
468 wg.short( name='short'),
469 wg.delay_short( 10.,'deg',name='ew'),
470 wg.delay_short( 80.,'deg',name='qw'),
471 wg.match(name='load'),
472 ]
473 measured = [self.measure(k) for k in actuals]
474
475 self.cal = rf.SDDL(
476 is_reciprocal = True,
477 ideals = ideals,
478 measured = measured,
479 )
480
481 @pytest.mark.skip(reason='not applicable ')
482 def test_from_coefs(self):
483 pass
484
485 @pytest.mark.skip(reason='not applicable ')
486 def test_from_coefs_ntwks(self):
487 pass
488
489
490 @pytest.mark.skip()
491 class PHNTest(OnePortTest):
492 """
493 """
494 def setUp(self):
495
496 self.n_ports = 1
497 self.wg = WG
498 wg = self.wg
499
500 self.E = wg.random(n_ports =2, name = 'E')
501 known1 = wg.random()
502 known2 = wg.random()
503 #known1 = wg.short()
504 #known2 = wg.load(rand() + rand()*1j)
505
506 ideals = [
507 wg.delay_short( 45.,'deg',name='ideal ew'),
508 wg.delay_short( 90.,'deg',name='ideal qw'),
509 known1,
510 known2,
511 ]
512 actuals = [
513 wg.delay_short( 33.,'deg',name='true ew'),
514 wg.delay_short( 110.,'deg',name='true qw'),
515 known1,
516 known2,
517 ]
518 measured = [self.measure(k) for k in actuals]
519 self.actuals = actuals
520
521 self.cal = PHN(
522 is_reciprocal = True,
523 ideals = ideals,
524 measured = measured,
525 )
526
527
528 def test_determine_ideals(self):
529 self.cal.run()
530
531 self.assertEqual(self.actuals[0], self.cal.ideals[0])
532 self.assertEqual(self.actuals[1], self.cal.ideals[1])
533
534 @pytest.mark.skip(reason='not applicable ')
535 def test_from_coefs(self):
536 pass
537
538 @pytest.mark.skip(reason='not applicable ')
539 def test_from_coefs_ntwks(self):
540 pass
541
542
543 class EightTermTest(unittest.TestCase, CalibrationTest):
544 def setUp(self):
545 self.n_ports = 2
546 self.wg =WG
547 wg= self.wg
548
549 self.X = wg.random(n_ports =2, name = 'X')
550 self.Y = wg.random(n_ports =2, name='Y')
551 #Isolation terms
552 self.If = wg.random(n_ports=1, name='If')
553 self.Ir = wg.random(n_ports=1, name='Ir')
554 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
555 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
556
557 ideals = [
558 wg.short(nports=2, name='short'),
559 wg.open(nports=2, name='open'),
560 wg.match(nports=2, name='load'),
561 wg.thru(name='thru'),
562 ]
563
564 measured = [self.measure(k) for k in ideals]
565
566 self.cal = rf.EightTerm(
567 ideals = ideals,
568 measured = measured,
569 isolation = measured[2],
570 switch_terms = (self.gamma_f, self.gamma_r)
571 )
572
573 def terminate(self, ntwk):
574 """
575 terminate a measured network with the switch terms
576 """
577 return terminate(ntwk,self.gamma_f, self.gamma_r)
578
579 def measure(self,ntwk):
580 out = self.terminate(self.X**ntwk**self.Y)
581 out.name = ntwk.name
582 out.s[:,1,0] += self.If.s[:,0,0]
583 out.s[:,0,1] += self.Ir.s[:,0,0]
584 return out
585
586 def test_renormalize(self):
587 dut = self.wg.random(n_ports=2, name='DUT')
588 dut_meas = self.measure(dut)
589 z_new = 100-30j
590 self.cal.renormalize(50, z_new)
591 dut_cal = self.cal.apply_cal(dut_meas)
592 dut.renormalize(z_new, s_def="traveling")
593 dut.z0 = 50
594 self.assertEqual(dut, dut_cal)
595
596 def test_renormalize2(self):
597 """
598 Test that roundtrip renormalization gives back the original calibration
599 """
600 old_coefs = copy.deepcopy(self.cal.coefs)
601 z_new = 10 + 20j
602 self.cal.renormalize(50, z_new)
603 self.cal.renormalize(z_new, 50)
604 for k in self.cal.coefs.keys():
605 np.testing.assert_array_almost_equal(old_coefs[k], self.cal.coefs[k], err_msg=k)
606
607 def test_unterminating(self):
608 a = self.wg.random(n_ports=self.n_ports)
609 #unterminated measurement
610 ut = self.X**a**self.Y
611 #terminated measurement
612 m = self.measure(a)
613 #Remove leakage
614 m.s[:,1,0] -= self.If.s[:,0,0]
615 m.s[:,0,1] -= self.Ir.s[:,0,0]
616 self.assertEqual(self.cal.unterminate(m), ut)
617
618 def test_forward_directivity_accuracy(self):
619 self.assertEqual(
620 self.X.s11,
621 self.cal.coefs_ntwks['forward directivity'])
622
623 def test_forward_source_match_accuracy(self):
624 self.assertEqual(
625 self.X.s22 ,
626 self.cal.coefs_ntwks['forward source match'] )
627
628 def test_forward_reflection_tracking_accuracy(self):
629 self.assertEqual(
630 self.X.s21 * self.X.s12 ,
631 self.cal.coefs_ntwks['forward reflection tracking'])
632
633 def test_reverse_source_match_accuracy(self):
634 self.assertEqual(
635 self.Y.s11 ,
636 self.cal.coefs_ntwks['reverse source match'] )
637
638 def test_reverse_directivity_accuracy(self):
639 self.assertEqual(
640 self.Y.s22 ,
641 self.cal.coefs_ntwks['reverse directivity'] )
642
643 def test_reverse_reflection_tracking_accuracy(self):
644 self.assertEqual(
645 self.Y.s21 * self.Y.s12 ,
646 self.cal.coefs_ntwks['reverse reflection tracking'])
647
648 def test_k_accuracy(self):
649 self.assertEqual(
650 self.X.s21/self.Y.s12 ,
651 self.cal.coefs_ntwks['k'] )
652
653 def test_forward_isolation_accuracy(self):
654 self.assertEqual(
655 self.If.s11 ,
656 self.cal.coefs_ntwks['forward isolation'] )
657
658 def test_reverse_isolation_accuracy(self):
659 self.assertEqual(
660 self.Ir.s11 ,
661 self.cal.coefs_ntwks['reverse isolation'] )
662
663 def test_verify_12term(self):
664 self.assertTrue(self.cal.verify_12term_ntwk.s_mag.max() < 1e-3)
665
666 def test_coefs_8term(self):
667 self.assertEqual(self.cal.coefs_8term, self.cal.coefs)
668
669 def test_coefs_12term(self):
670 _compare_dicts_allclose(self.cal.coefs_12term, rf.convert_8term_2_12term(self.cal.coefs))
671
672 def test_input_not_modified(self):
673 dut_before = self.X.copy()
674 self.cal.apply_cal(self.X)
675 self.assertEqual(dut_before, self.X)
676
677 def test_convert_8term_2_12term_subtests(self):
678
679 for st in [
680 (self.cal.coefs['forward switch term'], self.cal.coefs['reverse switch term']),
681 (np.zeros(self.cal.frequency.npoints, dtype=complex), np.zeros(self.cal.frequency.npoints, dtype=complex)),
682 (self.cal.coefs['forward switch term'], np.zeros(self.cal.frequency.npoints, dtype=complex)),
683 (np.zeros(self.cal.frequency.npoints, dtype=complex), self.cal.coefs['reverse switch term']),
684 ]:
685 with self.subTest(st=st):
686 coefs = copy.deepcopy(self.cal.coefs)
687
688 coefs['forward switch term'] = st[0]
689 coefs['reverse switch term'] = st[1]
690
691 coefs_12term = rf.convert_8term_2_12term(coefs)
692 coefs_8term = rf.convert_12term_2_8term(coefs_12term)
693 for k in self.cal.coefs.keys():
694 np.testing.assert_almost_equal(coefs_8term[k], coefs[k])
695
696 class TRLTest(EightTermTest):
697 def setUp(self):
698 self.n_ports = 2
699 self.wg = WG
700 wg= self.wg
701
702
703 self.X = wg.random(n_ports =2, name = 'X')
704 self.Y = wg.random(n_ports =2, name='Y')
705 self.If = wg.random(n_ports=1, name='If')
706 self.Ir = wg.random(n_ports=1, name='Ir')
707 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
708 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
709 # make error networks have s21,s12 >> s11,s22 so that TRL
710 # can guess at line length
711 self.X.s[:,0,0] *=1e-1
712 self.Y.s[:,0,0] *=1e-1
713 self.X.s[:,1,1] *=1e-1
714 self.Y.s[:,1,1] *=1e-1
715
716 actuals = [
717 wg.thru( name='thru'),
718 rf.two_port_reflect(wg.load(-.9-.1j),wg.load(-.9-.1j)),
719 wg.attenuator(-3,True, 45,'deg')
720 #wg.line(45,'deg',name='line'),
721 ]
722 self.actuals=actuals
723 ideals = [
724 wg.thru( name='thru'),
725 wg.short(nports=2, name='short'),
726 wg.line(90,'deg',name='line'),
727 ]
728
729 measured = [self.measure(k) for k in actuals]
730
731 self.cal = rf.TRL(
732 ideals = ideals,
733 measured = measured,
734 isolation = measured[1],
735 switch_terms = (self.gamma_f, self.gamma_r)
736 )
737
738 def test_found_line(self):
739 self.cal.run()
740 self.assertTrue(self.cal.ideals[2]==self.actuals[2])
741
742 def test_found_reflect(self):
743 self.cal.run()
744 self.assertTrue(self.cal.ideals[1]==self.actuals[1])
745
746 class TRLLongThruTest(EightTermTest):
747 """
748 Test TRL calibration with non-zero length thru.
749 In this case calibration is done at thru center.
750 """
751 def setUp(self):
752 self.n_ports = 2
753 self.wg = WG
754 wg = self.wg
755
756 self.X = wg.random(n_ports =2, name = 'X')
757 self.Y = wg.random(n_ports =2, name='Y')
758 self.If = wg.random(n_ports=1, name='If')
759 self.Ir = wg.random(n_ports=1, name='Ir')
760 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
761 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
762 # make error networks have s21,s12 >> s11,s22 so that TRL
763 # can guess at line length
764 self.X.s[:,0,0] *= 1e-1
765 self.Y.s[:,0,0] *= 1e-1
766 self.X.s[:,1,1] *= 1e-1
767 self.Y.s[:,1,1] *= 1e-1
768
769 # Reflect reference plane is at the thru center
770 self.reflect = wg.load(-.9-.1j)
771 reflect_shifted = wg.line(50, 'um') ** self.reflect
772
773 actuals = [
774 wg.line(100, 'um', name='thru'),
775 rf.two_port_reflect(reflect_shifted, reflect_shifted),
776 wg.line(1100, 'um', name='thru'),
777 ]
778
779 self.actuals = actuals
780
781 ideals = [
782 actuals[0],
783 wg.short(nports=2, name='short'),
784 actuals[2]
785 ]
786
787 measured = [self.measure(k) for k in actuals]
788
789 # Calibration is done at the center of the thru
790 # Add half thru to error networks so that tests pass
791 self.X = self.X ** wg.line(50, 'um')
792 self.Y = wg.line(50, 'um') ** self.Y
793
794 self.cal = rf.TRL(
795 ideals = ideals,
796 measured = measured,
797 isolation = measured[1],
798 switch_terms = (self.gamma_f, self.gamma_r)
799 )
800
801 def test_found_line(self):
802 self.cal.run()
803 # Solved line is difference between line and thru
804 self.assertTrue(self.cal.ideals[2]==self.actuals[2] ** self.actuals[0].inv)
805
806 def test_found_reflect(self):
807 self.cal.run()
808 # Solved reflect is at the thru center
809 self.assertTrue(self.cal.ideals[1]==rf.two_port_reflect(self.reflect, self.reflect))
810
811 class TRLWithNoIdealsTest(EightTermTest):
812 def setUp(self):
813 self.n_ports = 2
814 self.wg = WG
815 wg= self.wg
816
817 self.X = wg.random(n_ports =2, name = 'X')
818 self.Y = wg.random(n_ports =2, name='Y')
819 self.If = wg.random(n_ports=1, name='If')
820 self.Ir = wg.random(n_ports=1, name='Ir')
821 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
822 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
823 # make error networks have s21,s12 >> s11,s22 so that TRL
824 # can guess at line length
825 self.X.s[:,0,0] *=1e-1
826 self.Y.s[:,0,0] *=1e-1
827 self.X.s[:,1,1] *=1e-1
828 self.Y.s[:,1,1] *=1e-1
829
830 r = wg.delay_short(20,'deg')
831
832 self.actuals=[wg.thru( name='thru'),
833 rf.two_port_reflect(r,r),\
834 wg.attenuator(-3,True, 45,'deg')]
835
836 measured = [self.measure(k) for k in self.actuals]
837
838 self.cal = rf.TRL(
839 ideals = None,
840 measured = measured,
841 isolation = measured[1],
842 switch_terms = (self.gamma_f, self.gamma_r)
843 )
844
845 def test_found_line(self):
846 self.cal.run()
847 self.assertTrue(self.cal.ideals[2]==self.actuals[2])
848
849 def test_found_reflect(self):
850 self.cal.run()
851 self.assertTrue(self.cal.ideals[1]==self.actuals[1])
852
853
854 class TRLMultiline(EightTermTest):
855 def setUp(self):
856 self.n_ports = 2
857 self.wg = WG
858 wg= self.wg
859
860 self.X = wg.random(n_ports =2, name = 'X')
861 self.Y = wg.random(n_ports =2, name='Y')
862 self.If = wg.random(n_ports=1, name='If')
863 self.Ir = wg.random(n_ports=1, name='Ir')
864 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
865 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
866 # make error networks have s21,s12 >> s11,s22 so that TRL
867 # can guess at line length
868 #self.X.s[:,0,0] *=1e-1
869 #self.Y.s[:,0,0] *=1e-1
870 #self.X.s[:,1,1] *=1e-1
871 #self.Y.s[:,1,1] *=1e-1
872
873 actuals = [
874 wg.thru( name='thru'),
875 wg.short(nports=2, name='short'),
876 wg.open(nports=2, name='open'),
877 wg.attenuator(-3,True, 45,'deg'),
878 wg.attenuator(-6,True, 90,'deg'),
879 wg.attenuator(-8,True, 145,'deg'),
880 ]
881 self.actuals=actuals
882
883 measured = [self.measure(k) for k in actuals]
884
885 self.cal = rf.TRL(
886 ideals = [None, -1,1,None,None,None],
887 measured = measured,
888 isolation = measured[1],
889 switch_terms = (self.gamma_f, self.gamma_r),
890 n_reflects=2,
891 )
892
893 def test_found_line(self):
894 self.cal.run()
895 for k in range(2,5):
896 self.assertTrue(self.cal.ideals[k]==self.actuals[k])
897
898
899 class NISTMultilineTRLTest(EightTermTest):
900 def setUp(self):
901 self.n_ports = 2
902 self.wg = WG
903 wg= self.wg
904
905 self.X = wg.random(n_ports =2, name = 'X')
906 self.Y = wg.random(n_ports =2, name = 'Y')
907 self.If = wg.random(n_ports=1, name='If')
908 self.Ir = wg.random(n_ports=1, name='Ir')
909
910 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
911 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
912
913 actuals = [
914 wg.thru(),
915 rf.two_port_reflect(wg.load(-.98-.1j),wg.load(-.98-.1j)),
916 rf.two_port_reflect(wg.load(.99+0.05j),wg.load(.99+0.05j)),
917 wg.line(100,'um'),
918 wg.line(200,'um'),
919 wg.line(900,'um'),
920 ]
921
922 self.actuals=actuals
923
924 measured = [self.measure(k) for k in actuals]
925
926 self.cal = NISTMultilineTRL(
927 measured = measured,
928 isolation = measured[1],
929 Grefls = [-1, 1],
930 l = [0, 100e-6, 200e-6, 900e-6],
931 er_est = 1,
932 switch_terms = (self.gamma_f, self.gamma_r),
933 gamma_root_choice = 'real'
934 )
935
936 def test_gamma(self):
937 self.assertTrue(max(np.abs(self.wg.gamma-self.cal.gamma)) < 1e-3)
938
939
940 class NISTMultilineTRLTest2(NISTMultilineTRLTest):
941 """ Test characteristic impedance change and reference plane shift.
942 """
943 def setUp(self):
944 global NPTS
945 self.n_ports = 2
946 self.wg = WG
947 wg = self.wg
948
949 rng = np.random.default_rng()
950
951 r = rng.uniform(10,100,NPTS)
952 l = 1e-9*rng.uniform(100,200,NPTS)
953 g = np.zeros(NPTS)
954 c = 1e-12*rng.uniform(100,200,NPTS)
955
956 rlgc = DistributedCircuit(frequency=wg.frequency, z0=None, R=r, L=l, G=g, C=c)
957 self.rlgc = rlgc
958
959 self.X = wg.random(n_ports =2, name = 'X')
960 self.Y = wg.random(n_ports =2, name = 'Y')
961 self.If = wg.random(n_ports=1, name='If')
962 self.Ir = wg.random(n_ports=1, name='Ir')
963 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
964 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
965
966 actuals = [
967 rlgc.line(1000,'um'),
968 rlgc.line(500,'um') ** rlgc.short(nports=2) ** rlgc.line(500,'um'),
969 rlgc.line(1010,'um'),
970 rlgc.line(1100,'um'),
971 rlgc.line(1800,'um'),
972 ]
973
974 self.actuals=actuals
975
976 measured = [self.measure(k) for k in actuals]
977 self.measured = measured
978
979 self.cal = NISTMultilineTRL(
980 measured = measured,
981 isolation = measured[1],
982 Grefls = [-1],
983 refl_offset = 500e-6,
984 l = [1000e-6, 1010e-6, 1100e-6, 1800e-6],
985 switch_terms = (self.gamma_f, self.gamma_r),
986 c0=c,
987 z0_ref=50,
988 gamma_root_choice = 'real'
989 )
990
991 self.cal_shift = NISTMultilineTRL(
992 measured = measured,
993 isolation = measured[1],
994 Grefls = [-1],
995 refl_offset = 0,
996 l = [0, 10e-6, 100e-6, 800e-6],
997 switch_terms = (self.gamma_f, self.gamma_r),
998 ref_plane = -500e-6,
999 c0=c,
1000 z0_ref=50,
1001 gamma_root_choice = 'real'
1002 )
1003
1004 def test_gamma(self):
1005 self.assertTrue(max(np.abs(self.rlgc.gamma-self.cal.gamma)) < 1e-3)
1006
1007 def test_z0(self):
1008 self.assertTrue(max(np.abs(self.rlgc.z0-self.cal.z0)) < 1e-3)
1009
1010 def test_shift(self):
1011 for k in self.cal.coefs.keys():
1012 self.assertTrue(all(np.abs(self.cal.coefs[k] - self.cal_shift.coefs[k]) < 1e-9))
1013
1014
1015 def test_numpy_float_arguments(self):
1016 # see gh-895
1017 cal = NISTMultilineTRL(
1018 measured = self.measured[:3],
1019 Grefls = [-1],
1020 l = [np.float64(1000e-6), 1010e-6],
1021 switch_terms = (self.gamma_f, self.gamma_r),
1022 )
1023 cal.run()
1024 cal.apply_cal(self.measured[0])
1025
1026 cal = NISTMultilineTRL(
1027 measured = self.measured[:3],
1028 Grefls = [-1],
1029 l = [1000e-6, 1010e-6],
1030 z0_ref = np.float64(50),
1031 z0_line = np.float64(50),
1032 switch_terms = (self.gamma_f, self.gamma_r),
1033 )
1034 cal.run()
1035 cal.apply_cal(self.measured[0])
1036
1037 class TUGMultilineTest(EightTermTest):
1038 def setUp(self):
1039 self.n_ports = 2
1040 self.wg = WG
1041 wg= self.wg
1042
1043 self.X = wg.random(n_ports =2, name = 'X')
1044 self.Y = wg.random(n_ports =2, name = 'Y')
1045 self.If = wg.random(n_ports=1, name='If')
1046 self.Ir = wg.random(n_ports=1, name='Ir')
1047
1048 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
1049 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
1050
1051 actuals = [
1052 wg.thru(),
1053 rf.two_port_reflect(wg.load(-.98-.1j),wg.load(-.98-.1j)),
1054 rf.two_port_reflect(wg.load(.99+0.05j),wg.load(.99+0.05j)),
1055 wg.line(100,'um'),
1056 wg.line(200,'um'),
1057 wg.line(900,'um'),
1058 ]
1059
1060 self.actuals=actuals
1061
1062 measured = [self.measure(k) for k in actuals]
1063
1064 self.cal = TUGMultilineTRL(
1065 line_meas = [measured[0]] + measured[3:],
1066 line_lengths = [0, 100e-6, 200e-6, 900e-6],
1067 er_est = 1,
1068 reflect_meas = measured[1:3],
1069 reflect_est = [-1, 1],
1070 isolation = measured[1],
1071 switch_terms = (self.gamma_f, self.gamma_r)
1072 )
1073
1074 def test_gamma(self):
1075 self.assertTrue(max(np.abs(self.wg.gamma-self.cal.gamma)) < 1e-3)
1076
1077 @pytest.mark.skip()
1078 class TREightTermTest(unittest.TestCase, CalibrationTest):
1079 def setUp(self):
1080 self.n_ports = 2
1081 self.wg = WG
1082 wg= self.wg
1083
1084
1085 self.X = wg.random(n_ports =2, name = 'X')
1086 self.Y = wg.random(n_ports =2, name='Y')
1087
1088
1089 ideals = [
1090 wg.short(nports=2, name='short'),
1091 wg.open(nports=2, name='open'),
1092 wg.match(nports=2, name='load'),
1093 wg.thru(name='thru'),
1094 ]
1095
1096 measured = [self.measure_std(k) for k in ideals]
1097
1098 cal1 = rf.TwoPortOnePath(
1099 ideals = ideals,
1100 measured = measured
1101 )
1102 switch_terms = (cal1.coefs_ntwks['forward switch term'],
1103 cal1.coefs_ntwks['reverse switch term'])
1104
1105
1106 measured = [self.measure(k) for k in ideals]
1107 self.cal = rf.EightTerm(
1108 ideals = ideals,
1109 measured = measured,
1110 switch_terms = switch_terms,
1111 )
1112 raise ValueError()
1113
1114 def measure_std(self,ntwk):
1115 r= self.wg.random(2)
1116 m = ntwk.copy()
1117 mf = self.X**ntwk**self.Y
1118
1119 m.s[:,1,0] = mf.s[:,1,0]
1120 m.s[:,0,0] = mf.s[:,0,0]
1121 m.s[:,1,1] = r.s[:,1,1]
1122 m.s[:,0,1] = r.s[:,0,1]
1123 return m
1124 def measure(self,ntwk):
1125
1126 m = ntwk.copy()
1127 mf = self.X**ntwk**self.Y
1128 mr = self.X**ntwk.flipped()**self.Y
1129
1130 m.s[:,1,0] = mf.s[:,1,0]
1131 m.s[:,0,0] = mf.s[:,0,0]
1132 m.s[:,1,1] = mr.s[:,0,0]
1133 m.s[:,0,1] = mr.s[:,1,0]
1134 return m
1135
1136
1137 class TwelveTermTest(unittest.TestCase, CalibrationTest):
1138 """
1139 This test verifies the accuracy of the SOLT calibration. Generating
1140 measured networks requires different error networks for forward and
1141 reverse excitation states, these are described as follows
1142
1143 forward excitation
1144 used for S21 and S11
1145 Mf = Xf ** S ** Yf
1146
1147 reverse excitation
1148 used for S12 and S22
1149 Mr = Xr ** S ** Yr
1150
1151
1152 """
1153 def setUp(self):
1154 self.n_ports = 2
1155 self.wg = WG
1156 wg = self.wg
1157 self.Xf = wg.random(n_ports =2, name = 'Xf')
1158 self.Xr = wg.random(n_ports =2, name = 'Xr')
1159 self.Yf = wg.random(n_ports =2, name='Yf')
1160 self.Yr = wg.random(n_ports =2, name='Yr')
1161 self.If = wg.random(n_ports =1, name='If')
1162 self.Ir = wg.random(n_ports =1, name='Ir')
1163
1164 ideals = [
1165 wg.short(nports=2, name='short'),
1166 wg.open(nports=2, name='open'),
1167 wg.match(nports=2, name='load'),
1168 wg.random(2,name='rand1'),
1169 wg.random(2,name='rand2'),
1170 ]
1171
1172 measured = [ self.measure(k) for k in ideals]
1173
1174 self.cal = rf.TwelveTerm(
1175 ideals = ideals,
1176 measured = measured,
1177 n_thrus=2,
1178 isolation=measured[2]
1179 )
1180
1181 def measure(self,ntwk):
1182 m = ntwk.copy()
1183 mf = self.Xf**ntwk**self.Yf
1184 mr = self.Xr**ntwk**self.Yr
1185 m.s[:,1,0] = mf.s[:,1,0]
1186 m.s[:,0,0] = mf.s[:,0,0]
1187 m.s[:,0,1] = mr.s[:,0,1]
1188 m.s[:,1,1] = mr.s[:,1,1]
1189
1190 #Leakage
1191 m.s[:,1,0] += self.If.s[:,0,0]
1192 m.s[:,0,1] += self.Ir.s[:,0,0]
1193 return m
1194
1195 def test_forward_directivity_accuracy(self):
1196 self.assertEqual(
1197 self.Xf.s11,
1198 self.cal.coefs_ntwks['forward directivity'])
1199
1200 def test_forward_source_match_accuracy(self):
1201 self.assertEqual(
1202 self.Xf.s22 ,
1203 self.cal.coefs_ntwks['forward source match'] )
1204
1205 def test_forward_load_match_accuracy(self):
1206 self.assertEqual(
1207 self.Yf.s11 ,
1208 self.cal.coefs_ntwks['forward load match'])
1209
1210 def test_forward_reflection_tracking_accuracy(self):
1211 self.assertEqual(
1212 self.Xf.s21 * self.Xf.s12 ,
1213 self.cal.coefs_ntwks['forward reflection tracking'])
1214
1215 def test_forward_transmission_tracking_accuracy(self):
1216 self.assertEqual(
1217 self.Xf.s21*self.Yf.s21 ,
1218 self.cal.coefs_ntwks['forward transmission tracking'])
1219
1220 def test_reverse_source_match_accuracy(self):
1221 self.assertEqual(
1222 self.Yr.s11 ,
1223 self.cal.coefs_ntwks['reverse source match'] )
1224
1225 def test_reverse_directivity_accuracy(self):
1226 self.assertEqual(
1227 self.Yr.s22 ,
1228 self.cal.coefs_ntwks['reverse directivity'] )
1229
1230 def test_reverse_load_match_accuracy(self):
1231 self.assertEqual(
1232 self.Xr.s22 ,
1233 self.cal.coefs_ntwks['reverse load match'])
1234
1235 def test_reverse_reflection_tracking_accuracy(self):
1236 self.assertEqual(
1237 self.Yr.s21 * self.Yr.s12 ,
1238 self.cal.coefs_ntwks['reverse reflection tracking'])
1239
1240 def test_reverse_transmission_tracking_accuracy(self):
1241 self.assertEqual(
1242 self.Yr.s12*self.Xr.s12 ,
1243 self.cal.coefs_ntwks['reverse transmission tracking'])
1244
1245 def test_forward_isolation_accuracy(self):
1246 self.assertEqual(
1247 self.If.s11,
1248 self.cal.coefs_ntwks['forward isolation'])
1249
1250 def test_reverse_isolation_accuracy(self):
1251 self.assertEqual(
1252 self.Ir.s11,
1253 self.cal.coefs_ntwks['reverse isolation'])
1254
1255 def test_convert_12term_2_8term(self):
1256 converted = rf.convert_8term_2_12term(
1257 rf.convert_12term_2_8term(self.cal.coefs, redundant_k=True))
1258
1259 for k in converted:
1260 self.assertTrue(abs(self.cal.coefs[k] - converted[k])<1e-9)
1261
1262 def test_convert_12term_2_8term_correction_accuracy(self):
1263 converted = rf.convert_8term_2_12term(
1264 rf.convert_12term_2_8term(self.cal.coefs, redundant_k=True))
1265
1266 self.cal._coefs = converted
1267 a = self.wg.random(n_ports=2)
1268 m = self.measure(a)
1269 c = self.cal.apply_cal(m)
1270
1271 self.assertEqual(a,c)
1272
1273 def test_coefs_8term(self):
1274 self.assertEqual(self.cal.coefs_8term, rf.convert_12term_2_8term(self.cal.coefs))
1275
1276 def test_coefs_12term(self):
1277 self.assertEqual(self.cal.coefs_12term, self.cal.coefs)
1278
1279
1280 class TwelveTermSloppyInitTest(TwelveTermTest):
1281 """
1282 Test the TwelveTerm.__init__'s ability to
1283 1) determine the number of thrus (n_thrus) heuristically
1284 2) put the standards in correct order if they use sloppy_input
1285
1286 It must be a entirely separate test because we want to ensure it
1287 creates an accurate calibration.
1288 """
1289 @suppress_warning_decorator("dictionary passed, sloppy_input")
1290 @suppress_warning_decorator("n_thrus is None")
1291 def setUp(self):
1292 self.n_ports = 2
1293 self.wg = WG
1294 wg = self.wg
1295 self.Xf = wg.random(n_ports =2, name = 'Xf')
1296 self.Xr = wg.random(n_ports =2, name = 'Xr')
1297 self.Yf = wg.random(n_ports =2, name='Yf')
1298 self.Yr = wg.random(n_ports =2, name='Yr')
1299 #No leakage as it can interfere with thru detection, which is done
1300 #based on S21 and S12
1301 self.If = wg.match(n_ports =1, name='If')
1302 self.Ir = wg.match(n_ports =1, name='Ir')
1303
1304 ideals = [
1305 wg.short(nports=2, name='short'),
1306 wg.open(nports=2, name='open'),
1307 wg.match(nports=2, name='load'),
1308 wg.attenuator(-20,name='atten'),
1309 wg.line(45,'deg',name = 'line') ,
1310 ]
1311
1312
1313 measured = [ self.measure(k) for k in ideals]
1314
1315 self.cal= TwelveTerm(
1316 ideals = NetworkSet(ideals).to_dict(),
1317 measured = NetworkSet(measured).to_dict(),
1318 n_thrus=None,
1319 )
1320
1321 def measure(self,ntwk):
1322 m = ntwk.copy()
1323 mf = self.Xf**ntwk**self.Yf
1324 mr = self.Xr**ntwk**self.Yr
1325 m.s[:,1,0] = mf.s[:,1,0]
1326 m.s[:,0,0] = mf.s[:,0,0]
1327 m.s[:,0,1] = mr.s[:,0,1]
1328 m.s[:,1,1] = mr.s[:,1,1]
1329 return m
1330
1331
1332 class SOLTTest(TwelveTermTest):
1333 def setUp(self):
1334 self.n_ports = 2
1335 self.wg = WG
1336 wg = self.wg
1337 self.Xf = wg.random(n_ports =2, name = 'Xf')
1338 self.Xr = wg.random(n_ports =2, name = 'Xr')
1339 self.Yf = wg.random(n_ports =2, name='Yf')
1340 self.Yr = wg.random(n_ports =2, name='Yr')
1341 self.If = wg.random(n_ports =1, name='If')
1342 self.Ir = wg.random(n_ports =1, name='Ir')
1343
1344 ideals = [
1345 wg.short(nports=2, name='short'),
1346 wg.open(nports=2, name='open'),
1347 wg.match(nports=2, name='load'),
1348 None,
1349 ]
1350 actuals = [
1351 wg.short(nports=2, name='short'),
1352 wg.open(nports=2, name='open'),
1353 wg.match(nports=2, name='load'),
1354 wg.thru(),
1355 ]
1356
1357 measured = [ self.measure(k) for k in actuals]
1358
1359 self.cal = SOLT(
1360 ideals = ideals,
1361 measured = measured,
1362 n_thrus=1,
1363 isolation=measured[2]
1364 )
1365
1366
1367 class TwoPortOnePathTest(TwelveTermTest):
1368 @suppress_warning_decorator("divide by zero encountered in log10")
1369 @suppress_warning_decorator("n_thrus is None")
1370 def setUp(self):
1371 self.n_ports = 2
1372 self.wg =WG
1373 wg = self.wg
1374 self.Xf = wg.random(n_ports =2, name = 'Xf')
1375 self.Yf = wg.random(n_ports =2, name='Yf')
1376
1377 #No leakage
1378 self.If = wg.match(n_ports =1, name='If')
1379 self.Ir = wg.match(n_ports =1, name='Ir')
1380
1381 ideals = [
1382 wg.short(nports=2, name='short'),
1383 wg.open(nports=2, name='open'),
1384 wg.match(nports=2, name='load'),
1385 wg.random(2,name='rand1'),
1386 wg.random(2,name='rand2'),
1387 ]
1388
1389 measured = [ self.measure(k) for k in ideals]
1390 self.cal = TwoPortOnePath(
1391 ideals = ideals,
1392 measured = measured,
1393 source_port=1,
1394 #n_thrus=2,
1395 )
1396
1397 def measure(self,ntwk):
1398 r = self.wg.match(2)
1399 m = ntwk.copy()
1400 mf = self.Xf**ntwk**self.Yf
1401
1402 m.s[:,1,0] = mf.s[:,1,0]
1403 m.s[:,0,0] = mf.s[:,0,0]
1404 m.s[:,1,1] = r.s[:,1,1]
1405 m.s[:,0,1] = r.s[:,0,1]
1406 return m
1407
1408 def test_accuracy_of_dut_correction(self):
1409 a = self.wg.random(n_ports=self.n_ports, name = 'actual')
1410 f = self.measure(a)
1411 r = self.measure(a.flipped())
1412 c = self.cal.apply_cal((f,r))
1413 c.name = 'corrected'
1414 self.assertEqual(c,a)
1415
1416 def test_embed_then_apply_cal(self):
1417 a = self.wg.random(n_ports=self.n_ports)
1418 f = self.cal.embed(a)
1419 r = self.cal.embed(a.flipped())
1420 self.assertEqual(self.cal.apply_cal((f,r)),a)
1421
1422 @pytest.mark.skip(reason='measurement procedure is different so this test doesnt apply')
1423 def test_embed_equal_measure(self):
1424 pass
1425
1426 @suppress_warning_decorator("n_thrus is None")
1427 def test_from_coefs(self):
1428 cal_from_coefs = self.cal.from_coefs(self.cal.frequency, self.cal.coefs)
1429 ntwk = self.wg.random(n_ports=self.n_ports)
1430
1431 @suppress_warning_decorator("n_thrus is None")
1432 def test_from_coefs_ntwks(self):
1433 cal_from_coefs = self.cal.from_coefs_ntwks(self.cal.coefs_ntwks)
1434
1435 @pytest.mark.skip()
1436 def test_reverse_source_match_accuracy(self):
1437 pass
1438
1439 @pytest.mark.skip()
1440 def test_reverse_directivity_accuracy(self):
1441 pass
1442
1443 @pytest.mark.skip()
1444 def test_reverse_load_match_accuracy(self):
1445 pass
1446
1447 @pytest.mark.skip()
1448 def test_reverse_reflection_tracking_accuracy(self):
1449 pass
1450
1451 @pytest.mark.skip()
1452 def test_reverse_transmission_tracking_accuracy(self):
1453 pass
1454
1455 @pytest.mark.skip()
1456 def test_convert_12term_2_8term_correction_accuracy(self):
1457 pass
1458
1459
1460 class UnknownThruTest(EightTermTest):
1461 def setUp(self):
1462
1463 self.n_ports = 2
1464 self.wg = WG
1465 wg= self.wg
1466 self.X = wg.random(n_ports =2, name = 'X')
1467 self.Y = wg.random(n_ports =2, name='Y')
1468 #No leakage
1469 self.If = wg.match(n_ports=1, name='If')
1470 self.Ir = wg.match(n_ports=1, name='Ir')
1471 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
1472 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
1473
1474
1475 actuals = [
1476 wg.short(nports=2, name='short'),
1477 wg.open(nports=2, name='open'),
1478 wg.match(nports=2, name='match'),
1479 wg.impedance_mismatch(50,45)**wg.line(20,'deg',name='line')**wg.impedance_mismatch(45,50)
1480
1481 ]
1482
1483 ideals = [
1484 wg.short(nports=2, name='short'),
1485 wg.open(nports=2, name='open'),
1486 wg.match(nports=2, name='match'),
1487 wg.thru(name='thru'),
1488 ]
1489
1490 measured = [self.measure(k) for k in actuals]
1491
1492 self.cal = rf.UnknownThru(
1493 ideals = ideals,
1494 measured = measured,
1495 switch_terms = [self.gamma_f, self.gamma_r]
1496 )
1497
1498 class LRMTest(EightTermTest):
1499 def setUp(self):
1500
1501 self.n_ports = 2
1502 self.wg = WG
1503 wg = self.wg
1504 self.X = wg.random(n_ports=2, name='X')
1505 self.Y = wg.random(n_ports=2, name='Y')
1506 self.If = wg.random(n_ports=1, name='If')
1507 self.Ir = wg.random(n_ports=1, name='Ir')
1508 self.gamma_f = wg.random(n_ports=1, name='gamma_f')
1509 self.gamma_r = wg.random(n_ports=1, name='gamma_r')
1510
1511 # Our guess of the standards
1512 ss_i = wg.short(nports=2, name='short')
1513 thru = wg.line(d=50, unit='um', name='thru')
1514
1515 # Actual reflects with parasitics
1516 s = wg.inductor(5e-12) ** wg.load(-0.95, nports=1, name='short')
1517 ss = rf.two_port_reflect(s, s)
1518
1519 m = wg.load(0, nports=1, name='match')
1520 mm = rf.two_port_reflect(m, m)
1521
1522 # Store reflect for other tests
1523 self.s = s
1524
1525 approx_ideals = [
1526 thru,
1527 ss_i,
1528 mm
1529 ]
1530
1531 ideals = [
1532 thru,
1533 ss,
1534 mm
1535 ]
1536
1537 measured = [self.measure(k) for k in ideals]
1538
1539 self.cal = rf.LRM(
1540 ideals = approx_ideals,
1541 measured = measured,
1542 switch_terms = [self.gamma_f, self.gamma_r],
1543 isolation = measured[2]
1544 )
1545
1546 def test_solved_r(self):
1547 self.assertTrue(all(np.abs(self.s.s - self.cal.solved_r.s) < 1e-7))
1548
1549 class LRRMTest(EightTermTest):
1550 def setUp(self):
1551
1552 self.n_ports = 2
1553 self.wg = WG
1554 wg = self.wg
1555 self.X = wg.random(n_ports=2, name='X')
1556 self.Y = wg.random(n_ports=2, name='Y')
1557 self.If = wg.random(n_ports=1, name='If')
1558 self.Ir = wg.random(n_ports=1, name='Ir')
1559 self.gamma_f = wg.random(n_ports=1, name='gamma_f')
1560 self.gamma_r = wg.random(n_ports=1, name='gamma_r')
1561
1562 # Our guess of the standards
1563 o_i = wg.load(1, nports=1, name='open')
1564 s_i = wg.short(nports=1, name='short')
1565 m_i = wg.load(0.1, nports=1, name='load')
1566 thru = wg.line(d=50, z0=75, unit='um', name='thru')
1567 thru.renormalize(wg.z0)
1568
1569 # Make sure calibration works with non-symmetric thru
1570 thru.s[:,1,1] += 0.02 + 0.05j
1571
1572 # Actual reflects with parasitics
1573 s = wg.inductor(5e-12) ** wg.load(-0.95, nports=1, name='short')
1574 o = wg.shunt_capacitor(5e-15) ** wg.open(nports=1, name='open')
1575
1576 self.match_l = np.random.default_rng().uniform(1e-12, 20e-12)
1577 l = wg.inductor(L=self.match_l)
1578 m = l**m_i
1579
1580 # Make standards two-ports
1581 oo = rf.two_port_reflect(o, o)
1582 ss = rf.two_port_reflect(s, s)
1583 mm = rf.two_port_reflect(m, o)
1584
1585 oo_i = rf.two_port_reflect(o_i, o_i)
1586 ss_i = rf.two_port_reflect(s_i, s_i)
1587 mm_i = rf.two_port_reflect(m_i, m_i)
1588
1589 # Store open and short for other tests.
1590 self.s = s
1591 self.o = o
1592
1593 approx_ideals = [
1594 thru,
1595 ss_i,
1596 oo_i,
1597 mm_i
1598 ]
1599
1600 ideals = [
1601 thru,
1602 ss,
1603 oo,
1604 mm
1605 ]
1606
1607 measured = [self.measure(k) for k in ideals]
1608
1609 self.cal = rf.LRRM(
1610 ideals = approx_ideals,
1611 measured = measured,
1612 switch_terms = [self.gamma_f, self.gamma_r],
1613 isolation = measured[3]
1614 )
1615
1616 # Test the solved standards, don't use exact equality because of inductance
1617 # fitting tolerance.
1618 def test_solved_inductance(self):
1619 solved_l = np.mean(self.cal.solved_l)
1620 self.assertTrue(np.abs(self.match_l - solved_l) < 1e-3*self.match_l)
1621
1622 def test_solved_r1(self):
1623 self.assertTrue(all(np.abs(self.s.s - self.cal.solved_r1.s) < 1e-7))
1624
1625 def test_solved_r2(self):
1626 self.assertTrue(all(np.abs(self.o.s - self.cal.solved_r2.s) < 1e-7))
1627
1628 class LRRMTestNoFit(LRRMTest):
1629 def setUp(self):
1630
1631 self.n_ports = 2
1632 self.wg = WG
1633 wg = self.wg
1634 self.X = wg.random(n_ports=2, name='X')
1635 self.Y = wg.random(n_ports=2, name='Y')
1636 self.If = wg.random(n_ports=1, name='If')
1637 self.Ir = wg.random(n_ports=1, name='Ir')
1638 self.gamma_f = wg.random(n_ports=1, name='gamma_f')
1639 self.gamma_r = wg.random(n_ports=1, name='gamma_r')
1640
1641 # Our guess of the standards
1642 o_i = wg.load(1, nports=1, name='open')
1643 s_i = wg.short(nports=1, name='short')
1644 # Doesn't work correctly for non-50 ohm match.
1645 m_i = wg.load(0, nports=1, name='load')
1646 thru = wg.line(d=50, unit='um', name='thru')
1647
1648 # Actual reflects with parasitics
1649 s = wg.inductor(5e-12) ** wg.load(-0.95, nports=1, name='short')
1650 o = wg.shunt_capacitor(5e-15) ** wg.open(nports=1, name='open')
1651
1652 self.match_l = np.random.default_rng().uniform(1e-12, 20e-12)
1653 l = wg.inductor(L=self.match_l)
1654 m = l**m_i
1655
1656 # Make standards two-ports
1657 oo = rf.two_port_reflect(o, o)
1658 ss = rf.two_port_reflect(s, s)
1659 mm = rf.two_port_reflect(m, o)
1660
1661 oo_i = rf.two_port_reflect(o_i, o_i)
1662 ss_i = rf.two_port_reflect(s_i, s_i)
1663 mm_i = rf.two_port_reflect(m_i, m_i)
1664
1665 # Store open and short for other tests.
1666 self.s = s
1667 self.o = o
1668
1669 approx_ideals = [
1670 thru,
1671 ss_i,
1672 oo_i,
1673 mm_i
1674 ]
1675
1676 ideals = [
1677 thru,
1678 ss,
1679 oo,
1680 mm
1681 ]
1682
1683 measured = [self.measure(k) for k in ideals]
1684
1685 self.cal = rf.LRRM(
1686 ideals = approx_ideals,
1687 measured = measured,
1688 switch_terms = [self.gamma_f, self.gamma_r],
1689 isolation = measured[3],
1690 match_fit = 'none'
1691 )
1692
1693 class MRCTest(EightTermTest):
1694 def setUp(self):
1695
1696 self.n_ports = 2
1697 #Exact only with a lossless waveguide
1698 self.wg = WG_lossless
1699 wg= self.wg
1700 self.X = wg.random(n_ports =2, name = 'X')
1701 self.Y = wg.random(n_ports =2, name='Y')
1702 #No leakage
1703 self.If = wg.match(n_ports=1, name='If')
1704 self.Ir = wg.match(n_ports=1, name='Ir')
1705 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
1706 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
1707
1708
1709 def delay_shorts(d1,d2):
1710 ds1 = wg.delay_short(d1,'deg')
1711 ds2 = wg.delay_short(d2,'deg')
1712 return rf.two_port_reflect(ds1,ds2)
1713
1714 actuals = [
1715 wg.short(nports=2, name='short'),
1716 delay_shorts(65,130),
1717 delay_shorts(120,75),
1718 wg.load(.2+.2j,nports=2, name='match'),
1719 wg.impedance_mismatch(50,45)**wg.line(20,'deg',name='line')**wg.impedance_mismatch(45,50)
1720
1721 ]
1722
1723 ideals = [
1724 wg.short(nports=2, name='short'),
1725 delay_shorts(45,90),
1726 delay_shorts(90,45),
1727 wg.load(.2+.2j,nports=2, name='match'),
1728 wg.thru(name='thru'),
1729 ]
1730
1731 measured = [self.measure(k) for k in actuals]
1732
1733 self.cal = rf.MRC(
1734 ideals = ideals,
1735 measured = measured,
1736 switch_terms = [self.gamma_f, self.gamma_r]
1737 )
1738
1739
1740 class TwelveTermToEightTermTest(unittest.TestCase, CalibrationTest):
1741 """
1742 This test verifies the accuracy of the SOLT calibration, when used
1743 on an error-box (8-term) model.
1744
1745
1746 """
1747 @suppress_warning_decorator("n_thrus is None")
1748 def setUp(self):
1749 self.n_ports = 2
1750 wg= rf.wr10
1751 wg.frequency = rf.F.from_f([100], unit='GHz')
1752 self.wg = wg
1753 self.X = wg.random(n_ports =2, name = 'X')
1754 self.Y = wg.random(n_ports =2, name='Y')
1755 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
1756 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
1757 #Isolation terms
1758 self.If = wg.random(n_ports=1, name='If')
1759 self.Ir = wg.random(n_ports=1, name='Ir')
1760
1761 ideals = [
1762 wg.short(nports=2, name='short'),
1763 wg.open(nports=2, name='open'),
1764 wg.match(nports=2, name='load'),
1765 wg.thru(name='thru'),
1766 ]
1767
1768 measured = [ self.measure(k) for k in ideals]
1769 with warnings.catch_warnings(record=False):
1770 self.cal = rf.TwelveTerm(
1771 ideals = ideals,
1772 measured = measured,
1773 isolation=measured[2]
1774 )
1775
1776
1777 coefs = rf.calibration.convert_12term_2_8term(self.cal.coefs)
1778 coefs = NetworkSet.from_s_dict(coefs,
1779 frequency=self.cal.frequency).to_dict()
1780 self.coefs= coefs
1781
1782 def terminate(self, ntwk):
1783 """
1784 terminate a measured network with the switch terms
1785 """
1786 return terminate(ntwk,self.gamma_f, self.gamma_r)
1787
1788 def measure(self,ntwk):
1789 out = self.terminate(self.X**ntwk**self.Y)
1790 out.name = ntwk.name
1791 out.s[:,1,0] += self.If.s[:,0,0]
1792 out.s[:,0,1] += self.Ir.s[:,0,0]
1793 return out
1794
1795 def test_forward_isolation(self):
1796 self.assertEqual(self.coefs['forward isolation'], self.If.s11)
1797
1798 def test_reverse_isolation(self):
1799 self.assertEqual(self.coefs['reverse isolation'], self.Ir.s11)
1800
1801 def test_forward_switch_term(self):
1802 self.assertEqual(self.coefs['forward switch term'], self.gamma_f)
1803
1804 def test_reverse_switch_term(self):
1805 self.assertEqual(self.coefs['reverse switch term'], self.gamma_r)
1806
1807 def test_forward_directivity_accuracy(self):
1808 self.assertEqual(
1809 self.X.s11,
1810 self.coefs['forward directivity'])
1811
1812 def test_forward_source_match_accuracy(self):
1813 self.assertEqual(
1814 self.X.s22 ,
1815 self.coefs['forward source match'] )
1816
1817 def test_forward_reflection_tracking_accuracy(self):
1818 self.assertEqual(
1819 self.X.s21 * self.X.s12 ,
1820 self.coefs['forward reflection tracking'])
1821
1822 def test_reverse_source_match_accuracy(self):
1823 self.assertEqual(
1824 self.Y.s11 ,
1825 self.coefs['reverse source match'] )
1826
1827 def test_reverse_directivity_accuracy(self):
1828 self.assertEqual(
1829 self.Y.s22 ,
1830 self.coefs['reverse directivity'] )
1831
1832 def test_reverse_reflection_tracking_accuracy(self):
1833 self.assertEqual(
1834 self.Y.s21 * self.Y.s12 ,
1835 self.coefs['reverse reflection tracking'])
1836
1837 def test_k_accuracy(self):
1838 self.assertEqual(
1839 self.X.s21/self.Y.s12 ,
1840 self.coefs['k'] )
1841
1842 def test_verify_12term(self):
1843 self.assertTrue(self.cal.verify_12term_ntwk.s_mag.max() < 1e-3)
1844
1845
1846 class SixteenTermTest(unittest.TestCase, CalibrationTest):
1847 def setUp(self):
1848 self.n_ports = 2
1849 self.wg = WG
1850 wg = self.wg
1851
1852 #Port 0: VNA port 0
1853 #Port 1: DUT port 0
1854 #Port 2: DUT port 1
1855 #Port 3: VNA port 1
1856 self.Z = wg.random(n_ports = 4, name = 'Z')
1857 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
1858 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
1859
1860 o = wg.open(nports=1, name='open')
1861 s = wg.short(nports=1, name='short')
1862 m = wg.match(nports=1, name='load')
1863 om = rf.two_port_reflect(o, m)
1864 mo = rf.two_port_reflect(m, o)
1865 oo = rf.two_port_reflect(o, o)
1866 ss = rf.two_port_reflect(s, s)
1867 thru = wg.thru(name='thru')
1868
1869 ideals = [
1870 thru,
1871 om,
1872 mo,
1873 oo,
1874 ss
1875 ]
1876
1877 measured = [self.measure(k) for k in ideals]
1878
1879 self.cal = rf.SixteenTerm(
1880 measured = measured,
1881 ideals = ideals,
1882 switch_terms=(self.gamma_f, self.gamma_r)
1883 )
1884
1885 def terminate(self, ntwk):
1886 """
1887 terminate a measured network with the switch terms
1888 """
1889 return terminate(ntwk,self.gamma_f, self.gamma_r)
1890
1891 def measure(self,ntwk):
1892 out = self.terminate(rf.connect(self.Z, 1, ntwk, 0, num=2))
1893 out.name = ntwk.name
1894 return out
1895
1896 def test_renormalize(self):
1897 dut = self.wg.random(n_ports=2, name='DUT')
1898 dut_meas = self.measure(dut)
1899 z_new = 100-30j
1900 self.cal.renormalize(50, z_new)
1901 dut_cal = self.cal.apply_cal(dut_meas)
1902 dut.renormalize(z_new, s_def="traveling")
1903 dut.z0 = 50
1904 self.assertEqual(dut, dut_cal)
1905
1906 def test_renormalize2(self):
1907 """
1908 Test that roundtrip renormalization gives back the original calibration
1909 """
1910 old_coefs = copy.deepcopy(self.cal.coefs)
1911 z_new = 10 + 20j
1912 self.cal.renormalize(50, z_new)
1913 self.cal.renormalize(z_new, 50)
1914 for k in self.cal.coefs.keys():
1915 np.testing.assert_array_almost_equal(old_coefs[k], self.cal.coefs[k], err_msg=k)
1916
1917 def test_forward_directivity_accuracy(self):
1918 self.assertEqual(
1919 self.Z.s11,
1920 self.cal.coefs_ntwks['forward directivity'])
1921
1922 def test_forward_source_match_accuracy(self):
1923 self.assertEqual(
1924 self.Z.s22 ,
1925 self.cal.coefs_ntwks['forward source match'] )
1926
1927 def test_forward_reflection_tracking_accuracy(self):
1928 self.assertEqual(
1929 self.Z.s21 * self.Z.s12 ,
1930 self.cal.coefs_ntwks['forward reflection tracking'])
1931
1932 def test_reverse_source_match_accuracy(self):
1933 self.assertEqual(
1934 self.Z.s33 ,
1935 self.cal.coefs_ntwks['reverse source match'] )
1936
1937 def test_reverse_directivity_accuracy(self):
1938 self.assertEqual(
1939 self.Z.s44 ,
1940 self.cal.coefs_ntwks['reverse directivity'] )
1941
1942 def test_reverse_reflection_tracking_accuracy(self):
1943 self.assertEqual(
1944 self.Z.s34 * self.Z.s43 ,
1945 self.cal.coefs_ntwks['reverse reflection tracking'])
1946
1947 def test_forward_isolation(self):
1948 self.assertEqual(
1949 self.Z.s41 ,
1950 self.cal.coefs_ntwks['forward isolation'])
1951
1952 def test_reverse_isolation(self):
1953 self.assertEqual(
1954 self.Z.s14 ,
1955 self.cal.coefs_ntwks['reverse isolation'])
1956
1957 def test_reverse_port_isolation(self):
1958 self.assertEqual(
1959 self.Z.s23 ,
1960 self.cal.coefs_ntwks['reverse port isolation'])
1961
1962 def test_forward_port_isolation(self):
1963 self.assertEqual(
1964 self.Z.s32 ,
1965 self.cal.coefs_ntwks['forward port isolation'])
1966
1967 def test_k(self):
1968 self.assertEqual(
1969 self.Z.s21/self.Z.s34 ,
1970 self.cal.coefs_ntwks['k'])
1971
1972 def test_forward_port1_isolation(self):
1973 self.assertEqual(
1974 self.Z.s31/self.Z.s34 ,
1975 self.cal.coefs_ntwks['forward port 1 isolation'])
1976
1977 def test_reverse_port1_isolation(self):
1978 self.assertEqual(
1979 self.Z.s13*self.Z.s34 ,
1980 self.cal.coefs_ntwks['reverse port 1 isolation'])
1981
1982 def test_forward_port2_isolation(self):
1983 self.assertEqual(
1984 self.Z.s42*self.Z.s34 ,
1985 self.cal.coefs_ntwks['forward port 2 isolation'])
1986
1987 def test_reverse_port2_isolation(self):
1988 self.assertEqual(
1989 self.Z.s24/self.Z.s34 ,
1990 self.cal.coefs_ntwks['reverse port 2 isolation'])
1991
1992 class SixteenTermCoefficientsTest(unittest.TestCase):
1993 """Test that 16-term non-isolation coefficients are defined the same way as
1994 8-term coefficients."""
1995 def setUp(self):
1996 self.n_ports = 2
1997 self.wg = WG
1998 wg = self.wg
1999
2000 #Port 0: VNA port 0
2001 #Port 1: DUT port 0
2002 #Port 2: DUT port 1
2003 #Port 3: VNA port 1
2004 self.Z = wg.random(n_ports = 4, name = 'Z')
2005 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
2006 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
2007
2008 o = wg.open(nports=1, name='open')
2009 s = wg.short(nports=1, name='short')
2010 m = wg.match(nports=1, name='load')
2011 om = rf.two_port_reflect(o, m)
2012 mo = rf.two_port_reflect(m, o)
2013 oo = rf.two_port_reflect(o, o)
2014 ss = rf.two_port_reflect(s, s)
2015 thru = wg.thru(name='thru')
2016
2017 ideals = [
2018 thru,
2019 om,
2020 mo,
2021 oo,
2022 ss
2023 ]
2024
2025 measured = [self.measure(k) for k in ideals]
2026
2027 self.cal16 = rf.SixteenTerm(
2028 measured = measured,
2029 ideals = ideals,
2030 switch_terms=(self.gamma_f, self.gamma_r)
2031 )
2032
2033 r = wg.load(.95+.1j,nports=1)
2034 m = wg.match(nports=1)
2035 mm = rf.two_port_reflect(m, m)
2036 rm = rf.two_port_reflect(r, m)
2037 mr = rf.two_port_reflect(m, r)
2038 rr = rf.two_port_reflect(r, r)
2039
2040 ideals = [
2041 thru,
2042 mm,
2043 rr,
2044 rm,
2045 mr
2046 ]
2047
2048 measured = [self.measure(k) for k in ideals]
2049
2050 self.cal_lmr16 = rf.LMR16(
2051 measured = measured,
2052 ideals = [thru],
2053 ideal_is_reflect = False,
2054 #Automatic sign detection doesn't work if the
2055 #error terms aren't symmetric enough
2056 sign = 1,
2057 switch_terms=(self.gamma_f, self.gamma_r)
2058 )
2059
2060 #Same error network, but without leakage terms
2061
2062 #Primary leakage
2063 self.Z.s[:,3,0] = 0 # forward isolation
2064 self.Z.s[:,0,3] = 0 # reverse isolation
2065 self.Z.s[:,2,1] = 0 # forward port isolation
2066 self.Z.s[:,1,2] = 0 # reverse port isolation
2067
2068 #Cross leakage
2069 self.Z.s[:,3,1] = 0 # forward port 2 isolation
2070 self.Z.s[:,1,3] = 0 # reverse port 2 isolation
2071 self.Z.s[:,2,0] = 0 # forward port 1 isolation
2072 self.Z.s[:,0,2] = 0 # reverse port 1 isolation
2073
2074 measured = [self.measure(k) for k in ideals]
2075
2076 self.cal8 = rf.EightTerm(
2077 measured = measured,
2078 ideals = ideals,
2079 switch_terms=(self.gamma_f, self.gamma_r),
2080 )
2081
2082 def terminate(self, ntwk):
2083 """
2084 terminate a measured network with the switch terms
2085 """
2086 return terminate(ntwk,self.gamma_f, self.gamma_r)
2087
2088 def measure(self,ntwk):
2089 out = self.terminate(rf.connect(self.Z, 1, ntwk, 0, num=2))
2090 out.name = ntwk.name
2091 return out
2092
2093 def test_coefficients(self):
2094 for k in self.cal8.coefs.keys():
2095 if k in self.cal16.coefs.keys():
2096 if 'isolation' in k:
2097 continue
2098 self.assertTrue(all(np.abs(self.cal8.coefs[k] - self.cal16.coefs[k]) < 1e-10))
2099 self.assertTrue(all(np.abs(self.cal8.coefs[k] - self.cal_lmr16.coefs[k]) < 1e-10))
2100
2101
2102 class LMR16Test(SixteenTermTest):
2103 def setUp(self):
2104 self.n_ports = 2
2105 self.wg = WG
2106 wg = self.wg
2107
2108 #Port 0: VNA port 0
2109 #Port 1: DUT port 0
2110 #Port 2: DUT port 1
2111 #Port 3: VNA port 1
2112 self.Z = wg.random(n_ports = 4, name = 'Z')
2113 self.gamma_f = wg.random(n_ports =1, name='gamma_f')
2114 self.gamma_r = wg.random(n_ports =1, name='gamma_r')
2115
2116 r = wg.short(nports=1, name='short')
2117 m = wg.match(nports=1, name='load')
2118 mm = rf.two_port_reflect(m, m)
2119 rm = rf.two_port_reflect(r, m)
2120 mr = rf.two_port_reflect(m, r)
2121 rr = rf.two_port_reflect(r, r)
2122
2123 thru_length = np.random.default_rng().uniform(0,10)
2124 thru = wg.line(thru_length,'deg',name='line')
2125
2126 self.thru = thru
2127
2128 ideals = [
2129 thru,
2130 mm,
2131 rr,
2132 rm,
2133 mr
2134 ]
2135
2136 measured = [self.measure(k) for k in ideals]
2137
2138 self.cal = rf.LMR16(
2139 measured = measured,
2140 ideals = [r],
2141 ideal_is_reflect = True,
2142 #Automatic sign detection doesn't work if the
2143 #error terms aren't symmetric enough
2144 sign = -1,
2145 switch_terms=(self.gamma_f, self.gamma_r)
2146 )
2147
2148 def test_solved_through(self):
2149 self.assertEqual(
2150 self.thru,
2151 self.cal.solved_through)
2152
2153 class MultiportCalTest(unittest.TestCase):
2154 """Multi-port NISTMultilineTRL calibration test"""
2155
2156 def test_cal(self):
2157 self.wg = WG
2158 wg = self.wg
2159
2160 self.n_ports = 3
2161 nports = self.n_ports
2162
2163 self.make_error_networks(wg, nports)
2164
2165 cal_dict = {}
2166
2167 actuals = [
2168 wg.thru(),
2169 rf.two_port_reflect(wg.load(-.98-.1j),wg.load(-.98-.1j)),
2170 wg.line(100,'um'),
2171 wg.line(900,'um'),
2172 ]
2173
2174 dut = self.wg.random(n_ports=self.n_ports, name='dut')
2175 dut_meas = self.measure(dut)
2176
2177 l = [0, 100e-6, 900e-6]
2178
2179 for p in [(0, 1), (0, 2)]:
2180
2181 measured = [self.measure(rf.twoport_to_nport(k, p[0], p[1], nports)) for k in actuals]
2182 cal_dict[p] = {}
2183 cal_dict[p]['method'] = NISTMultilineTRL
2184 cal_dict[p]['measured'] = measured
2185 # Two-port switch terms are in reverse order from multi-port switch terms.
2186 cal_dict[p]['switch_terms'] = [self.gammas[i] for i in p][::-1]
2187 cal_dict[p]['l'] = l
2188 cal_dict[p]['Grefls'] = [-1]
2189 cal_dict[p]['er_est'] = 1
2190 cal_dict[p]['gamma_root_choice'] = 'real'
2191
2192 isolation = self.measure(wg.match(nports=nports))
2193
2194 self.cal = MultiportCal(cal_dict, isolation=isolation)
2195
2196 # Test coefs
2197 nports = self.n_ports
2198 for e, c in enumerate(self.cal.coefs):
2199 assert np.allclose(c['directivity'], self.Z.s[:,e,e])
2200 assert np.allclose(c['source match'], self.Z.s[:,nports+e,nports+e])
2201 assert np.allclose(c['reflection tracking'], self.Z.s[:,e,nports+e] * self.Z.s[:,nports+e,e])
2202 assert np.allclose(c['switch term'], self.gammas[e].s)
2203 assert np.allclose(c['k']/self.cal.coefs[0]['k'], self.Z.s[:,nports+0,0]/self.Z.s[:,nports+e,e])
2204
2205 # Test DUT correction
2206 dut_cal = self.cal.apply_cal(dut_meas)
2207 self.assertEqual(dut_cal, dut)
2208
2209 # Test cal inverts embed
2210 a = self.wg.random(n_ports=self.n_ports)
2211 self.assertEqual(self.cal.apply_cal(self.cal.embed(a)),a)
2212
2213 # Test embed equals measure
2214 a = self.wg.random(n_ports=self.n_ports)
2215 self.assertEqual(self.cal.embed(a),self.measure(a))
2216
2217 # Test gamma solved by TRL
2218 for p in [(0, 1), (0, 2)]:
2219 self.assertTrue(max(np.abs(self.wg.gamma-self.cal.cals[p].gamma)) < 1e-3)
2220
2221 def make_error_networks(self, wg, nports):
2222 self.Z = wg.random(n_ports = 2*nports, name = 'Z')
2223
2224 # Isolation terms are between all ports.
2225 # No error in the same port.
2226 self.isolation = wg.random(n_ports=nports, name='I')
2227 for i in range(nports):
2228 self.isolation.s[:, i, i] = 0
2229
2230 def port_type(n):
2231 return 'VNA' if n < nports else 'DUT'
2232 def port_number(n):
2233 return n if n < nports else n - nports
2234 # Remove leakage terms
2235 for i in range(2*nports):
2236 for j in range(i+1, 2*nports):
2237 # No connection between different VNA/DUT ports.
2238 # No connection between VNA and DUT ports with different number.
2239 if port_type(i) == port_type(j) or port_number(i) != port_number(j):
2240 self.Z.s[:,i,j] = 0
2241 self.Z.s[:,j,i] = 0
2242
2243 self.gammas = []
2244 for i in range(nports):
2245 self.gammas.append(wg.random(n_ports=1, name=f'gamma_{i}'))
2246
2247 def terminate(self, ntwk):
2248 """
2249 Terminate a measured network with the switch terms
2250 """
2251 return terminate_nport(ntwk, self.gammas)
2252
2253 def measure(self, ntwk):
2254 out = self.terminate(rf.connect(self.Z, self.n_ports, ntwk, 0, num=self.n_ports))
2255 out = out + self.isolation
2256 out.name = ntwk.name
2257 return out
2258
2259 class MultiportSOLTTest(MultiportCalTest):
2260
2261 def test_cal(self):
2262 nport_list = [3, 4]
2263 method_list = [SOLT, UnknownThru]
2264
2265 self.wg = WG
2266 wg = self.wg
2267
2268 for nport in nport_list:
2269 self.n_ports = nport
2270 nports = self.n_ports
2271
2272 self.make_error_networks(wg, nports)
2273
2274 o = wg.open(nports=nports, name='open')
2275 s = wg.short(nports=nports, name='short')
2276 m = wg.match(nports=nports, name='load')
2277
2278 # thru = wg.thru(name='thru')
2279 thru = wg.impedance_mismatch(50, 45) ** wg.line(20, 'deg') ** wg.impedance_mismatch(45, 50)
2280
2281 ideals = []
2282 # nports-1 thrus from port 0 to all other ports.
2283 for i in range(1, nports):
2284 thru_i = rf.twoport_to_nport(thru, 0, i, nports)
2285 ideals.append(thru_i)
2286
2287 ideals.extend([o,s,m])
2288 measured = [self.measure(k) for k in ideals]
2289
2290 dut = self.wg.random(n_ports=self.n_ports, name='dut')
2291 dut_meas = self.measure(dut)
2292
2293 for method in method_list:
2294
2295 if method == SOLT:
2296 self.cal = MultiportSOLT(method, measured, ideals, isolation=measured[-1])
2297 else:
2298 self.cal = MultiportSOLT(method, measured, ideals, isolation=measured[-1], switch_terms=self.gammas)
2299
2300 # Test coefs
2301 nports = self.n_ports
2302 for e, c in enumerate(self.cal.coefs):
2303 assert np.allclose(c['directivity'], self.Z.s[:,e,e])
2304 assert np.allclose(c['source match'], self.Z.s[:,nports+e,nports+e])
2305 assert np.allclose(c['reflection tracking'], self.Z.s[:,e,nports+e] * self.Z.s[:,nports+e,e])
2306 assert np.allclose(c['switch term'], self.gammas[e].s)
2307 assert np.allclose(c['k']/self.cal.coefs[0]['k'], self.Z.s[:,nports+0,0]/self.Z.s[:,nports+e,e])
2308
2309 # Test DUT correction
2310 dut_cal = self.cal.apply_cal(dut_meas)
2311 self.assertEqual(dut_cal, dut)
2312
2313 # Test cal inverts embed
2314 a = self.wg.random(n_ports=self.n_ports)
2315 self.assertEqual(self.cal.apply_cal(self.cal.embed(a)),a)
2316
2317 # Test embed equals measure
2318 a = self.wg.random(n_ports=self.n_ports)
2319 self.assertEqual(self.cal.embed(a),self.measure(a))
2320
2321 if __name__ == "__main__":
2322 unittest.main()