Compare commits
3 Commits
339dbe255e
...
c5dc320989
Author | SHA1 | Date | |
---|---|---|---|
c5dc320989 | |||
452dddc19c | |||
3c02a4b388 |
@@ -1,8 +1,9 @@
|
|||||||
# %% imports
|
# %% imports
|
||||||
import copy
|
import copy
|
||||||
|
import pickle
|
||||||
from enum import IntEnum, unique
|
from enum import IntEnum, unique
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Callable, Dict, Literal, Tuple
|
from typing import Any, Callable, Dict, List, Literal, Tuple
|
||||||
|
|
||||||
import adi
|
import adi
|
||||||
import iio
|
import iio
|
||||||
@@ -305,7 +306,16 @@ class Charon:
|
|||||||
|
|
||||||
return np.mean(data[1] / data[0])
|
return np.mean(data[1] / data[0])
|
||||||
|
|
||||||
def capture(self, callback: Callable[int, int] | None = None):
|
def capture(
|
||||||
|
self,
|
||||||
|
callback: Callable[int, int] | None = None,
|
||||||
|
*,
|
||||||
|
measurements: List[Tuple[int, int]] = None,
|
||||||
|
):
|
||||||
|
if measurements is None:
|
||||||
|
measurements = [(m, n) for n in self.ports for m in self.ports]
|
||||||
|
measurements = list(measurements)
|
||||||
|
|
||||||
s = xr.DataArray(
|
s = xr.DataArray(
|
||||||
np.zeros(
|
np.zeros(
|
||||||
[len(self.frequency), len(self.ports), len(self.ports)],
|
[len(self.frequency), len(self.ports), len(self.ports)],
|
||||||
@@ -319,30 +329,31 @@ class Charon:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
total_count = len(s.m) * len(s.n) * len(s.frequency)
|
total_count = len(measurements) * len(s.frequency)
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
for m in s.m.data:
|
for m in s.m.data:
|
||||||
for n in s.n.data:
|
for n in s.n.data:
|
||||||
self.set_switches(b=m - 1, a=n - 1)
|
if (m, n) in measurements:
|
||||||
|
self.set_switches(b=m - 1, a=n - 1)
|
||||||
|
|
||||||
for ff, freq in enumerate(s.frequency.data):
|
for ff, freq in enumerate(s.frequency.data):
|
||||||
if callback is not None:
|
if callback is not None:
|
||||||
# report progress during sweep
|
# report progress during sweep
|
||||||
callback(count, total_count)
|
callback(count, total_count)
|
||||||
|
|
||||||
self.set_output(frequency=freq, power=-5)
|
self.set_output(frequency=freq, power=-5)
|
||||||
self.sdr.rx_destroy_buffer()
|
self.sdr.rx_destroy_buffer()
|
||||||
self.sdr.rx_lo = int(freq)
|
self.sdr.rx_lo = int(freq)
|
||||||
self.sdr.rx_enabled_channels = [0, 1]
|
self.sdr.rx_enabled_channels = [0, 1]
|
||||||
self.sdr.gain_control_mode_chan0 = "manual"
|
self.sdr.gain_control_mode_chan0 = "manual"
|
||||||
self.sdr.gain_control_mode_chan1 = "manual"
|
self.sdr.gain_control_mode_chan1 = "manual"
|
||||||
self.sdr.rx_hardwaregain_chan0 = 40
|
self.sdr.rx_hardwaregain_chan0 = 40
|
||||||
self.sdr.rx_hardwaregain_chan1 = 40
|
self.sdr.rx_hardwaregain_chan1 = 40
|
||||||
rx = self.sdr.rx()
|
rx = self.sdr.rx()
|
||||||
s.loc[dict(frequency=freq, m=m, n=n)] = np.mean(rx[1] / rx[0])
|
s.loc[dict(frequency=freq, m=m, n=n)] = np.mean(rx[1] / rx[0])
|
||||||
|
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
if callback is not None:
|
if callback is not None:
|
||||||
# mark capture as complete
|
# mark capture as complete
|
||||||
@@ -350,6 +361,92 @@ class Charon:
|
|||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
def calibrate_sol(self, prompt: Callable[[str], None] | None = None, **kwargs) -> None:
|
||||||
|
if len(self.ports) != 1:
|
||||||
|
raise ValueError(
|
||||||
|
f"SOL calibration needs only one port but {len(self.ports)} ports are enabled. "
|
||||||
|
"Did you mean to use SOLT?"
|
||||||
|
)
|
||||||
|
|
||||||
|
if prompt is None:
|
||||||
|
prompt = lambda s: input(f"{s}\nENTER to continue...")
|
||||||
|
|
||||||
|
ideal = rf.media.DefinedGammaZ0(frequency=rf.media.Frequency.from_f(self.frequency, unit="Hz"))
|
||||||
|
ideals = [ideal.short(), ideal.open(), ideal.load(0)]
|
||||||
|
|
||||||
|
names = ["short", "open", "load"]
|
||||||
|
|
||||||
|
measured = list()
|
||||||
|
for name in names:
|
||||||
|
prompt(f"Connect standard {name} to port {self.ports[0]}")
|
||||||
|
measured.append(self.capture(**kwargs))
|
||||||
|
|
||||||
|
cal = rf.OnePort(measured=[s2net(m) for m in measured], ideals=ideals)
|
||||||
|
|
||||||
|
self.calibration = cal
|
||||||
|
|
||||||
|
def calibrate_solt(self, prompt: Callable[[str], None] | None = None, **kwargs) -> None:
|
||||||
|
if len(self.ports) < 2:
|
||||||
|
raise ValueError(
|
||||||
|
f"SOLT calibration needs at least two ports but {len(self.ports)} ports are enabled. "
|
||||||
|
"Did you mean to use SOL?"
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(self.ports) > 2:
|
||||||
|
raise NotImplementedError("SOLT calibration with more than two ports not yet supported")
|
||||||
|
|
||||||
|
if prompt is None:
|
||||||
|
prompt = lambda s: input(f"{s}\nENTER to continue...")
|
||||||
|
|
||||||
|
ideal = rf.media.DefinedGammaZ0(frequency=rf.media.Frequency.from_f(self.frequency, unit="Hz"))
|
||||||
|
ideals = [ideal.short(), ideal.open(), ideal.load(0)]
|
||||||
|
ideals = [rf.two_port_reflect(id, id) for id in ideals]
|
||||||
|
|
||||||
|
thru = np.zeros((len(self.frequency), 2, 2), dtype=np.complex128)
|
||||||
|
thru[:, 0, 1] = 1
|
||||||
|
thru[:, 1, 0] = 1
|
||||||
|
thru = rf.Network(frequency=self.frequency, f_unit="Hz", s=thru)
|
||||||
|
|
||||||
|
ideals.append(thru)
|
||||||
|
|
||||||
|
names_1p = ["short", "open", "load"]
|
||||||
|
names_2p = ["thru"]
|
||||||
|
|
||||||
|
measured = list()
|
||||||
|
for name in names_1p:
|
||||||
|
measured_param = list()
|
||||||
|
for port in self.ports:
|
||||||
|
prompt(f"Connect standard {name} to port {port}")
|
||||||
|
measured_param.append(self.capture(measurements=[(port, port)], **kwargs).sel(m=port, n=port))
|
||||||
|
measured.append(rf.two_port_reflect(*[s2net(m) for m in measured_param]))
|
||||||
|
|
||||||
|
for name in names_2p:
|
||||||
|
prompt(f"Connect standard {name} between ports {self.ports[0]} and {self.ports[1]}")
|
||||||
|
measured.append(s2net(self.capture(**kwargs)))
|
||||||
|
|
||||||
|
cal = rf.SOLT(measured=measured, ideals=ideals)
|
||||||
|
|
||||||
|
self.calibration = cal
|
||||||
|
|
||||||
|
def save_calibration(self, path: Path | str):
|
||||||
|
path = Path(path)
|
||||||
|
if path.suffix.lower() == ".pkl":
|
||||||
|
with open(str(path), "wb") as f:
|
||||||
|
pickle.dump(self.calibration, f)
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(f"Unknown calibration file extension: {path.suffix}")
|
||||||
|
|
||||||
|
def load_calibration(self, path: Path | str):
|
||||||
|
path = Path(path)
|
||||||
|
if path.suffix.lower() == ".pkl":
|
||||||
|
with open(str(path), "rb") as f:
|
||||||
|
cal = pickle.load(f)
|
||||||
|
if not isinstance(cal, rf.calibration.Calibration):
|
||||||
|
raise ValueError(f"Expected {rf.calibration.Calibration}, got {type(cal)}")
|
||||||
|
self.calibration = cal
|
||||||
|
else:
|
||||||
|
raise NotImplementedError(f"Unknown calibration file extension: {path.suffix}")
|
||||||
|
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@@ -2,10 +2,11 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
|
|
||||||
|
from charon_vna.util import db20, net2s, s2net
|
||||||
from charon_vna.vna import Charon
|
from charon_vna.vna import Charon
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
frequency = np.linspace(80e6, 280e6, 31)
|
frequency = np.linspace(80e6, 280e6, 301)
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
vna = Charon(frequency=frequency, ports=2)
|
vna = Charon(frequency=frequency, ports=2)
|
||||||
@@ -16,9 +17,55 @@ s = vna.capture()
|
|||||||
# %%
|
# %%
|
||||||
for m in s.m.data:
|
for m in s.m.data:
|
||||||
for n in s.n.data:
|
for n in s.n.data:
|
||||||
plt.plot(s.frequency, 20 * np.log10(np.abs(s.sel(m=m, n=n))), label="$S_{" + str(m) + str(n) + "}$")
|
plt.plot(s.frequency, db20(s.sel(m=m, n=n)), label="$S_{" + str(m) + str(n) + "}$")
|
||||||
plt.grid(True)
|
plt.grid(True)
|
||||||
plt.legend()
|
plt.legend()
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
vna.calibrate_sol()
|
||||||
|
|
||||||
|
# %%
|
||||||
|
vna.calibrate_solt()
|
||||||
|
|
||||||
|
# %%
|
||||||
|
vna.save_calibration("./calibration.pkl")
|
||||||
|
|
||||||
|
# %%
|
||||||
|
vna.load_calibration("./calibration.pkl")
|
||||||
|
|
||||||
|
# %%
|
||||||
|
s2 = net2s(vna.calibration.apply_cal(s2net(s)))
|
||||||
|
# s2.coords["m"] = s.m
|
||||||
|
# s2.coords["n"] = s.n
|
||||||
|
|
||||||
|
for m in s.m.data:
|
||||||
|
for n in s.n.data:
|
||||||
|
plt.plot(s.frequency, db20(s.sel(m=m, n=n)), label="$S_{" + str(m) + str(n) + "}$ (uncalibrated)")
|
||||||
|
plt.plot(s2.frequency, db20(s2.sel(m=m, n=n)), label="$S_{" + str(m) + str(n) + "}$ (calibrated)")
|
||||||
|
plt.grid(True)
|
||||||
|
plt.legend()
|
||||||
|
plt.ylabel("Magnitude [dB]")
|
||||||
|
# plt.ylim(-30, 5)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
for m in s.m.data:
|
||||||
|
for n in s.n.data:
|
||||||
|
if m != n:
|
||||||
|
plt.plot(
|
||||||
|
s.frequency,
|
||||||
|
np.angle(s.sel(m=m, n=n), deg=True),
|
||||||
|
label="$S_{" + str(m) + str(n) + "}$ (uncalibrated)",
|
||||||
|
)
|
||||||
|
plt.plot(
|
||||||
|
s2.frequency,
|
||||||
|
np.angle(s2.sel(m=m, n=n), deg=True),
|
||||||
|
label="$S_{" + str(m) + str(n) + "}$ (calibrated)",
|
||||||
|
)
|
||||||
|
plt.grid(True)
|
||||||
|
plt.legend()
|
||||||
|
plt.ylabel("Phase [deg]")
|
||||||
|
plt.show()
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
|
Reference in New Issue
Block a user