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
|
2022-01-09 14:24:04 +00:00
|
|
|
# Copyright (C) 2020ff 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
|
2020-05-18 18:58:18 +00:00
|
|
|
from typing import List
|
2019-09-01 21:13:21 +00:00
|
|
|
|
2021-07-01 19:36:17 +00:00
|
|
|
from PyQt5 import QtGui
|
2019-09-01 21:13:21 +00:00
|
|
|
|
2020-05-18 18:58:18 +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-11-08 09:17:58 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-11-08 12:58:14 +00:00
|
|
|
|
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
class VSWRChart(FrequencyChart):
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
def __init__(self, name=""):
|
2019-11-08 12:58:14 +00:00
|
|
|
super().__init__(name)
|
2021-06-22 21:23:39 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
self.maxDisplayValue = 25
|
|
|
|
self.minDisplayValue = 1
|
2019-11-08 12:58:14 +00:00
|
|
|
|
2021-06-22 21:23:39 +00:00
|
|
|
self.maxVSWR = 3
|
|
|
|
self.span = 2
|
|
|
|
|
2021-02-21 20:48:28 +00:00
|
|
|
def logarithmicYAllowed(self) -> bool:
|
|
|
|
return True
|
2019-11-08 12:58:14 +00:00
|
|
|
|
|
|
|
def drawValues(self, qp: QtGui.QPainter):
|
|
|
|
if len(self.data) == 0 and len(self.reference) == 0:
|
|
|
|
return
|
|
|
|
if self.fixedSpan:
|
|
|
|
fstart = self.minFrequency
|
|
|
|
fstop = self.maxFrequency
|
2020-05-18 18:58:18 +00:00
|
|
|
elif len(self.data) > 0:
|
|
|
|
fstart = self.data[0].freq
|
|
|
|
fstop = self.data[len(self.data)-1].freq
|
2019-11-08 12:58:14 +00:00
|
|
|
else:
|
2020-05-18 18:58:18 +00:00
|
|
|
fstart = self.reference[0].freq
|
|
|
|
fstop = self.reference[len(self.reference) - 1].freq
|
2019-11-08 12:58:14 +00:00
|
|
|
self.fstart = fstart
|
|
|
|
self.fstop = fstop
|
|
|
|
|
|
|
|
# Draw bands if required
|
|
|
|
if self.bands.enabled:
|
|
|
|
self.drawBands(qp, fstart, fstop)
|
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
# Find scaling
|
2019-11-17 20:45:08 +00:00
|
|
|
if self.fixedValues:
|
2020-05-18 18:58:18 +00:00
|
|
|
minVSWR = max(1, self.minDisplayValue)
|
|
|
|
maxVSWR = self.maxDisplayValue
|
2019-11-17 20:45:08 +00:00
|
|
|
else:
|
2020-05-18 18:58:18 +00:00
|
|
|
minVSWR = 1
|
|
|
|
maxVSWR = 3
|
2019-11-17 20:45:08 +00:00
|
|
|
for d in self.data:
|
2020-05-18 18:58:18 +00:00
|
|
|
vswr = d.vswr
|
|
|
|
if vswr > maxVSWR:
|
|
|
|
maxVSWR = vswr
|
2022-01-09 14:24:04 +00:00
|
|
|
try:
|
|
|
|
maxVSWR = min(self.maxDisplayValue, math.ceil(maxVSWR))
|
|
|
|
except OverflowError:
|
|
|
|
maxVSWR = self.maxDisplayValue
|
2020-05-18 18:58:18 +00:00
|
|
|
self.maxVSWR = maxVSWR
|
|
|
|
span = maxVSWR-minVSWR
|
2019-11-17 20:45:08 +00:00
|
|
|
if span == 0:
|
2020-05-18 18:58:18 +00:00
|
|
|
span = 0.01
|
2019-11-17 20:45:08 +00:00
|
|
|
self.span = span
|
|
|
|
|
2021-06-26 22:55:43 +00:00
|
|
|
target_ticks = math.floor(self.dim.height / 60)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
if self.logarithmicY:
|
|
|
|
for i in range(target_ticks):
|
2021-06-26 22:55:43 +00:00
|
|
|
y = int(self.topMargin + (i / target_ticks) * self.dim.height)
|
2020-05-18 18:58:18 +00:00
|
|
|
vswr = self.valueAtPosition(y)[0]
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(Chart.color.text)
|
2020-05-18 18:58:18 +00:00
|
|
|
if vswr != 0:
|
|
|
|
digits = max(0, min(2, math.floor(3 - math.log10(abs(vswr)))))
|
|
|
|
if digits == 0:
|
|
|
|
vswrstr = str(round(vswr))
|
|
|
|
else:
|
|
|
|
vswrstr = str(round(vswr, digits))
|
|
|
|
qp.drawText(3, y+3, vswrstr)
|
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)
|
|
|
|
qp.drawLine(self.leftMargin - 5, self.topMargin + self.dim.height,
|
|
|
|
self.leftMargin + self.dim.width, self.topMargin + self.dim.height)
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(Chart.color.text)
|
2020-05-18 18:58:18 +00:00
|
|
|
digits = max(0, min(2, math.floor(3 - math.log10(abs(minVSWR)))))
|
|
|
|
if digits == 0:
|
|
|
|
vswrstr = str(round(minVSWR))
|
2019-11-17 20:45:08 +00:00
|
|
|
else:
|
2020-05-18 18:58:18 +00:00
|
|
|
vswrstr = str(round(minVSWR, digits))
|
2021-06-26 22:55:43 +00:00
|
|
|
qp.drawText(3, self.topMargin + self.dim.height, vswrstr)
|
2019-11-17 20:45:08 +00:00
|
|
|
else:
|
2020-05-18 18:58:18 +00:00
|
|
|
for i in range(target_ticks):
|
|
|
|
vswr = minVSWR + i * self.span/target_ticks
|
|
|
|
y = self.getYPositionFromValue(vswr)
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(Chart.color.text)
|
2020-05-18 18:58:18 +00:00
|
|
|
if vswr != 0:
|
|
|
|
digits = max(0, min(2, math.floor(3 - math.log10(abs(vswr)))))
|
|
|
|
if digits == 0:
|
|
|
|
vswrstr = str(round(vswr))
|
|
|
|
else:
|
|
|
|
vswrstr = str(round(vswr, digits))
|
|
|
|
qp.drawText(3, y+3, vswrstr)
|
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:18 +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:18 +00:00
|
|
|
self.topMargin)
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(Chart.color.text)
|
2020-05-18 18:58:18 +00:00
|
|
|
digits = max(0, min(2, math.floor(3 - math.log10(abs(maxVSWR)))))
|
|
|
|
if digits == 0:
|
|
|
|
vswrstr = str(round(maxVSWR))
|
|
|
|
else:
|
|
|
|
vswrstr = str(round(maxVSWR, digits))
|
|
|
|
qp.drawText(3, 35, vswrstr)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(Chart.color.swr)
|
2020-05-18 18:58:18 +00:00
|
|
|
for vswr in self.swrMarkers:
|
|
|
|
y = self.getYPositionFromValue(vswr)
|
2021-06-26 22:55:43 +00:00
|
|
|
qp.drawLine(self.leftMargin, y, self.leftMargin + self.dim.width, y)
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.drawText(self.leftMargin + 3, y - 1, str(vswr))
|
|
|
|
|
2021-06-22 21:23:39 +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-11-17 20:45:08 +00:00
|
|
|
self.drawMarkers(qp)
|
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
def getYPositionFromValue(self, vswr) -> int:
|
|
|
|
if self.logarithmicY:
|
|
|
|
min_val = self.maxVSWR - self.span
|
|
|
|
if self.maxVSWR > 0 and min_val > 0 and vswr > 0:
|
|
|
|
span = math.log(self.maxVSWR) - math.log(min_val)
|
|
|
|
else:
|
|
|
|
return -1
|
|
|
|
return (
|
|
|
|
self.topMargin +
|
2021-06-26 22:55:43 +00:00
|
|
|
round((math.log(self.maxVSWR) - math.log(vswr)) / span * self.dim.height))
|
2022-01-09 14:24:04 +00:00
|
|
|
try:
|
|
|
|
return self.topMargin + round((self.maxVSWR - vswr) / self.span * self.dim.height)
|
|
|
|
except OverflowError:
|
|
|
|
return self.topMargin
|
2020-05-18 18:58:18 +00:00
|
|
|
|
2019-11-17 20:45:08 +00:00
|
|
|
def getYPosition(self, d: Datapoint) -> int:
|
2020-05-18 18:58:18 +00:00
|
|
|
return self.getYPositionFromValue(d.vswr)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
def valueAtPosition(self, y) -> List[float]:
|
|
|
|
absy = y - self.topMargin
|
2020-05-18 18:58:18 +00:00
|
|
|
if self.logarithmicY:
|
|
|
|
min_val = self.maxVSWR - self.span
|
|
|
|
if self.maxVSWR > 0 and min_val > 0:
|
|
|
|
span = math.log(self.maxVSWR) - math.log(min_val)
|
2021-06-26 22:55:43 +00:00
|
|
|
step = span / self.dim.height
|
2020-05-18 18:58:18 +00:00
|
|
|
val = math.exp(math.log(self.maxVSWR) - absy * step)
|
|
|
|
else:
|
|
|
|
val = -1
|
|
|
|
else:
|
2021-06-26 22:55:43 +00:00
|
|
|
val = -1 * ((absy / self.dim.height * self.span) - self.maxVSWR)
|
2020-05-18 18:58:18 +00:00
|
|
|
return [val]
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
def resetDisplayLimits(self):
|
|
|
|
self.maxDisplayValue = 25
|
|
|
|
super().resetDisplayLimits()
|