| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import dataclasses |
| 4 |
from abc import ABC, abstractmethod |
| 5 |
|
| 6 |
import numpy as np |
| 7 |
|
| 8 |
import skrf |
| 9 |
|
| 10 |
|
| 11 |
class SweepSection(ABC): |
| 12 |
@abstractmethod |
| 13 |
def get_hz(self) -> list[float]: |
| 14 |
''' List of hz represented by this section after applying mask ''' |
| 15 |
pass |
| 16 |
|
| 17 |
def get_raw_hz(self) -> list[float]: |
| 18 |
''' List of hz fetched from the instrument before applying mask ''' |
| 19 |
return self.get_hz() |
| 20 |
|
| 21 |
@abstractmethod |
| 22 |
def apply_8510(self, hp8510c): |
| 23 |
pass |
| 24 |
|
| 25 |
def mask_8510(self, network : skrf.Network) -> skrf.Network: |
| 26 |
return network |
| 27 |
|
| 28 |
|
| 29 |
@dataclasses.dataclass |
| 30 |
class LinearBuiltinSweepSection(SweepSection): |
| 31 |
hz_min: float |
| 32 |
hz_max: float |
| 33 |
n_points: int |
| 34 |
|
| 35 |
def get_hz(self) -> list[float]: |
| 36 |
return np.linspace(self.hz_min, self.hz_max, self.n_points) |
| 37 |
|
| 38 |
def apply_8510(self, hp8510c): |
| 39 |
hp8510c._set_instrument_step_state(self.hz_min, self.hz_max, self.n_points) |
| 40 |
|
| 41 |
|
| 42 |
@dataclasses.dataclass |
| 43 |
class LinearMaskedSweepSection(SweepSection): |
| 44 |
hz_min: float |
| 45 |
hz_max: float |
| 46 |
n_points: int |
| 47 |
mask: list[bool] |
| 48 |
|
| 49 |
def get_hz(self) -> list[float]: |
| 50 |
return self.get_raw_hz()[self.mask] |
| 51 |
|
| 52 |
def get_raw_hz(self) -> list[float]: |
| 53 |
return np.linspace(self.hz_min, self.hz_max, self.n_points) |
| 54 |
|
| 55 |
def apply_8510(self, hp8510c): |
| 56 |
hp8510c._set_instrument_step_state(self.hz_min, self.hz_max, self.n_points) |
| 57 |
|
| 58 |
def mask_8510(self, network : skrf.Network) -> skrf.Network: |
| 59 |
return network[self.mask] |
| 60 |
|
| 61 |
|
| 62 |
@dataclasses.dataclass |
| 63 |
class LinearCustomSweepSection(SweepSection): |
| 64 |
hz_min: float |
| 65 |
hz_max: float |
| 66 |
n_points: int |
| 67 |
|
| 68 |
def get_hz(self) -> list[float]: |
| 69 |
return np.linspace(self.hz_min, self.hz_max, self.n_points) |
| 70 |
|
| 71 |
def get_raw_hz(self) -> list[float]: |
| 72 |
if self.n_points==1: |
| 73 |
return [self.hz_min, self.hz_min+1] |
| 74 |
return np.linspace(self.hz_min, self.hz_max, self.n_points) |
| 75 |
|
| 76 |
def apply_8510(self, hp8510c): |
| 77 |
assert self.n_points <= 792 |
| 78 |
if self.n_points==1: |
| 79 |
hp8510c._set_instrument_step_state(self.hz_min, self.hz_max, 2) |
| 80 |
return |
| 81 |
hp8510c._set_instrument_step_state(self.hz_min, self.hz_max, self.n_points) |
| 82 |
|
| 83 |
def mask_8510(self, network : skrf.Network) -> skrf.Network: |
| 84 |
if self.n_points==1: |
| 85 |
return network[0] |
| 86 |
return network |
| 87 |
|
| 88 |
|
| 89 |
@dataclasses.dataclass |
| 90 |
class RandomSweepSection(SweepSection): |
| 91 |
hz_list: list[float] |
| 92 |
|
| 93 |
def get_hz(self) -> list[float]: |
| 94 |
return self.hz_list |
| 95 |
|
| 96 |
def get_raw_hz(self) -> list[float]: |
| 97 |
''' List of hz fetched from the instrument before applying mask ''' |
| 98 |
if len(self.hz_list)==1: |
| 99 |
return [self.hz_list[0], self.hz_list[0]+1] |
| 100 |
return self.get_hz() |
| 101 |
|
| 102 |
def apply_8510(self, hp8510c): |
| 103 |
hp8510c._set_instrument_cwstep_state(self.hz_list) |
| 104 |
|
| 105 |
def mask_8510(self, network : skrf.Network) -> skrf.Network: |
| 106 |
if len(self.hz_list)==1: |
| 107 |
return network[0] |
| 108 |
return network |
| 109 |
|
| 110 |
def _sweep_sectionsfrom_hz(hz) -> list[SweepSection]: |
| 111 |
""" |
| 112 |
Take a list of hz, return a SweepPlan, a list of 8510C compatible |
| 113 |
SweepSections that can be executed to cover the original list of frequencies. |
| 114 |
Limitations: No overlapping linear sweeps. No log sweeps. |
| 115 |
""" |
| 116 |
hz = np.array(sorted(hz)) |
| 117 |
n = len(hz) |
| 118 |
misfits = [] |
| 119 |
growing_window = [] |
| 120 |
growing_window_d = -1 |
| 121 |
sweep_sections = [] |
| 122 |
|
| 123 |
def finalize_window(growing_window, misfits, sweep_sections): |
| 124 |
""" When a growing_window has grown as far as it can, finalize_window is called to |
| 125 |
turn it into a sweep section + misfits.""" |
| 126 |
|
| 127 |
if len(growing_window) <= 2: |
| 128 |
misfits.extend(growing_window) |
| 129 |
growing_window.clear() |
| 130 |
return |
| 131 |
|
| 132 |
def try_builtin_window_len(builtin_len): |
| 133 |
while len(growing_window) >= builtin_len: |
| 134 |
chunk = growing_window[0:builtin_len] |
| 135 |
del growing_window[0:builtin_len] |
| 136 |
ns = LinearBuiltinSweepSection( |
| 137 |
hz_min=chunk[0], hz_max=chunk[-1], n_points=len(chunk) |
| 138 |
) |
| 139 |
sweep_sections.append(ns) |
| 140 |
|
| 141 |
try_builtin_window_len(801) |
| 142 |
try_builtin_window_len(401) |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
while len(growing_window): |
| 149 |
chunk = growing_window[0:792] |
| 150 |
del growing_window[0:792] |
| 151 |
ns = LinearCustomSweepSection( |
| 152 |
hz_min=chunk[0], hz_max=chunk[-1], n_points=len(chunk) |
| 153 |
) |
| 154 |
assert len(chunk) == len(ns.get_hz()) |
| 155 |
assert np.allclose(chunk, ns.get_hz()) |
| 156 |
sweep_sections.append(ns) |
| 157 |
|
| 158 |
|
| 159 |
for i in range(n): |
| 160 |
wl = len(growing_window) |
| 161 |
if wl == 0: |
| 162 |
growing_window.append(hz[i]) |
| 163 |
if wl == 1: |
| 164 |
growing_window.append(hz[i]) |
| 165 |
growing_window_d = hz[i] - hz[i - 1] |
| 166 |
if wl >= 2: |
| 167 |
i_fits_in_growing_window = ( |
| 168 |
abs((hz[i] - hz[i - 1]) - growing_window_d) < 0.5 |
| 169 |
) |
| 170 |
if i_fits_in_growing_window: |
| 171 |
growing_window.append(hz[i]) |
| 172 |
else: |
| 173 |
finalize_window(growing_window, misfits, sweep_sections) |
| 174 |
growing_window.append(hz[i]) |
| 175 |
finalize_window(growing_window, misfits, sweep_sections) |
| 176 |
while len(misfits): |
| 177 |
hz = misfits[:29] |
| 178 |
sweep_sections.append(RandomSweepSection(hz_list=hz)) |
| 179 |
del misfits[:29] |
| 180 |
return sweep_sections |
| 181 |
|
| 182 |
class SweepPlan: |
| 183 |
""" |
| 184 |
The user requests a big sweep with different spacings in different frequency |
| 185 |
blocks, but the HP8510 instrument only supports smaller sweeps with fixed |
| 186 |
spacing. What to do? Break up the big sweep, naturally. |
| 187 |
|
| 188 |
SweepPlan.from_hz(array_of_hz) |
| 189 |
-> |
| 190 |
SweepPlan |
| 191 |
SweepSection |
| 192 |
SweepSection |
| 193 |
SweepSection |
| 194 |
|
| 195 |
Each SweepSection represents a sweep the instrument can actually perform. |
| 196 |
Together, the SweepSections in a SweepPlan satisfy the user's request. |
| 197 |
|
| 198 |
There is room for cleverness: conducting an 800 point sweep by running an |
| 199 |
801 point sweep and discarding a point saved 20 minutes in a recent script. |
| 200 |
""" |
| 201 |
|
| 202 |
_sections: list[SweepSection] |
| 203 |
|
| 204 |
def __init__(self, sections): |
| 205 |
self._sections = sections |
| 206 |
|
| 207 |
@classmethod |
| 208 |
def from_hz(cls, hz : list[float]): |
| 209 |
sweep_sections = _sweep_sectionsfrom_hz(hz) |
| 210 |
plan = SweepPlan(sweep_sections) |
| 211 |
assert plan._matches_f_list(hz) |
| 212 |
return plan |
| 213 |
|
| 214 |
def get_sections(self): |
| 215 |
return self._sections |
| 216 |
|
| 217 |
def get_hz(self) -> list[float]: |
| 218 |
"""Get a list of all frequency points in the entire sweep plan.""" |
| 219 |
ret = [] |
| 220 |
for s in self._sections: |
| 221 |
ret.extend(s.get_hz()) |
| 222 |
return ret |
| 223 |
|
| 224 |
def _matches_f_list(self, golden_hz : list[float]): |
| 225 |
""" |
| 226 |
Returns True iff the frequencies this SweepPlan intends to sweep |
| 227 |
equal those in the list golden_hz. |
| 228 |
""" |
| 229 |
plan_hz = np.array(sorted(self.get_hz())) |
| 230 |
if len(golden_hz) != len(plan_hz): |
| 231 |
print("Planner output has different length.") |
| 232 |
return False |
| 233 |
good = True |
| 234 |
for h in golden_hz: |
| 235 |
if not np.any(np.isclose(h, plan_hz)): |
| 236 |
print(f"In original list but not plan: {h}") |
| 237 |
good = False |
| 238 |
for ph in plan_hz: |
| 239 |
if not np.any(np.isclose(ph, golden_hz)): |
| 240 |
print(f"In plan but not in original list: {h}") |
| 241 |
good = False |
| 242 |
if not np.allclose(golden_hz, plan_hz): |
| 243 |
print("All were not close.") |
| 244 |
good = False |
| 245 |
return good |
| 246 |
|
| 247 |
@classmethod |
| 248 |
def from_ssn(cls, hz_start, hz_stop, n): |
| 249 |
return cls.from_hz(np.linspace(hz_start, hz_stop, n)) |