2020-05-18 18:58:18 +00:00
|
|
|
# NanoVNASaver
|
2020-06-25 17:52:30 +00:00
|
|
|
#
|
2020-05-18 18:58:18 +00:00
|
|
|
# A python program to view and export Touchstone data from a NanoVNA
|
2020-06-25 17:52:30 +00:00
|
|
|
# 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
|
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 12:58:14 +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 QualityFactorChart(FrequencyChart):
|
|
|
|
def __init__(self, name=""):
|
2019-11-08 12:58:14 +00:00
|
|
|
super().__init__(name)
|
2020-05-18 18:58:18 +00:00
|
|
|
self.leftMargin = 35
|
2021-06-26 22:55:43 +00:00
|
|
|
self.dim.width = 250
|
|
|
|
self.dim.height = 250
|
2019-11-08 12:58:14 +00:00
|
|
|
self.fstart = 0
|
|
|
|
self.fstop = 0
|
2020-05-18 18:58:18 +00:00
|
|
|
self.minQ = 0
|
|
|
|
self.maxQ = 0
|
2019-11-08 12:58:14 +00:00
|
|
|
self.span = 0
|
2019-11-17 20:45:08 +00:00
|
|
|
self.minDisplayValue = 0
|
|
|
|
self.maxDisplayValue = 100
|
|
|
|
|
|
|
|
def drawChart(self, qp: QtGui.QPainter):
|
2020-05-18 18:58:18 +00:00
|
|
|
super().drawChart(qp)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
# Make up some sensible scaling here
|
2019-11-17 20:45:08 +00:00
|
|
|
if self.fixedValues:
|
2020-05-18 18:58:18 +00:00
|
|
|
maxQ = self.maxDisplayValue
|
|
|
|
minQ = self.minDisplayValue
|
2019-11-17 20:45:08 +00:00
|
|
|
else:
|
2020-05-18 18:58:18 +00:00
|
|
|
minQ = 0
|
|
|
|
maxQ = 0
|
2019-11-17 20:45:08 +00:00
|
|
|
for d in self.data:
|
2020-05-18 18:58:18 +00:00
|
|
|
Q = d.qFactor()
|
|
|
|
if Q > maxQ:
|
|
|
|
maxQ = Q
|
|
|
|
scale = 0
|
|
|
|
if maxQ > 0:
|
|
|
|
scale = max(scale, math.floor(math.log10(maxQ)))
|
|
|
|
maxQ = math.ceil(maxQ / 10 ** scale) * 10 ** scale
|
|
|
|
self.minQ = minQ
|
|
|
|
self.maxQ = maxQ
|
|
|
|
self.span = self.maxQ - self.minQ
|
|
|
|
if self.span == 0:
|
|
|
|
return # No data to draw the graph from
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2021-06-26 22:55:43 +00:00
|
|
|
tickcount = math.floor(self.dim.height / 60)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
2020-05-18 18:58:18 +00:00
|
|
|
for i in range(tickcount):
|
|
|
|
q = self.minQ + i * self.span / tickcount
|
2021-06-26 22:55:43 +00:00
|
|
|
y = self.topMargin + round((self.maxQ - q) / self.span * self.dim.height)
|
2020-05-18 18:58:18 +00:00
|
|
|
if q < 10:
|
|
|
|
q = round(q, 2)
|
|
|
|
elif q < 20:
|
|
|
|
q = round(q, 1)
|
|
|
|
else:
|
|
|
|
q = round(q)
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(QtGui.QPen(Chart.color.text))
|
2020-05-18 18:58:18 +00:00
|
|
|
qp.drawText(3, y+3, str(q))
|
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, self.topMargin)
|
2021-07-06 15:01:20 +00:00
|
|
|
qp.setPen(Chart.color.text)
|
2020-05-18 18:58:18 +00:00
|
|
|
if maxQ < 10:
|
|
|
|
qstr = str(round(maxQ, 2))
|
|
|
|
elif maxQ < 20:
|
|
|
|
qstr = str(round(maxQ, 1))
|
|
|
|
else:
|
|
|
|
qstr = str(round(maxQ))
|
|
|
|
qp.drawText(3, 35, qstr)
|
2019-11-17 20:45:08 +00:00
|
|
|
|
|
|
|
def drawValues(self, qp: QtGui.QPainter):
|
|
|
|
if len(self.data) == 0 and len(self.reference) == 0:
|
|
|
|
return
|
2020-05-18 18:58:18 +00:00
|
|
|
if self.span == 0:
|
|
|
|
return
|
2021-07-06 15:01:20 +00:00
|
|
|
pen = QtGui.QPen(Chart.color.sweep)
|
2021-06-26 22:55:43 +00:00
|
|
|
pen.setWidth(self.dim.point)
|
2021-07-06 15:01:20 +00:00
|
|
|
line_pen = QtGui.QPen(Chart.color.sweep)
|
2021-06-26 22:55:43 +00:00
|
|
|
line_pen.setWidth(self.dim.line)
|
2019-11-17 20:45:08 +00:00
|
|
|
highlighter = QtGui.QPen(QtGui.QColor(20, 0, 255))
|
|
|
|
highlighter.setWidth(1)
|
2021-06-27 09:45:22 +00:00
|
|
|
|
|
|
|
self._set_start_stop()
|
2019-11-17 20:45:08 +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-11-17 20:45:08 +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)
|
|
|
|
|
|
|
|
def getYPosition(self, d: Datapoint) -> int:
|
2020-05-18 18:58:18 +00:00
|
|
|
Q = d.qFactor()
|
2021-06-26 22:55:43 +00:00
|
|
|
return self.topMargin + round((self.maxQ - Q) / self.span * self.dim.height)
|
2019-11-17 20:45:08 +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.maxQ)
|
2020-05-18 18:58:18 +00:00
|
|
|
return [val]
|