2020-06-25 17:52:30 +00:00
|
|
|
# NanoVNASaver
|
|
|
|
#
|
|
|
|
# A python program to view and export Touchstone data from a NanoVNA
|
|
|
|
# Copyright (C) 2019, 2020 Rune B. Broberg
|
2021-06-30 05:21:14 +00:00
|
|
|
# Copyright (C) 2020,2021 NanoVNA-Saver Authors
|
2019-09-01 21:13:21 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-09-05 12:56:40 +00:00
|
|
|
import math
|
2019-09-22 11:42:05 +00:00
|
|
|
import logging
|
2019-09-24 21:29:26 +00:00
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
from typing import List
|
|
|
|
import numpy as np
|
2019-11-17 21:26:00 +00:00
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
from PyQt5 import QtWidgets, QtGui
|
2019-09-24 21:29:26 +00:00
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
from NanoVNASaver.RFTools import Datapoint
|
2021-07-06 15:01:20 +00:00
|
|
|
from NanoVNASaver.Charts.Chart import Chart
|
|
|
|
from NanoVNASaver.Charts.Frequency import FrequencyChart
|
2019-09-19 12:15:47 +00:00
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-09-19 12:15:47 +00:00
|
|
|
|
|
|
|
|
2019-09-25 10:25:50 +00:00
|
|
|
class PhaseChart(FrequencyChart):
|
2019-09-05 12:56:40 +00:00
|
|
|
def __init__(self, name=""):
|
2019-09-19 12:15:47 +00:00
|
|
|
super().__init__(name)
|
2021-06-22 21:23:39 +00:00
|
|
|
|
2019-09-12 12:30:50 +00:00
|
|
|
self.minAngle = 0
|
2019-09-29 14:34:50 +00:00
|
|
|
self.maxAngle = 0
|
2019-09-12 12:30:50 +00:00
|
|
|
self.span = 0
|
2019-10-03 12:17:04 +00:00
|
|
|
self.unwrap = False
|
|
|
|
|
|
|
|
self.unwrappedData = []
|
|
|
|
self.unwrappedReference = []
|
2019-09-05 12:56:40 +00:00
|
|
|
|
2019-09-29 09:50:58 +00:00
|
|
|
self.minDisplayValue = -180
|
|
|
|
self.maxDisplayValue = 180
|
2019-09-25 10:25:50 +00:00
|
|
|
|
2019-10-03 12:17:04 +00:00
|
|
|
self.y_menu.addSeparator()
|
|
|
|
self.action_unwrap = QtWidgets.QAction("Unwrap")
|
|
|
|
self.action_unwrap.setCheckable(True)
|
|
|
|
self.action_unwrap.triggered.connect(lambda: self.setUnwrap(self.action_unwrap.isChecked()))
|
|
|
|
self.y_menu.addAction(self.action_unwrap)
|
|
|
|
|
2019-10-13 21:10:01 +00:00
|
|
|
def copy(self):
|
2021-06-22 21:23:39 +00:00
|
|
|
new_chart = super().copy()
|
2019-10-13 21:10:01 +00:00
|
|
|
new_chart.setUnwrap(self.unwrap)
|
|
|
|
new_chart.action_unwrap.setChecked(self.unwrap)
|
2019-10-15 14:39:47 +00:00
|
|
|
return new_chart
|
2019-10-13 21:10:01 +00:00
|
|
|
|
2019-10-03 12:17:04 +00:00
|
|
|
def setUnwrap(self, unwrap: bool):
|
|
|
|
self.unwrap = unwrap
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def drawValues(self, qp: QtGui.QPainter):
|
|
|
|
if len(self.data) == 0 and len(self.reference) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.unwrap:
|
|
|
|
rawData = []
|
|
|
|
for d in self.data:
|
2019-11-16 14:33:11 +00:00
|
|
|
rawData.append(d.phase)
|
2019-10-03 12:17:04 +00:00
|
|
|
|
|
|
|
rawReference = []
|
|
|
|
for d in self.reference:
|
2019-11-16 14:33:11 +00:00
|
|
|
rawReference.append(d.phase)
|
2019-10-03 12:17:04 +00:00
|
|
|
|
2019-11-10 15:02:42 +00:00
|
|
|
self.unwrappedData = np.degrees(np.unwrap(rawData))
|
|
|
|
self.unwrappedReference = np.degrees(np.unwrap(rawReference))
|
2019-10-03 12:17:04 +00:00
|
|
|
|
2019-09-29 09:50:58 +00:00
|
|
|
if self.fixedValues:
|
|
|
|
minAngle = self.minDisplayValue
|
|
|
|
maxAngle = self.maxDisplayValue
|
2019-10-03 12:17:04 +00:00
|
|
|
elif self.unwrap and self.data:
|
|
|
|
minAngle = math.floor(np.min(self.unwrappedData))
|
|
|
|
maxAngle = math.ceil(np.max(self.unwrappedData))
|
|
|
|
elif self.unwrap and self.reference:
|
|
|
|
minAngle = math.floor(np.min(self.unwrappedReference))
|
|
|
|
maxAngle = math.ceil(np.max(self.unwrappedReference))
|
2019-09-29 09:50:58 +00:00
|
|
|
else:
|
|
|
|
minAngle = -180
|
|
|
|
maxAngle = 180
|
2019-10-03 12:17:04 +00:00
|
|
|
|
|
|
|
span = maxAngle - minAngle
|
2019-10-29 08:27:14 +00:00
|
|
|
if span == 0:
|
|
|
|
span = 0.01
|
2019-09-12 12:30:50 +00:00
|
|
|
self.minAngle = minAngle
|
2019-09-29 14:34:50 +00:00
|
|
|
self.maxAngle = maxAngle
|
2019-09-12 12:30:50 +00:00
|
|
|
self.span = span
|
2019-10-06 20:03:45 +00:00
|
|
|
|
2021-06-26 22:55:43 +00:00
|
|
|
tickcount = math.floor(self.dim.height / 60)
|
2019-10-06 20:03:45 +00:00
|
|
|
|
|
|
|
for i in range(tickcount):
|
|
|
|
angle = minAngle + span * i / tickcount
|
2021-06-26 22:55:43 +00:00
|
|
|
y = self.topMargin + round((self.maxAngle - angle) / self.span * self.dim.height)
|
2019-10-06 20:03:45 +00:00
|
|
|
if angle != minAngle and angle != maxAngle:
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(QtGui.QPen(Chart.color.text))
|
2019-10-06 20:03:45 +00:00
|
|
|
if angle != 0:
|
|
|
|
digits = max(0, min(2, math.floor(3 - math.log10(abs(angle)))))
|
|
|
|
if digits == 0:
|
|
|
|
anglestr = str(round(angle))
|
|
|
|
else:
|
|
|
|
anglestr = str(round(angle, digits))
|
|
|
|
else:
|
|
|
|
anglestr = "0"
|
|
|
|
qp.drawText(3, y + 3, anglestr + "°")
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(QtGui.QPen(Chart.color.foreground))
|
2021-06-26 22:55:43 +00:00
|
|
|
qp.drawLine(self.leftMargin - 5, y, self.leftMargin + self.dim.width, y)
|
2020-05-18 18:58:17 +00:00
|
|
|
qp.drawLine(self.leftMargin - 5,
|
|
|
|
self.topMargin,
|
2021-06-26 22:55:43 +00:00
|
|
|
self.leftMargin + self.dim.width,
|
2020-05-18 18:58:17 +00:00
|
|
|
self.topMargin)
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(Chart.color.text)
|
2019-10-06 20:03:45 +00:00
|
|
|
qp.drawText(3, self.topMargin + 5, str(maxAngle) + "°")
|
2021-06-26 22:55:43 +00:00
|
|
|
qp.drawText(3, self.dim.height + self.topMargin, str(minAngle) + "°")
|
2019-09-29 16:56:53 +00:00
|
|
|
|
2021-06-27 09:45:22 +00:00
|
|
|
self._set_start_stop()
|
2019-09-26 20:57:34 +00:00
|
|
|
|
|
|
|
# Draw bands if required
|
|
|
|
if self.bands.enabled:
|
2021-06-27 09:45:22 +00:00
|
|
|
self.drawBands(qp, self.fstart, self.fstop)
|
2019-09-26 20:57:34 +00:00
|
|
|
|
2019-10-12 20:33:45 +00:00
|
|
|
self.drawFrequencyTicks(qp)
|
2021-07-06 15:01:20 +00:00
|
|
|
self.drawData(qp, self.data, Chart.color.sweep)
|
|
|
|
self.drawData(qp, self.reference, Chart.color.reference)
|
2019-09-29 16:56:53 +00:00
|
|
|
self.drawMarkers(qp)
|
2019-09-12 12:30:50 +00:00
|
|
|
|
|
|
|
def getYPosition(self, d: Datapoint) -> int:
|
2019-10-03 12:17:04 +00:00
|
|
|
if self.unwrap:
|
|
|
|
if d in self.data:
|
|
|
|
angle = self.unwrappedData[self.data.index(d)]
|
|
|
|
elif d in self.reference:
|
|
|
|
angle = self.unwrappedReference[self.reference.index(d)]
|
|
|
|
else:
|
2019-11-16 14:33:11 +00:00
|
|
|
angle = math.degrees(d.phase)
|
2019-10-03 12:17:04 +00:00
|
|
|
else:
|
2019-11-16 14:33:11 +00:00
|
|
|
angle = math.degrees(d.phase)
|
2021-06-26 22:55:43 +00:00
|
|
|
return self.topMargin + round((self.maxAngle - angle) / self.span * self.dim.height)
|
2019-09-05 12:56:40 +00:00
|
|
|
|
2019-11-08 09:17:58 +00:00
|
|
|
def valueAtPosition(self, y) -> List[float]:
|
|
|
|
absy = y - self.topMargin
|
2021-06-26 22:55:43 +00:00
|
|
|
val = -1 * ((absy / self.dim.height * self.span) - self.maxAngle)
|
2019-11-08 09:17:58 +00:00
|
|
|
return [val]
|