nanovna-saver/src/NanoVNASaver/Analysis/VSWRAnalysis.py

113 wiersze
3.8 KiB
Python
Czysty Zwykły widok Historia

# NanoVNASaver
2020-06-25 17:52:30 +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-10-04 10:51:20 +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/>.
import logging
2023-03-12 07:02:58 +00:00
from PyQt6 import QtWidgets
2019-10-04 10:51:20 +00:00
import NanoVNASaver.AnalyticTools as at
from NanoVNASaver.Analysis.Base import Analysis, QHLine
from NanoVNASaver.Formatting import format_frequency, format_vswr
2019-10-04 10:51:20 +00:00
logger = logging.getLogger(__name__)
class VSWRAnalysis(Analysis):
2020-07-01 18:47:15 +00:00
max_dips_shown = 3
vswr_limit_value = 1.5
2020-12-19 12:38:53 +00:00
def __init__(self, app):
super().__init__(app)
self._widget = QtWidgets.QWidget()
self.layout = QtWidgets.QFormLayout()
self._widget.setLayout(self.layout)
self.input_vswr_limit = QtWidgets.QDoubleSpinBox()
self.input_vswr_limit.setValue(VSWRAnalysis.vswr_limit_value)
self.input_vswr_limit.setSingleStep(0.1)
self.input_vswr_limit.setMinimum(1)
self.input_vswr_limit.setMaximum(25)
self.input_vswr_limit.setDecimals(2)
self.checkbox_move_marker = QtWidgets.QCheckBox()
self.layout.addRow(QtWidgets.QLabel("<b>Settings</b>"))
self.layout.addRow("VSWR limit", self.input_vswr_limit)
self.layout.addRow(QHLine())
2020-12-20 14:42:57 +00:00
self.results_label = QtWidgets.QLabel("<b>Results</b>")
self.layout.addRow(self.results_label)
self.minimums: list[int] = []
2020-12-20 14:42:57 +00:00
def runAnalysis(self):
if not self.app.data.s11:
return
s11 = self.app.data.s11
2020-12-20 14:42:57 +00:00
data = [d.vswr for d in s11]
threshold = self.input_vswr_limit.value()
2020-12-20 14:42:57 +00:00
2023-03-08 08:40:39 +00:00
minima = sorted(at.minima(data, threshold), key=lambda i: data[i])[
: VSWRAnalysis.max_dips_shown
]
self.minimums = minima
2020-12-20 14:42:57 +00:00
results_header = self.layout.indexOf(self.results_label)
2023-03-08 08:40:39 +00:00
logger.debug(
"Results start at %d, out of %d",
results_header,
self.layout.rowCount(),
)
2021-06-26 22:34:06 +00:00
for _ in range(results_header, self.layout.rowCount()):
2020-12-20 14:42:57 +00:00
self.layout.removeRow(self.layout.rowCount() - 1)
if not minima:
2023-03-08 08:40:39 +00:00
self.layout.addRow(
QtWidgets.QLabel(
f"No areas found with VSWR below {format_vswr(threshold)}."
)
)
return
for idx in minima:
rng = at.take_from_idx(data, idx, lambda i: i[1] < threshold)
begin, end = rng[0], rng[-1]
2022-09-15 19:59:55 +00:00
self.layout.addRow(
2023-03-08 08:40:39 +00:00
"Start", QtWidgets.QLabel(format_frequency(s11[begin].freq))
)
self.layout.addRow(
"Minimum",
QtWidgets.QLabel(
f"{format_frequency(s11[idx].freq)}"
f" ({round(s11[idx].vswr, 2)})"
),
)
self.layout.addRow(
"End", QtWidgets.QLabel(format_frequency(s11[end].freq))
)
self.layout.addRow(
"Span",
QtWidgets.QLabel(
format_frequency((s11[end].freq - s11[begin].freq))
),
)
self.layout.addWidget(QHLine())
2020-12-22 20:28:04 +00:00
self.layout.removeRow(self.layout.rowCount() - 1)