| 1 |
r""" |
| 2 |
Qfactor (:mod:`skrf.qfactor`) |
| 3 |
======================================== |
| 4 |
Module for estimating the Quality (Q) factor(s) from S-parameters. |
| 5 |
|
| 6 |
This class implements methods for determining *loaded* and *unloaded* Q-factor |
| 7 |
from frequency-domain S-parameters, that can be applied to measurements |
| 8 |
of transmission or reflection. |
| 9 |
|
| 10 |
Documentation and implementation are adapted from [MAT58]_ |
| 11 |
|
| 12 |
Q-factor |
| 13 |
-------- |
| 14 |
.. autosummary:: |
| 15 |
:toctree: generated/ |
| 16 |
:nosignatures: |
| 17 |
:recursive: |
| 18 |
|
| 19 |
Qfactor |
| 20 |
|
| 21 |
.. currentmodule:: skrf.qfactor |
| 22 |
|
| 23 |
|
| 24 |
Loaded and Unloaded Q-factor |
| 25 |
---------------------------- |
| 26 |
The Quality factor (Q-factor) of a resonator is defined by [Pozar]_: |
| 27 |
|
| 28 |
.. math:: |
| 29 |
|
| 30 |
Q = \frac{2 \pi U}{\Delta U} |
| 31 |
|
| 32 |
|
| 33 |
where :math:`U` is the average energy stored by the resonator and |
| 34 |
:math:`\Delta U` is the decrease in the average stored energy per wave cycle |
| 35 |
at the resonant frequency, that is, the average power loss. |
| 36 |
|
| 37 |
The *loaded* Q-factor, :math:`Q_L`, describes energy dissipation within the |
| 38 |
entire resonant system comprising of the resonator itself and the instrument |
| 39 |
used for observing resonances. The term loading refers to the effect that the |
| 40 |
external circuit has on measured quantities. |
| 41 |
|
| 42 |
The external circuit consists of the measuring instrument and uncalibrated lines, |
| 43 |
but not the couplings of microwave resonators. Loading by an instrument that |
| 44 |
has 50 Ohm impedance, such as a VNA, causes :math:`Q_L` to be reduced substantially |
| 45 |
if strong coupling is used. |
| 46 |
|
| 47 |
For many applications the quantity that is desired is the *unloaded* Q-factor :math:`Q_0`, |
| 48 |
which is determined by energy dissipation associated with the resonator only |
| 49 |
and therefore gives the best description of the resonant mode(s). |
| 50 |
|
| 51 |
In other words, :math:`Q_0` is the Q-factor of the uncoupled resonator. The value of |
| 52 |
:math:`Q_0` can be estimated from measurements of :math:`Q_L`, but cannot be measured directly. |
| 53 |
:math:`Q_0` is largely governed by ohmic loss arising from surface currents |
| 54 |
in the metal conductors (walls and loop couplings), and from dielectric loss |
| 55 |
in any insulating materials that may be present. |
| 56 |
|
| 57 |
|
| 58 |
Relationships between Loaded and Unloaded Q-factors |
| 59 |
--------------------------------------------------- |
| 60 |
Energy dissipation in the external circuit is characterised by the *external* Q-factor, |
| 61 |
:math:`Q_e`. For both series and parallel equivalent circuits, the three |
| 62 |
Q-factors are related by [Pozar]_: |
| 63 |
|
| 64 |
.. math:: |
| 65 |
|
| 66 |
\frac{1}{Q_L} = \frac{1}{Q_0} + \frac{1}{Q_e} |
| 67 |
|
| 68 |
|
| 69 |
The coupling factor :math:`\beta` is defined for each port as: |
| 70 |
|
| 71 |
.. math:: |
| 72 |
|
| 73 |
\beta = \frac{Q_0}{Q_e} |
| 74 |
|
| 75 |
|
| 76 |
Finding the unloaded Q from measured S-parameters consists in first finding |
| 77 |
the coupling factor, then measure :math:`Q_L` from the 3 dB bandwidth |
| 78 |
and using the relationships above. |
| 79 |
|
| 80 |
Fortunately, scikit-rf implements methods for determining loaded and |
| 81 |
unloaded Q-factors from frequency-domain S-parameters. The implemented methods |
| 82 |
are described in detail in [MAT58]_, and can be applied to measurements of |
| 83 |
transmission or reflection. |
| 84 |
|
| 85 |
Q-factor determination through equivalent-circuit models |
| 86 |
-------------------------------------------------------- |
| 87 |
Characterisation of resonances from measurements in the frequency-domain |
| 88 |
can be achieved through equivalent-circuit models [MAT58]_. Resonators can be |
| 89 |
modelled as an ideal RLC resonator connected to an external circuit, |
| 90 |
incorporating elements to account for a lossy coupling and coupling reactances. |
| 91 |
|
| 92 |
For high Q-factor resonators (in practice, :math:`Q_L` > 100), the S-parameter |
| 93 |
response of a resonator measured in a calibrated system with reference planes |
| 94 |
at the resonator couplings can be expressed like [MAT58]_, [Galwas]_ : |
| 95 |
|
| 96 |
.. math:: |
| 97 |
|
| 98 |
S = S_D + d \frac{e^{−2j\delta}}{1 + j Q_L t} |
| 99 |
|
| 100 |
|
| 101 |
where :math:`S_D` is the detuned S-parameter measured at frequencies far above or below |
| 102 |
resonance, :math:`d` is The diameter of the Q-circle, :math:`\delta` is a |
| 103 |
real-valued constant that defines the orientation of the Q-circle, and :math:`t` |
| 104 |
is the fractional offset frequency given by: |
| 105 |
|
| 106 |
.. math:: |
| 107 |
|
| 108 |
t = \frac{f}{f_L} - \frac{f_L}{f} \approx 2 \frac{f − f_L}{f_L} |
| 109 |
|
| 110 |
|
| 111 |
where :math:`f_L` is the loaded resonant frequency and :math:`f` the frequency |
| 112 |
at which S is measured. This equation can be applied to measurements |
| 113 |
by transmission (S21 or S12) or reflection (S11 or S22). |
| 114 |
|
| 115 |
The S-parameters are fitted against a modified expression of the above equations |
| 116 |
to deduce the resonant frequency, loaded and unloaded Q-factors and other properties. |
| 117 |
|
| 118 |
References |
| 119 |
---------- |
| 120 |
.. [MAT58] "Q-factor Measurement by using a Vector Network Analyser", |
| 121 |
A. P. Gregory, National Physical Laboratory Report MAT 58 (2021) |
| 122 |
https://eprintspublications.npl.co.uk/9304/ |
| 123 |
|
| 124 |
.. [Pozar] D. M. Pozar, Microwave engineering, 4th ed. J. Wiley, 2012. |
| 125 |
|
| 126 |
.. [Galwas] B. A. Galwas, ‘Scattering Matrix Description of Microwave |
| 127 |
Resonators’, IEEE Trans. Microwave Theory Techn., vol. 31, no. 8, |
| 128 |
pp. 669–671, Aug. 1983, doi: 10.1109/TMTT.1983.1131566. |
| 129 |
|
| 130 |
""" |
| 131 |
from __future__ import annotations |
| 132 |
|
| 133 |
from warnings import warn |
| 134 |
|
| 135 |
import numpy as np |
| 136 |
|
| 137 |
from .constants import NumberLike |
| 138 |
from .frequency import Frequency |
| 139 |
from .network import Network |
| 140 |
|
| 141 |
|
| 142 |
RESONANCE_TYPES = ['reflection', 'reflection_method2', |
| 143 |
'transmission', 'absorption'] |
| 144 |
|
| 145 |
|
| 146 |
class OptimizedResult(dict): |
| 147 |
"""Represent Q-factor optimisation result. |
| 148 |
|
| 149 |
Attributes |
| 150 |
---------- |
| 151 |
Q_L : float |
| 152 |
Loaded Quality factor |
| 153 |
f_L : float |
| 154 |
Resonance frequency [Hz] |
| 155 |
success: bool |
| 156 |
Is the fit method has been successfully performed |
| 157 |
method: str |
| 158 |
Fitting method used. |
| 159 |
m1, m2, m3, ...: float |
| 160 |
Coefficients described in [MAT58]_ |
| 161 |
number_iterations: int |
| 162 |
Number of iterations performed. |
| 163 |
|
| 164 |
Notes |
| 165 |
----- |
| 166 |
`OptimizedResult` may have additional attributes not listed here depending |
| 167 |
on the specific fitting method used. Since this class is essentially a |
| 168 |
subclass of dict with attribute accessors, one can see which |
| 169 |
attributes are available using the `OptimizedResult.keys` method. |
| 170 |
|
| 171 |
References |
| 172 |
---------- |
| 173 |
.. [MAT58] "Q-factor Measurement by using a Vector Network Analyser", |
| 174 |
A. P. Gregory, National Physical Laboratory Report MAT 58 (2021) |
| 175 |
https://eprintspublications.npl.co.uk/9304/ |
| 176 |
|
| 177 |
""" |
| 178 |
|
| 179 |
def __getattr__(self, name): |
| 180 |
try: |
| 181 |
return self[name] |
| 182 |
except KeyError as e: |
| 183 |
raise AttributeError(name) from e |
| 184 |
|
| 185 |
__setattr__ = dict.__setitem__ |
| 186 |
__delattr__ = dict.__delitem__ |
| 187 |
|
| 188 |
def __repr__(self): |
| 189 |
if self.keys(): |
| 190 |
m = max(map(len, list(self.keys()))) + 1 |
| 191 |
return '\n'.join([k.rjust(m) + ': ' + repr(v) |
| 192 |
for k, v in sorted(self.items())]) |
| 193 |
else: |
| 194 |
return self.__class__.__name__ + "()" |
| 195 |
|
| 196 |
def __dir__(self): |
| 197 |
return list(self.keys()) |
| 198 |
|
| 199 |
|
| 200 |
class Qfactor: |
| 201 |
""" |
| 202 |
Q-factor calculation class. |
| 203 |
|
| 204 |
This class implements methods for determining *loaded* and *unloaded* Q-factor |
| 205 |
from frequency-domain S-parameters, that can be applied to measurements |
| 206 |
of transmission or reflection. |
| 207 |
|
| 208 |
Parameters |
| 209 |
---------- |
| 210 |
ntwk : :class:`~skrf.network.Network` object |
| 211 |
A 1-port scikit-rf Network. |
| 212 |
If your device is a N-port, pass the desired sub-S-parameters to fit |
| 213 |
the data from, like `ntwk.s21`. |
| 214 |
res_type : str |
| 215 |
Specifies the resonance type: 'reflection', 'transmission', |
| 216 |
'reflection_method2' or 'absorption': |
| 217 |
'reflection' is generally suited for undercoupled resonators, |
| 218 |
while 'reflection_method2' is favoured for coupling with large loop. |
| 219 |
Q_L0 : float, optional. Default is None. |
| 220 |
Estimated loaded Q-factor, used to improve fitting. |
| 221 |
f_L0 : float, optional. Default is None. |
| 222 |
Estimated loaded resonant frequency, used to improve fitting [Hz] |
| 223 |
If None, automatically search for the min or max, |
| 224 |
depending on the resonance type defined by `res_type`. |
| 225 |
verbose : bool, optional. Default is False. |
| 226 |
Boolean flag controlling output of information to the console. |
| 227 |
|
| 228 |
Raises |
| 229 |
------ |
| 230 |
ValueError |
| 231 |
If the passed Network is not 1-port or if the resonance type is unknown. |
| 232 |
|
| 233 |
Notes |
| 234 |
----- |
| 235 |
Uncalibrated line should be de-embedded (if it has significant |
| 236 |
length) from the S-parameter data before calling the functions |
| 237 |
in this module to get best results. |
| 238 |
|
| 239 |
""" |
| 240 |
|
| 241 |
def __init__(self, |
| 242 |
ntwk: Network, |
| 243 |
res_type: str, |
| 244 |
Q_L0: None | float = None, |
| 245 |
f_L0: None | float = None, |
| 246 |
verbose: bool = False): |
| 247 |
"""Q-factor initializer.""" |
| 248 |
|
| 249 |
if ntwk.nports != 1: |
| 250 |
raise ValueError('The Network is not a 1-port Network.') |
| 251 |
if res_type not in RESONANCE_TYPES: |
| 252 |
raise ValueError(f'res_type must be in: {RESONANCE_TYPES}.') |
| 253 |
|
| 254 |
self.s = ntwk.s |
| 255 |
self.f = ntwk.f |
| 256 |
self.f_scaled = ntwk.frequency.f_scaled |
| 257 |
self.f_multiplier = ntwk.frequency.multiplier |
| 258 |
self.f_unit = ntwk.frequency.unit |
| 259 |
self._ntwk = ntwk |
| 260 |
self.res_type = res_type |
| 261 |
self.tol = 1.0e-5 |
| 262 |
self.verbose = verbose |
| 263 |
self.fitted = False |
| 264 |
self.opt_res = None |
| 265 |
|
| 266 |
self.N = len(self.f) |
| 267 |
|
| 268 |
|
| 269 |
self._initial_fit(self.N, Q_L0, f_L0) |
| 270 |
|
| 271 |
def __str__(self) -> str: |
| 272 |
if self.fitted: |
| 273 |
status = f"fitted: f_L={float(self.f_L/self.f_multiplier):.3f}{self.f_unit}, Q_L={float(self.Q_L):.3f}" |
| 274 |
else: |
| 275 |
status = 'not fitted' |
| 276 |
|
| 277 |
_str = f"Q-factor of Network {self._ntwk.name}. ({status})" |
| 278 |
return _str |
| 279 |
|
| 280 |
def __repr__(self) -> str: |
| 281 |
return self.__str__() |
| 282 |
|
| 283 |
def fit(self, |
| 284 |
method: str = "NLQFIT6", |
| 285 |
loop_plan: str = 'fwfwc' |
| 286 |
) -> OptimizedResult: |
| 287 |
"""Fit Q-factor from S-parameter data. |
| 288 |
|
| 289 |
Fitting overwrites the parameters `Q_L` and `f_L`. |
| 290 |
|
| 291 |
Parameters |
| 292 |
---------- |
| 293 |
method : str, optional |
| 294 |
Fitting method : 'NLQFIT6' (default), 'NLQFIT7', 'NLQFIT8': |
| 295 |
|
| 296 |
- 'NLQFIT6': Least Square Minimum of Eq.21 [MAT58]_ with 6 coefficients. |
| 297 |
- 'NLQFIT7': Least Square Minimum of Eq.26 [MAT58]_ with 7 coefficients, |
| 298 |
including one that characterize the trans. line length. |
| 299 |
- 'NLQFIT8': Least Square Minimum of Eq.43 [MAT58]_ with 8 coefficients, |
| 300 |
A model for frequency-dependent leakage. |
| 301 |
loop_plan : str, optional |
| 302 |
Defines order of steps used by the fitting process. |
| 303 |
The convergence algorithm uses a number of steps set by loop_plan, |
| 304 |
a string of characters as follows: |
| 305 |
|
| 306 |
- 'f': fit once without testing for convergence |
| 307 |
- 'c': repeated fit, iterating until convergence is obtained |
| 308 |
- 'w': re-calculate weighting factors on basis of previous fit |
| 309 |
- Initially the weighting factors are all unity. |
| 310 |
- The first character in `loop_plan` must not be 'w'. |
| 311 |
e.g.: 'fwfwc' (default). |
| 312 |
|
| 313 |
Returns |
| 314 |
------- |
| 315 |
result : :class:`~skrf.qfactor.OptimizedResult` |
| 316 |
|
| 317 |
References |
| 318 |
---------- |
| 319 |
.. [MAT58] "Q-factor Measurement by using a Vector Network Analyser", |
| 320 |
A. P. Gregory, National Physical Laboratory Report MAT 58 (2021) |
| 321 |
https://eprintspublications.npl.co.uk/9304/ |
| 322 |
|
| 323 |
""" |
| 324 |
|
| 325 |
for op in loop_plan: |
| 326 |
if op not in ['f', 'c', 'w']: |
| 327 |
raise ValueError("Unexpected character in loop_plan") |
| 328 |
if loop_plan[-1] == "w": |
| 329 |
raise ValueError("Last item in loop_plan must not be w (weight calculation)") |
| 330 |
if loop_plan[0] == "w": |
| 331 |
raise ValueError("First item in loop_plan must not be w (weight calculation)") |
| 332 |
if loop_plan[-1] != "c": |
| 333 |
warn("Last item in loop_plan is not c so convergence not tested!", stacklevel=2) |
| 334 |
|
| 335 |
self.method = method |
| 336 |
self.loop_plan = loop_plan |
| 337 |
|
| 338 |
|
| 339 |
if method == 'NLQFIT6': |
| 340 |
result = self._optimise_fit6(self.N) |
| 341 |
elif method == 'NLQFIT7': |
| 342 |
result = self._optimise_fit7(self.N) |
| 343 |
elif method == 'NLQFIT8': |
| 344 |
result = self._optimise_fit8(self.N) |
| 345 |
|
| 346 |
|
| 347 |
self.Q_L = result.Q_L |
| 348 |
self.f_L = result.f_L |
| 349 |
self.fitted = True |
| 350 |
self.opt_res = result |
| 351 |
|
| 352 |
if result.Q_L < 0: |
| 353 |
warn('Negative Q_L, fitting may be inaccurate.', stacklevel=2) |
| 354 |
|
| 355 |
return result |
| 356 |
|
| 357 |
@staticmethod |
| 358 |
def angular_weights(f: NumberLike, |
| 359 |
f_L: NumberLike, |
| 360 |
Q_L: NumberLike |
| 361 |
) -> NumberLike: |
| 362 |
r"""Diagonal elements W_i of weights matrix. |
| 363 |
|
| 364 |
.. math:: |
| 365 |
|
| 366 |
W_i = \frac{1}{\left[ \frac{2 Q_L (f_i - f_L)}{f_L} \right]^2 + 1} |
| 367 |
|
| 368 |
|
| 369 |
The weights are needed when frequencies are equally-spaced |
| 370 |
(rather than points equally spaced around the Q-circle), and help |
| 371 |
reducing systematic error [MAT58]_. |
| 372 |
|
| 373 |
Parameters |
| 374 |
---------- |
| 375 |
f : np.ndarray |
| 376 |
Frequency values array. |
| 377 |
f_L : float |
| 378 |
Loaded resonant frequency. |
| 379 |
Q_L : float |
| 380 |
Loaded Q-factor. |
| 381 |
|
| 382 |
Returns |
| 383 |
------- |
| 384 |
W_i : np.ndarray |
| 385 |
Weighting factors in proportion to the rate of change of angle |
| 386 |
with frequency relative to the centre of the Q-circle. |
| 387 |
|
| 388 |
References |
| 389 |
---------- |
| 390 |
.. [MAT58] "Q-factor Measurement by using a Vector Network Analyser", |
| 391 |
A. P. Gregory, National Physical Laboratory Report MAT 58 (2021) |
| 392 |
https://eprintspublications.npl.co.uk/9304/ |
| 393 |
section 2.4, eqn. (28). |
| 394 |
|
| 395 |
""" |
| 396 |
ptmp = 2 * Q_L * (f - f_L) / f_L |
| 397 |
W_i = 1 / (ptmp ** 2 + 1) |
| 398 |
return W_i |
| 399 |
|
| 400 |
def _initial_fit(self, |
| 401 |
N: int, |
| 402 |
Q_L0: float | None = None, |
| 403 |
f_L0: None | float = None |
| 404 |
): |
| 405 |
"""Initial Linear least squares Q-factor fit. |
| 406 |
|
| 407 |
As this is not optimised in this function (use `fit`), the solution |
| 408 |
will only be approximate. This method is called during the |
| 409 |
initialization of the `Qfactor` class. Note that a reasonable estimate |
| 410 |
for the resonant frequency should be supplied is multiple resonances |
| 411 |
are present. |
| 412 |
|
| 413 |
Also calculate the internal parameters a, b and QL from ([MAT58]_, eqn. 17) |
| 414 |
|
| 415 |
Parameters |
| 416 |
---------- |
| 417 |
N : int |
| 418 |
Number of points. |
| 419 |
Q_L0 : float, optional. Default is None. |
| 420 |
Estimated loaded Q-factor (will be improved by fitting). |
| 421 |
f_L0 : float, optional. Default is None. |
| 422 |
Estimated loaded resonant frequency, used to improve fitting [Hz] |
| 423 |
|
| 424 |
Returns |
| 425 |
------- |
| 426 |
None |
| 427 |
|
| 428 |
References |
| 429 |
---------- |
| 430 |
.. [MAT58] "Q-factor Measurement by using a Vector Network Analyser", |
| 431 |
A. P. Gregory, National Physical Laboratory Report MAT 58 (2021) |
| 432 |
https://eprintspublications.npl.co.uk/9304/ |
| 433 |
section 2.1, eqn. (17). |
| 434 |
|
| 435 |
""" |
| 436 |
if f_L0 is None: |
| 437 |
|
| 438 |
if self.res_type in ['reflection', 'reflection_method2', 'absorption']: |
| 439 |
|
| 440 |
index_min = np.argmin(np.abs(self.s)) |
| 441 |
f_L0 = self.f[index_min] |
| 442 |
else: |
| 443 |
|
| 444 |
index_max = np.argmax(np.abs(self.s)) |
| 445 |
f_L0 = self.f[index_max] |
| 446 |
|
| 447 |
|
| 448 |
if Q_L0 is None: |
| 449 |
|
| 450 |
|
| 451 |
mult = 5.0 |
| 452 |
Q_L0 = mult * f_L0/(self.f[-1] - self.f[0]) |
| 453 |
|
| 454 |
if self.verbose: |
| 455 |
print(f'Initial estimation: Q_L0={Q_L0}, f_L0={f_L0}') |
| 456 |
|
| 457 |
N2 = 2 * N |
| 458 |
M = np.zeros([N2, 5]) |
| 459 |
G = np.zeros(N2)[:, np.newaxis] |
| 460 |
|
| 461 |
for i in range(N): |
| 462 |
i2 = i + N |
| 463 |
t = 2.0 * (self.f[i] / f_L0 - 1.0) |
| 464 |
y = 1.0 / complex(1.0, Q_L0 * t) |
| 465 |
v = t * y |
| 466 |
v1 = y * self.s[i, 0, 0] |
| 467 |
G[i] = v1.real |
| 468 |
G[i2] = v1.imag |
| 469 |
v2 = v1 * t |
| 470 |
M[i, :] = np.array([v.real, -v.imag, y.real, -y.imag, v2.imag]) |
| 471 |
M[i2, :] = np.array([v.imag, v.real, y.imag, y.real, -v2.real]) |
| 472 |
|
| 473 |
T = M.transpose() |
| 474 |
C = T @ M |
| 475 |
q = T @ G |
| 476 |
sv = np.linalg.solve(C, q) |
| 477 |
a_re, a_im, b_re, b_im, Q_L = sv |
| 478 |
|
| 479 |
self._a = (a_re + 1j*a_im)[0] |
| 480 |
self._b = (b_re + 1j*b_im)[0] |
| 481 |
self.Q_L = Q_L[0] |
| 482 |
self.f_L = f_L0 |
| 483 |
|
| 484 |
if self.verbose: |
| 485 |
print(f'Preliminary estimation: Q_L={self.Q_L}, f_L={self.f_L}') |
| 486 |
|
| 487 |
|
| 488 |
def _optimise_fit6(self, N: int): |
| 489 |
"""Iterative non-linear fit, NLQFIT6 Step (2). |
| 490 |
|
| 491 |
Optimised fit of Q-factor (Q_L) and resonant frequency (f_L) |
| 492 |
by the gradient-descent method [MAT58]_. |
| 493 |
|
| 494 |
Uses the results of the initial fit as the starting |
| 495 |
values for the iteration. |
| 496 |
|
| 497 |
Parameters |
| 498 |
---------- |
| 499 |
N : int |
| 500 |
Number of points. |
| 501 |
|
| 502 |
Returns |
| 503 |
------- |
| 504 |
res : OptimizedResult |
| 505 |
Fitted values. |
| 506 |
|
| 507 |
References |
| 508 |
---------- |
| 509 |
.. [MAT58] MAT 58, section 2.2, eqn. (22). |
| 510 |
|
| 511 |
""" |
| 512 |
N2 = N * 2 |
| 513 |
iterations = 0 |
| 514 |
PV = np.ones(N) |
| 515 |
PV2 = np.ones(N2) |
| 516 |
|
| 517 |
m1 = self._a.imag / self.Q_L |
| 518 |
m2 = -self._a.real / self.Q_L |
| 519 |
m3 = self._b.real - m1 |
| 520 |
m4 = self._b.imag - m2 |
| 521 |
m5 = self.Q_L |
| 522 |
Flwst = self.f[0] |
| 523 |
m6 = Flwst * m5 / self.f_L |
| 524 |
last_op = "n" |
| 525 |
weighting_ratio = None |
| 526 |
number_iterations = 0 |
| 527 |
|
| 528 |
|
| 529 |
for op in self.loop_plan: |
| 530 |
if op == "w": |
| 531 |
PV = self.angular_weights(self.f, Flwst * float(m5) / float(m6), float(m5)) |
| 532 |
|
| 533 |
weighting_ratio = max(PV) / min(PV) |
| 534 |
PV2 = np.concatenate((PV, PV)) |
| 535 |
if self.verbose: |
| 536 |
print("Op w, Calculate weights") |
| 537 |
last_op = "n" |
| 538 |
elif op == "c": |
| 539 |
seek_convergence = True |
| 540 |
elif op == "f": |
| 541 |
seek_convergence = False |
| 542 |
|
| 543 |
TerminationConditionMet = False |
| 544 |
RMS_Error = None |
| 545 |
while not (TerminationConditionMet): |
| 546 |
number_iterations += 1 |
| 547 |
M = np.zeros([N2, 6]) |
| 548 |
G = np.zeros(N2)[:, np.newaxis] |
| 549 |
c1 = complex(-m4, m3) |
| 550 |
c2 = complex(m1, m2) |
| 551 |
c3 = complex(m3, m4) |
| 552 |
for i in range(N): |
| 553 |
i2 = i + N |
| 554 |
y = 1.0 / complex(1.0, 2 * (m6 * self.f[i] / Flwst - m5)) |
| 555 |
u = c1 * y * y * 2 |
| 556 |
u2 = -u * self.f[i] / Flwst |
| 557 |
M[i, :] = 1.0, 0.0, y.real, -y.imag, u.real, u2.real |
| 558 |
M[i2, :] = 0.0, 1.0, y.imag, y.real, u.imag, u2.imag |
| 559 |
v = c2 + c3 * y |
| 560 |
r = self.s[i] - v |
| 561 |
G[i] = r.real |
| 562 |
G[i2] = r.imag |
| 563 |
X = M.transpose() |
| 564 |
T = np.multiply(X, PV2) |
| 565 |
C = np.dot(T, M) |
| 566 |
q = np.dot(T, G) |
| 567 |
dm = np.linalg.solve(C, q)[:,0] |
| 568 |
m1 += dm[0] |
| 569 |
m2 += dm[1] |
| 570 |
m3 += dm[2] |
| 571 |
m4 += dm[3] |
| 572 |
m5 += dm[4] |
| 573 |
m6 += dm[5] |
| 574 |
del G, X, T, C, dm |
| 575 |
iterations = iterations + 1 |
| 576 |
if RMS_Error is not None: |
| 577 |
Last_RMS_Error = RMS_Error |
| 578 |
else: |
| 579 |
Last_RMS_Error = None |
| 580 |
SumNum = 0.0 |
| 581 |
SumDen = 0.0 |
| 582 |
for i in range(N): |
| 583 |
den = complex(1.0, 2 * (m6 * self.f[i] / Flwst - m5)) |
| 584 |
ip = PV[i] |
| 585 |
E = self.s[i] - complex(m1, m2) - complex(m3, m4) / den |
| 586 |
SumNum = SumNum + ip * (E.real * E.real + E.imag * E.imag) |
| 587 |
SumDen = SumDen + ip |
| 588 |
RMS_Error = np.sqrt(SumNum / SumDen) |
| 589 |
if self.verbose: |
| 590 |
if last_op == "c": |
| 591 |
print(f"Iteration {iterations}, RMS Error: {RMS_Error}") |
| 592 |
else: |
| 593 |
print(f"op {op}, Iteration {iterations}, RMS Error: {RMS_Error}") |
| 594 |
last_op = op |
| 595 |
|
| 596 |
if seek_convergence: |
| 597 |
if Last_RMS_Error is not None: |
| 598 |
delta_S = abs(RMS_Error - Last_RMS_Error) |
| 599 |
TerminationConditionMet = delta_S < self.tol |
| 600 |
else: |
| 601 |
TerminationConditionMet = True |
| 602 |
|
| 603 |
if self.verbose: |
| 604 |
print("Optimization done.") |
| 605 |
|
| 606 |
|
| 607 |
return OptimizedResult({ |
| 608 |
'success': TerminationConditionMet, |
| 609 |
'm1': m1, 'm2': m2, 'm3': m3, 'm4': m4, |
| 610 |
'Q_L': m5, |
| 611 |
'f_L': m5 * Flwst / m6, |
| 612 |
'weighting_ratio': weighting_ratio, |
| 613 |
'number_iterations': number_iterations, |
| 614 |
'RMS_Error': RMS_Error, |
| 615 |
'method': self.method, |
| 616 |
}) |
| 617 |
|
| 618 |
def _optimise_fit7(self, N): |
| 619 |
"""Iterative non-linear fit, NLQFIT7 Step (2). |
| 620 |
|
| 621 |
Optimised fit of Q-factor (QL) and resonant frequency (FL) |
| 622 |
by the gradient-descent method. |
| 623 |
|
| 624 |
Uses the results of the initial fit (sv) as the starting |
| 625 |
values for the iteration. |
| 626 |
|
| 627 |
Parameters |
| 628 |
---------- |
| 629 |
F : np.ndarray |
| 630 |
Frequency points |
| 631 |
S : np.ndarray |
| 632 |
Complex data S-parameter to be fitted. |
| 633 |
N : int |
| 634 |
Number of points. |
| 635 |
Fseed : float |
| 636 |
Estimated resonant frequency. |
| 637 |
sv : list |
| 638 |
Initial solution (numpy vector or a list) found with initial_fit. |
| 639 |
loop_plan : str |
| 640 |
Characters which defines order of steps used by the fitting process |
| 641 |
e.g. 'fwfwc': |
| 642 |
|
| 643 |
- 'f': fit once without testing for convergence. |
| 644 |
- 'c': repeated fit, iterating until convergence is obtained. |
| 645 |
- 'w': re-calculate weighting factors on basis of previous fit. |
| 646 |
Tol : float |
| 647 |
Criterion for the convergence test. |
| 648 |
Recommend using 1.0E-5 for reflection or max(abs(S))*1.0E-5 |
| 649 |
for transmission. |
| 650 |
quiet : bool |
| 651 |
Boolean flag controlling output of information to the console. |
| 652 |
|
| 653 |
Returns |
| 654 |
------- |
| 655 |
list |
| 656 |
list of fitted parameters: [m1, m2, m3, m4, m5, m5 * Flwst / m6, m7 / Flwst] |
| 657 |
weighting_ratio : float |
| 658 |
number_iterations : int |
| 659 |
RMS_Error : float |
| 660 |
|
| 661 |
References |
| 662 |
---------- |
| 663 |
.. [MAT58] MAT 58, section 2.3, eqn. (26). |
| 664 |
|
| 665 |
""" |
| 666 |
N2 = N * 2 |
| 667 |
iterations = 0 |
| 668 |
PV = np.ones(N) |
| 669 |
PV2 = np.ones(N2) |
| 670 |
|
| 671 |
m1 = self._a.imag / self.Q_L |
| 672 |
m2 = -self._a.real / self.Q_L |
| 673 |
m3 = self._b.real - m1 |
| 674 |
m4 = self._b.imag - m2 |
| 675 |
m5 = self.Q_L |
| 676 |
Flwst = self.f[0] |
| 677 |
m6 = Flwst * self.Q_L / self.f_L |
| 678 |
m7 = 0.0 |
| 679 |
last_op = "n" |
| 680 |
weighting_ratio = None |
| 681 |
number_iterations = 0 |
| 682 |
|
| 683 |
|
| 684 |
for op in self.loop_plan: |
| 685 |
|
| 686 |
if op == "w": |
| 687 |
PV = self.angular_weights(self.f, Flwst * m5 / m6, m5) |
| 688 |
weighting_ratio = max(PV) / min(PV) |
| 689 |
PV2 = np.concatenate((PV, PV)) |
| 690 |
if self.verbose: |
| 691 |
print("Op w, Calculate weights") |
| 692 |
last_op = "n" |
| 693 |
continue |
| 694 |
if op == "c": |
| 695 |
seek_convergence = True |
| 696 |
elif op == "f": |
| 697 |
seek_convergence = False |
| 698 |
else: |
| 699 |
assert 0, "Unexpected character in loop_plan" |
| 700 |
|
| 701 |
TerminationConditionMet = False |
| 702 |
RMS_Error = None |
| 703 |
while not (TerminationConditionMet): |
| 704 |
number_iterations += 1 |
| 705 |
M = np.zeros([N2, 7]) |
| 706 |
G = np.zeros(N2)[:, np.newaxis] |
| 707 |
c1 = complex(-m4, m3) |
| 708 |
c2 = complex(m1, m2) |
| 709 |
c3 = complex(m3, m4) |
| 710 |
for i in range(N): |
| 711 |
i2 = i + N |
| 712 |
y = 1.0 / complex(1.0, 2 * (m6 * self.f[i] / Flwst - m5)) |
| 713 |
fdn = self.f[i] / Flwst - m5 / m6 |
| 714 |
pj = complex(0.0, m7 * fdn) |
| 715 |
expm7 = np.exp(pj) |
| 716 |
ym = y * expm7 |
| 717 |
u = c1 * y * ym * 2 |
| 718 |
u2 = -u * self.f[i] / Flwst |
| 719 |
v = (c2 + y * c3) * expm7 |
| 720 |
u3 = v * fdn |
| 721 |
M[i, :] = np.array( |
| 722 |
[expm7.real, |
| 723 |
-expm7.imag, |
| 724 |
ym.real, |
| 725 |
-ym.imag, |
| 726 |
u.real, |
| 727 |
u2.real, |
| 728 |
-u3.imag] |
| 729 |
) |
| 730 |
M[i2, :] = np.array( |
| 731 |
[expm7.imag, |
| 732 |
expm7.real, |
| 733 |
ym.imag, |
| 734 |
ym.real, |
| 735 |
u.imag, |
| 736 |
u2.imag, |
| 737 |
u3.real] |
| 738 |
) |
| 739 |
r = self.s[i] - v |
| 740 |
G[i] = r.real |
| 741 |
G[i2] = r.imag |
| 742 |
X = M.transpose() |
| 743 |
T = np.multiply(X, PV2) |
| 744 |
C = np.dot(T, M) |
| 745 |
q = np.dot(T, G) |
| 746 |
dm = np.linalg.solve(C, q)[:,0] |
| 747 |
m1 += dm[0] |
| 748 |
m2 += dm[1] |
| 749 |
m3 += dm[2] |
| 750 |
m4 += dm[3] |
| 751 |
m5 += dm[4] |
| 752 |
m6 += dm[5] |
| 753 |
m7 += dm[6] |
| 754 |
del G, X, T, C, dm |
| 755 |
iterations = iterations + 1 |
| 756 |
if RMS_Error is not None: |
| 757 |
Last_RMS_Error = RMS_Error |
| 758 |
else: |
| 759 |
Last_RMS_Error = None |
| 760 |
|
| 761 |
SumNum = 0.0 |
| 762 |
SumDen = 0.0 |
| 763 |
for i in range(N): |
| 764 |
fdn = self.f[i] / Flwst - m5 / m6 |
| 765 |
den = complex(1.0, 2 * (m6 * self.f[i] / Flwst - m5)) |
| 766 |
pj = complex(0.0, m7 * fdn) |
| 767 |
E = self.s[i] - (c2 + c3 / den) * np.exp(pj) |
| 768 |
ip = PV[i] |
| 769 |
SumNum = SumNum + ip * (E.real * E.real + E.imag * E.imag) |
| 770 |
SumDen = SumDen + ip |
| 771 |
RMS_Error = np.sqrt(SumNum / SumDen) |
| 772 |
if self.verbose: |
| 773 |
if last_op == "c": |
| 774 |
print(f"Iteration {iterations}, RMS Error: {RMS_Error}") |
| 775 |
else: |
| 776 |
print(f"op {op}, Iteration {iterations}, RMS Error: {RMS_Error}") |
| 777 |
|
| 778 |
last_op = op |
| 779 |
|
| 780 |
if seek_convergence: |
| 781 |
if Last_RMS_Error is not None: |
| 782 |
delta_S = abs(RMS_Error - Last_RMS_Error) |
| 783 |
TerminationConditionMet = delta_S < self.tol |
| 784 |
else: |
| 785 |
TerminationConditionMet = True |
| 786 |
|
| 787 |
if self.verbose: |
| 788 |
print("Optimization done.") |
| 789 |
|
| 790 |
return OptimizedResult({ |
| 791 |
'success': TerminationConditionMet, |
| 792 |
'm1': m1, 'm2': m2, 'm3': m3, 'm4': m4, |
| 793 |
'Q_L': m5, |
| 794 |
'f_L': m5 * Flwst / m6, |
| 795 |
'm7a' : m7 / Flwst, |
| 796 |
'weighting_ratio': weighting_ratio, |
| 797 |
'number_iterations': number_iterations, |
| 798 |
'RMS_Error': RMS_Error, |
| 799 |
'method': self.method, |
| 800 |
}) |
| 801 |
|
| 802 |
def _optimise_fit8(self, N): |
| 803 |
"""Iterative non-linear fit, NLQFIT8 Step (2). |
| 804 |
|
| 805 |
Optimised fit of Q-factor (QL) and resonant frequency (FL) |
| 806 |
by the gradient-descent method. |
| 807 |
|
| 808 |
Uses the results of the initial fit (sv) as the starting |
| 809 |
values for the iteration. |
| 810 |
|
| 811 |
Parameters |
| 812 |
---------- |
| 813 |
F : np.ndarray |
| 814 |
Frequency points |
| 815 |
S : np.ndarray |
| 816 |
Complex data S-parameter to be fitted. |
| 817 |
N : int |
| 818 |
Number of points. |
| 819 |
Fseed : float |
| 820 |
Estimated resonant frequency. |
| 821 |
sv : list |
| 822 |
Initial solution (numpy vector or a list) found with initial_fit. |
| 823 |
loop_plan : str |
| 824 |
Characters which defines order of steps used by the fitting process |
| 825 |
e.g. 'fwfwc': |
| 826 |
'f' - fit once without testing for convergence. |
| 827 |
'c' - repeated fit, iterating until convergence is obtained. |
| 828 |
'w' - re-calculate weighting factors on basis of previous fit. |
| 829 |
Tol : float |
| 830 |
Criterion for the convergence test. |
| 831 |
Recommend using 1.0E-5 for reflection or max(abs(S))*1.0E-5 |
| 832 |
for transmission. |
| 833 |
quiet : bool |
| 834 |
Boolean flag controlling output of information to the console. |
| 835 |
|
| 836 |
Returns |
| 837 |
------- |
| 838 |
list |
| 839 |
list of fitted parameters: [m1, m2, m3, m4, m5, m5 * Flwst / m6, m7 / Flwst] |
| 840 |
weighting_ratio : float |
| 841 |
number_iterations : int |
| 842 |
RMS_Error : float |
| 843 |
|
| 844 |
References |
| 845 |
---------- |
| 846 |
.. [MAT58] MAT 58, sec 4.5, eqn. (43). |
| 847 |
|
| 848 |
""" |
| 849 |
N2 = N * 2 |
| 850 |
iterations = 0 |
| 851 |
PV = np.ones(N) |
| 852 |
PV2 = np.ones(N2) |
| 853 |
|
| 854 |
m1 = self._a.imag / self.Q_L |
| 855 |
m2 = -self._a.real / self.Q_L |
| 856 |
m3 = self._b.real - m1 |
| 857 |
m4 = self._b.imag - m2 |
| 858 |
m5 = self.Q_L |
| 859 |
Flwst = self.f[0] |
| 860 |
m6 = Flwst * self.Q_L / self.f_L |
| 861 |
m8 = 0.0 |
| 862 |
m9 = 0.0 |
| 863 |
last_op = "n" |
| 864 |
weighting_ratio = None |
| 865 |
number_iterations = 0 |
| 866 |
|
| 867 |
|
| 868 |
for op in self.loop_plan: |
| 869 |
|
| 870 |
if op == "w": |
| 871 |
PV = self.angular_weights(self.f, Flwst * float(m5) / float(m6), float(m5)) |
| 872 |
weighting_ratio = max(PV) / min(PV) |
| 873 |
PV2 = np.concatenate((PV, PV)) |
| 874 |
if self.verbose: |
| 875 |
print("Op w, Calculate weights") |
| 876 |
last_op = "n" |
| 877 |
continue |
| 878 |
if op == "c": |
| 879 |
seek_convergence = True |
| 880 |
elif op == "f": |
| 881 |
seek_convergence = False |
| 882 |
else: |
| 883 |
assert 0, "Unexpected character in loop_plan" |
| 884 |
|
| 885 |
TerminationConditionMet = False |
| 886 |
RMS_Error = None |
| 887 |
while not (TerminationConditionMet): |
| 888 |
number_iterations += 1 |
| 889 |
M = np.zeros([N2, 8]) |
| 890 |
G = np.zeros(N2)[:, np.newaxis] |
| 891 |
c1 = complex(-m4, m3) |
| 892 |
c2 = complex(m1, m2) |
| 893 |
c3 = complex(m3, m4) |
| 894 |
for i in range(N): |
| 895 |
i2 = i + N |
| 896 |
y = 1.0 / complex(1.0, 2 * (m6 * self.f[i] / Flwst - m5)) |
| 897 |
u = c1 * y * y * 2 |
| 898 |
u2 = -u * self.f[i] / Flwst |
| 899 |
FL = Flwst * m5 / m6 |
| 900 |
t = 2 * (self.f[i] - FL) / FL |
| 901 |
M[i, :] = np.array([1.0, 0.0, y.real, -y.imag, u.real, u2.real, t, 0.0]) |
| 902 |
M[i2, :] = np.array([0.0, 1.0, y.imag, y.real, u.imag, u2.imag, 0.0, t]) |
| 903 |
v = c2 + c3 * y + (m8 + 1j * m9) * t |
| 904 |
r = self.s[i] - v |
| 905 |
G[i] = r.real |
| 906 |
G[i2] = r.imag |
| 907 |
X = M.transpose() |
| 908 |
T = np.multiply(X, PV2) |
| 909 |
C = np.dot(T, M) |
| 910 |
q = np.dot(T, G) |
| 911 |
dm = np.linalg.solve(C, q)[:,0] |
| 912 |
m1 += dm[0] |
| 913 |
m2 += dm[1] |
| 914 |
m3 += dm[2] |
| 915 |
m4 += dm[3] |
| 916 |
m5 += dm[4] |
| 917 |
m6 += dm[5] |
| 918 |
m8 += dm[6] |
| 919 |
m9 += dm[7] |
| 920 |
del G, X, T, C, dm |
| 921 |
iterations = iterations + 1 |
| 922 |
if RMS_Error is not None: |
| 923 |
Last_RMS_Error = RMS_Error |
| 924 |
else: |
| 925 |
Last_RMS_Error = None |
| 926 |
|
| 927 |
SumNum = 0.0 |
| 928 |
SumDen = 0.0 |
| 929 |
for i in range(N): |
| 930 |
den = complex(1.0, 2 * (m6 * self.f[i] / Flwst - m5)) |
| 931 |
FL = Flwst * m5 / m6 |
| 932 |
t = 2 * (self.f[i] - FL) / FL |
| 933 |
ip = PV[i] |
| 934 |
E = ( |
| 935 |
self.s[i] |
| 936 |
- complex(m1, m2) |
| 937 |
- complex(m8, m9) * t |
| 938 |
- complex(m3, m4) / den |
| 939 |
) |
| 940 |
SumNum = SumNum + ip * (E.real * E.real + E.imag * E.imag) |
| 941 |
SumDen = SumDen + ip |
| 942 |
RMS_Error = np.sqrt(SumNum / SumDen) |
| 943 |
if self.verbose: |
| 944 |
if last_op == "c": |
| 945 |
print(f"Iteration {iterations}, RMS Error: {RMS_Error}") |
| 946 |
else: |
| 947 |
print(f"{op}, Iteration {iterations}, RMS Error: {RMS_Error}") |
| 948 |
|
| 949 |
last_op = op |
| 950 |
|
| 951 |
if seek_convergence: |
| 952 |
if Last_RMS_Error is not None: |
| 953 |
delta_S = abs(RMS_Error - Last_RMS_Error) |
| 954 |
TerminationConditionMet = delta_S < self.tol |
| 955 |
else: |
| 956 |
TerminationConditionMet = True |
| 957 |
|
| 958 |
if self.verbose: |
| 959 |
print("Optimization done.") |
| 960 |
|
| 961 |
return OptimizedResult({ |
| 962 |
'success': TerminationConditionMet, |
| 963 |
'm1': m1, 'm2': m2, 'm3': m3, 'm4': m4, |
| 964 |
'Q_L': m5, |
| 965 |
'f_L': m5 * Flwst / m6, |
| 966 |
'weighting_ratio': weighting_ratio, |
| 967 |
'number_iterations': number_iterations, |
| 968 |
'RMS_Error': RMS_Error, |
| 969 |
'method': self.method, |
| 970 |
}) |
| 971 |
|
| 972 |
def Q_circle(self, |
| 973 |
opt_res: None | OptimizedResult = None, |
| 974 |
A: None | float = None |
| 975 |
) -> list: |
| 976 |
r"""Q-circle diameter. |
| 977 |
|
| 978 |
The diameter of the Q-circle (as displayed in a VNA) provides a visual |
| 979 |
indication of whether the coupling is strong or weak [MAT58]_. |
| 980 |
|
| 981 |
Parameters |
| 982 |
---------- |
| 983 |
opt_res : None or :class:`~skrf.qfactor.OptimizedResult`. Default is None. |
| 984 |
Solution produced by the :meth:`~skrf.qfactor.Qfactor.fit` method. |
| 985 |
If None, uses the solution previously calculated, if performed. |
| 986 |
A : None of float. Default is None. |
| 987 |
Scaling factor as defined in MAT 58 [MAT58]_. |
| 988 |
For `reflection` resonance type, can be set as None |
| 989 |
to use the magnitude of the fitted detuned reflection coefficient S_V |
| 990 |
|
| 991 |
Returns |
| 992 |
------- |
| 993 |
diam : float |
| 994 |
Q-circle diameter d. |
| 995 |
S_V : complex |
| 996 |
Off-resonance Reflection Coefficient. |
| 997 |
S_T : complex |
| 998 |
Tuned Reflection Coefficient. |
| 999 |
|
| 1000 |
References |
| 1001 |
---------- |
| 1002 |
.. [MAT58] "Q-factor Measurement by using a Vector Network Analyser", |
| 1003 |
A. P. Gregory, National Physical Laboratory Report MAT 58 (2021) |
| 1004 |
https://eprintspublications.npl.co.uk/9304/, |
| 1005 |
section 2.5, eqn. (31). |
| 1006 |
|
| 1007 |
""" |
| 1008 |
|
| 1009 |
if opt_res is None: |
| 1010 |
if self.opt_res is None: |
| 1011 |
raise ValueError('No solution found or passed.') |
| 1012 |
else: |
| 1013 |
opt_res = self.opt_res |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
m1, m2, m3, m4 = (opt_res[key] for key in ['m1', 'm2', 'm3', 'm4']) |
| 1020 |
|
| 1021 |
if A is None: |
| 1022 |
A = 1.0 / abs(complex(m1, m2)) |
| 1023 |
elif not isinstance(A, int | float): |
| 1024 |
raise ValueError("A should be a float or None") |
| 1025 |
|
| 1026 |
aqratio = complex(m1, m2) |
| 1027 |
b = complex(m1 + m3, m2 + m4) |
| 1028 |
diam = abs(b - aqratio) * A |
| 1029 |
S_V = complex(m1, m2) * A |
| 1030 |
S_T = b * A |
| 1031 |
return diam, S_V, S_T |
| 1032 |
|
| 1033 |
|
| 1034 |
def Q_unloaded(self, |
| 1035 |
opt_res: None| OptimizedResult = None, |
| 1036 |
A: None | float = None |
| 1037 |
) -> float: |
| 1038 |
"""Unloaded Q-factor Q0. |
| 1039 |
|
| 1040 |
The value of the unloaded Q-factor Q0 cannot be measured directly but |
| 1041 |
can be estimated from the measurement of the loaded Q-factor Q_L [MAT58]_ . |
| 1042 |
|
| 1043 |
Parameters |
| 1044 |
---------- |
| 1045 |
opt_res : None or :class:`~skrf.qfactor.OptimizedResult`. Default is None. |
| 1046 |
Solution produced by the :meth:`~skrf.qfactor.Qfactor.fit` method. |
| 1047 |
If None, uses the solution previously calculated, if performed. |
| 1048 |
A : float or None. Default is None. |
| 1049 |
Scaling factor as defined in MAT 58 [MAT58]_. |
| 1050 |
For `reflection` resonance type, can be set as None |
| 1051 |
to use the magnitude of the fitted detuned reflection coefficient S_V |
| 1052 |
|
| 1053 |
Returns |
| 1054 |
------- |
| 1055 |
Q0 : float |
| 1056 |
Unloaded Q-factor. |
| 1057 |
|
| 1058 |
References |
| 1059 |
---------- |
| 1060 |
.. [MAT58] "Q-factor Measurement by using a Vector Network Analyser", |
| 1061 |
A. P. Gregory, National Physical Laboratory Report MAT 58 (2021) |
| 1062 |
https://eprintspublications.npl.co.uk/9304/ |
| 1063 |
|
| 1064 |
""" |
| 1065 |
|
| 1066 |
if opt_res is None: |
| 1067 |
if self.opt_res is None: |
| 1068 |
raise ValueError('No solution found or passed.') |
| 1069 |
else: |
| 1070 |
opt_res = self.opt_res |
| 1071 |
|
| 1072 |
if A is None: |
| 1073 |
auto_flag = True |
| 1074 |
elif isinstance(A, int | float): |
| 1075 |
auto_flag = False |
| 1076 |
else: |
| 1077 |
raise ValueError("Illegal Scaling factor; should be a float or None") |
| 1078 |
|
| 1079 |
m1, m2, m3, m4, m5 = (opt_res[key] for key in ['m1', 'm2', 'm3', 'm4', 'Q_L']) |
| 1080 |
|
| 1081 |
if self.res_type == "transmission": |
| 1082 |
if auto_flag: |
| 1083 |
raise ValueError('Scaling factor must be defined for transmission case') |
| 1084 |
cal_diam, cal_gamma_V, cal_gamma_T = self.Q_circle(opt_res, A) |
| 1085 |
if cal_diam == 1.0: |
| 1086 |
raise ZeroDivisionError("Divide by zero forestalled in calculation of Q0") |
| 1087 |
Q0 = m5 / (1.0 - cal_diam) |
| 1088 |
|
| 1089 |
elif self.res_type == "reflection": |
| 1090 |
if auto_flag: |
| 1091 |
if self.verbose: |
| 1092 |
print('A is undefined: using fitted data to estimate it') |
| 1093 |
A = 1.0 / abs(complex(m1, m2)) |
| 1094 |
cal_diam, S_V, S_T = self.Q_circle(opt_res, A) |
| 1095 |
cal_touching_circle_diam = 2.0 |
| 1096 |
if self.verbose: |
| 1097 |
print(f"Q-circle diam = {cal_diam}, touching_circle_diam = {cal_touching_circle_diam}") |
| 1098 |
den = cal_touching_circle_diam / cal_diam - 1.0 |
| 1099 |
Q0 = m5 * (1.0 + 1.0 / den) |
| 1100 |
|
| 1101 |
elif self.res_type == "reflection_method2": |
| 1102 |
if auto_flag: |
| 1103 |
raise ValueError('Scaling factor must be defined for Method 2') |
| 1104 |
cal_diam, S_V, S_T = self.Q_circle(opt_res, A) |
| 1105 |
gv = abs(S_V) |
| 1106 |
gv2 = gv * gv |
| 1107 |
mb = abs(S_T) |
| 1108 |
cosphi = (gv2 + cal_diam * cal_diam - mb * mb) / ( |
| 1109 |
2.0 * gv * cal_diam |
| 1110 |
) |
| 1111 |
cal_touching_circle_diam = (1.0 - gv2) / (1.0 - gv * cosphi) |
| 1112 |
if self.verbose: |
| 1113 |
print(f"Q-circle diam = {cal_diam}, touching_circle_diam = {cal_touching_circle_diam}") |
| 1114 |
den = cal_touching_circle_diam / cal_diam - 1.0 |
| 1115 |
Q0 = m5 * (1.0 + 1.0 / den) |
| 1116 |
|
| 1117 |
elif self.res_type == "notch" or self.res_type == "absorption": |
| 1118 |
if auto_flag: |
| 1119 |
if self.verbose: |
| 1120 |
print( |
| 1121 |
'Notch/absorption Qo calculation: using fitted data to estimate scaling factor' |
| 1122 |
) |
| 1123 |
|
| 1124 |
A = 1.0 / abs(complex(m1, m2)) |
| 1125 |
cal_diam, S_V, S_T = self.Q_circle(opt_res, A) |
| 1126 |
if self.verbose: |
| 1127 |
print(f"Q-circle diam = {cal_diam}") |
| 1128 |
if cal_diam == 1.0: |
| 1129 |
raise ZeroDivisionError("Divide by zero forestalled in calculation of Qo") |
| 1130 |
den = 1.0 / cal_diam - 1.0 |
| 1131 |
Q0 = m5 * ( |
| 1132 |
1.0 + 1.0 / den |
| 1133 |
) |
| 1134 |
|
| 1135 |
else: |
| 1136 |
raise ValueError("Unknown resonance type {self.res_type}") |
| 1137 |
|
| 1138 |
return Q0 |
| 1139 |
|
| 1140 |
def fitted_s(self, |
| 1141 |
opt_res: None | OptimizedResult = None, |
| 1142 |
f: None | np.ndarray = None |
| 1143 |
) -> np.ndarray: |
| 1144 |
|
| 1145 |
r"""S-parameter response of an equivalent circuit model resonator. |
| 1146 |
|
| 1147 |
The approximate solution and estimated is obtained from [MAT58]_: |
| 1148 |
|
| 1149 |
.. math:: |
| 1150 |
|
| 1151 |
S = m_1 + j m_2 + \frac{m_3 + j m_4}{1 + j Q_L t} |
| 1152 |
|
| 1153 |
where the m coefficients come from the fitted solution and |
| 1154 |
|
| 1155 |
.. math:: |
| 1156 |
|
| 1157 |
t = \frac{f}{f_L} - \frac{f_L}{f} \approx 2 \frac{f − f_L}{f_L} |
| 1158 |
|
| 1159 |
|
| 1160 |
Parameters |
| 1161 |
---------- |
| 1162 |
opt_res : None or :class:`~skrf.qfactor.OptimizedResult`. Default is None. |
| 1163 |
Solution produced by the :meth:`~skrf.qfactor.Qfactor.fit` method. |
| 1164 |
If None, uses the solution previously calculated, if performed. |
| 1165 |
f : None or np.ndarray. Default is None. |
| 1166 |
frequency array [Hz]. If None, use the self frequencies. |
| 1167 |
|
| 1168 |
Returns |
| 1169 |
------- |
| 1170 |
s : np.ndarray |
| 1171 |
S-parameter response. |
| 1172 |
|
| 1173 |
References |
| 1174 |
---------- |
| 1175 |
.. [MAT58] "Q-factor Measurement by using a Vector Network Analyser", |
| 1176 |
A. P. Gregory, National Physical Laboratory Report MAT 58 (2021) |
| 1177 |
https://eprintspublications.npl.co.uk/9304/, |
| 1178 |
section 2.2, eqn. (21). |
| 1179 |
|
| 1180 |
""" |
| 1181 |
if opt_res is None: |
| 1182 |
if self.opt_res is None: |
| 1183 |
raise ValueError('No solution found or passed.') |
| 1184 |
else: |
| 1185 |
opt_res = self.opt_res |
| 1186 |
|
| 1187 |
if f is None: |
| 1188 |
f = self.f |
| 1189 |
|
| 1190 |
|
| 1191 |
t = f/opt_res.f_L - opt_res.f_L/f |
| 1192 |
|
| 1193 |
y = 1/(1 + 1j*opt_res.Q_L*t) |
| 1194 |
s = opt_res.m1 +1j*opt_res.m2 + (opt_res.m3 + 1j*opt_res.m4) * y |
| 1195 |
return s |
| 1196 |
|
| 1197 |
def fitted_network(self, |
| 1198 |
opt_res: None | OptimizedResult = None, |
| 1199 |
frequency: None | Frequency = None, |
| 1200 |
) -> Network: |
| 1201 |
"""Fitted Network. |
| 1202 |
|
| 1203 |
Return the Network corresponding to the fitted response. |
| 1204 |
|
| 1205 |
Parameters |
| 1206 |
---------- |
| 1207 |
opt_res : None or :class:`~skrf.qfactor.OptimizedResult`. Default is None. |
| 1208 |
Solution produced by the :meth:`~skrf.qfactor.Qfactor.fit` method. |
| 1209 |
If None, uses the solution previously calculated, if performed. |
| 1210 |
frequency : None or :class:`~skrf.frequency.Frequency`. Default is None. |
| 1211 |
Frequency for the fitted Network. If None, use the same |
| 1212 |
Frequency than the one used to create the QFactor. |
| 1213 |
|
| 1214 |
Returns |
| 1215 |
------- |
| 1216 |
ntwk : :class:`~skrf.network.Network` |
| 1217 |
Fitted Network for the passed Frequency. |
| 1218 |
|
| 1219 |
""" |
| 1220 |
|
| 1221 |
if opt_res is None: |
| 1222 |
if self.opt_res is None: |
| 1223 |
raise ValueError('No solution found or passed.') |
| 1224 |
else: |
| 1225 |
opt_res = self.opt_res |
| 1226 |
|
| 1227 |
if frequency is None: |
| 1228 |
frequency = self._ntwk.frequency |
| 1229 |
|
| 1230 |
s = self.fitted_s(opt_res, f=frequency.f) |
| 1231 |
|
| 1232 |
ntwk = Network(s=s, frequency=frequency) |
| 1233 |
return ntwk |
| 1234 |
|
| 1235 |
@property |
| 1236 |
def f_L_scaled(self) -> float: |
| 1237 |
""" |
| 1238 |
Resonant Frequency in the frequency unit.. |
| 1239 |
|
| 1240 |
Returns |
| 1241 |
------- |
| 1242 |
float |
| 1243 |
Resonant frequency in the frequency unit. |
| 1244 |
|
| 1245 |
See Also |
| 1246 |
-------- |
| 1247 |
f_L : Resonant Frequency in Hz. |
| 1248 |
|
| 1249 |
""" |
| 1250 |
if not self.fitted: |
| 1251 |
warn('Q-factor not fitted, result may be inaccurate. Use the .fit() method before.', stacklevel=2) |
| 1252 |
return self.f_L/self.f_multiplier |
| 1253 |
|
| 1254 |
@property |
| 1255 |
def BW(self) -> float: |
| 1256 |
r"""3-dB Bandwidth. |
| 1257 |
|
| 1258 |
Return the half-power fractional bandwidth (aka 3-dB bandwidth) |
| 1259 |
defined as: |
| 1260 |
|
| 1261 |
.. math:: |
| 1262 |
|
| 1263 |
BW = \frac{f_L}{Q_L} |
| 1264 |
|
| 1265 |
|
| 1266 |
Returns |
| 1267 |
------- |
| 1268 |
float |
| 1269 |
3-dB Bandwidth in Hz. |
| 1270 |
|
| 1271 |
See Also |
| 1272 |
-------- |
| 1273 |
BW_scaled : 3-dB Bandwidth scaled to the frequency unit. |
| 1274 |
|
| 1275 |
""" |
| 1276 |
if not self.fitted: |
| 1277 |
warn('Q-factor not fitted, result may be inaccurate. Use the .fit() method before.', stacklevel=2) |
| 1278 |
return self.f_L/self.Q_L |
| 1279 |
|
| 1280 |
@property |
| 1281 |
def BW_scaled(self) -> float: |
| 1282 |
r"""3-dB Bandwidth scaled to the frequency unit. |
| 1283 |
|
| 1284 |
Returns |
| 1285 |
------- |
| 1286 |
float |
| 1287 |
3-dB Bandwidth in the frequency unit. |
| 1288 |
|
| 1289 |
See Also |
| 1290 |
-------- |
| 1291 |
BW : 3-dB Bandwidth in Hz. |
| 1292 |
|
| 1293 |
""" |
| 1294 |
if not self.fitted: |
| 1295 |
warn('Q-factor not fitted, result may be inaccurate. Use the .fit() method before.', stacklevel=2) |
| 1296 |
return self.BW/self.f_multiplier |