Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to modify number of samples used for averaging #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions qspectrumanalyzer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from qspectrumanalyzer.utils import str_to_color, human_time

from qspectrumanalyzer.settings import QSpectrumAnalyzerSettings
from qspectrumanalyzer.average import QSpectrumAnalyzerAverage
from qspectrumanalyzer.smoothing import QSpectrumAnalyzerSmoothing
from qspectrumanalyzer.persistence import QSpectrumAnalyzerPersistence
from qspectrumanalyzer.colors import QSpectrumAnalyzerColors
Expand Down Expand Up @@ -445,6 +446,15 @@ def on_baselineButton_clicked(self):
settings.value("baseline_file", None)
)

@QtCore.Slot()
def on_averageButton_clicked(self):
dialog = QSpectrumAnalyzerAverage(self)
if dialog.exec_():
settings = QtCore.QSettings()
self.data_storage.set_average(
settings.value("average_samples", 0, int)
)

@QtCore.Slot()
def on_smoothButton_clicked(self):
dialog = QSpectrumAnalyzerSmoothing(self)
Expand Down
21 changes: 21 additions & 0 deletions qspectrumanalyzer/average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from Qt import QtCore, QtWidgets

from qspectrumanalyzer.ui_qspectrumanalyzer_average import Ui_QSpectrumAnalyzerAverage


class QSpectrumAnalyzerAverage(QtWidgets.QDialog, Ui_QSpectrumAnalyzerAverage):
"""QSpectrumAnalyzer spectrum smoothing dialog"""
def __init__(self, parent=None):
# Initialize UI
super().__init__(parent)
self.setupUi(self)

# Load settings
settings = QtCore.QSettings()
self.averageSamplesSpinBox.setValue(settings.value("average_samples", 0, int))

def accept(self):
"""Save settings when dialog is accepted"""
settings = QtCore.QSettings()
settings.setValue("average_samples", self.averageSamplesSpinBox.value())
QtWidgets.QDialog.accept(self)
8 changes: 7 additions & 1 deletion qspectrumanalyzer/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, max_history_size=100, parent=None):
self.prev_baseline = None
self.baseline = None
self.baseline_x = None
self.average_samples = 0

# Use only one worker thread because it is not faster
# with more threads (and memory consumption is much higher)
Expand Down Expand Up @@ -154,7 +155,8 @@ def update_average(self, data):
if self.average is None:
self.average = data["y"].copy()
else:
self.average = np.average((self.average, data["y"]), axis=0, weights=(self.average_counter - 1, 1))
weight = self.average_samples if self.average_samples > 0 else self.average_counter - 1
self.average = np.average((self.average, data["y"]), axis=0, weights=(weight, 1))
self.average_updated.emit(self)

def update_peak_hold_max(self, data):
Expand All @@ -177,6 +179,10 @@ def smooth_data(self, y):
"""Apply smoothing function to data"""
return smooth(y, window_len=self.smooth_length, window=self.smooth_window)

def set_average(self, average=0):
"""Set average params"""
self.average_samples = average

def set_smooth(self, toggle, length=11, window="hanning"):
"""Toggle smoothing and set smoothing params"""
if toggle != self.smooth or length != self.smooth_length or window != self.smooth_window:
Expand Down
9 changes: 8 additions & 1 deletion qspectrumanalyzer/qspectrumanalyzer.ui
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<x>0</x>
<y>0</y>
<width>1200</width>
<height>32</height>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
Expand Down Expand Up @@ -525,6 +525,13 @@
</property>
</widget>
</item>
<item row="6" column="2">
<widget class="QToolButton" name="averageButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
Expand Down
108 changes: 108 additions & 0 deletions qspectrumanalyzer/qspectrumanalyzer_average.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QSpectrumAnalyzerAverage</class>
<widget class="QDialog" name="QSpectrumAnalyzerAverage">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>530</width>
<height>130</height>
</rect>
</property>
<property name="windowTitle">
<string>Average - QSpectrumAnalyzer</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Number of samples:</string>
</property>
<property name="buddy">
<cstring>averageSamplesSpinBox</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="averageSamplesSpinBox">
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>5000</number>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>1</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>averageSamplesSpinBox</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>QSpectrumAnalyzerAverage</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>218</x>
<y>104</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>129</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>QSpectrumAnalyzerAverage</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>218</x>
<y>110</y>
</hint>
<hint type="destinationlabel">
<x>224</x>
<y>129</y>
</hint>
</hints>
</connection>
</connections>
</ui>
8 changes: 6 additions & 2 deletions qspectrumanalyzer/ui_qspectrumanalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'qspectrumanalyzer/qspectrumanalyzer.ui'
#
# Created by: PyQt5 UI code generator 5.8
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

Expand Down Expand Up @@ -41,7 +41,7 @@ def setupUi(self, QSpectrumAnalyzerMainWindow):
self.horizontalLayout.addWidget(self.plotSplitter)
QSpectrumAnalyzerMainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(QSpectrumAnalyzerMainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1200, 32))
self.menubar.setGeometry(QtCore.QRect(0, 0, 1200, 22))
self.menubar.setObjectName("menubar")
self.menu_File = QtWidgets.QMenu(self.menubar)
self.menu_File.setObjectName("menu_File")
Expand Down Expand Up @@ -240,6 +240,9 @@ def setupUi(self, QSpectrumAnalyzerMainWindow):
self.subtractBaselineCheckBox = QtWidgets.QCheckBox(self.settingsDockWidgetContents)
self.subtractBaselineCheckBox.setObjectName("subtractBaselineCheckBox")
self.gridLayout.addWidget(self.subtractBaselineCheckBox, 10, 0, 1, 1)
self.averageButton = QtWidgets.QToolButton(self.settingsDockWidgetContents)
self.averageButton.setObjectName("averageButton")
self.gridLayout.addWidget(self.averageButton, 6, 2, 1, 1)
self.settingsDockWidget.setWidget(self.settingsDockWidgetContents)
QSpectrumAnalyzerMainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(2), self.settingsDockWidget)
self.levelsDockWidget = QtWidgets.QDockWidget(QSpectrumAnalyzerMainWindow)
Expand Down Expand Up @@ -346,6 +349,7 @@ def retranslateUi(self, QSpectrumAnalyzerMainWindow):
self.baselineCheckBox.setText(_translate("QSpectrumAnalyzerMainWindow", "Baseline"))
self.baselineButton.setText(_translate("QSpectrumAnalyzerMainWindow", "..."))
self.subtractBaselineCheckBox.setText(_translate("QSpectrumAnalyzerMainWindow", "Subtract baseline"))
self.averageButton.setText(_translate("QSpectrumAnalyzerMainWindow", "..."))
self.levelsDockWidget.setWindowTitle(_translate("QSpectrumAnalyzerMainWindow", "Levels"))
self.action_Settings.setText(_translate("QSpectrumAnalyzerMainWindow", "&Settings..."))
self.action_Quit.setText(_translate("QSpectrumAnalyzerMainWindow", "&Quit"))
Expand Down
48 changes: 48 additions & 0 deletions qspectrumanalyzer/ui_qspectrumanalyzer_average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'qspectrumanalyzer/qspectrumanalyzer_average.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from Qt import QtCore, QtGui, QtWidgets

class Ui_QSpectrumAnalyzerAverage(object):
def setupUi(self, QSpectrumAnalyzerAverage):
QSpectrumAnalyzerAverage.setObjectName("QSpectrumAnalyzerAverage")
QSpectrumAnalyzerAverage.resize(530, 130)
self.verticalLayout = QtWidgets.QVBoxLayout(QSpectrumAnalyzerAverage)
self.verticalLayout.setObjectName("verticalLayout")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.label = QtWidgets.QLabel(QSpectrumAnalyzerAverage)
self.label.setObjectName("label")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
self.averageSamplesSpinBox = QtWidgets.QSpinBox(QSpectrumAnalyzerAverage)
self.averageSamplesSpinBox.setMinimum(0)
self.averageSamplesSpinBox.setMaximum(5000)
self.averageSamplesSpinBox.setProperty("value", 0)
self.averageSamplesSpinBox.setObjectName("averageSamplesSpinBox")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.averageSamplesSpinBox)
self.verticalLayout.addLayout(self.formLayout)
spacerItem = QtWidgets.QSpacerItem(20, 1, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.buttonBox = QtWidgets.QDialogButtonBox(QSpectrumAnalyzerAverage)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.verticalLayout.addWidget(self.buttonBox)
self.label.setBuddy(self.averageSamplesSpinBox)

self.retranslateUi(QSpectrumAnalyzerAverage)
self.buttonBox.accepted.connect(QSpectrumAnalyzerAverage.accept)
self.buttonBox.rejected.connect(QSpectrumAnalyzerAverage.reject)
QtCore.QMetaObject.connectSlotsByName(QSpectrumAnalyzerAverage)
QSpectrumAnalyzerAverage.setTabOrder(self.averageSamplesSpinBox, self.buttonBox)

def retranslateUi(self, QSpectrumAnalyzerAverage):
_translate = QtCore.QCoreApplication.translate
QSpectrumAnalyzerAverage.setWindowTitle(_translate("QSpectrumAnalyzerAverage", "Average - QSpectrumAnalyzer"))
self.label.setText(_translate("QSpectrumAnalyzerAverage", "Number of samples:"))