kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
128 wiersze
4.6 KiB
Python
128 wiersze
4.6 KiB
Python
# NanoVNASaver
|
|
#
|
|
# A python program to view and export Touchstone data from a NanoVNA
|
|
# Copyright (C) 2019, 2020 Rune B. Broberg
|
|
# Copyright (C) 2020,2021 NanoVNA-Saver Authors
|
|
#
|
|
# 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
|
|
|
|
from PyQt5 import QtWidgets
|
|
import numpy as np
|
|
|
|
from NanoVNASaver.Analysis.Base import Analysis, QHLine
|
|
from NanoVNASaver.Formatting import format_frequency
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SimplePeakSearchAnalysis(Analysis):
|
|
def __init__(self, app):
|
|
super().__init__(app)
|
|
|
|
self.label['peak_freq'] = QtWidgets.QLabel()
|
|
self.label['peak_db'] = QtWidgets.QLabel()
|
|
|
|
self.button = {
|
|
'vswr': QtWidgets.QRadioButton("VSWR"),
|
|
'resistance': QtWidgets.QRadioButton("Resistance"),
|
|
'reactance': QtWidgets.QRadioButton("Reactance"),
|
|
'gain': QtWidgets.QRadioButton("S21 Gain"),
|
|
'peak_h': QtWidgets.QRadioButton("Highest value"),
|
|
'peak_l': QtWidgets.QRadioButton("Lowest value"),
|
|
'move_marker': QtWidgets.QCheckBox()
|
|
}
|
|
|
|
self.button['gain'].setChecked(True)
|
|
self.button['peak_h'].setChecked(True)
|
|
|
|
self.btn_group = {
|
|
'data': QtWidgets.QButtonGroup(),
|
|
'peak': QtWidgets.QButtonGroup(),
|
|
}
|
|
|
|
for btn in ('vswr', 'resistance', 'reactance', 'gain'):
|
|
self.btn_group['data'].addButton(self.button[btn])
|
|
self.btn_group['peak'].addButton(self.button['peak_h'])
|
|
self.btn_group['peak'].addButton(self.button['peak_l'])
|
|
|
|
layout = self.layout
|
|
layout.addRow(self.label['titel'])
|
|
layout.addRow(QHLine())
|
|
layout.addRow(QtWidgets.QLabel("<b>Settings</b>"))
|
|
layout.addRow("Data source", self.button['vswr'])
|
|
layout.addRow("", self.button['resistance'])
|
|
layout.addRow("", self.button['reactance'])
|
|
layout.addRow("", self.button['gain'])
|
|
layout.addRow(QHLine())
|
|
layout.addRow("Peak type", self.button['peak_h'])
|
|
layout.addRow("", self.button['peak_l'])
|
|
layout.addRow(QHLine())
|
|
layout.addRow("Move marker to peak", self.button['move_marker'])
|
|
layout.addRow(QHLine())
|
|
layout.addRow(self.label['result'])
|
|
layout.addRow("Peak frequency:", self.label['peak_freq'])
|
|
layout.addRow("Peak value:", self.label['peak_db'])
|
|
|
|
self.set_titel('Simple peak search')
|
|
|
|
def runAnalysis(self):
|
|
if not self.app.data.s11:
|
|
return
|
|
|
|
s11 = self.app.data.s11
|
|
s21 = self.app.data.s21
|
|
|
|
if not s21:
|
|
self.button['gain'].setEnabled(False)
|
|
if self.button['gain'].isChecked():
|
|
self.button['vswr'].setChecked(True)
|
|
else:
|
|
self.button['gain'].setEnabled(True)
|
|
|
|
if self.button['vswr'].isChecked():
|
|
suffix = ""
|
|
data = [d.vswr for d in s11]
|
|
elif self.button['resistance'].isChecked():
|
|
suffix = " \N{OHM SIGN}"
|
|
data = [d.impedance().real for d in s11]
|
|
elif self.button['reactance'].isChecked():
|
|
suffix = " \N{OHM SIGN}"
|
|
data = [d.impedance().imag for d in s11]
|
|
elif self.button['gain'].isChecked():
|
|
suffix = " dB"
|
|
data = [d.gain for d in s21]
|
|
else:
|
|
logger.warning("Searching for peaks on unknown data")
|
|
return
|
|
|
|
if self.button['peak_h'].isChecked():
|
|
idx_peak = np.argmax(data)
|
|
elif self.button['peak_l'].isChecked():
|
|
idx_peak = np.argmin(data)
|
|
else:
|
|
# Both is not yet in
|
|
logger.warning(
|
|
"Searching for peaks,"
|
|
" but neither looking at positive nor negative?")
|
|
return
|
|
|
|
self.label['peak_freq'].setText(
|
|
format_frequency(s11[idx_peak].freq))
|
|
self.label['peak_db'].setText(f"{round(data[idx_peak], 3)}{suffix}")
|
|
|
|
if self.button['move_marker'].isChecked() and self.app.markers:
|
|
self.app.markers[0].setFrequency(f"{s11[idx_peak].freq}")
|