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-17 20:45:08 +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 CombinedLogMagChart(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
|
2020-05-18 18:58:17 +00:00
|
|
|
self.minDisplayValue = -80
|
|
|
|
self.maxDisplayValue = 10
|
|
|
|
|
|
|
|
self.data11: List[Datapoint] = []
|
|
|
|
self.data21: List[Datapoint] = []
|
|
|
|
|
|
|
|
self.reference11: List[Datapoint] = []
|
|
|
|
self.reference21: List[Datapoint] = []
|
2019-11-17 20:45:08 +00:00
|
|
|
|
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
|
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
self.isInverted = False
|
|
|
|
|
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)
|
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
def setCombinedData(self, data11, data21):
|
|
|
|
self.data11 = data11
|
|
|
|
self.data21 = data21
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def setCombinedReference(self, data11, data21):
|
|
|
|
self.reference11 = data11
|
|
|
|
self.reference21 = data21
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def resetReference(self):
|
|
|
|
self.reference11 = []
|
|
|
|
self.reference21 = []
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def resetDisplayLimits(self):
|
|
|
|
self.reference11 = []
|
|
|
|
self.reference21 = []
|
|
|
|
self.update()
|
|
|
|
|
2019-11-17 20:45:08 +00:00
|
|
|
def drawChart(self, qp: QtGui.QPainter):
|
|
|
|
qp.setPen(QtGui.QPen(self.textColor))
|
2020-05-18 18:58:17 +00:00
|
|
|
qp.drawText(int(round(self.chartWidth / 2)) - 20, 15, self.name + " (dB)")
|
|
|
|
qp.drawText(10, 15, "S11")
|
|
|
|
qp.drawText(self.leftMargin + self.chartWidth - 8, 15, "S21")
|
2019-11-17 20:45:08 +00:00
|
|
|
qp.setPen(QtGui.QPen(self.foregroundColor))
|
2020-05-18 18:58:17 +00:00
|
|
|
qp.drawLine(self.leftMargin, self.topMargin - 5,
|
|
|
|
self.leftMargin, self.topMargin+self.chartHeight+5)
|
2019-11-22 13:07:38 +00:00
|
|
|
qp.drawLine(self.leftMargin-5, self.topMargin+self.chartHeight,
|
|
|
|
self.leftMargin+self.chartWidth, self.topMargin + self.chartHeight)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
def drawValues(self, qp: QtGui.QPainter):
|
2020-05-18 18:58:17 +00:00
|
|
|
if len(self.data11) == 0 and len(self.reference11) == 0:
|
2019-11-17 20:45:08 +00:00
|
|
|
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:
|
2020-05-18 18:58:17 +00:00
|
|
|
if len(self.data11) > 0:
|
|
|
|
fstart = self.data11[0].freq
|
|
|
|
fstop = self.data11[len(self.data11)-1].freq
|
2019-11-17 20:45:08 +00:00
|
|
|
else:
|
2020-05-18 18:58:17 +00:00
|
|
|
fstart = self.reference11[0].freq
|
|
|
|
fstop = self.reference11[len(self.reference11) - 1].freq
|
2019-11-17 20:45:08 +00:00
|
|
|
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
|
|
|
|
for d in self.data11:
|
|
|
|
logmag = self.logMag(d)
|
|
|
|
if math.isinf(logmag):
|
|
|
|
continue
|
|
|
|
if logmag > maxValue:
|
|
|
|
maxValue = logmag
|
|
|
|
if logmag < minValue:
|
|
|
|
minValue = logmag
|
|
|
|
for d in self.data21:
|
|
|
|
logmag = self.logMag(d)
|
|
|
|
if math.isinf(logmag):
|
|
|
|
continue
|
|
|
|
if logmag > maxValue:
|
|
|
|
maxValue = logmag
|
|
|
|
if logmag < minValue:
|
|
|
|
minValue = logmag
|
|
|
|
|
|
|
|
for d in self.reference11:
|
2019-11-17 20:45:08 +00:00
|
|
|
if d.freq < self.fstart or d.freq > self.fstop:
|
|
|
|
continue
|
2020-05-18 18:58:17 +00:00
|
|
|
logmag = self.logMag(d)
|
|
|
|
if math.isinf(logmag):
|
|
|
|
continue
|
|
|
|
if logmag > maxValue:
|
|
|
|
maxValue = logmag
|
|
|
|
if logmag < minValue:
|
|
|
|
minValue = logmag
|
|
|
|
for d in self.reference21:
|
|
|
|
if d.freq < self.fstart or d.freq > self.fstop:
|
|
|
|
continue
|
|
|
|
logmag = self.logMag(d)
|
|
|
|
if math.isinf(logmag):
|
|
|
|
continue
|
|
|
|
if logmag > maxValue:
|
|
|
|
maxValue = logmag
|
|
|
|
if logmag < minValue:
|
|
|
|
minValue = logmag
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.maxValue = maxValue
|
2019-11-17 20:45:08 +00:00
|
|
|
|
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
|
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
if self.span >= 50:
|
|
|
|
# Ticks per 10dB step
|
|
|
|
tick_count = math.floor(self.span/10)
|
|
|
|
first_tick = math.ceil(self.minValue/10) * 10
|
|
|
|
tick_step = 10
|
|
|
|
if first_tick == minValue:
|
|
|
|
first_tick += 10
|
|
|
|
elif self.span >= 20:
|
|
|
|
# 5 dB ticks
|
|
|
|
tick_count = math.floor(self.span/5)
|
|
|
|
first_tick = math.ceil(self.minValue/5) * 5
|
|
|
|
tick_step = 5
|
|
|
|
if first_tick == minValue:
|
|
|
|
first_tick += 5
|
|
|
|
elif self.span >= 10:
|
|
|
|
# 2 dB ticks
|
|
|
|
tick_count = math.floor(self.span/2)
|
|
|
|
first_tick = math.ceil(self.minValue/2) * 2
|
|
|
|
tick_step = 2
|
|
|
|
if first_tick == minValue:
|
|
|
|
first_tick += 2
|
|
|
|
elif self.span >= 5:
|
|
|
|
# 1dB ticks
|
|
|
|
tick_count = math.floor(self.span)
|
|
|
|
first_tick = math.ceil(minValue)
|
|
|
|
tick_step = 1
|
|
|
|
if first_tick == minValue:
|
|
|
|
first_tick += 1
|
|
|
|
elif self.span >= 2:
|
|
|
|
# .5 dB ticks
|
|
|
|
tick_count = math.floor(self.span*2)
|
|
|
|
first_tick = math.ceil(minValue*2) / 2
|
|
|
|
tick_step = .5
|
|
|
|
if first_tick == minValue:
|
|
|
|
first_tick += .5
|
|
|
|
else:
|
|
|
|
# .1 dB ticks
|
|
|
|
tick_count = math.floor(self.span*10)
|
|
|
|
first_tick = math.ceil(minValue*10) / 10
|
|
|
|
tick_step = .1
|
|
|
|
if first_tick == minValue:
|
|
|
|
first_tick += .1
|
|
|
|
|
|
|
|
for i in range(tick_count):
|
|
|
|
db = first_tick + i * tick_step
|
|
|
|
y = self.topMargin + round((maxValue - db)/span*self.chartHeight)
|
2019-11-17 20:45:08 +00:00
|
|
|
qp.setPen(QtGui.QPen(self.foregroundColor))
|
2020-05-18 18:58:17 +00:00
|
|
|
qp.drawLine(self.leftMargin-5, y, self.leftMargin+self.chartWidth, y)
|
|
|
|
if db > minValue and db != maxValue:
|
|
|
|
qp.setPen(QtGui.QPen(self.textColor))
|
|
|
|
if tick_step < 1:
|
|
|
|
dbstr = str(round(db, 1))
|
|
|
|
else:
|
|
|
|
dbstr = str(db)
|
|
|
|
qp.drawText(3, y + 4, dbstr)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-05-18 18:58:17 +00:00
|
|
|
qp.setPen(self.swrColor)
|
|
|
|
for vswr in self.swrMarkers:
|
|
|
|
if vswr <= 1:
|
|
|
|
continue
|
|
|
|
logMag = 20 * math.log10((vswr-1)/(vswr+1))
|
|
|
|
if self.isInverted:
|
|
|
|
logMag = logMag * -1
|
|
|
|
y = self.topMargin + round((self.maxValue - logMag) / self.span * self.chartHeight)
|
|
|
|
qp.drawLine(self.leftMargin, y, self.leftMargin + self.chartWidth, y)
|
|
|
|
qp.drawText(self.leftMargin + 3, y - 1, "VSWR: " + str(vswr))
|
|
|
|
|
|
|
|
if len(self.data11) > 0:
|
|
|
|
c = QtGui.QColor(self.sweepColor)
|
|
|
|
c.setAlpha(255)
|
|
|
|
pen = QtGui.QPen(c)
|
|
|
|
pen.setWidth(2)
|
|
|
|
qp.setPen(pen)
|
|
|
|
qp.drawLine(33, 9, 38, 9)
|
|
|
|
c = QtGui.QColor(self.secondarySweepColor)
|
|
|
|
c.setAlpha(255)
|
|
|
|
pen = QtGui.QPen(c)
|
|
|
|
pen.setWidth(2)
|
|
|
|
qp.setPen(pen)
|
|
|
|
qp.drawLine(self.leftMargin + self.chartWidth - 20, 9, self.leftMargin + self.chartWidth - 15, 9)
|
|
|
|
|
|
|
|
if len(self.reference11) > 0:
|
|
|
|
c = QtGui.QColor(self.referenceColor)
|
|
|
|
c.setAlpha(255)
|
|
|
|
pen = QtGui.QPen(c)
|
|
|
|
pen.setWidth(2)
|
|
|
|
qp.setPen(pen)
|
|
|
|
qp.drawLine(33, 14, 38, 14)
|
|
|
|
c = QtGui.QColor(self.secondaryReferenceColor)
|
|
|
|
c.setAlpha(255)
|
|
|
|
pen = QtGui.QPen(c)
|
|
|
|
pen.setWidth(2)
|
|
|
|
qp.setPen(pen)
|
|
|
|
qp.drawLine(self.leftMargin + self.chartWidth - 20, 14, self.leftMargin + self.chartWidth - 15, 14)
|
|
|
|
|
|
|
|
self.drawData(qp, self.data11, self.sweepColor)
|
|
|
|
self.drawData(qp, self.data21, self.secondarySweepColor)
|
|
|
|
self.drawData(qp, self.reference11, self.referenceColor)
|
|
|
|
self.drawData(qp, self.reference21, self.secondaryReferenceColor)
|
|
|
|
self.drawMarkers(qp, data=self.data11)
|
|
|
|
self.drawMarkers(qp, data=self.data21)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
def getYPosition(self, d: Datapoint) -> int:
|
2020-05-18 18:58:17 +00:00
|
|
|
logMag = self.logMag(d)
|
|
|
|
if math.isinf(logMag):
|
|
|
|
return None
|
|
|
|
return self.topMargin + round((self.maxValue - logMag) / 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]
|
|
|
|
|
|
|
|
def logMag(self, p: Datapoint) -> float:
|
|
|
|
if self.isInverted:
|
|
|
|
return -p.gain
|
|
|
|
else:
|
|
|
|
return p.gain
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
def copy(self):
|
2020-05-18 18:58:17 +00:00
|
|
|
new_chart: LogMagChart = super().copy()
|
|
|
|
new_chart.isInverted = self.isInverted
|
2019-11-17 20:45:08 +00:00
|
|
|
new_chart.span = self.span
|
2020-05-18 18:58:17 +00:00
|
|
|
new_chart.data11 = self.data11
|
|
|
|
new_chart.data21 = self.data21
|
|
|
|
new_chart.reference11 = self.reference11
|
|
|
|
new_chart.reference21 = self.reference21
|
2019-11-17 20:45:08 +00:00
|
|
|
return new_chart
|