From b862c148a8c0a60d14ba23b4adbd2282167e8639 Mon Sep 17 00:00:00 2001 From: Rune Broberg Date: Mon, 30 Sep 2019 10:37:58 +0200 Subject: [PATCH] Loading of RFSim files. Some crashes fixed when using simulated data, some still left. --- NanoVNASaver/Chart.py | 4 ++-- NanoVNASaver/Marker.py | 5 +++-- NanoVNASaver/NanoVNASaver.py | 19 ++++++++++++++----- NanoVNASaver/Touchstone.py | 4 ++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/NanoVNASaver/Chart.py b/NanoVNASaver/Chart.py index 1376b66..c7b4820 100644 --- a/NanoVNASaver/Chart.py +++ b/NanoVNASaver/Chart.py @@ -410,8 +410,8 @@ class FrequencyChart(Chart): qp.drawLine(int(x) - 3, int(y) - 3, int(x) + 3, int(y) - 3) def isPlotable(self, x, y): - return self.leftMargin < x <= self.leftMargin + self.chartWidth and \ - self.topMargin < y <= self.topMargin + self.chartHeight + return self.leftMargin <= x <= self.leftMargin + self.chartWidth and \ + self.topMargin <= y <= self.topMargin + self.chartHeight def getPlotable(self, x, y, distantx, distanty): p1 = np.array([x, y]) diff --git a/NanoVNASaver/Marker.py b/NanoVNASaver/Marker.py index 1b0f021..68d1dc2 100644 --- a/NanoVNASaver/Marker.py +++ b/NanoVNASaver/Marker.py @@ -190,7 +190,8 @@ class Marker(QtCore.QObject): self.impedance_label.setText(str(re50) + im50str) self.parallel_r_label.setText(str(rp) + " \N{OHM SIGN}") self.parallel_x_label.setText(xpstr) - self.returnloss_label.setText(str(round(20 * math.log10((vswr - 1) / (vswr + 1)), 3)) + " dB") + #self.returnloss_label.setText(str(round(20 * math.log10((vswr - 1) / (vswr + 1)), 3)) + " dB") + self.returnloss_label.setText(str(round(NanoVNASaver.gain(s11data[self.location]), 3)) + " dB") capacitance = NanoVNASaver.capacitanceEquivalent(im50, s11data[self.location].freq) inductance = NanoVNASaver.inductanceEquivalent(im50, s11data[self.location].freq) self.inductance_label.setText(inductance) @@ -204,6 +205,6 @@ class Marker(QtCore.QObject): str(round(PhaseChart.angle(s11data[self.location]), 2)) + "\N{DEGREE SIGN}") if len(s21data) == len(s11data): _, _, vswr = NanoVNASaver.vswr(s21data[self.location]) - self.gain_label.setText(str(round(20 * math.log10((vswr - 1) / (vswr + 1)), 3)) + " dB") + self.gain_label.setText(str(round(NanoVNASaver.gain(s21data[self.location]), 3)) + " dB") self.s21_phase_label.setText( str(round(PhaseChart.angle(s21data[self.location]), 2)) + "\N{DEGREE SIGN}") diff --git a/NanoVNASaver/NanoVNASaver.py b/NanoVNASaver/NanoVNASaver.py index 987cd34..1d594a7 100644 --- a/NanoVNASaver/NanoVNASaver.py +++ b/NanoVNASaver/NanoVNASaver.py @@ -742,7 +742,11 @@ class NanoVNASaver(QtWidgets.QWidget): if minVSWRfreq > -1: self.s11_min_swr_label.setText(str(round(minVSWR, 3)) + " @ " + self.formatFrequency(minVSWRfreq)) - self.s11_min_rl_label.setText(str(round(20*math.log10((minVSWR-1)/(minVSWR+1)), 3)) + " dB") + if minVSWR > 1: + self.s11_min_rl_label.setText(str(round(20*math.log10((minVSWR-1)/(minVSWR+1)), 3)) + " dB") + else: + # Infinite return loss? + self.s11_min_rl_label.setText("\N{INFINITY} dB") else: self.s11_min_swr_label.setText("") self.s11_min_rl_label.setText("") @@ -774,9 +778,11 @@ class NanoVNASaver(QtWidgets.QWidget): @staticmethod def vswr(data: Datapoint): re50, im50 = NanoVNASaver.normalize50(data) - mag = math.sqrt((re50 - 50) * (re50 - 50) + im50 * im50) / math.sqrt((re50 + 50) * (re50 + 50) + im50 * im50) - # mag = math.sqrt(re * re + im * im) # Is this even right? - vswr = (1 + mag) / (1 - mag) + try: + mag = math.sqrt((re50 - 50) * (re50 - 50) + im50 * im50) / math.sqrt((re50 + 50) * (re50 + 50) + im50 * im50) + vswr = (1 + mag) / (1 - mag) + except ZeroDivisionError as e: + vswr = 1 return im50, re50, vswr @staticmethod @@ -822,7 +828,10 @@ class NanoVNASaver(QtWidgets.QWidget): # Calculate the gain / reflection coefficient mag = math.sqrt((re50 - 50) * (re50 - 50) + im50 * im50) / math.sqrt( (re50 + 50) * (re50 + 50) + im50 * im50) - return 20 * math.log10(mag) + if mag > 0: + return 20 * math.log10(mag) + else: + return 0 @staticmethod def normalize50(data): diff --git a/NanoVNASaver/Touchstone.py b/NanoVNASaver/Touchstone.py index 73937a8..cc98a7d 100644 --- a/NanoVNASaver/Touchstone.py +++ b/NanoVNASaver/Touchstone.py @@ -53,7 +53,7 @@ class Touchstone: logger.info(line) continue if line.startswith("#") and not parsed_header: - pattern = "^# (.?HZ) S RI R 50$" + pattern = "^# (.?HZ) (S )?RI( R 50)?$" match = re.match(pattern, line.upper()) if match: logger.debug("Found header for RealImaginary and %s", match.group(1)) @@ -72,7 +72,7 @@ class Touchstone: factor = 10**9 # Default Touchstone frequency unit is GHz continue - pattern = "^# (.?HZ) S MA R 50$" + pattern = "^# (.?HZ) (S )?MA( R 50)?$" match = re.match(pattern, line.upper()) if match: logger.debug("Found header for MagnitudeAngle and %s", match.group(1))