2020-05-18 18:58:17 +00:00
|
|
|
# NanoVNASaver
|
|
|
|
# A python program to view and export Touchstone data from a NanoVNA
|
2019-09-01 21:13:21 +00:00
|
|
|
# Copyright (C) 2019. Rune B. Broberg
|
|
|
|
#
|
|
|
|
# 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
|
2020-05-18 18:58:17 +00:00
|
|
|
from typing import List
|
2019-09-01 21:13:21 +00:00
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
from PyQt5 import QtWidgets, QtGui
|
2019-09-01 21:13:21 +00:00
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
from NanoVNASaver.RFTools import Datapoint
|
|
|
|
from .Frequency import FrequencyChart
|
|
|
|
from .LogMag import LogMagChart
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2019-12-09 15:22:25 +00:00
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
class MagnitudeZChart(FrequencyChart):
|
2019-11-17 20:45:08 +00:00
|
|
|
def __init__(self, name=""):
|
|
|
|
super().__init__(name)
|
|
|
|
self.leftMargin = 30
|
|
|
|
self.chartWidth = 250
|
|
|
|
self.chartHeight = 250
|
|
|
|
self.minDisplayValue = 0
|
|
|
|
self.maxDisplayValue = 100
|
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
self.minValue = 0
|
2019-11-17 20:45:08 +00:00
|
|
|
self.maxValue = 1
|
|
|
|
self.span = 1
|
|
|
|
|
2019-11-22 13:07:38 +00:00
|
|
|
self.setMinimumSize(self.chartWidth + self.rightMargin + self.leftMargin,
|
|
|
|
self.chartHeight + self.topMargin + self.bottomMargin)
|
|
|
|
self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding,
|
|
|
|
QtWidgets.QSizePolicy.MinimumExpanding))
|
2019-11-17 20:45:08 +00:00
|
|
|
pal = QtGui.QPalette()
|
|
|
|
pal.setColor(QtGui.QPalette.Background, self.backgroundColor)
|
|
|
|
self.setPalette(pal)
|
|
|
|
self.setAutoFillBackground(True)
|
|
|
|
|
|
|
|
def drawValues(self, qp: QtGui.QPainter):
|
|
|
|
if len(self.data) == 0 and len(self.reference) == 0:
|
|
|
|
return
|
|
|
|
pen = QtGui.QPen(self.sweepColor)
|
|
|
|
pen.setWidth(self.pointSize)
|
|
|
|
line_pen = QtGui.QPen(self.sweepColor)
|
|
|
|
line_pen.setWidth(self.lineThickness)
|
|
|
|
highlighter = QtGui.QPen(QtGui.QColor(20, 0, 255))
|
|
|
|
highlighter.setWidth(1)
|
|
|
|
if not self.fixedSpan:
|
|
|
|
if len(self.data) > 0:
|
|
|
|
fstart = self.data[0].freq
|
|
|
|
fstop = self.data[len(self.data)-1].freq
|
|
|
|
else:
|
|
|
|
fstart = self.reference[0].freq
|
|
|
|
fstop = self.reference[len(self.reference) - 1].freq
|
|
|
|
self.fstart = fstart
|
|
|
|
self.fstop = fstop
|
|
|
|
else:
|
|
|
|
fstart = self.fstart = self.minFrequency
|
|
|
|
fstop = self.fstop = self.maxFrequency
|
|
|
|
|
|
|
|
# Draw bands if required
|
|
|
|
if self.bands.enabled:
|
|
|
|
self.drawBands(qp, fstart, fstop)
|
|
|
|
|
|
|
|
if self.fixedValues:
|
2020-05-18 18:58:17 +00:00
|
|
|
maxValue = self.maxDisplayValue
|
|
|
|
minValue = self.minDisplayValue
|
2019-11-17 20:45:08 +00:00
|
|
|
self.maxValue = maxValue
|
|
|
|
self.minValue = minValue
|
|
|
|
else:
|
|
|
|
# Find scaling
|
2020-05-18 18:58:17 +00:00
|
|
|
minValue = 100
|
|
|
|
maxValue = 0
|
2019-11-17 20:45:08 +00:00
|
|
|
for d in self.data:
|
2020-05-18 18:58:17 +00:00
|
|
|
mag = self.magnitude(d)
|
|
|
|
if mag > maxValue:
|
|
|
|
maxValue = mag
|
|
|
|
if mag < minValue:
|
|
|
|
minValue = mag
|
2019-11-17 20:45:08 +00:00
|
|
|
for d in self.reference: # Also check min/max for the reference sweep
|
|
|
|
if d.freq < self.fstart or d.freq > self.fstop:
|
|
|
|
continue
|
2020-05-18 18:58:17 +00:00
|
|
|
mag = self.magnitude(d)
|
|
|
|
if mag > maxValue:
|
|
|
|
maxValue = mag
|
|
|
|
if mag < minValue:
|
|
|
|
minValue = mag
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
minValue = 10*math.floor(minValue/10)
|
2019-11-17 20:45:08 +00:00
|
|
|
self.minValue = minValue
|
2020-05-18 18:58:17 +00:00
|
|
|
maxValue = 10*math.ceil(maxValue/10)
|
2019-11-17 20:45:08 +00:00
|
|
|
self.maxValue = maxValue
|
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
span = maxValue-minValue
|
2019-11-17 20:45:08 +00:00
|
|
|
if span == 0:
|
2020-05-18 18:58:17 +00:00
|
|
|
span = 0.01
|
2019-11-17 20:45:08 +00:00
|
|
|
self.span = span
|
|
|
|
|
|
|
|
target_ticks = math.floor(self.chartHeight / 60)
|
2020-05-18 18:58:17 +00:00
|
|
|
|
2019-11-17 20:45:08 +00:00
|
|
|
for i in range(target_ticks):
|
|
|
|
val = minValue + (i / target_ticks) * span
|
|
|
|
y = self.topMargin + round((self.maxValue - val) / self.span * self.chartHeight)
|
|
|
|
qp.setPen(self.textColor)
|
|
|
|
if val != minValue:
|
2020-05-18 18:58:17 +00:00
|
|
|
digits = max(0, min(2, math.floor(3 - math.log10(abs(val)))))
|
|
|
|
if digits == 0:
|
|
|
|
vswrstr = str(round(val))
|
|
|
|
else:
|
|
|
|
vswrstr = str(round(val, digits))
|
|
|
|
qp.drawText(3, y + 3, vswrstr)
|
2019-11-17 20:45:08 +00:00
|
|
|
qp.setPen(QtGui.QPen(self.foregroundColor))
|
|
|
|
qp.drawLine(self.leftMargin - 5, y, self.leftMargin + self.chartWidth, y)
|
|
|
|
|
|
|
|
qp.setPen(QtGui.QPen(self.foregroundColor))
|
|
|
|
qp.drawLine(self.leftMargin - 5, self.topMargin,
|
|
|
|
self.leftMargin + self.chartWidth, self.topMargin)
|
|
|
|
qp.setPen(self.textColor)
|
2020-05-18 18:58:17 +00:00
|
|
|
qp.drawText(3, self.topMargin + 4, str(maxValue))
|
|
|
|
qp.drawText(3, self.chartHeight+self.topMargin, str(minValue))
|
2019-11-17 20:45:08 +00:00
|
|
|
self.drawFrequencyTicks(qp)
|
|
|
|
|
|
|
|
self.drawData(qp, self.data, self.sweepColor)
|
|
|
|
self.drawData(qp, self.reference, self.referenceColor)
|
|
|
|
self.drawMarkers(qp)
|
|
|
|
|
|
|
|
def getYPosition(self, d: Datapoint) -> int:
|
2020-05-18 18:58:17 +00:00
|
|
|
mag = self.magnitude(d)
|
|
|
|
return self.topMargin + round((self.maxValue - mag) / self.span * self.chartHeight)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
def valueAtPosition(self, y) -> List[float]:
|
|
|
|
absy = y - self.topMargin
|
|
|
|
val = -1 * ((absy / self.chartHeight * self.span) - self.maxValue)
|
2020-05-18 18:58:17 +00:00
|
|
|
return [val]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def magnitude(p: Datapoint) -> float:
|
|
|
|
return abs(p.impedance())
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
def copy(self):
|
2020-05-18 18:58:17 +00:00
|
|
|
new_chart: LogMagChart = super().copy()
|
2019-11-17 20:45:08 +00:00
|
|
|
new_chart.span = self.span
|
|
|
|
return new_chart
|