2020-06-15 07:42:16 +00:00
|
|
|
# NanoVNASaver
|
2020-06-25 17:52:30 +00:00
|
|
|
#
|
2020-06-15 07:42:16 +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-11-01 11:48:03 +00:00
|
|
|
import numpy as np
|
2023-03-08 08:40:39 +00:00
|
|
|
|
2022-10-08 07:25:42 +00:00
|
|
|
# pylint: disable=import-error, no-name-in-module
|
2022-10-06 16:15:59 +00:00
|
|
|
from scipy.signal import find_peaks, peak_prominences
|
2019-10-04 10:51:20 +00:00
|
|
|
|
2022-09-20 17:52:34 +00:00
|
|
|
from NanoVNASaver.Analysis.Base import QHLine
|
|
|
|
from NanoVNASaver.Analysis.SimplePeakSearchAnalysis import (
|
2023-03-08 08:40:39 +00:00
|
|
|
SimplePeakSearchAnalysis,
|
|
|
|
)
|
2022-09-20 17:52:34 +00:00
|
|
|
|
2020-12-23 19:56:16 +00:00
|
|
|
from NanoVNASaver.Formatting import format_frequency_short
|
2020-06-15 09:28:38 +00:00
|
|
|
|
|
|
|
|
2019-10-04 10:51:20 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-09-20 17:52:34 +00:00
|
|
|
class PeakSearchAnalysis(SimplePeakSearchAnalysis):
|
2019-11-01 11:48:03 +00:00
|
|
|
def __init__(self, app):
|
|
|
|
super().__init__(app)
|
|
|
|
|
2022-09-20 17:52:34 +00:00
|
|
|
self.peak_cnt = QtWidgets.QSpinBox()
|
|
|
|
self.peak_cnt.setValue(1)
|
|
|
|
self.peak_cnt.setMinimum(1)
|
|
|
|
self.peak_cnt.setMaximum(10)
|
|
|
|
|
|
|
|
self.layout.addRow("Max number of peaks", self.peak_cnt)
|
|
|
|
self.layout.addRow(QHLine())
|
2020-12-23 19:56:16 +00:00
|
|
|
self.layout.addRow(QtWidgets.QLabel("<b>Results</b>"))
|
|
|
|
self.results_header = self.layout.rowCount()
|
2019-11-01 11:48:03 +00:00
|
|
|
|
2023-03-08 08:40:39 +00:00
|
|
|
self.set_titel("Peak search")
|
2022-09-20 17:52:34 +00:00
|
|
|
|
2019-11-01 11:48:03 +00:00
|
|
|
def runAnalysis(self):
|
2022-09-20 17:52:34 +00:00
|
|
|
if not self.app.data.s11:
|
|
|
|
return
|
2020-12-20 14:41:38 +00:00
|
|
|
self.reset()
|
2022-09-20 17:52:34 +00:00
|
|
|
|
|
|
|
s11 = self.app.data.s11
|
2022-09-21 15:45:08 +00:00
|
|
|
data, fmt_fnc = self.data_and_format()
|
2022-09-20 17:52:34 +00:00
|
|
|
|
2022-09-21 15:45:08 +00:00
|
|
|
inverted = False
|
2023-03-08 08:40:39 +00:00
|
|
|
if self.button["peak_l"].isChecked():
|
2022-09-21 15:45:08 +00:00
|
|
|
inverted = True
|
2022-10-06 16:15:59 +00:00
|
|
|
peaks, _ = find_peaks(
|
2023-03-08 08:40:39 +00:00
|
|
|
-np.array(data), width=3, distance=3, prominence=1
|
|
|
|
)
|
2022-09-21 15:45:08 +00:00
|
|
|
else:
|
2023-03-08 08:40:39 +00:00
|
|
|
self.button["peak_h"].setChecked(True)
|
|
|
|
peaks, _ = find_peaks(data, width=3, distance=3, prominence=1)
|
2019-11-01 11:48:03 +00:00
|
|
|
|
|
|
|
# Having found the peaks, get the prominence data
|
2020-12-20 14:41:38 +00:00
|
|
|
for i, p in np.ndenumerate(peaks):
|
2023-03-14 18:22:46 +00:00
|
|
|
logger.debug("Peak %s at %s", i, p)
|
2022-10-06 16:15:59 +00:00
|
|
|
prominences = peak_prominences(data, peaks)[0]
|
2019-11-01 11:48:03 +00:00
|
|
|
logger.debug("%d prominences", len(prominences))
|
|
|
|
|
|
|
|
# Find the peaks with the most extreme values
|
|
|
|
# Alternately, allow the user to select "most prominent"?
|
2022-09-21 15:45:08 +00:00
|
|
|
count = self.peak_cnt.value()
|
|
|
|
if count > len(prominences):
|
|
|
|
count = len(prominences)
|
|
|
|
self.peak_cnt.setValue(count)
|
|
|
|
|
2019-11-01 11:48:03 +00:00
|
|
|
indices = np.argpartition(prominences, -count)[-count:]
|
|
|
|
logger.debug("%d indices", len(indices))
|
|
|
|
for i in indices:
|
2022-09-21 15:45:08 +00:00
|
|
|
pos = peaks[i]
|
2020-12-23 19:56:16 +00:00
|
|
|
self.layout.addRow(
|
2022-09-21 15:45:08 +00:00
|
|
|
f"Freq: {format_frequency_short(s11[pos].freq)}",
|
|
|
|
QtWidgets.QLabel(
|
|
|
|
f" Value: {fmt_fnc(-data[pos] if inverted else data[pos])}"
|
2023-03-08 08:40:39 +00:00
|
|
|
),
|
|
|
|
)
|
2019-11-01 11:48:03 +00:00
|
|
|
|
2023-03-08 08:40:39 +00:00
|
|
|
if self.button["move_marker"].isChecked():
|
2019-11-01 11:48:03 +00:00
|
|
|
if count > len(self.app.markers):
|
|
|
|
logger.warning("More peaks found than there are markers")
|
|
|
|
for i in range(min(count, len(self.app.markers))):
|
2020-06-15 09:28:38 +00:00
|
|
|
self.app.markers[i].setFrequency(
|
2023-03-08 08:40:39 +00:00
|
|
|
str(s11[peaks[indices[i]]].freq)
|
|
|
|
)
|
2019-11-01 11:48:03 +00:00
|
|
|
|
|
|
|
def reset(self):
|
2022-09-21 15:45:08 +00:00
|
|
|
super().reset()
|
2023-03-08 08:40:39 +00:00
|
|
|
logger.debug(
|
|
|
|
"Results start at %d, out of %d",
|
|
|
|
self.results_header,
|
|
|
|
self.layout.rowCount(),
|
|
|
|
)
|
2021-06-26 22:34:06 +00:00
|
|
|
for _ in range(self.results_header, self.layout.rowCount()):
|
2020-12-23 19:56:16 +00:00
|
|
|
logger.debug("deleting %s", self.layout.rowCount())
|
|
|
|
self.layout.removeRow(self.layout.rowCount() - 1)
|