Debian Codegraph @

skrf/tests/test_qfactor.py

raw symbols SCIP
1 import os
2 import unittest
3
4 import numpy as np
5 from numpy.testing import assert_allclose, assert_almost_equal, assert_array_equal
6
7 import skrf as rf
8
9
10 class QfactorTests(unittest.TestCase):
11 """ Q-factor class tests.
12
13 References
14 ----------
15 "Q-factor Measurement by using a Vector Network Analyser",
16 A. P. Gregory, National Physical Laboratory Report MAT 58 (2021)
17 https://eprintspublications.npl.co.uk/9304/
18 """
19
20 def setUp(self):
21 """
22 Q-factor tests initializer.
23 """
24 self.test_dir = os.path.dirname(os.path.abspath(__file__)) + "/"
25 self.ntwk_2port = rf.data.ring_slot
26 self.ntwk_1port = rf.data.ring_slot_meas
27
28 def csv_file_example_to_network(self, file: str) -> rf.Network:
29 """Convert the S-parameter txt file to Network.
30
31 Parameters
32 ----------
33 file : str
34 file path.
35
36 Returns
37 -------
38 ntwk : rf.Network (1-port)
39
40 """
41 # Load frequency and S-parameter data from a file.
42 try:
43 f, s_re, s_im = np.loadtxt(file, comments="%", unpack=True)
44 except ValueError:
45 f, s_re, s_im, s_abs, s_mag = np.loadtxt(file, comments="%", unpack=True)
46
47 s = s_re + 1j * s_im
48 freq = rf.Frequency.from_f(f, unit='GHz')
49 return rf.Network(s=s, frequency=freq)
50
51 def test_constructor(self):
52 """
53 Test the Qfactor() constructor.
54 """
55 # constructor tests
56 _Q1 = rf.Qfactor(self.ntwk_1port, res_type='reflection')
57 _Q2 = rf.Qfactor(self.ntwk_1port, res_type='reflection', Q_L0=3)
58 _Q3 = rf.Qfactor(self.ntwk_1port, res_type='reflection', f_L0=85e9)
59 _Q3 = rf.Qfactor(self.ntwk_1port, res_type='reflection', Q_L0=3, f_L0=85e9)
60
61 def test_exceptions(self):
62 "Test the raised exceptions."
63
64 # Passing a 2-port Network raises a ValueError
65 self.assertRaises(ValueError, rf.Qfactor, self.ntwk_2port, 'reflection')
66
67 # Incorrect resonance type raises a ValueError
68 self.assertRaises(ValueError, rf.Qfactor, self.ntwk_1port, 'dummy')
69
70 # Asking for fitted S-param and Network without prior fit raises a ValueError
71 _Q = rf.Qfactor(self.ntwk_1port, res_type='reflection')
72 self.assertRaises(ValueError, _Q.fitted_s)
73 self.assertRaises(ValueError, _Q.fitted_network)
74
75 def test_NLQFIT6(self):
76 """
77 Fit FL and QL to transmission (S21) data by using the NLQFIT6 algorithm.
78
79 References
80 ----------
81 Test data is read from file Figure6b.txt (as used in Figure 6(b) of MAT 58).
82
83 """
84 # File 'Figure6b.txt' contains S21 data for Fig. 6(b) in MAT 58
85 ntwk = self.csv_file_example_to_network(self.test_dir + "qfactor_data/Figure6b.txt")
86
87 Q = rf.Qfactor(ntwk, res_type='transmission', verbose=True)
88
89 # Test against expected solutions
90 assert_almost_equal(Q._a, 0.8104 - 1.6928j, decimal=4)
91 assert_almost_equal(Q._b, 0.0077 - 0.0071j, decimal=4)
92 assert_almost_equal(Q.Q_L, 7440.848, decimal=3)
93
94 # Optimised weighted fit --> result vector
95 res = Q.fit(method="NLQFIT6")
96
97 # Test against expected solutions
98 assert_allclose(res.f_L, 3.987848e9)
99 assert_almost_equal(res.Q_L, 7454, decimal=0)
100 assert_almost_equal(res.RMS_Error, 0.00001216)
101 assert_array_equal(res.f_L, Q.f_L)
102 assert_array_equal(res.Q_L, Q.Q_L)
103
104 # Now calculate unloaded Q-factor and some other useful quantities.
105 # Reciprocal of |S21| of a thru in place of resonator
106 scaling_factor_A = 1 / 0.874 # 1/|S21_thru|
107 Q0 = Q.Q_unloaded(res, scaling_factor_A)
108 cal_diam, cal_gamma_V, cal_gamma_T = Q.Q_circle(res, scaling_factor_A)
109
110 # Q-factor of uncoupled two-port resonator (unloaded Q-factor)
111 assert_almost_equal(Q0, 7546, decimal=0)
112 assert_almost_equal(cal_diam, 0.0121, decimal=4)
113 # S21 detuned = leakage vector
114 assert_almost_equal(cal_gamma_V, -0.00008895 + 0.00003852j)
115 # S21 tuned
116 assert_almost_equal(cal_gamma_T, 0.00849357 - 0.00845349j)
117
118 def test_NLQFIT6_2(self):
119 """
120 Fit FL and QL to transmission (S21) data by using the NLQFIT6 algorithm.
121
122 From a two-port superconducting absorption resonator.
123
124 References
125 ----------
126 Test data is read from file Figure27.txt (as used in Figure 27 of MAT 58).
127
128 """
129 ntwk = self.csv_file_example_to_network(self.test_dir + "qfactor_data/Figure27.txt")
130
131 Q = rf.Qfactor(ntwk, res_type='absorption', verbose=True)
132
133 # Test against expected solutions
134 assert_almost_equal(Q._a, -17072.3098 + 9047.0761j, decimal=4)
135 assert_almost_equal(Q._b, 0.0063 + 0.0168j, decimal=4)
136
137 # Q.tol = 1.0e-5 * np.argmax(np.abs(Q.s))
138
139 # Step 2: Optimised weighted fit --> result vector
140 res = Q.fit(method="NLQFIT6")
141
142 assert_allclose(res.f_L, 6.07225567e9)
143 assert_almost_equal(res.Q_L, 56019.85, decimal=0)
144 assert_almost_equal(res.weighting_ratio, 4.714, decimal=3)
145 assert_almost_equal(res.RMS_Error, 0.01394722)
146 assert_allclose(Q.Q_L, res.Q_L)
147 assert_allclose(Q.f_L, res.f_L)
148
149 Q0 = Q.Q_unloaded(res)
150 cal_diam, cal_gamma_V, cal_gamma_T = Q.Q_circle(res)
151
152 assert_allclose(Q0, 1846782, rtol=1/100)
153 assert_allclose(cal_diam, 0.970, rtol=1/100)
154 assert_almost_equal(2.0 * res.f_L / res.Q_L/1e9, 0.00021678942580071302, decimal=10)
155
156
157
158 def test_NLQFIT7(self):
159 """
160 Fit FL and QL to reflection (S11) data by using the NLQFIT7 algorithm.
161
162 References
163 ----------
164 Test data is read from file Table6c27.txt (as used in Figure 16 and
165 Table 6(c) of MAT 58).
166
167 """
168 ntwk = self.csv_file_example_to_network(self.test_dir + "qfactor_data/Table6c27.txt")
169
170 Q = rf.Qfactor(ntwk, res_type='reflection', verbose=True)
171 # Expected results after initial fit
172 assert_almost_equal(Q._a, 760.9731 + 67.7804j, decimal=4)
173 assert_almost_equal(Q._b, 0.0609 - 0.6432j, decimal=4)
174 assert_almost_equal(Q.Q_L, 779.068, decimal=3)
175
176 # Expected results after fit
177 res = Q.fit(method='NLQFIT7')
178 # Fitted length of uncalibrated line [m]
179 assert_almost_equal(-res.m7a*rf.c/(4.0*np.pi*1.3), 57.47056249053462e-3)
180 assert_allclose(Q.f_L, 3.65293800e9)
181 assert_almost_equal(Q.Q_L, 708, decimal=0)
182
183 # Unloaded Q-factor and some other useful quantities.
184 print("Q-factor of unloaded one-port resonator by Method 1:")
185 print("Assumes attenuating uncalibrated line")
186 Q0 = Q.Q_unloaded(res)
187 cal_diam, cal_gamma_V, cal_gamma_T = Q.Q_circle(res)
188
189 # Test against expected solutions
190 assert_almost_equal(Q0, 862, decimal=0)
191 assert_almost_equal(cal_diam, 0.3573, decimal=4)
192 assert_almost_equal(cal_gamma_V, 0.09084890 - 0.99586469j) # S11 detuned
193 assert_almost_equal(cal_gamma_T, 0.05878773 - 0.64003179j) # S11 tuned
194
195 print("Q-factor of unloaded one-port resonator by Method 2:")
196 print("Scaling factor A = 1.0 (assume no attenuation in uncalibrated line)")
197 Q2 = rf.Qfactor(ntwk, res_type='reflection_method2', verbose=True)
198 res2 = Q2.fit(method='NLQFIT7')
199
200 Q0_2 = Q.Q_unloaded(res2, A=1)
201 cal_diam2, cal_gamma_V2, cal_gamma_T2 = Q.Q_circle(res2, A=1)
202
203 # Test against expected solutions
204 assert_almost_equal(Q0_2, 862, decimal=0)
205 assert_almost_equal(res2.weighting_ratio, 28.317, decimal=3)
206 assert_almost_equal(res2.RMS_Error, 0.00145957)
207 assert_almost_equal(cal_diam2, 0.3573, decimal=2)
208 assert_almost_equal(cal_gamma_V2, 0.08996562 - 0.98618239j)
209 assert_almost_equal(cal_gamma_T2, 0.05821616 - 0.63380908j)
210
211 def test_NLQFIT8(self):
212 """
213 Fits to transmission (S21) data by using the NLQFIT8 algorithm.
214
215 frequency-dependent leakage
216
217 References
218 ----------
219 Test data is read from file Figure23.txt (shown in Figure 23 of MAT 58)
220
221 """
222 ntwk = self.csv_file_example_to_network(self.test_dir + "qfactor_data/Figure23.txt")
223
224 Q = rf.Qfactor(ntwk, res_type='transmission')
225
226 # # De-embed cables
227 # N = len(Q.f)
228 ncablelen = 1.2 # root_eps * cable length in metres
229 # D = ntwk.s[:,0,0]* np.exp(-1j * 2*np.pi *Q.f * ncablelen / rf.c)
230
231 # ntwk2 = ntwk.copy()
232 # ntwk2.s[:,0,0] = D
233
234 # piece of transmission line to deembbed
235 gamma = 0 - 1j*ntwk.frequency.w * ncablelen / rf.c
236 coax = rf.media.DefinedGammaZ0(frequency=ntwk.frequency, gamma=gamma)
237 line = coax.line(1/2, unit='m')
238 ntwk2 = line.inv ** ntwk
239
240 # Find peak in |S21| - this is used to give initial value of freq.
241 # Tol is 1.0E-5 * |S21| at peak.
242 Mg = np.abs(ntwk2.s).squeeze()
243 index_max = np.argmax(Mg)
244 Fseed = Q.f[index_max]
245
246 # Set Qseed: An order-of-magnitude estimate for Q-factor
247 mult = 5.0 # Not critical. A value of around 5.0 will work well for initial and optimised fits (Section 2.6).
248 Qseed = mult * Fseed / (Q.f[-1] - Q.f[0])
249
250 Q = rf.Qfactor(ntwk2, res_type='transmission', Q_L0=Qseed, f_L0=Fseed)
251 assert_almost_equal(Q._a, 8.9408 + 2.1298j, decimal=4)
252 assert_almost_equal(Q._b, 0.0054 - 0.0045j, decimal=4)
253 assert_almost_equal(Q.Q_L, 4664.2418, decimal=3)
254
255 # Step 2: Optimised weighted fit
256 res = Q.fit(method="NLQFIT8", loop_plan="fwfwfwc")
257
258 # Unloaded Q-factor and some other useful quantities.
259 # Reciprocal of |S21| of a thru in place of resonator
260 scaling_factor_A = 1 / 0.949 # 1/|S21_thru|
261 Q0 = Q.Q_unloaded(res, scaling_factor_A)
262 cal_diam, cal_gamma_V, cal_gamma_T = Q.Q_circle(res, scaling_factor_A)
263
264 # Test against expected solutions
265 assert_allclose(Q.f_L, 9.76015571e9)
266 assert_almost_equal(Q.Q_L, 4760.04, decimal=0)
267 assert_almost_equal(Q0, 4789.49, decimal=0)
268 assert_almost_equal(res.weighting_ratio, 5.078, decimal=3)
269 assert_almost_equal(res.RMS_Error, 0.00000910)
270 assert_almost_equal(cal_diam, 0.0061, decimal=2)
271
272 def test_Q_unloaded(self):
273 """Test unloaded Q factor method."""
274 Q = rf.Qfactor(self.ntwk_1port, res_type='reflection')
275 res = Q.fit()
276 self.assertRaises(ValueError, Q.Q_unloaded, A='dummy')
277 self.assertRaises(ValueError, Q.Q_unloaded, A=1j)
278 self.assertRaises(ValueError, Q.Q_unloaded, res, A='dummy')
279 self.assertRaises(ValueError, Q.Q_unloaded, res, A=1j)
280
281 # passing of not the fitted results after fit should be the same
282 self.assertEqual(Q.Q_unloaded(res), Q.Q_unloaded())
283 # passing a different solution should lead to different values
284 res2 = Q.fit(method="NLQFIT7")
285 self.assertNotEqual(Q.Q_unloaded(res), Q.Q_unloaded(res2))
286
287 def test_Q_circle(self):
288 """Test Q-circle method."""
289 Q = rf.Qfactor(self.ntwk_1port, res_type='reflection')
290 res = Q.fit(method="NLQFIT6")
291 self.assertRaises(ValueError, Q.Q_circle, A='dummy')
292 self.assertRaises(ValueError, Q.Q_circle, A=1j)
293 self.assertRaises(ValueError, Q.Q_circle, res, A='dummy')
294 self.assertRaises(ValueError, Q.Q_circle, res, A=1j)
295
296 # passing of not the fitted results after fit should be the same
297 self.assertEqual(Q.Q_circle(res), Q.Q_circle())
298 # passing a different solution should lead to different values
299 res2 = Q.fit(method="NLQFIT7")
300 self.assertNotEqual(Q.Q_circle(res), Q.Q_circle(res2))
301
302 def test_f_L(self):
303 "Test resonant frequency values."
304 # expected values
305 f_L_expected = self.ntwk_2port.f[np.argmin(self.ntwk_2port.s11.s_mag)]
306 f_L_expected_scaled = f_L_expected/self.ntwk_2port.frequency.multiplier
307 # fitted values
308 Q = rf.Qfactor(self.ntwk_2port.s11, res_type='reflection')
309 # before the fit, warnings should be raised
310 with self.assertWarns(Warning):
311 # the resonance frequency corresponds to min value before fitting
312 assert_almost_equal(Q.f_L, f_L_expected)
313 assert_almost_equal(Q.f_L_scaled, f_L_expected_scaled)
314 # NB: after the fit this should not be the case anymore (slight deviation)
315
316 def test_BW(self):
317 "Test bandwidth values."
318 Q = rf.Qfactor(self.ntwk_2port.s11, res_type='reflection')
319 # before the fit, warnings should be raised
320 with self.assertWarns(Warning):
321 Q.BW
322 Q.BW_scaled