From 1a5f0a3c4a807a7160079d678fa0efefd9ad098f Mon Sep 17 00:00:00 2001 From: Maans Jacobsson Date: Tue, 9 Apr 2024 14:45:07 +0200 Subject: [PATCH 1/2] Added me and Otto as authors --- AUTHORS.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 4361e13..c278320 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -42,3 +42,5 @@ Contributors * Thomas de Lellis <24543390+t52ta6ek@users.noreply.github.com> * zstadler * tbergkvist +* timmythetank +* ottoedgren From 94f853f903136dfd525e072f7662ccdef3b5e028 Mon Sep 17 00:00:00 2001 From: tbergkvist Date: Tue, 9 Apr 2024 16:05:26 +0200 Subject: [PATCH 2/2] Black formatting. --- main.py | 2 +- src/Calibration.py | 39 +++++++++++-------------------------- src/Hardware/Hardware.py | 8 +++----- src/Hardware/NanoVNA.py | 8 ++------ src/Hardware/NanoVNA_V2.py | 33 ++++++++----------------------- src/Hardware/TinySA.py | 7 ++----- src/Hardware/VNA.py | 8 ++------ src/Hardware/Version.py | 26 +++++++++++++------------ src/NanoVNASaverHeadless.py | 14 ++++++++++--- src/RFTools.py | 8 ++------ src/SITools.py | 4 +--- 11 files changed, 57 insertions(+), 100 deletions(-) diff --git a/main.py b/main.py index e21e0dc..325accc 100644 --- a/main.py +++ b/main.py @@ -5,5 +5,5 @@ from src.NanoVNASaverHeadless import NanoVNASaverHeadless vna = NanoVNASaverHeadless(vna_index=0, verbose=True) vna.set_sweep(2.9e9, 3.1e9) vna.stream_data() -#vna.calibrate() +# vna.calibrate() vna.kill() diff --git a/src/Calibration.py b/src/Calibration.py index 2e31a30..31c3f3c 100644 --- a/src/Calibration.py +++ b/src/Calibration.py @@ -149,29 +149,22 @@ class CalDataSet(UserDict): + "\n" + "# Hz ShortR ShortI OpenR OpenI LoadR LoadI" + ( - " ThroughR ThroughI ThrureflR" - " ThrureflI IsolationR IsolationI\n" + " ThroughR ThroughI ThrureflR" " ThrureflI IsolationR IsolationI\n" if self.complete2port() else "\n" ) - + "\n".join( - [f"{self.data.get(freq)}" for freq in self.frequencies()] - ) + + "\n".join([f"{self.data.get(freq)}" for freq in self.frequencies()]) + "\n" ) if self.complete1port() else "" ) - def _append_match( - self, m: re.Match, header: str, line_nr: int, line: str - ) -> None: + def _append_match(self, m: re.Match, header: str, line_nr: int, line: str) -> None: cal = m.groupdict() columns = {col[:-1] for col in cal.keys() if cal[col] and col != "freq"} if "through" in columns and header == "sol": - logger.warning( - "Through data with sol header. %i: %s", line_nr, line - ) + logger.warning("Through data with sol header. %i: %s", line_nr, line) # fix short data (without thrurefl) if "thrurefl" in columns and "isolation" not in columns: cal["isolationr"] = cal["thrureflr"] @@ -201,9 +194,7 @@ class CalDataSet(UserDict): continue if m := RXP_CAL_HEADER.search(line): if header: - logger.warning( - "Duplicate header in cal data. %i: %s", i, line - ) + logger.warning("Duplicate header in cal data. %i: %s", i, line) header = "through" if m.group("through") else "sol" continue if not line or line.startswith("#"): @@ -214,9 +205,7 @@ class CalDataSet(UserDict): logger.warning("Illegal caldata. Line %i: %s", i, line) continue if not header: - logger.warning( - "Caldata without having read header: %i: %s", i, line - ) + logger.warning("Caldata without having read header: %i: %s", i, line) self._append_match(m, header, line, i) return self @@ -290,7 +279,7 @@ class Calibration: def isValid2Port(self) -> bool: return self.dataset.complete2port() - + def _calc_port_1(self, freq: int, cal: CalData): g1 = self.gamma_short(freq) g2 = self.gamma_open(freq) @@ -309,8 +298,7 @@ class Calibration: cal.e00 = ( -( (g2 * gm3 - g3 * gm3) * g1 * gm2 - - (g2 * g3 * gm2 - g2 * g3 * gm3 - (g3 * gm2 - g2 * gm3) * g1) - * gm1 + - (g2 * g3 * gm2 - g2 * g3 * gm3 - (g3 * gm2 - g2 * gm3) * g1) * gm1 ) / denominator ) @@ -388,9 +376,7 @@ class Calibration: return ( (Zsp / 50.0 - 1.0) / (Zsp / 50.0 + 1.0) - * cmath.exp( - complex(0.0, -4.0 * math.pi * freq * cal_element.short_length) - ) + * cmath.exp(complex(0.0, -4.0 * math.pi * freq * cal_element.short_length)) ) def gamma_open(self, freq: int) -> complex: @@ -430,9 +416,7 @@ class Calibration: return ( (Zl / 50.0 - 1.0) / (Zl / 50.0 + 1.0) - * cmath.exp( - complex(0.0, -4 * math.pi * freq * cal_element.load_length) - ) + * cmath.exp(complex(0.0, -4 * math.pi * freq * cal_element.load_length)) ) def gamma_through(self, freq: int) -> complex: @@ -524,8 +508,7 @@ class Calibration: i = self.interp s21 = (dp.z - i["e30"](dp.freq)) / i["e10e32"](dp.freq) s21 = s21 * ( - i["e10e01"](dp.freq) - / (i["e11"](dp.freq) * dp11.z - i["delta_e"](dp.freq)) + i["e10e01"](dp.freq) / (i["e11"](dp.freq) * dp11.z - i["delta_e"](dp.freq)) ) return Datapoint(dp.freq, s21.real, s21.imag) diff --git a/src/Hardware/Hardware.py b/src/Hardware/Hardware.py index 8d0e1b7..cb4ac3e 100644 --- a/src/Hardware/Hardware.py +++ b/src/Hardware/Hardware.py @@ -84,11 +84,7 @@ def _fix_v2_hwinfo(dev): def usb_typename(device: ListPortInfo) -> str: return next( - ( - t.name - for t in USBDEVICETYPES - if device.vid == t.vid and device.pid == t.pid - ), + (t.name for t in USBDEVICETYPES if device.vid == t.vid and device.pid == t.pid), "", ) @@ -135,9 +131,11 @@ def get_portinfos() -> list[str]: portinfos.append(version) return portinfos + def get_VNA(iface: Interface) -> VNA: return NAME2DEVICE[iface.comment](iface) + def get_comment(iface: Interface) -> str: logger.info("Finding correct VNA type...") with iface.lock: diff --git a/src/Hardware/NanoVNA.py b/src/Hardware/NanoVNA.py index 5217550..4aa480b 100644 --- a/src/Hardware/NanoVNA.py +++ b/src/Hardware/NanoVNA.py @@ -62,9 +62,7 @@ class NanoVNA(VNA): self.serial.write("capture\r".encode("ascii")) self.serial.readline() self.serial.timeout = 4 - image_data = self.serial.read( - self.screenwidth * self.screenheight * 2 - ) + image_data = self.serial.read(self.screenwidth * self.screenheight * 2) self.serial.timeout = timeout self.serial.timeout = timeout return image_data @@ -145,9 +143,7 @@ class NanoVNA(VNA): f"scan {self.start} {self.stop} {self.datapoints} 0b110" ): data = line.split() - self._sweepdata.append( - (f"{data[0]} {data[1]}", f"{data[2]} {data[3]}") - ) + self._sweepdata.append((f"{data[0]} {data[1]}", f"{data[2]} {data[3]}")) if value == "data 0": return [x[0] for x in self._sweepdata] if value == "data 1": diff --git a/src/Hardware/NanoVNA_V2.py b/src/Hardware/NanoVNA_V2.py index 686a8b9..354c8af 100644 --- a/src/Hardware/NanoVNA_V2.py +++ b/src/Hardware/NanoVNA_V2.py @@ -121,10 +121,7 @@ class NanoVNA_V2(VNA): self.txPowerRanges = [ ( (140e6, self.sweep_max_freq_Hz), - [ - _ADF4350_TXPOWER_DESC_MAP[value] - for value in (3, 2, 1, 0) - ], + [_ADF4350_TXPOWER_DESC_MAP[value] for value in (3, 2, 1, 0)], ), ] @@ -172,14 +169,10 @@ class NanoVNA_V2(VNA): self.serial.write(pack(" "Version": - result = self._read_version( - _ADDR_DEVICE_VARIANT, _ADDR_HARDWARE_REVISION - ) + result = self._read_version(_ADDR_DEVICE_VARIANT, _ADDR_HARDWARE_REVISION) logger.debug("read_board_revision: %s", result) return result @@ -279,12 +266,8 @@ class NanoVNA_V2(VNA): _ADDR_SWEEP_START, max(50000, int(self.sweepStartHz - (self.sweepStepHz * s21hack))), ) - cmd += pack( - " int: return round( - 20 - + 20 * (datapoints / 101) - + (1000 / bandwidth) ** 1.30 * (datapoints / 101) + 20 + 20 * (datapoints / 101) + (1000 / bandwidth) ** 1.30 * (datapoints / 101) ) @@ -149,9 +147,7 @@ class VNA: ] def set_bandwidth(self, bandwidth: int): - bw_val = ( - DISLORD_BW[bandwidth] if self.bw_method == "dislord" else bandwidth - ) + bw_val = DISLORD_BW[bandwidth] if self.bw_method == "dislord" else bandwidth result = " ".join(self.exec_command(f"bandwidth {bw_val}")) if self.bw_method == "ttrftech" and result: raise IOError(f"set_bandwith({bandwidth}: {result}") diff --git a/src/Hardware/Version.py b/src/Hardware/Version.py index 8ef790c..4f75887 100644 --- a/src/Hardware/Version.py +++ b/src/Hardware/Version.py @@ -22,13 +22,16 @@ import typing logger = logging.getLogger(__name__) -_RXP = re.compile(r"""^ +_RXP = re.compile( + r"""^ \D* (?P\d+)\. (?P\d+)\.? (?P\d+)? (?P.*) - $""", re.VERBOSE) + $""", + re.VERBOSE, +) class _Version(typing.NamedTuple): @@ -38,18 +41,17 @@ class _Version(typing.NamedTuple): note: str def __str__(self) -> str: - return ( - f'{self.major}.{self.minor}' - f'.{self.revision}{self.note}' - ) + return f"{self.major}.{self.minor}" f".{self.revision}{self.note}" -def Version(vstring: str = "0.0.0") -> '_Version': +def Version(vstring: str = "0.0.0") -> "_Version": if (match := _RXP.search(vstring)) is None: logger.error("Unable to parse version: %s", vstring) - return _Version(0, 0, 0, '') + return _Version(0, 0, 0, "") - return _Version(int(match.group('major')), - int(match.group('minor')), - int(match.group('revision') or '0'), - match.group('note')) + return _Version( + int(match.group("major")), + int(match.group("minor")), + int(match.group("revision") or "0"), + match.group("note"), + ) diff --git a/src/NanoVNASaverHeadless.py b/src/NanoVNASaverHeadless.py index 0e6dc36..701d1b7 100644 --- a/src/NanoVNASaverHeadless.py +++ b/src/NanoVNASaverHeadless.py @@ -1,6 +1,7 @@ from .Hardware import Hardware as hw from .Hardware.VNA import VNA + class NanoVNASaverHeadless: def __init__(self, vna_index=0, verbose=False): self.verbose = verbose @@ -16,7 +17,14 @@ class NanoVNASaverHeadless: def set_sweep(self, start, stop): self.vna.setSweep(start, stop) - print("Sweep set from " + str(self.vna.readFrequencies()[0]/1e9) + "e9" + " to " + str(self.vna.readFrequencies()[-1]/1e9) + "e9") + print( + "Sweep set from " + + str(self.vna.readFrequencies()[0] / 1e9) + + "e9" + + " to " + + str(self.vna.readFrequencies()[-1] / 1e9) + + "e9" + ) def stream_data(self): data = self.get_data() @@ -36,7 +44,7 @@ class NanoVNASaverHeadless: values = item.split() real.append(float(values[0])) imaginary.append(float(values[1])) - #add exception handling + # add exception handling return real, imaginary def kill(self): @@ -46,4 +54,4 @@ class NanoVNASaverHeadless: else: if self.verbose: print("Disconnected VNA.") - return \ No newline at end of file + return diff --git a/src/RFTools.py b/src/RFTools.py index 1184df8..fbc1d99 100644 --- a/src/RFTools.py +++ b/src/RFTools.py @@ -76,9 +76,7 @@ class Datapoint(NamedTuple): return -1 if imp.real == 0.0 else abs(imp.imag / imp.real) def capacitiveEquivalent(self, ref_impedance: float = 50) -> float: - return impedance_to_capacitance( - self.impedance(ref_impedance), self.freq - ) + return impedance_to_capacitance(self.impedance(ref_impedance), self.freq) def inductiveEquivalent(self, ref_impedance: float = 50) -> float: return impedance_to_inductance(self.impedance(ref_impedance), self.freq) @@ -125,9 +123,7 @@ def norm_to_impedance(z: complex, ref_impedance: float = 50) -> complex: def parallel_to_serial(z: complex) -> complex: """Convert parallel impedance to serial impedance equivalent""" z_sq_sum = z.real**2 + z.imag**2 or 10.0e-30 - return complex( - z.real * z.imag**2 / z_sq_sum, z.real**2 * z.imag / z_sq_sum - ) + return complex(z.real * z.imag**2 / z_sq_sum, z.real**2 * z.imag / z_sq_sum) def reflection_coefficient(z: complex, ref_impedance: float = 50) -> complex: diff --git a/src/SITools.py b/src/SITools.py index 7749650..937a3ae 100644 --- a/src/SITools.py +++ b/src/SITools.py @@ -116,9 +116,7 @@ class Value: fmt = self.fmt if math.isnan(self._value): return f"-{fmt.space_str}{self._unit}" - if fmt.assume_infinity and abs(self._value) >= 10 ** ( - (fmt.max_offset + 1) * 3 - ): + if fmt.assume_infinity and abs(self._value) >= 10 ** ((fmt.max_offset + 1) * 3): return ( ("-" if self._value < 0 else "") + "\N{INFINITY}"