class Input_Info(QWidget): """ Create widget for displaying infos about filter specs and filter design method """ sig_rx = pyqtSignal(object) # incoming signals from input_tab_widgets sig_tx = pyqtSignal(object) from pyfda.libs.pyfda_qt_lib import emit def __init__(self, parent=None): super(Input_Info, self).__init__(parent) self.tab_label = 'Info' self.tool_tip = ( "<span>Display the achieved filter specifications" " and more info about the filter design algorithm.</span>") self._construct_UI() self.load_dict() def process_sig_rx(self, dict_sig=None): """ Process signals coming from sig_rx """ # logger.debug("Processing {0}: {1}".format(type(dict_sig).__name__, dict_sig)) if 'data_changed' in dict_sig or 'view_changed' in dict_sig\ or 'specs_changed' in dict_sig: self.load_dict() def _construct_UI(self): """ Intitialize the widget, consisting of: - Checkboxes for selecting the info to be displayed - A large text window for displaying infos about the filter design algorithm """ bfont = QFont() bfont.setBold(True) # ============== UI Layout ===================================== # widget / subwindow for filter infos # self.butFiltPerf = QToolButton("H(f)", self) self.butFiltPerf = QPushButton(self) self.butFiltPerf.setText("H(f)") self.butFiltPerf.setCheckable(True) self.butFiltPerf.setChecked(True) self.butFiltPerf.setToolTip("Display frequency response at test frequencies.") self.butDebug = QPushButton(self) self.butDebug.setText("Debug") self.butDebug.setCheckable(True) self.butDebug.setChecked(False) self.butDebug.setToolTip("Show debugging options.") self.butAbout = QPushButton("About", self) # pop-up "About" window self.butSettings = QPushButton("Settings", self) # self.butSettings.setCheckable(True) self.butSettings.setChecked(False) self.butSettings.setToolTip("Display and set some settings") layHControls1 = QHBoxLayout() layHControls1.addWidget(self.butFiltPerf) layHControls1.addWidget(self.butAbout) layHControls1.addWidget(self.butSettings) layHControls1.addWidget(self.butDebug) self.butDocstring = QPushButton("Doc$", self) self.butDocstring.setCheckable(True) self.butDocstring.setChecked(False) self.butDocstring.setToolTip("Display docstring from python filter method.") self.butRichText = QPushButton("RTF", self) self.butRichText.setCheckable(HAS_DOCUTILS) self.butRichText.setChecked(HAS_DOCUTILS) self.butRichText.setEnabled(HAS_DOCUTILS) self.butRichText.setToolTip("Render documentation in Rich Text Format.") self.butFiltDict = QPushButton("FiltDict", self) self.butFiltDict.setToolTip("Show filter dictionary for debugging.") self.butFiltDict.setCheckable(True) self.butFiltDict.setChecked(False) self.butFiltTree = QPushButton("FiltTree", self) self.butFiltTree.setToolTip("Show filter tree for debugging.") self.butFiltTree.setCheckable(True) self.butFiltTree.setChecked(False) layHControls2 = QHBoxLayout() layHControls2.addWidget(self.butDocstring) # layHControls2.addStretch(1) layHControls2.addWidget(self.butRichText) # layHControls2.addStretch(1) layHControls2.addWidget(self.butFiltDict) # layHControls2.addStretch(1) layHControls2.addWidget(self.butFiltTree) self.frmControls2 = QFrame(self) self.frmControls2.setLayout(layHControls2) self.frmControls2.setVisible(self.butDebug.isChecked()) self.frmControls2.setContentsMargins(0, 0, 0, 0) lbl_settings_NFFT = QLabel(to_html("N_FFT =", frmt='bi'), self) self.led_settings_NFFT = QLineEdit(self) self.led_settings_NFFT.setText(str(params['N_FFT'])) self.led_settings_NFFT.setToolTip("<span>Number of FFT points for frequency " "domain widgets.</span>") layGSettings = QGridLayout() layGSettings.addWidget(lbl_settings_NFFT, 1, 0) layGSettings.addWidget(self.led_settings_NFFT, 1, 1) self.frmSettings = QFrame(self) self.frmSettings.setLayout(layGSettings) self.frmSettings.setVisible(self.butSettings.isChecked()) self.frmSettings.setContentsMargins(0, 0, 0, 0) layVControls = QVBoxLayout() layVControls.addLayout(layHControls1) layVControls.addWidget(self.frmControls2) layVControls.addWidget(self.frmSettings) self.frmMain = QFrame(self) self.frmMain.setLayout(layVControls) self.tblFiltPerf = QTableWidget(self) self.tblFiltPerf.setAlternatingRowColors(True) # self.tblFiltPerf.verticalHeader().setVisible(False) self.tblFiltPerf.horizontalHeader().setHighlightSections(False) self.tblFiltPerf.horizontalHeader().setFont(bfont) self.tblFiltPerf.verticalHeader().setHighlightSections(False) self.tblFiltPerf.verticalHeader().setFont(bfont) self.txtFiltInfoBox = QTextBrowser(self) self.txtFiltDict = QTextBrowser(self) self.txtFiltTree = QTextBrowser(self) layVMain = QVBoxLayout() layVMain.addWidget(self.frmMain) # layVMain.addLayout(self.layHControls) splitter = QSplitter(self) splitter.setOrientation(Qt.Vertical) splitter.addWidget(self.tblFiltPerf) splitter.addWidget(self.txtFiltInfoBox) splitter.addWidget(self.txtFiltDict) splitter.addWidget(self.txtFiltTree) # setSizes uses absolute pixel values, but can be "misused" by specifying values # that are way too large: in this case, the space is distributed according # to the _ratio_ of the values: splitter.setSizes([3000, 10000, 1000, 1000]) layVMain.addWidget(splitter) layVMain.setContentsMargins(*params['wdg_margins']) self.setLayout(layVMain) # ---------------------------------------------------------------------- # GLOBAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.sig_rx.connect(self.process_sig_rx) # ---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.butFiltPerf.clicked.connect(self._show_filt_perf) self.butAbout.clicked.connect(self._about_window) self.butSettings.clicked.connect(self._show_settings) self.led_settings_NFFT.editingFinished.connect(self._update_settings_nfft) self.butDebug.clicked.connect(self._show_debug) self.butFiltDict.clicked.connect(self._show_filt_dict) self.butFiltTree.clicked.connect(self._show_filt_tree) self.butDocstring.clicked.connect(self._show_doc) self.butRichText.clicked.connect(self._show_doc) def _about_window(self): self.about_widget = AboutWindow(self) # important: Handle must be class attribute # self.opt_widget.show() # modeless dialog, i.e. non-blocking self.about_widget.exec_() # modal dialog (blocking) # ------------------------------------------------------------------------------ def _show_debug(self): """ Show / hide debug options depending on the state of the debug button """ self.frmControls2.setVisible(self.butDebug.isChecked()) # ------------------------------------------------------------------------------ def _show_settings(self): """ Show / hide settings options depending on the state of the settings button """ self.frmSettings.setVisible(self.butSettings.isChecked()) def _update_settings_nfft(self): """ Update value for self.par1 from QLineEdit Widget""" params['N_FFT'] = safe_eval(self.led_settings_NFFT.text(), params['N_FFT'], sign='pos', return_type='int') self.led_settings_NFFT.setText(str(params['N_FFT'])) self.emit({'data_changed': 'n_fft'}) # ------------------------------------------------------------------------------ def load_dict(self): """ update docs and filter performance """ self._show_doc() self._show_filt_perf() self._show_filt_dict() self._show_filt_tree() # ------------------------------------------------------------------------------ def _show_doc(self): """ Display info from filter design file and docstring """ if hasattr(ff.fil_inst, 'info'): if self.butRichText.isChecked(): self.txtFiltInfoBox.setText(publish_string( self._clean_doc(ff.fil_inst.info), writer_name='html', settings_overrides={'output_encoding': 'unicode'})) else: self.txtFiltInfoBox.setText(textwrap.dedent(ff.fil_inst.info)) else: self.txtFiltInfoBox.setText("") if self.butDocstring.isChecked() and hasattr(ff.fil_inst, 'info_doc'): if self.butRichText.isChecked(): self.txtFiltInfoBox.append( '<hr /><b>Python module docstring:</b>\n') for doc in ff.fil_inst.info_doc: self.txtFiltInfoBox.append(publish_string( self._clean_doc(doc), writer_name='html', settings_overrides={'output_encoding': 'unicode'})) else: self.txtFiltInfoBox.append('\nPython module docstring:\n') for doc in ff.fil_inst.info_doc: self.txtFiltInfoBox.append(self._clean_doc(doc)) self.txtFiltInfoBox.moveCursor(QTextCursor.Start) def _clean_doc(self, doc): """ Remove uniform number of leading blanks from docstrings for subsequent processing of rich text. The first line is treated differently, _all_ leading blanks are removed (if any). This allows for different formats of docstrings. """ lines = doc.splitlines() result = lines[0].lstrip() + "\n" + textwrap.dedent("\n".join(lines[1:])) return result # ------------------------------------------------------------------------------ def _show_filt_perf(self): """ Print filter properties in a table at frequencies of interest. When specs are violated, colour the table entry in red. """ antiC = False def _find_min_max(self, f_start, f_stop, unit='dB'): """ Find minimum and maximum magnitude and the corresponding frequencies for the filter defined in the filter dict in a given frequency band [f_start, f_stop]. """ w = np.linspace(f_start, f_stop, params['N_FFT'])*2*np.pi [w, H] = sig.freqz(bb, aa, worN=w) # add antiCausals if we have them if (antiC): # # Evaluate transfer function of anticausal half on the same freq grid. # wa, ha = sig.freqz(bbA, aaA, worN=w) ha = ha.conjugate() # # Total transfer function is the product # H = H*ha f = w / (2.0 * pi) # frequency normalized to f_S H_abs = abs(H) H_max = max(H_abs) H_min = min(H_abs) F_max = f[np.argmax(H_abs)] # find the frequency where H_abs F_min = f[np.argmin(H_abs)] # becomes max resp. min if unit == 'dB': H_max = 20*log10(H_max) H_min = 20*log10(H_min) return F_min, H_min, F_max, H_max # ------------------------------------------------------------------ self.tblFiltPerf.setVisible(self.butFiltPerf.isChecked()) if self.butFiltPerf.isChecked(): bb = fb.fil[0]['ba'][0] aa = fb.fil[0]['ba'][1] # 'rpk' means nonCausal filter if 'rpk' in fb.fil[0]: antiC = True bbA = fb.fil[0]['baA'][0] aaA = fb.fil[0]['baA'][1] bbA = bbA.conjugate() aaA = aaA.conjugate() f_S = fb.fil[0]['f_S'] f_lbls = [] f_vals = [] a_lbls = [] a_targs = [] a_targs_dB = [] a_test = [] ft = fb.fil[0]['ft'] # get filter type ('IIR', 'FIR') unit = fb.fil[0]['amp_specs_unit'] unit = 'dB' # fix this for the moment # construct pairs of corner frequency and corresponding amplitude # labels in ascending frequency for each response type if fb.fil[0]['rt'] in {'LP', 'HP', 'BP', 'BS', 'HIL'}: if fb.fil[0]['rt'] == 'LP': f_lbls = ['F_PB', 'F_SB'] a_lbls = ['A_PB', 'A_SB'] elif fb.fil[0]['rt'] == 'HP': f_lbls = ['F_SB', 'F_PB'] a_lbls = ['A_SB', 'A_PB'] elif fb.fil[0]['rt'] == 'BP': f_lbls = ['F_SB', 'F_PB', 'F_PB2', 'F_SB2'] a_lbls = ['A_SB', 'A_PB', 'A_PB', 'A_SB2'] elif fb.fil[0]['rt'] == 'BS': f_lbls = ['F_PB', 'F_SB', 'F_SB2', 'F_PB2'] a_lbls = ['A_PB', 'A_SB', 'A_SB', 'A_PB2'] elif fb.fil[0]['rt'] == 'HIL': f_lbls = ['F_PB', 'F_PB2'] a_lbls = ['A_PB', 'A_PB'] # Try to get lists of frequency / amplitude specs from the filter dict # that correspond to the f_lbls / a_lbls pairs defined above # When one of the labels doesn't exist in the filter dict, delete # all corresponding amplitude and frequency entries. err = [False] * len(f_lbls) # initialize error list f_vals = [] a_targs = [] for i in range(len(f_lbls)): try: f = fb.fil[0][f_lbls[i]] f_vals.append(f) except KeyError as e: f_vals.append('') err[i] = True logger.debug(e) try: a = fb.fil[0][a_lbls[i]] a_dB = lin2unit(fb.fil[0][a_lbls[i]], ft, a_lbls[i], unit) a_targs.append(a) a_targs_dB.append(a_dB) except KeyError as e: a_targs.append('') a_targs_dB.append('') err[i] = True logger.debug(e) for i in range(len(f_lbls)): if err[i]: del f_lbls[i] del f_vals[i] del a_lbls[i] del a_targs[i] del a_targs_dB[i] f_vals = np.asarray(f_vals) # convert to numpy array logger.debug("F_test_labels = %s" % f_lbls) # Calculate frequency response at test frequencies [w_test, a_test] = sig.freqz(bb, aa, 2.0 * pi * f_vals.astype(float)) # add antiCausals if we have them if (antiC): wa, ha = sig.freqz(bbA, aaA, 2.0 * pi * f_vals.astype(float)) ha = ha.conjugate() a_test = a_test*ha (F_min, H_min, F_max, H_max) = _find_min_max(self, 0, 1, unit='V') # append frequencies and values for min. and max. filter reponse to # test vector f_lbls += ['Min.', 'Max.'] # QTableView does not support direct formatting, use QLabel f_vals = np.append(f_vals, [F_min, F_max]) a_targs = np.append(a_targs, [np.nan, np.nan]) a_targs_dB = np.append(a_targs_dB, [np.nan, np.nan]) a_test = np.append(a_test, [H_min, H_max]) # calculate response of test frequencies in dB a_test_dB = -20*log10(abs(a_test)) # get filter type ('IIR', 'FIR') for dB <-> lin conversion ft = fb.fil[0]['ft'] # unit = fb.fil[0]['amp_specs_unit'] unit = 'dB' # make this fixed for the moment # build a list with the corresponding target specs: a_targs_pass = [] eps = 1e-3 for i in range(len(f_lbls)): if 'PB' in f_lbls[i]: a_targs_pass.append((a_test_dB[i] - a_targs_dB[i]) < eps) a_test[i] = 1 - abs(a_test[i]) elif 'SB' in f_lbls[i]: a_targs_pass.append(a_test_dB[i] >= a_targs_dB[i]) else: a_targs_pass.append(True) self.targs_spec_passed = np.all(a_targs_pass) logger.debug( "H_targ = {0}\n" "H_test = {1}\n" "H_test_dB = {2}\n" "F_test = {3}\n" "H_targ_pass = {4}\n" "passed: {5}\n".format(a_targs, a_test, a_test_dB, f_vals, a_targs_pass, self.targs_spec_passed)) self.tblFiltPerf.setRowCount(len(a_test)) # number of table rows self.tblFiltPerf.setColumnCount(5) # number of table columns self.tblFiltPerf.setHorizontalHeaderLabels([ 'f/{0:s}'.format(fb.fil[0]['freq_specs_unit']), 'Spec\n(dB)', '|H(f)|\n(dB)', 'Spec', '|H(f)|']) self.tblFiltPerf.setVerticalHeaderLabels(f_lbls) for row in range(len(a_test)): self.tblFiltPerf.setItem( row, 0, QTableWidgetItem(str('{0:.4g}'.format(f_vals[row]*f_S)))) self.tblFiltPerf.setItem( row, 1, QTableWidgetItem(str('%2.3g'%(-a_targs_dB[row])))) self.tblFiltPerf.setItem( row, 2, QTableWidgetItem(str('%2.3f'%(-a_test_dB[row])))) if a_targs[row] < 0.01: self.tblFiltPerf.setItem( row, 3, QTableWidgetItem(str('%.3e'%(a_targs[row])))) else: self.tblFiltPerf.setItem( row, 3, QTableWidgetItem(str('%2.4f'%(a_targs[row])))) if a_test[row] < 0.01: self.tblFiltPerf.setItem( row, 4, QTableWidgetItem(str('%.3e'%(abs(a_test[row]))))) else: self.tblFiltPerf.setItem( row, 4, QTableWidgetItem(str('%.4f'%(abs(a_test[row]))))) if not a_targs_pass[row]: self.tblFiltPerf.item(row, 1).setBackground(QtGui.QColor('red')) self.tblFiltPerf.item(row, 3).setBackground(QtGui.QColor('red')) self.tblFiltPerf.resizeColumnsToContents() self.tblFiltPerf.resizeRowsToContents() # ------------------------------------------------------------------------------ def _show_filt_dict(self): """ Print filter dict for debugging """ self.txtFiltDict.setVisible(self.butFiltDict.isChecked()) fb_sorted = [str(key) + ' : ' + str(fb.fil[0][key]) for key in sorted(fb.fil[0].keys())] dictstr = pprint.pformat(fb_sorted) # dictstr = pprint.pformat(fb.fil[0]) self.txtFiltDict.setText(dictstr) # ------------------------------------------------------------------------------ def _show_filt_tree(self): """ Print filter tree for debugging """ self.txtFiltTree.setVisible(self.butFiltTree.isChecked()) ftree_sorted = ['<b>' + str(key) + ' : ' + '</b>' + str(fb.fil_tree[key]) for key in sorted(fb.fil_tree.keys())] dictstr = pprint.pformat(ftree_sorted, indent=4) # dictstr = pprint.pformat(fb.fil[0]) self.txtFiltTree.setText(dictstr)
class UI_W(QWidget): """ Widget for entering integer and fractional bits. The result can be read out via the attributes `self.WI`, `self.WF` and `self.W`. The constructor accepts a dictionary for initial widget settings. The following keys are defined; default values are used for missing keys: 'wdg_name' : 'ui_w' # widget name 'label' : 'WI.WF' # widget text label 'visible' : True # Is widget visible? 'enabled' : True # Is widget enabled? 'fractional' : True # Display WF, otherwise WF=0 'lbl_sep' : '.' # label between WI and WF field 'max_led_width' : 30 # max. length of lineedit field 'WI' : 0 # number of frac. *bits* 'WI_len' : 2 # max. number of integer *digits* 'tip_WI' : 'Number of integer bits' # Mouse-over tooltip 'WF' : 15 # number of frac. *bits* 'WF_len' : 2 # max. number of frac. *digits* 'tip_WF' : 'Number of frac. bits' # Mouse-over tooltip 'lock_visible' : False # Pushbutton for locking visible 'tip_lock' : 'Lock input/output quant.'# Tooltip for lock push button 'combo_visible' : False # Enable integrated combo widget 'combo_items' : ['auto', 'full', 'man'] # Combo selection 'tip_combo' : 'Calculate Acc. width.' # tooltip for combo """ # sig_rx = pyqtSignal(object) # incoming, sig_tx = pyqtSignal(object) # outcgoing from pyfda.libs.pyfda_qt_lib import emit def __init__(self, parent, q_dict, **kwargs): super(UI_W, self).__init__(parent) self.q_dict = q_dict # pass a dict with initial settings for construction self._construct_UI(**kwargs) self.ui2dict(s='init') # initialize the class attributes def _construct_UI(self, **kwargs): """ Construct widget from quantization dict, individual settings and the default dict below """ # default settings dict_ui = { 'wdg_name': 'ui_w', 'label': 'WI.WF', 'lbl_sep': '.', 'max_led_width': 30, 'WI': 0, 'WI_len': 2, 'tip_WI': 'Number of integer bits', 'WF': 15, 'WF_len': 2, 'tip_WF': 'Number of fractional bits', 'enabled': True, 'visible': True, 'fractional': True, 'combo_visible': False, 'combo_items': ['auto', 'full', 'man'], 'tip_combo': 'Calculate Acc. width.', 'lock_visible': False, 'tip_lock': 'Lock input/output quantization.' } #: default values if self.q_dict: dict_ui.update(self.q_dict) for k, v in kwargs.items(): if k not in dict_ui: logger.warning("Unknown key {0}".format(k)) else: dict_ui.update({k: v}) self.wdg_name = dict_ui['wdg_name'] if not dict_ui['fractional']: dict_ui['WF'] = 0 self.WI = dict_ui['WI'] self.WF = dict_ui['WF'] self.W = int(self.WI + self.WF + 1) if self.q_dict: self.q_dict.update({'WI': self.WI, 'WF': self.WF, 'W': self.W}) else: self.q_dict = {'WI': self.WI, 'WF': self.WF, 'W': self.W} lblW = QLabel(to_html(dict_ui['label'], frmt='bi'), self) self.cmbW = QComboBox(self) self.cmbW.addItems(dict_ui['combo_items']) self.cmbW.setVisible(dict_ui['combo_visible']) self.cmbW.setToolTip(dict_ui['tip_combo']) self.cmbW.setObjectName("cmbW") self.butLock = QPushButton(self) self.butLock.setCheckable(True) self.butLock.setChecked(False) self.butLock.setVisible(dict_ui['lock_visible']) self.butLock.setToolTip(dict_ui['tip_lock']) self.ledWI = QLineEdit(self) self.ledWI.setToolTip(dict_ui['tip_WI']) self.ledWI.setMaxLength(dict_ui['WI_len']) # maximum of 2 digits self.ledWI.setFixedWidth( dict_ui['max_led_width']) # width of lineedit in points self.ledWI.setObjectName("WI") lblDot = QLabel(dict_ui['lbl_sep'], self) lblDot.setVisible(dict_ui['fractional']) self.ledWF = QLineEdit(self) self.ledWF.setToolTip(dict_ui['tip_WF']) self.ledWF.setMaxLength(dict_ui['WI_len']) # maximum of 2 digits self.ledWF.setFixedWidth( dict_ui['max_led_width']) # width of lineedit in points self.ledWF.setVisible(dict_ui['fractional']) self.ledWF.setObjectName("WF") layH = QHBoxLayout() layH.addWidget(lblW) layH.addStretch() layH.addWidget(self.cmbW) layH.addWidget(self.butLock) layH.addWidget(self.ledWI) layH.addWidget(lblDot) layH.addWidget(self.ledWF) layH.setContentsMargins(0, 0, 0, 0) frmMain = QFrame(self) frmMain.setLayout(layH) layVMain = QVBoxLayout() # Widget main layout layVMain.addWidget(frmMain) layVMain.setContentsMargins(0, 5, 0, 0) # *params['wdg_margins']) self.setLayout(layVMain) # ---------------------------------------------------------------------- # INITIAL SETTINGS # ---------------------------------------------------------------------- self.ledWI.setText(qstr(dict_ui['WI'])) self.ledWF.setText(qstr(dict_ui['WF'])) frmMain.setEnabled(dict_ui['enabled']) frmMain.setVisible(dict_ui['visible']) # ---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.ledWI.editingFinished.connect(self.ui2dict) self.ledWF.editingFinished.connect(self.ui2dict) self.butLock.clicked.connect(self.butLock_clicked) self.cmbW.currentIndexChanged.connect(self.ui2dict) # initialize button icon self.butLock_clicked(self.butLock.isChecked()) def quant_coeffs(self, q_dict: dict, coeffs: iterable, to_int: bool = False) -> list: """ Quantize the coefficients, scale and convert them to integer and return them as a list of integers This is called every time one of the coefficient subwidgets is edited or changed. Parameters: ----------- q_dict: dict Dictionary with quantizer settings for coefficients coeffs: iterable a list or ndarray of coefficients to be quantized Returns: -------- A list of integer coeffcients, quantized and scaled with the settings of the passed quantization dict """ # Create coefficient quantizer instance using the passed quantization parameters # dict from `input_widgets/input_coeffs.py` (and stored in the central # filter dict) Q_coeff = fx.Fixed(q_dict) Q_coeff.frmt = 'dec' # always use decimal format for coefficients if coeffs is None: logger.error("Coeffs empty!") # quantize floating point coefficients with the selected scale (WI.WF), # next convert array float -> array of fixp # -> list of int (scaled by 2^WF) when `to_int == True` if to_int: return list(Q_coeff.float2frmt(coeffs) * (1 << Q_coeff.WF)) else: return list(Q_coeff.fixp(coeffs)) # -------------------------------------------------------------------------- def butLock_clicked(self, clicked): """ Update the icon of the push button depending on its state """ if clicked: self.butLock.setIcon(QIcon(':/lock-locked.svg')) else: self.butLock.setIcon(QIcon(':/lock-unlocked.svg')) q_icon_size = self.butLock.iconSize( ) # <- uncomment this for manual sizing self.butLock.setIconSize(q_icon_size) dict_sig = {'wdg_name': self.wdg_name, 'ui': 'butLock'} self.emit(dict_sig) # -------------------------------------------------------------------------- def ui2dict(self, s=None): """ Update the attributes `self.WI`, `self.WF` and `self.W` and `self.q_dict` when one of the QLineEdit widgets has been edited. Emit a signal with `{'ui':objectName of the sender}`. """ self.WI = int( safe_eval(self.ledWI.text(), self.WI, return_type="int", sign='poszero')) self.ledWI.setText(qstr(self.WI)) self.WF = int( safe_eval(self.ledWF.text(), self.WF, return_type="int", sign='poszero')) self.ledWF.setText(qstr(self.WF)) self.W = int(self.WI + self.WF + 1) self.q_dict.update({'WI': self.WI, 'WF': self.WF, 'W': self.W}) if self.sender(): obj_name = self.sender().objectName() logger.debug("sender: {0}".format(obj_name)) dict_sig = {'wdg_name': self.wdg_name, 'ui': obj_name} self.emit(dict_sig) elif s == 'init': logger.debug("called by __init__") else: logger.error("sender without name!") # -------------------------------------------------------------------------- def dict2ui(self, q_dict=None): """ Update the widgets `WI` and `WF` and the corresponding attributes from the dict passed as the argument """ if q_dict is None: q_dict = self.q_dict if 'WI' in q_dict: self.WI = safe_eval(q_dict['WI'], self.WI, return_type="int", sign='poszero') self.ledWI.setText(qstr(self.WI)) else: logger.warning("No key 'WI' in dict!") if 'WF' in q_dict: self.WF = safe_eval(q_dict['WF'], self.WF, return_type="int", sign='poszero') self.ledWF.setText(qstr(self.WF)) else: logger.warning("No key 'WF' in dict!") self.W = self.WF + self.WI + 1
class Input_Specs(QWidget): """ Build widget for entering all filter specs """ # class variables (shared between instances if more than one exists) sig_rx_local = pyqtSignal( object) # incoming from subwidgets -> process_sig_rx_local sig_rx = pyqtSignal(object) # incoming from subwidgets -> process_sig_rx sig_tx = pyqtSignal(object) # from process_sig_rx: propagate local signals from pyfda.libs.pyfda_qt_lib import emit def __init__(self, parent=None): super(Input_Specs, self).__init__(parent) self.tab_label = "Specs" self.tool_tip = "Enter and view filter specifications." self._construct_UI() def process_sig_rx_local(self, dict_sig=None): """ Flag signals coming in from local subwidgets with `propagate=True` before proceeding with processing in `process_sig_rx`. """ self.process_sig_rx(dict_sig, propagate=True) def process_sig_rx(self, dict_sig=None, propagate=False): """ Process signals coming in via subwidgets and sig_rx All signals terminate here unless the flag `propagate=True`. The sender name of signals coming in from local subwidgets is changed to its parent widget (`input_specs`) to prevent infinite loops. """ # logger.debug(f"SIG_RX: {pprint_log(dict_sig)}") if dict_sig['id'] == id(self): # logger.warning(f"Stopped infinite loop:\n\tPropagate = {propagate}\ # \n{pprint_log(dict_sig)}") return elif 'view_changed' in dict_sig: self.f_specs.load_dict() self.t_specs.load_dict() elif 'specs_changed' in dict_sig: self.f_specs.sort_dict_freqs() self.t_specs.f_specs.sort_dict_freqs() self.color_design_button("changed") elif 'filt_changed' in dict_sig: # Changing the filter design requires updating UI because number or # kind of input fields changes -> call update_UI self.update_UI(dict_sig) elif 'data_changed' in dict_sig: if dict_sig['data_changed'] == 'filter_loaded': """ Called when a new filter has been LOADED: Pass new filter data from the global filter dict by specifically calling SelectFilter.load_dict() """ self.sel_fil.load_dict() # update select_filter widget # Pass new filter data from the global filter dict & set button = "ok" self.load_dict() if propagate: # local signals are propagated with the name of this widget, # global signals terminate here dict_sig.update({'class': self.__class__.__name__}) self.emit(dict_sig) def _construct_UI(self): """ Construct User Interface from all input subwidgets """ self.butLoadFilt = QPushButton("LOAD FILTER", self) self.butLoadFilt.setToolTip("Load filter from disk") self.butSaveFilt = QPushButton("SAVE FILTER", self) self.butSaveFilt.setToolTip("Save filter todisk") layHButtons1 = QHBoxLayout() layHButtons1.addWidget(self.butLoadFilt) # <Load Filter> button layHButtons1.addWidget(self.butSaveFilt) # <Save Filter> button layHButtons1.setContentsMargins(*params['wdg_margins_spc']) self.butDesignFilt = QPushButton("DESIGN FILTER", self) self.butDesignFilt.setToolTip("Design filter with chosen specs") self.butQuit = QPushButton("Quit", self) self.butQuit.setToolTip("Exit pyfda tool") layHButtons2 = QHBoxLayout() layHButtons2.addWidget(self.butDesignFilt) # <Design Filter> button layHButtons2.addWidget(self.butQuit) # <Quit> button layHButtons2.setContentsMargins(*params['wdg_margins']) # Subwidget for selecting filter with response type rt (LP, ...), # filter type ft (IIR, ...) and filter class fc (cheby1, ...) self.sel_fil = select_filter.SelectFilter(self) self.sel_fil.setObjectName("select_filter") self.sel_fil.sig_tx.connect(self.sig_rx_local) # Subwidget for selecting the frequency unit and range self.f_units = freq_units.FreqUnits(self) self.f_units.setObjectName("freq_units") self.f_units.sig_tx.connect(self.sig_rx_local) # Changing the frequency unit requires re-display of frequency specs # but it does not influence the actual specs (no specsChanged ) # Activating the "Sort" button emits 'view_changed'?specs_changed'?, requiring # sorting and storing the frequency entries # Changing filter parameters / specs requires reloading of parameters # in other hierarchy levels, e.g. in the plot tabs # Subwidget for Frequency Specs self.f_specs = freq_specs.FreqSpecs(self) self.f_specs.setObjectName("freq_specs") self.f_specs.sig_tx.connect(self.sig_rx_local) self.sig_tx.connect(self.f_specs.sig_rx) # Subwidget for Amplitude Specs self.a_specs = amplitude_specs.AmplitudeSpecs(self) self.a_specs.setObjectName("amplitude_specs") self.a_specs.sig_tx.connect(self.sig_rx_local) # Subwidget for Weight Specs self.w_specs = weight_specs.WeightSpecs(self) self.w_specs.setObjectName("weight_specs") self.w_specs.sig_tx.connect(self.sig_rx_local) # Subwidget for target specs (frequency and amplitude) self.t_specs = target_specs.TargetSpecs(self, title="Target Specifications") self.t_specs.setObjectName("target_specs") self.t_specs.sig_tx.connect(self.sig_rx_local) self.sig_tx.connect(self.t_specs.sig_rx) # Subwidget for displaying infos on the design method self.lblMsg = QLabel(self) self.lblMsg.setWordWrap(True) layVMsg = QVBoxLayout() layVMsg.addWidget(self.lblMsg) self.frmMsg = QFrame(self) self.frmMsg.setLayout(layVMsg) layVFrm = QVBoxLayout() layVFrm.addWidget(self.frmMsg) layVFrm.setContentsMargins(*params['wdg_margins']) # ---------------------------------------------------------------------- # LAYOUT for input specifications and buttons # ---------------------------------------------------------------------- layVMain = QVBoxLayout(self) layVMain.addLayout(layHButtons1) # <Load> & <Save> buttons layVMain.addWidget(self.sel_fil) # Design method (IIR - ellip, ...) layVMain.addLayout(layHButtons2) # <Design> & <Quit> buttons layVMain.addWidget(self.f_units) # Frequency units layVMain.addWidget(self.t_specs) # Target specs layVMain.addWidget(self.f_specs) # Freq. specifications layVMain.addWidget(self.a_specs) # Amplitude specs layVMain.addWidget(self.w_specs) # Weight specs layVMain.addLayout(layVFrm) # Text message layVMain.addStretch() layVMain.setContentsMargins(*params['wdg_margins']) self.setLayout(layVMain) # main layout of widget # ---------------------------------------------------------------------- # GLOBAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.sig_rx.connect(self.process_sig_rx) # ---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.sig_rx_local.connect(self.process_sig_rx_local) self.butLoadFilt.clicked.connect(lambda: load_filter(self)) self.butSaveFilt.clicked.connect(lambda: save_filter(self)) self.butDesignFilt.clicked.connect(self.start_design_filt) self.butQuit.clicked.connect(self.quit_program) # emit 'quit_program' # ---------------------------------------------------------------------- self.update_UI() # first time initialization self.start_design_filt() # design first filter using default values # ------------------------------------------------------------------------------ def update_UI(self, dict_sig={}): """ update_UI is called every time the filter design method or order (min / man) has been changed as this usually requires a different set of frequency and amplitude specs. At this time, the actual filter object instance has been created from the name of the design method (e.g. 'cheby1') in select_filter.py. Its handle has been stored in fb.fil_inst. fb.fil[0] (currently selected filter) is read, then general information for the selected filter type and order (min/man) is gathered from the filter tree [fb.fil_tree], i.e. which parameters are needed, which widgets are visible and which message shall be displayed. Then, the UIs of all subwidgets are updated using their "update_UI" method. """ rt = fb.fil[0]['rt'] # e.g. 'LP' ft = fb.fil[0]['ft'] # e.g. 'FIR' fc = fb.fil[0]['fc'] # e.g. 'equiripple' fo = fb.fil[0]['fo'] # e.g. 'man' # the keys of the all_widgets dict are the names of the subwidgets, # the values are a tuple with the corresponding parameters all_widgets = fb.fil_tree[rt][ft][fc][fo] # logger.debug("rt: {0} - ft: {1} - fc: {2} - fo: {3}".format(rt, ft, fc, fo)) # logger.debug("fb.fil_tree[rt][ft][fc][fo]:\n{0}".format(fb.fil_tree[rt][ft][fc][fo])) # update filter order subwidget, called by select_filter: # self.sel_fil.load_filter_order() # TARGET SPECS: is widget in the dict and is it visible (marker != 'i')? if ('tspecs' in all_widgets and len(all_widgets['tspecs']) > 1 and all_widgets['tspecs'][0] != 'i'): self.t_specs.setVisible(True) # disable all subwidgets with marker 'd': self.t_specs.setEnabled(all_widgets['tspecs'][0] != 'd') self.t_specs.update_UI(new_labels=all_widgets['tspecs'][1]) else: self.t_specs.hide() # FREQUENCY SPECS if ('fspecs' in all_widgets and len(all_widgets['fspecs']) > 1 and all_widgets['fspecs'][0] != 'i'): self.f_specs.setVisible(True) self.f_specs.setEnabled(all_widgets['fspecs'][0] != 'd') self.f_specs.update_UI(new_labels=all_widgets['fspecs']) else: self.f_specs.hide() # AMPLITUDE SPECS if ('aspecs' in all_widgets and len(all_widgets['aspecs']) > 1 and all_widgets['aspecs'][0] != 'i'): self.a_specs.setVisible(True) self.a_specs.setEnabled(all_widgets['aspecs'][0] != 'd') self.a_specs.update_UI(new_labels=all_widgets['aspecs']) else: self.a_specs.hide() # WEIGHT SPECS if ('wspecs' in all_widgets and len(all_widgets['wspecs']) > 1 and all_widgets['wspecs'][0] != 'i'): self.w_specs.setVisible(True) self.w_specs.setEnabled(all_widgets['wspecs'][0] != 'd') self.w_specs.update_UI(new_labels=all_widgets['wspecs']) else: self.w_specs.hide() # MESSAGE PANE if ('msg' in all_widgets and len(all_widgets['msg']) > 1 and all_widgets['msg'][0] != 'i'): self.frmMsg.setVisible(True) self.frmMsg.setEnabled(all_widgets['msg'][0] != 'd') self.lblMsg.setText(all_widgets['msg'][1:][0]) else: self.frmMsg.hide() # Update state of "DESIGN FILTER" button # It is disabled for "Manual_IIR" and "Manual_FIR" filter classes self.color_design_button("changed") # ------------------------------------------------------------------------------ def load_dict(self): """ Reload all specs/parameters entries from global dict fb.fil[0], using the "load_dict" methods of the individual classes """ self.sel_fil.load_dict() # select filter widget self.f_units.load_dict() # frequency units widget self.f_specs.load_dict() # frequency specification widget self.a_specs.load_dict() # magnitude specs with unit self.w_specs.load_dict() # weight specification self.t_specs.load_dict() # target specs self.color_design_button("ok") # ------------------------------------------------------------------------------ def start_design_filt(self): """ Start the actual filter design process: - store the entries of all input widgets in the global filter dict. - call the design method, passing the whole dictionary as the argument: let the design method pick the needed specs - update the input widgets in case weights, corner frequencies etc. have been changed by the filter design method - the plots are updated via signal-slot connection """ try: logger.info( "Start filter design using method\n\t'{0}.{1}{2}'".format( str(fb.fil[0]['fc']), str(fb.fil[0]['rt']), str(fb.fil[0]['fo']))) # ---------------------------------------------------------------------- # A globally accessible instance fb.fil_inst of selected filter class fc # has been instantiated in InputFilter.set_design_method, now # call the method specified in the filter dict fil[0]. # The name of the instance method is constructed from the response # type (e.g. 'LP') and the filter order (e.g. 'man'), giving e.g. 'LPman'. # The filter is designed by passing the specs in fil[0] to the method, # resulting in e.g. cheby1.LPman(fb.fil[0]) and writing back coefficients, # P/Z etc. back to fil[0]. err = ff.fil_factory.call_fil_method( fb.fil[0]['rt'] + fb.fil[0]['fo'], fb.fil[0]) # this is the same as e.g. # from pyfda.filter_design import ellip # inst = ellip.ellip() # inst.LPmin(fb.fil[0]) # ----------------------------------------------------------------------- if err > 0: self.color_design_button("error") elif err == -1: # filter design cancelled by user return else: # Update filter order. weights and freq display in case they # have been changed by the design algorithm self.sel_fil.load_filter_order() self.w_specs.load_dict() self.f_specs.load_dict() self.color_design_button("ok") self.emit({'data_changed': 'filter_designed'}) logger.info('Designed filter with order = {0}'.format( str(fb.fil[0]['N']))) # ============================================================================= # logger.debug("Results:\n" # "F_PB = %s, F_SB = %s " # "Filter order N = %s\n" # "NDim fil[0]['ba'] = %s\n\n" # "b,a = %s\n\n" # "zpk = %s\n", # str(fb.fil[0]['F_PB']), str(fb.fil[0]['F_SB']), str(fb.fil[0]['N']), # str(np.ndim(fb.fil[0]['ba'])), pformat(fb.fil[0]['ba']), # pformat(fb.fil[0]['zpk'])) # # ============================================================================= except Exception as e: if ('__doc__' in str(e)): logger.warning("Filter design:\n %s\n %s\n", e.__doc__, e) else: logger.warning("{0}".format(e)) self.color_design_button("error") def color_design_button(self, state): man = "manual" in fb.fil[0]['fc'].lower() self.butDesignFilt.setDisabled(man) if man: state = 'ok' fb.design_filt_state = state qstyle_widget(self.butDesignFilt, state) # ------------------------------------------------------------------------------ def quit_program(self): """ When <QUIT> button is pressed, send 'quit_program' """ self.emit({'quit_program': ''})
class PlotImpz_UI(QWidget): """ Create the UI for the PlotImpz class """ # incoming: not implemented at the moment, update_N is triggered directly # by plot_impz # sig_rx = pyqtSignal(object) # outgoing: from various UI elements to PlotImpz ('ui_changed':'xxx') sig_tx = pyqtSignal(object) # outgoing to local fft window sig_tx_fft = pyqtSignal(object) def __init__(self, parent): """ Pass instance `parent` of parent class (FilterCoeffs) """ super(PlotImpz_UI, self).__init__(parent) """ Intitialize the widget, consisting of: - top chkbox row - coefficient table - two bottom rows with action buttons """ # initial settings self.N_start = 0 self.N_user = 0 self.N = 0 # time self.plt_time_resp = "Stem" self.plt_time_stim = "None" self.plt_time_stmq = "None" self.plt_time_spgr = "None" self.bottom_t = -80 # initial value for log. scale (time) self.nfft_spgr_time = 256 # number of fft points per spectrogram segment self.ovlp_spgr_time = 128 # number of overlap points between spectrogram segments self.mode_spgr_time = "magnitude" # stimuli self.stim = "Impulse" self.chirp_method = 'Linear' self.noise = "None" self.f1 = 0.02 self.f2 = 0.03 self.A1 = 1.0 self.A2 = 0.0 self.phi1 = self.phi2 = 0 self.noi = 0.1 self.noise = 'none' self.DC = 0.0 self.stim_formula = "A1 * abs(sin(2 * pi * f1 * n))" # frequency self.plt_freq_resp = "Line" self.plt_freq_stim = "None" self.plt_freq_stmq = "None" self.bottom_f = -120 # initial value for log. scale self.param = None # dictionary for fft window settings self.win_dict = fb.fil[0]['win_fft'] self.fft_window = None # handle for FFT window pop-up widget self.window_name = "Rectangular" self._construct_UI() self._enable_stim_widgets() self.update_N(emit=False) # also updates window function self._update_noi() def _construct_UI(self): # ----------- --------------------------------------------------- # Run control widgets # --------------------------------------------------------------- self.chk_auto_run = QCheckBox("Auto", self) self.chk_auto_run.setObjectName("chk_auto_run") self.chk_auto_run.setToolTip("<span>Update response automatically when " "parameters have been changed.</span>") self.chk_auto_run.setChecked(True) self.but_run = QPushButton(self) self.but_run.setText("RUN") self.but_run.setToolTip("Run simulation") self.but_run.setEnabled(not self.chk_auto_run.isChecked()) self.cmb_sim_select = QComboBox(self) self.cmb_sim_select.addItems(["Float","Fixpoint"]) qset_cmb_box(self.cmb_sim_select, "Float") self.cmb_sim_select.setToolTip("<span>Simulate floating-point or fixpoint response." "</span>") self.lbl_N_points = QLabel(to_html("N", frmt='bi') + " =", self) self.led_N_points = QLineEdit(self) self.led_N_points.setText(str(self.N)) self.led_N_points.setToolTip("<span>Number of displayed data points. " "<i>N</i> = 0 tries to choose for you.</span>") self.lbl_N_start = QLabel(to_html("N_0", frmt='bi') + " =", self) self.led_N_start = QLineEdit(self) self.led_N_start.setText(str(self.N_start)) self.led_N_start.setToolTip("<span>First point to plot.</span>") self.chk_fx_scale = QCheckBox("Int. scale", self) self.chk_fx_scale.setObjectName("chk_fx_scale") self.chk_fx_scale.setToolTip("<span>Display data with integer (fixpoint) scale.</span>") self.chk_fx_scale.setChecked(False) self.chk_stim_options = QCheckBox("Stim. Options", self) self.chk_stim_options.setObjectName("chk_stim_options") self.chk_stim_options.setToolTip("<span>Show stimulus options.</span>") self.chk_stim_options.setChecked(True) self.but_fft_win = QPushButton(self) self.but_fft_win.setText("WIN FFT") self.but_fft_win.setToolTip('<span> time and frequency response of FFT Window ' '(can be modified in the "Frequency" tab)</span>') self.but_fft_win.setCheckable(True) self.but_fft_win.setChecked(False) layH_ctrl_run = QHBoxLayout() layH_ctrl_run.addWidget(self.but_run) #layH_ctrl_run.addWidget(self.lbl_sim_select) layH_ctrl_run.addWidget(self.cmb_sim_select) layH_ctrl_run.addWidget(self.chk_auto_run) layH_ctrl_run.addStretch(1) layH_ctrl_run.addWidget(self.lbl_N_start) layH_ctrl_run.addWidget(self.led_N_start) layH_ctrl_run.addStretch(1) layH_ctrl_run.addWidget(self.lbl_N_points) layH_ctrl_run.addWidget(self.led_N_points) layH_ctrl_run.addStretch(2) layH_ctrl_run.addWidget(self.chk_fx_scale) layH_ctrl_run.addStretch(2) layH_ctrl_run.addWidget(self.chk_stim_options) layH_ctrl_run.addStretch(2) layH_ctrl_run.addWidget(self.but_fft_win) layH_ctrl_run.addStretch(10) #layH_ctrl_run.setContentsMargins(*params['wdg_margins']) self.wdg_ctrl_run = QWidget(self) self.wdg_ctrl_run.setLayout(layH_ctrl_run) # --- end of run control ---------------------------------------- # ----------- --------------------------------------------------- # Controls for time domain # --------------------------------------------------------------- plot_styles_list = ["None","Dots","Line","Line*","Stem","Stem*","Step","Step*"] lbl_plt_time_title = QLabel("<b>View:</b>", self) self.lbl_plt_time_stim = QLabel(to_html("Stimulus x", frmt='bi'), self) self.cmb_plt_time_stim = QComboBox(self) self.cmb_plt_time_stim.addItems(plot_styles_list) qset_cmb_box(self.cmb_plt_time_stim, self.plt_time_stim) self.cmb_plt_time_stim.setToolTip("<span>Plot style for stimulus.</span>") self.lbl_plt_time_stmq = QLabel(to_html(" Fixp. Stim. x_Q", frmt='bi'), self) self.cmb_plt_time_stmq = QComboBox(self) self.cmb_plt_time_stmq.addItems(plot_styles_list) qset_cmb_box(self.cmb_plt_time_stmq, self.plt_time_stmq) self.cmb_plt_time_stmq.setToolTip("<span>Plot style for <em>fixpoint</em> (quantized) stimulus.</span>") lbl_plt_time_resp = QLabel(to_html(" Response y", frmt='bi'), self) self.cmb_plt_time_resp = QComboBox(self) self.cmb_plt_time_resp.addItems(plot_styles_list) qset_cmb_box(self.cmb_plt_time_resp, self.plt_time_resp) self.cmb_plt_time_resp.setToolTip("<span>Plot style for response.</span>") lbl_win_time = QLabel(to_html(" FFT Window", frmt='bi'), self) self.chk_win_time = QCheckBox(self) self.chk_win_time.setObjectName("chk_win_time") self.chk_win_time.setToolTip('<span>Show FFT windowing function (can be modified in the "Frequency" tab).</span>') self.chk_win_time.setChecked(False) lbl_log_time = QLabel(to_html("dB", frmt='b'), self) self.chk_log_time = QCheckBox(self) self.chk_log_time.setObjectName("chk_log_time") self.chk_log_time.setToolTip("<span>Logarithmic scale for y-axis.</span>") self.chk_log_time.setChecked(False) self.lbl_log_bottom_time = QLabel(to_html("min =", frmt='bi'), self) self.lbl_log_bottom_time.setVisible(True) self.led_log_bottom_time = QLineEdit(self) self.led_log_bottom_time.setText(str(self.bottom_t)) self.led_log_bottom_time.setToolTip("<span>Minimum display value for time " "and spectrogram plots with log. scale.</span>") self.led_log_bottom_time.setVisible(True) lbl_plt_time_spgr = QLabel(to_html(" Spectrogram", frmt='bi'), self) self.cmb_plt_time_spgr = QComboBox(self) self.cmb_plt_time_spgr.addItems(["None", "x[n]", "x_q[n]", "y[n]"]) qset_cmb_box(self.cmb_plt_time_spgr, self.plt_time_spgr) self.cmb_plt_time_spgr.setToolTip("<span>Show Spectrogram for selected signal.</span>") spgr_en = self.plt_time_spgr != "None" self.lbl_log_spgr_time = QLabel(to_html(" dB", frmt='b'), self) self.lbl_log_spgr_time.setVisible(spgr_en) self.chk_log_spgr_time = QCheckBox(self) self.chk_log_spgr_time.setObjectName("chk_log_spgr") self.chk_log_spgr_time.setToolTip("<span>Logarithmic scale for spectrogram.</span>") self.chk_log_spgr_time.setChecked(True) self.chk_log_spgr_time.setVisible(spgr_en) self.lbl_nfft_spgr_time = QLabel(to_html(" N_FFT =", frmt='bi'), self) self.lbl_nfft_spgr_time.setVisible(spgr_en) self.led_nfft_spgr_time = QLineEdit(self) self.led_nfft_spgr_time.setText(str(self.nfft_spgr_time)) self.led_nfft_spgr_time.setToolTip("<span>Number of FFT points per spectrogram segment.</span>") self.led_nfft_spgr_time.setVisible(spgr_en) self.lbl_ovlp_spgr_time = QLabel(to_html(" N_OVLP =", frmt='bi'), self) self.lbl_ovlp_spgr_time.setVisible(spgr_en) self.led_ovlp_spgr_time = QLineEdit(self) self.led_ovlp_spgr_time.setText(str(self.ovlp_spgr_time)) self.led_ovlp_spgr_time.setToolTip("<span>Number of overlap data points between spectrogram segments.</span>") self.led_ovlp_spgr_time.setVisible(spgr_en) self.lbl_mode_spgr_time = QLabel(to_html(" Mode", frmt='bi'), self) self.lbl_mode_spgr_time.setVisible(spgr_en) self.cmb_mode_spgr_time = QComboBox(self) spgr_modes = [("PSD","psd"), ("Mag.","magnitude"),\ ("Angle","angle"), ("Phase","phase")] for i in spgr_modes: self.cmb_mode_spgr_time.addItem(*i) qset_cmb_box(self.cmb_mode_spgr_time, self.mode_spgr_time, data=True) self.cmb_mode_spgr_time.setToolTip("<span>Spectrogram display mode.</span>") self.cmb_mode_spgr_time.setVisible(spgr_en) self.lbl_byfs_spgr_time = QLabel(to_html(" per f_S", frmt='b'), self) self.lbl_byfs_spgr_time.setVisible(spgr_en) self.chk_byfs_spgr_time = QCheckBox(self) self.chk_byfs_spgr_time.setObjectName("chk_log_spgr") self.chk_byfs_spgr_time.setToolTip("<span>Display spectral density i.e. scale by f_S</span>") self.chk_byfs_spgr_time.setChecked(True) self.chk_byfs_spgr_time.setVisible(spgr_en) # self.lbl_colorbar_time = QLabel(to_html(" Col.bar", frmt='b'), self) # self.lbl_colorbar_time.setVisible(spgr_en) # self.chk_colorbar_time = QCheckBox(self) # self.chk_colorbar_time.setObjectName("chk_colorbar_time") # self.chk_colorbar_time.setToolTip("<span>Enable colorbar</span>") # self.chk_colorbar_time.setChecked(True) # self.chk_colorbar_time.setVisible(spgr_en) self.chk_fx_limits = QCheckBox("Min/max.", self) self.chk_fx_limits.setObjectName("chk_fx_limits") self.chk_fx_limits.setToolTip("<span>Display limits of fixpoint range.</span>") self.chk_fx_limits.setChecked(False) layH_ctrl_time = QHBoxLayout() layH_ctrl_time.addWidget(lbl_plt_time_title) layH_ctrl_time.addStretch(1) layH_ctrl_time.addWidget(self.lbl_plt_time_stim) layH_ctrl_time.addWidget(self.cmb_plt_time_stim) # layH_ctrl_time.addWidget(self.lbl_plt_time_stmq) layH_ctrl_time.addWidget(self.cmb_plt_time_stmq) # layH_ctrl_time.addWidget(lbl_plt_time_resp) layH_ctrl_time.addWidget(self.cmb_plt_time_resp) # layH_ctrl_time.addWidget(lbl_win_time) layH_ctrl_time.addWidget(self.chk_win_time) layH_ctrl_time.addStretch(1) layH_ctrl_time.addWidget(lbl_log_time) layH_ctrl_time.addWidget(self.chk_log_time) layH_ctrl_time.addWidget(self.lbl_log_bottom_time) layH_ctrl_time.addWidget(self.led_log_bottom_time) # layH_ctrl_time.addStretch(1) # layH_ctrl_time.addWidget(lbl_plt_time_spgr) layH_ctrl_time.addWidget(self.cmb_plt_time_spgr) layH_ctrl_time.addWidget(self.lbl_log_spgr_time) layH_ctrl_time.addWidget(self.chk_log_spgr_time) layH_ctrl_time.addWidget(self.lbl_nfft_spgr_time) layH_ctrl_time.addWidget(self.led_nfft_spgr_time) layH_ctrl_time.addWidget(self.lbl_ovlp_spgr_time) layH_ctrl_time.addWidget(self.led_ovlp_spgr_time) layH_ctrl_time.addWidget(self.lbl_mode_spgr_time) layH_ctrl_time.addWidget(self.cmb_mode_spgr_time) layH_ctrl_time.addWidget(self.lbl_byfs_spgr_time) layH_ctrl_time.addWidget(self.chk_byfs_spgr_time) layH_ctrl_time.addStretch(2) layH_ctrl_time.addWidget(self.chk_fx_limits) layH_ctrl_time.addStretch(10) #layH_ctrl_time.setContentsMargins(*params['wdg_margins']) self.wdg_ctrl_time = QWidget(self) self.wdg_ctrl_time.setLayout(layH_ctrl_time) # ---- end time domain ------------------ # --------------------------------------------------------------- # Controls for frequency domain # --------------------------------------------------------------- lbl_plt_freq_title = QLabel("<b>View:</b>", self) self.lbl_plt_freq_stim = QLabel(to_html("Stimulus X", frmt='bi'), self) self.cmb_plt_freq_stim = QComboBox(self) self.cmb_plt_freq_stim.addItems(plot_styles_list) qset_cmb_box(self.cmb_plt_freq_stim, self.plt_freq_stim) self.cmb_plt_freq_stim.setToolTip("<span>Plot style for stimulus.</span>") self.lbl_plt_freq_stmq = QLabel(to_html(" Fixp. Stim. X_Q", frmt='bi'), self) self.cmb_plt_freq_stmq = QComboBox(self) self.cmb_plt_freq_stmq.addItems(plot_styles_list) qset_cmb_box(self.cmb_plt_freq_stmq, self.plt_freq_stmq) self.cmb_plt_freq_stmq.setToolTip("<span>Plot style for <em>fixpoint</em> (quantized) stimulus.</span>") lbl_plt_freq_resp = QLabel(to_html(" Response Y", frmt='bi'), self) self.cmb_plt_freq_resp = QComboBox(self) self.cmb_plt_freq_resp.addItems(plot_styles_list) qset_cmb_box(self.cmb_plt_freq_resp, self.plt_freq_resp) self.cmb_plt_freq_resp.setToolTip("<span>Plot style for response.</span>") lbl_log_freq = QLabel(to_html("dB", frmt='b'), self) self.chk_log_freq = QCheckBox(self) self.chk_log_freq.setObjectName("chk_log_freq") self.chk_log_freq.setToolTip("<span>Logarithmic scale for y-axis.</span>") self.chk_log_freq.setChecked(True) self.lbl_log_bottom_freq = QLabel(to_html("min =", frmt='bi'), self) self.lbl_log_bottom_freq.setVisible(self.chk_log_freq.isChecked()) self.led_log_bottom_freq = QLineEdit(self) self.led_log_bottom_freq.setText(str(self.bottom_f)) self.led_log_bottom_freq.setToolTip("<span>Minimum display value for log. scale.</span>") self.led_log_bottom_freq.setVisible(self.chk_log_freq.isChecked()) if not self.chk_log_freq.isChecked(): self.bottom_f = 0 lbl_re_im_freq = QLabel(to_html("Re / Im", frmt='b'), self) self.chk_re_im_freq = QCheckBox(self) self.chk_re_im_freq.setObjectName("chk_re_im_freq") self.chk_re_im_freq.setToolTip("<span>Show real and imaginary part of spectrum</span>") self.chk_re_im_freq.setChecked(False) self.lbl_win_fft = QLabel(to_html("Window", frmt='bi'), self) self.cmb_win_fft = QComboBox(self) self.cmb_win_fft.addItems(get_window_names()) self.cmb_win_fft.setToolTip("FFT window type.") qset_cmb_box(self.cmb_win_fft, self.window_name) self.cmb_win_fft_variant = QComboBox(self) self.cmb_win_fft_variant.setToolTip("FFT window variant.") self.cmb_win_fft_variant.setVisible(False) self.lblWinPar1 = QLabel("Param1") self.ledWinPar1 = QLineEdit(self) self.ledWinPar1.setText("1") self.ledWinPar1.setObjectName("ledWinPar1") self.lblWinPar2 = QLabel("Param2") self.ledWinPar2 = QLineEdit(self) self.ledWinPar2.setText("2") self.ledWinPar2.setObjectName("ledWinPar2") self.chk_Hf = QCheckBox(self) self.chk_Hf.setObjectName("chk_Hf") self.chk_Hf.setToolTip("<span>Show ideal frequency response, calculated " "from the filter coefficients.</span>") self.chk_Hf.setChecked(False) self.chk_Hf_lbl = QLabel(to_html("H_id (f)", frmt="bi"), self) lbl_show_info_freq = QLabel(to_html("Info", frmt='b'), self) self.chk_show_info_freq = QCheckBox(self) self.chk_show_info_freq.setObjectName("chk_show_info_freq") self.chk_show_info_freq.setToolTip("<span>Show infos about signal power " "and window properties.</span>") self.chk_show_info_freq.setChecked(False) layH_ctrl_freq = QHBoxLayout() layH_ctrl_freq.addWidget(lbl_plt_freq_title) layH_ctrl_freq.addStretch(1) layH_ctrl_freq.addWidget(self.lbl_plt_freq_stim) layH_ctrl_freq.addWidget(self.cmb_plt_freq_stim) # layH_ctrl_freq.addWidget(self.lbl_plt_freq_stmq) layH_ctrl_freq.addWidget(self.cmb_plt_freq_stmq) # layH_ctrl_freq.addWidget(lbl_plt_freq_resp) layH_ctrl_freq.addWidget(self.cmb_plt_freq_resp) # layH_ctrl_freq.addWidget(self.chk_Hf_lbl) layH_ctrl_freq.addWidget(self.chk_Hf) layH_ctrl_freq.addStretch(1) layH_ctrl_freq.addWidget(lbl_log_freq) layH_ctrl_freq.addWidget(self.chk_log_freq) layH_ctrl_freq.addWidget(self.lbl_log_bottom_freq) layH_ctrl_freq.addWidget(self.led_log_bottom_freq) layH_ctrl_freq.addStretch(1) layH_ctrl_freq.addWidget(lbl_re_im_freq) layH_ctrl_freq.addWidget(self.chk_re_im_freq) layH_ctrl_freq.addStretch(2) layH_ctrl_freq.addWidget(self.lbl_win_fft) layH_ctrl_freq.addWidget(self.cmb_win_fft) layH_ctrl_freq.addWidget(self.cmb_win_fft_variant) layH_ctrl_freq.addWidget(self.lblWinPar1) layH_ctrl_freq.addWidget(self.ledWinPar1) layH_ctrl_freq.addWidget(self.lblWinPar2) layH_ctrl_freq.addWidget(self.ledWinPar2) layH_ctrl_freq.addStretch(1) layH_ctrl_freq.addWidget(lbl_show_info_freq) layH_ctrl_freq.addWidget(self.chk_show_info_freq) layH_ctrl_freq.addStretch(10) #layH_ctrl_freq.setContentsMargins(*params['wdg_margins']) self.wdg_ctrl_freq = QWidget(self) self.wdg_ctrl_freq.setLayout(layH_ctrl_freq) # ---- end Frequency Domain ------------------ # --------------------------------------------------------------- # Controls for stimuli # --------------------------------------------------------------- lbl_title_stim = QLabel("<b>Stimulus:</b>", self) self.lblStimulus = QLabel(to_html("Type", frmt='bi'), self) self.cmbStimulus = QComboBox(self) self.cmbStimulus.addItems(["None","Impulse","Step","StepErr","Cos","Sine", "Chirp", "Triang","Saw","Rect","Comb","AM","PM / FM","Formula"]) self.cmbStimulus.setToolTip("Stimulus type.") qset_cmb_box(self.cmbStimulus, self.stim) self.chk_stim_bl = QCheckBox("BL", self) self.chk_stim_bl.setToolTip("<span>The signal is bandlimited to the Nyquist frequency " "to avoid aliasing. However, it is much slower to generate " "than the regular version.</span>") self.chk_stim_bl.setChecked(True) self.chk_stim_bl.setObjectName("stim_bl") self.cmbChirpMethod = QComboBox(self) for t in [("Lin","Linear"),("Square","Quadratic"),("Log", "Logarithmic"), ("Hyper", "Hyperbolic")]: self.cmbChirpMethod.addItem(*t) qset_cmb_box(self.cmbChirpMethod, self.chirp_method, data=False) self.chk_scale_impz_f = QCheckBox("Scale", self) self.chk_scale_impz_f.setToolTip("<span>Scale the FFT of the impulse response with <i>N<sub>FFT</sub></i> " "so that it has the same magnitude as |H(f)|. DC and Noise need to be " "turned off.</span>") self.chk_scale_impz_f.setChecked(True) self.chk_scale_impz_f.setObjectName("scale_impz_f") self.lblDC = QLabel(to_html("DC =", frmt='bi'), self) self.ledDC = QLineEdit(self) self.ledDC.setText(str(self.DC)) self.ledDC.setToolTip("DC Level") self.ledDC.setObjectName("stimDC") layHCmbStim = QHBoxLayout() layHCmbStim.addWidget(self.cmbStimulus) layHCmbStim.addWidget(self.chk_stim_bl) layHCmbStim.addWidget(self.chk_scale_impz_f) layHCmbStim.addWidget(self.cmbChirpMethod) #---------------------------------------------- self.lblAmp1 = QLabel(to_html(" A_1", frmt='bi') + " =", self) self.ledAmp1 = QLineEdit(self) self.ledAmp1.setText(str(self.A1)) self.ledAmp1.setToolTip("Stimulus amplitude, complex values like 3j - 1 are allowed") self.ledAmp1.setObjectName("stimAmp1") self.lblAmp2 = QLabel(to_html(" A_2", frmt='bi') + " =", self) self.ledAmp2 = QLineEdit(self) self.ledAmp2.setText(str(self.A2)) self.ledAmp2.setToolTip("Stimulus amplitude 2, complex values like 3j - 1 are allowed") self.ledAmp2.setObjectName("stimAmp2") #---------------------------------------------- self.lblPhi1 = QLabel(to_html(" φ_1", frmt='bi') + " =", self) self.ledPhi1 = QLineEdit(self) self.ledPhi1.setText(str(self.phi1)) self.ledPhi1.setToolTip("Stimulus phase") self.ledPhi1.setObjectName("stimPhi1") self.lblPhU1 = QLabel(to_html("°", frmt='b'), self) self.lblPhi2 = QLabel(to_html(" φ_2", frmt='bi') + " =", self) self.ledPhi2 = QLineEdit(self) self.ledPhi2.setText(str(self.phi2)) self.ledPhi2.setToolTip("Stimulus phase 2") self.ledPhi2.setObjectName("stimPhi2") self.lblPhU2 = QLabel(to_html("°", frmt='b'), self) #---------------------------------------------- self.lblFreq1 = QLabel(to_html(" f_1", frmt='bi') + " =", self) self.ledFreq1 = QLineEdit(self) self.ledFreq1.setText(str(self.f1)) self.ledFreq1.setToolTip("Stimulus frequency 1") self.ledFreq1.setObjectName("stimFreq1") self.lblFreqUnit1 = QLabel("f_S", self) self.lblFreq2 = QLabel(to_html(" f_2", frmt='bi') + " =", self) self.ledFreq2 = QLineEdit(self) self.ledFreq2.setText(str(self.f2)) self.ledFreq2.setToolTip("Stimulus frequency 2") self.ledFreq2.setObjectName("stimFreq2") self.lblFreqUnit2 = QLabel("f_S", self) #---------------------------------------------- self.lblNoise = QLabel(to_html(" Noise", frmt='bi'), self) self.cmbNoise = QComboBox(self) self.cmbNoise.addItems(["None","Gauss","Uniform","PRBS"]) self.cmbNoise.setToolTip("Type of additive noise.") qset_cmb_box(self.cmbNoise, self.noise) self.lblNoi = QLabel("not initialized", self) self.ledNoi = QLineEdit(self) self.ledNoi.setText(str(self.noi)) self.ledNoi.setToolTip("not initialized") self.ledNoi.setObjectName("stimNoi") layGStim = QGridLayout() layGStim.addWidget(self.lblStimulus, 0, 0) layGStim.addWidget(self.lblDC, 1, 0) layGStim.addLayout(layHCmbStim, 0, 1) layGStim.addWidget(self.ledDC, 1, 1) layGStim.addWidget(self.lblAmp1, 0, 2) layGStim.addWidget(self.lblAmp2, 1, 2) layGStim.addWidget(self.ledAmp1, 0, 3) layGStim.addWidget(self.ledAmp2, 1, 3) layGStim.addWidget(self.lblPhi1, 0, 4) layGStim.addWidget(self.lblPhi2, 1, 4) layGStim.addWidget(self.ledPhi1, 0, 5) layGStim.addWidget(self.ledPhi2, 1, 5) layGStim.addWidget(self.lblPhU1, 0, 6) layGStim.addWidget(self.lblPhU2, 1, 6) layGStim.addWidget(self.lblFreq1, 0, 7) layGStim.addWidget(self.lblFreq2, 1, 7) layGStim.addWidget(self.ledFreq1, 0, 8) layGStim.addWidget(self.ledFreq2, 1, 8) layGStim.addWidget(self.lblFreqUnit1, 0, 9) layGStim.addWidget(self.lblFreqUnit2, 1, 9) layGStim.addWidget(self.lblNoise, 0, 10) layGStim.addWidget(self.lblNoi, 1, 10) layGStim.addWidget(self.cmbNoise, 0, 11) layGStim.addWidget(self.ledNoi, 1, 11) #---------------------------------------------- self.lblStimFormula = QLabel(to_html("x =", frmt='bi'), self) self.ledStimFormula = QLineEdit(self) self.ledStimFormula.setText(str(self.stim_formula)) self.ledStimFormula.setToolTip("<span>Enter formula for stimulus in numexpr syntax" "</span>") self.ledStimFormula.setObjectName("stimFormula") layH_ctrl_stim_formula = QHBoxLayout() layH_ctrl_stim_formula.addWidget(self.lblStimFormula) layH_ctrl_stim_formula.addWidget(self.ledStimFormula,10) #---------------------------------------------- #layG_ctrl_stim = QGridLayout() layH_ctrl_stim_par = QHBoxLayout() layH_ctrl_stim_par.addLayout(layGStim) layV_ctrl_stim = QVBoxLayout() layV_ctrl_stim.addLayout(layH_ctrl_stim_par) layV_ctrl_stim.addLayout(layH_ctrl_stim_formula) layH_ctrl_stim = QHBoxLayout() layH_ctrl_stim.addWidget(lbl_title_stim) layH_ctrl_stim.addStretch(1) layH_ctrl_stim.addLayout(layV_ctrl_stim) layH_ctrl_stim.addStretch(10) self.wdg_ctrl_stim = QWidget(self) self.wdg_ctrl_stim.setLayout(layH_ctrl_stim) # --------- end stimuli --------------------------------- # frequency widgets require special handling as they are scaled with f_s self.ledFreq1.installEventFilter(self) self.ledFreq2.installEventFilter(self) #---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs #---------------------------------------------------------------------- # --- run control --- self.led_N_start.editingFinished.connect(self.update_N) self.led_N_points.editingFinished.connect(self.update_N) # --- frequency control --- # careful! currentIndexChanged passes the current index to _update_win_fft self.cmb_win_fft.currentIndexChanged.connect(self._update_win_fft) self.ledWinPar1.editingFinished.connect(self._read_param1) self.ledWinPar2.editingFinished.connect(self._read_param2) # --- stimulus control --- self.chk_stim_options.clicked.connect(self._show_stim_options) self.chk_stim_bl.clicked.connect(self._enable_stim_widgets) self.cmbStimulus.currentIndexChanged.connect(self._enable_stim_widgets) self.cmbNoise.currentIndexChanged.connect(self._update_noi) self.ledNoi.editingFinished.connect(self._update_noi) self.ledAmp1.editingFinished.connect(self._update_amp1) self.ledAmp2.editingFinished.connect(self._update_amp2) self.ledPhi1.editingFinished.connect(self._update_phi1) self.ledPhi2.editingFinished.connect(self._update_phi2) self.cmbChirpMethod.currentIndexChanged.connect(self._update_chirp_method) self.ledDC.editingFinished.connect(self._update_DC) self.ledStimFormula.editingFinished.connect(self._update_stim_formula) #------------------------------------------------------------------------------ def eventFilter(self, source, event): """ Filter all events generated by the monitored widgets. Source and type of all events generated by monitored objects are passed to this eventFilter, evaluated and passed on to the next hierarchy level. - When a QLineEdit widget gains input focus (``QEvent.FocusIn``), display the stored value from filter dict with full precision - When a key is pressed inside the text field, set the `spec_edited` flag to True. - When a QLineEdit widget loses input focus (``QEvent.FocusOut``), store current value normalized to f_S with full precision (only if ``spec_edited == True``) and display the stored value in selected format """ def _store_entry(source): if self.spec_edited: if source.objectName() == "stimFreq1": self.f1 = safe_eval(source.text(), self.f1 * fb.fil[0]['f_S'], return_type='float') / fb.fil[0]['f_S'] source.setText(str(params['FMT'].format(self.f1 * fb.fil[0]['f_S']))) elif source.objectName() == "stimFreq2": self.f2 = safe_eval(source.text(), self.f2 * fb.fil[0]['f_S'], return_type='float') / fb.fil[0]['f_S'] source.setText(str(params['FMT'].format(self.f2 * fb.fil[0]['f_S']))) self.spec_edited = False # reset flag self.sig_tx.emit({'sender':__name__, 'ui_changed':'stim'}) # if isinstance(source, QLineEdit): # if source.objectName() in {"stimFreq1","stimFreq2"}: if event.type() in {QEvent.FocusIn,QEvent.KeyPress, QEvent.FocusOut}: if event.type() == QEvent.FocusIn: self.spec_edited = False self.load_fs() elif event.type() == QEvent.KeyPress: self.spec_edited = True # entry has been changed key = event.key() if key in {Qt.Key_Return, Qt.Key_Enter}: _store_entry(source) elif key == Qt.Key_Escape: # revert changes self.spec_edited = False if source.objectName() == "stimFreq1": source.setText(str(params['FMT'].format(self.f1 * fb.fil[0]['f_S']))) elif source.objectName() == "stimFreq2": source.setText(str(params['FMT'].format(self.f2 * fb.fil[0]['f_S']))) elif event.type() == QEvent.FocusOut: _store_entry(source) # Call base class method to continue normal event processing: return super(PlotImpz_UI, self).eventFilter(source, event) #------------------------------------------------------------- def _show_stim_options(self): """ Hide / show panel with stimulus options """ self.wdg_ctrl_stim.setVisible(self.chk_stim_options.isChecked()) def _enable_stim_widgets(self): """ Enable / disable widgets depending on the selected stimulus""" self.stim = qget_cmb_box(self.cmbStimulus, data=False) f1_en = self.stim in {"Cos","Sine","Chirp","PM / FM","AM","Formula","Rect","Saw","Triang","Comb"} f2_en = self.stim in {"Cos","Sine","Chirp","PM / FM","AM","Formula"} dc_en = self.stim not in {"Step", "StepErr"} self.chk_stim_bl.setVisible(self.stim in {"Triang", "Saw", "Rect"}) self.lblAmp1.setVisible(self.stim != "None") self.ledAmp1.setVisible(self.stim != "None") self.chk_scale_impz_f.setVisible(self.stim == 'Impulse') self.chk_scale_impz_f.setEnabled((self.noi == 0 or self.cmbNoise.currentText() == 'None')\ and self.DC == 0) self.cmbChirpMethod.setVisible(self.stim == 'Chirp') self.lblPhi1.setVisible(f1_en) self.ledPhi1.setVisible(f1_en) self.lblPhU1.setVisible(f1_en) self.lblFreq1.setVisible(f1_en) self.ledFreq1.setVisible(f1_en) self.lblFreqUnit1.setVisible(f1_en) self.lblFreq2.setVisible(f2_en) self.ledFreq2.setVisible(f2_en) self.lblFreqUnit2.setVisible(f2_en) self.lblAmp2.setVisible(f2_en and self.stim != "Chirp") self.ledAmp2.setVisible(f2_en and self.stim != "Chirp") self.lblPhi2.setVisible(f2_en and self.stim != "Chirp") self.ledPhi2.setVisible(f2_en and self.stim != "Chirp") self.lblPhU2.setVisible(f2_en and self.stim != "Chirp") self.lblDC.setVisible(dc_en) self.ledDC.setVisible(dc_en) self.lblStimFormula.setVisible(self.stim == "Formula") self.ledStimFormula.setVisible(self.stim == "Formula") self.sig_tx.emit({'sender':__name__, 'ui_changed':'stim'}) #------------------------------------------------------------- def load_fs(self): """ Reload sampling frequency from filter dictionary and transform the displayed frequency spec input fields according to the units setting (i.e. f_S). Spec entries are always stored normalized w.r.t. f_S in the dictionary; when f_S or the unit are changed, only the displayed values of the frequency entries are updated, not the dictionary! load_fs() is called during init and when the frequency unit or the sampling frequency have been changed. It should be called when sigSpecsChanged or sigFilterDesigned is emitted at another place, indicating that a reload is required. """ # recalculate displayed freq spec values for (maybe) changed f_S if self.ledFreq1.hasFocus(): # widget has focus, show full precision self.ledFreq1.setText(str(self.f1 * fb.fil[0]['f_S'])) elif self.ledFreq2.hasFocus(): # widget has focus, show full precision self.ledFreq2.setText(str(self.f2 * fb.fil[0]['f_S'])) else: # widgets have no focus, round the display self.ledFreq1.setText( str(params['FMT'].format(self.f1 * fb.fil[0]['f_S']))) self.ledFreq2.setText( str(params['FMT'].format(self.f2 * fb.fil[0]['f_S']))) def _update_amp1(self): """ Update value for self.A1 from QLineEditWidget""" self.A1 = safe_eval(self.ledAmp1.text(), self.A1, return_type='cmplx') self.ledAmp1.setText(str(self.A1)) self.sig_tx.emit({'sender':__name__, 'ui_changed':'a1'}) def _update_amp2(self): """ Update value for self.A2 from the QLineEditWidget""" self.A2 = safe_eval(self.ledAmp2.text(), self.A2, return_type='cmplx') self.ledAmp2.setText(str(self.A2)) self.sig_tx.emit({'sender':__name__, 'ui_changed':'a2'}) def _update_phi1(self): """ Update value for self.phi1 from QLineEditWidget""" self.phi1 = safe_eval(self.ledPhi1.text(), self.phi1, return_type='float') self.ledPhi1.setText(str(self.phi1)) self.sig_tx.emit({'sender':__name__, 'ui_changed':'phi1'}) def _update_phi2(self): """ Update value for self.phi2 from the QLineEditWidget""" self.phi2 = safe_eval(self.ledPhi2.text(), self.phi2, return_type='float') self.ledPhi2.setText(str(self.phi2)) self.sig_tx.emit({'sender':__name__, 'ui_changed':'phi2'}) def _update_chirp_method(self): """ Update value for self.chirp_method from the QLineEditWidget""" self.chirp_method = qget_cmb_box(self.cmbChirpMethod) # read current data string self.sig_tx.emit({'sender':__name__, 'ui_changed':'chirp_method'}) def _update_noi(self): """ Update type + value + label for self.noi for noise""" self.noise = qget_cmb_box(self.cmbNoise, data=False).lower() self.lblNoi.setVisible(self.noise!='none') self.ledNoi.setVisible(self.noise!='none') if self.noise!='none': self.noi = safe_eval(self.ledNoi.text(), 0, return_type='cmplx') self.ledNoi.setText(str(self.noi)) if self.noise == 'gauss': self.lblNoi.setText(to_html(" σ =", frmt='bi')) self.ledNoi.setToolTip("<span>Standard deviation of statistical process," "noise power is <i>P</i> = σ<sup>2</sup></span>") elif self.noise == 'uniform': self.lblNoi.setText(to_html(" Δ =", frmt='bi')) self.ledNoi.setToolTip("<span>Interval size for uniformly distributed process " "(e.g. quantization step size for quantization noise), " "centered around 0. Noise power is " "<i>P</i> = Δ<sup>2</sup>/12.</span>") elif self.noise == 'prbs': self.lblNoi.setText(to_html(" A =", frmt='bi')) self.ledNoi.setToolTip("<span>Amplitude of bipolar Pseudorandom Binary Sequence. " "Noise power is <i>P</i> = A<sup>2</sup>.</span>") self.sig_tx.emit({'sender':__name__, 'ui_changed':'noi'}) def _update_DC(self): """ Update value for self.DC from the QLineEditWidget""" self.DC = safe_eval(self.ledDC.text(), 0, return_type='cmplx') self.ledDC.setText(str(self.DC)) self.sig_tx.emit({'sender':__name__, 'ui_changed':'dc'}) def _update_stim_formula(self): """Update string with formula to be evaluated by numexpr""" self.stim_formula = self.ledStimFormula.text().strip() self.ledStimFormula.setText(str(self.stim_formula)) self.sig_tx.emit({'sender':__name__, 'ui_changed':'stim_formula'}) # ------------------------------------------------------------------------- def update_N(self, emit=True): # called directly from impz or locally # between local triggering and updates upstream """ Update values for self.N and self.N_start from the QLineEditWidget, update the window and fire "ui_changed" """ if not isinstance(emit, bool): logger.error("update N: emit={0}".format(emit)) self.N_start = safe_eval(self.led_N_start.text(), self.N_start, return_type='int', sign='poszero') self.led_N_start.setText(str(self.N_start)) # update widget self.N_user = safe_eval(self.led_N_points.text(), self.N_user, return_type='int', sign='poszero') if self.N_user == 0: # automatic calculation self.N = self.calc_n_points(self.N_user) # widget remains set to 0 self.led_N_points.setText("0") # update widget else: self.N = self.N_user self.led_N_points.setText(str(self.N)) # update widget self.N_end = self.N + self.N_start # total number of points to be calculated: N + N_start # FFT window needs to be updated due to changed number of data points self._update_win_fft(emit=False) # don't emit anything here if emit: self.sig_tx.emit({'sender':__name__, 'ui_changed':'N'}) def _read_param1(self): """Read out textbox when editing is finished and update dict and fft window""" param = safe_eval(self.ledWinPar1.text(), self.win_dict['par'][0]['val'], return_type='float') if param < self.win_dict['par'][0]['min']: param = self.win_dict['par'][0]['min'] elif param > self.win_dict['par'][0]['max']: param = self.win_dict['par'][0]['max'] self.ledWinPar1.setText(str(param)) self.win_dict['par'][0]['val'] = param self._update_win_fft() def _read_param2(self): """Read out textbox when editing is finished and update dict and fft window""" param = safe_eval(self.ledWinPar2.text(), self.win_dict['par'][1]['val'], return_type='float') if param < self.win_dict['par'][1]['min']: param = self.win_dict['par'][1]['min'] elif param > self.win_dict['par'][1]['max']: param = self.win_dict['par'][1]['max'] self.ledWinPar2.setText(str(param)) self.win_dict['par'][1]['val'] = param self._update_win_fft() #------------------------------------------------------------------------------ def _update_win_fft(self, arg=None, emit=True): """ Update window type for FFT with different arguments: - signal-slot connection to combo-box -> index (int), absorbed by `arg` emit is not set -> emit=True - called by _read_param() -> empty -> emit=True - called by update_N(emit=False) """ if not isinstance(emit, bool): logger.error("update win: emit={0}".format(emit)) self.window_name = qget_cmb_box(self.cmb_win_fft, data=False) self.win = calc_window_function(self.win_dict, self.window_name, N=self.N, sym=False) n_par = self.win_dict['n_par'] self.lblWinPar1.setVisible(n_par > 0) self.ledWinPar1.setVisible(n_par > 0) self.lblWinPar2.setVisible(n_par > 1) self.ledWinPar2.setVisible(n_par > 1) if n_par > 0: self.lblWinPar1.setText(to_html(self.win_dict['par'][0]['name'] + " =", frmt='bi')) self.ledWinPar1.setText(str(self.win_dict['par'][0]['val'])) self.ledWinPar1.setToolTip(self.win_dict['par'][0]['tooltip']) if n_par > 1: self.lblWinPar2.setText(to_html(self.win_dict['par'][1]['name'] + " =", frmt='bi')) self.ledWinPar2.setText(str(self.win_dict['par'][1]['val'])) self.ledWinPar2.setToolTip(self.win_dict['par'][1]['tooltip']) self.nenbw = self.N * np.sum(np.square(self.win)) / (np.square(np.sum(self.win))) self.cgain = np.sum(self.win) / self.N # coherent gain self.win /= self.cgain # correct gain for periodic signals # only emit a signal for local triggers to prevent infinite loop: # - signal-slot connection passes a bool or an integer # - local function calls don't pass anything if emit is True: self.sig_tx.emit({'sender':__name__, 'ui_changed':'win'}) # ... but always notify the FFT widget via sig_tx_fft self.sig_tx_fft.emit({'sender':__name__, 'view_changed':'win'}) #------------------------------------------------------------------------------ def show_fft_win(self): """ Pop-up FFT window """ if self.but_fft_win.isChecked(): qstyle_widget(self.but_fft_win, "changed") else: qstyle_widget(self.but_fft_win, "normal") if self.fft_window is None: # no handle to the window? Create a new instance if self.but_fft_win.isChecked(): # Important: Handle to window must be class attribute otherwise it # (and the attached window) is deleted immediately when it goes out of scope self.fft_window = Plot_FFT_win(self, win_dict=self.win_dict, sym=False, title="pyFDA Spectral Window Viewer") self.sig_tx_fft.connect(self.fft_window.sig_rx) self.fft_window.sig_tx.connect(self.close_fft_win) self.fft_window.show() # modeless i.e. non-blocking popup window else: if not self.but_fft_win.isChecked(): if self.fft_window is None: logger.warning("FFT window is already closed!") else: self.fft_window.close() def close_fft_win(self): self.fft_window = None self.but_fft_win.setChecked(False) qstyle_widget(self.but_fft_win, "normal") #------------------------------------------------------------------------------ def calc_n_points(self, N_user = 0): """ Calculate number of points to be displayed, depending on type of filter (FIR, IIR) and user input. If the user selects 0 points, the number is calculated automatically. An improvement would be to calculate the dominant pole and the corresponding settling time. """ if N_user == 0: # set number of data points automatically if fb.fil[0]['ft'] == 'IIR': N = 100 else: N = min(len(fb.fil[0]['ba'][0]),100) # FIR: N = number of coefficients (max. 100) else: N = N_user return N
class PlotImpz_UI(QWidget): """ Create the UI for the PlotImpz class """ # incoming: not implemented at the moment, update_N is triggered directly # by plot_impz # sig_rx = pyqtSignal(object) # outgoing: from various UI elements to PlotImpz ('ui_changed':'xxx') sig_tx = pyqtSignal(object) # outgoing: to fft related widgets (FFT window widget, qfft_win_select) sig_tx_fft = pyqtSignal(object) from pyfda.libs.pyfda_qt_lib import emit # ------------------------------------------------------------------------------ def process_sig_rx(self, dict_sig=None): """ Process signals coming from - FFT window widget - qfft_win_select """ # logger.debug("PROCESS_SIG_RX - vis: {0}\n{1}" # .format(self.isVisible(), pprint_log(dict_sig))) if 'id' in dict_sig and dict_sig['id'] == id(self): logger.warning("Stopped infinite loop:\n{0}".format( pprint_log(dict_sig))) return # --- signals coming from the FFT window widget or the FFT window selector if dict_sig['class'] in {'Plot_FFT_win', 'QFFTWinSelector'}: if 'closeEvent' in dict_sig: # hide FFT window widget and return self.hide_fft_wdg() return else: # check for value 'fft_win*': if 'view_changed' in dict_sig and 'fft_win' in dict_sig[ 'view_changed']: # local connection to FFT window widget and qfft_win_select self.emit(dict_sig, sig_name='sig_tx_fft') # global connection to e.g. plot_impz self.emit(dict_sig) # ------------------------------------------------------------------------------ def __init__(self): super().__init__() """ Intitialize the widget, consisting of: - top chkbox row - coefficient table - two bottom rows with action buttons """ # initial settings self.N_start = 0 self.N_user = 0 self.N = 0 self.N_frame_user = 0 self.N_frame = 0 # time self.plt_time_resp = "stem" self.plt_time_stim = "line" self.plt_time_stmq = "none" self.plt_time_spgr = "none" self.bottom_t = -80 # initial value for log. scale (time) self.time_nfft_spgr = 256 # number of fft points per spectrogram segment self.time_ovlp_spgr = 128 # number of overlap points between spectrogram segments self.mode_spgr_time = "psd" # frequency self.cmb_freq_display_item = "mag" self.plt_freq_resp = "line" self.plt_freq_stim = "none" self.plt_freq_stmq = "none" self.bottom_f = -120 # initial value for log. scale self.param = None self.f_scale = fb.fil[0]['f_S'] self.t_scale = fb.fil[0]['T_S'] # list of windows that are available for FFT analysis win_names_list = [ "Boxcar", "Rectangular", "Barthann", "Bartlett", "Blackman", "Blackmanharris", "Bohman", "Cosine", "Dolph-Chebyshev", "Flattop", "General Gaussian", "Gauss", "Hamming", "Hann", "Kaiser", "Nuttall", "Parzen", "Slepian", "Triangular", "Tukey" ] self.cur_win_name = "Rectangular" # set initial window type # initialize windows dict with the list above self.win_dict = get_windows_dict(win_names_list=win_names_list, cur_win_name=self.cur_win_name) # instantiate FFT window with default windows dict self.fft_widget = Plot_FFT_win(self, self.win_dict, sym=False, title="pyFDA Spectral Window Viewer") # hide window initially, this is modeless i.e. a non-blocking popup window self.fft_widget.hide() # data / icon / tooltipp (none) for plotting styles self.plot_styles_list = [ ("Plot style"), ("none", QIcon(":/plot_style-none"), "off"), ("dots*", QIcon(":/plot_style-mkr"), "markers only"), ("line", QIcon(":/plot_style-line"), "line"), ("line*", QIcon(":/plot_style-line-mkr"), "line + markers"), ("stem", QIcon(":/plot_style-stem"), "stems"), ("stem*", QIcon(":/plot_style-stem-mkr"), "stems + markers"), ("steps", QIcon(":/plot_style-steps"), "steps"), ("steps*", QIcon(":/plot_style-steps-mkr"), "steps + markers") ] self.cmb_time_spgr_items = [ "<span>Show Spectrogram for selected signal.</span>", ("none", "None", ""), ("xn", "x[n]", "input"), ("xqn", "x_q[n]", "quantized input"), ("yn", "y[n]", "output") ] self.cmb_mode_spgr_time_items = [ "<span>Spectrogram display mode.</span>", ("psd", "PSD", "<span>Power Spectral Density, either per bin or referred to " "<i>f<sub>S</sub></i></span>"), ("magnitude", "Mag.", "Signal magnitude"), ("angle", "Angle", "Phase, wrapped to ± π"), ("phase", "Phase", "Phase (unwrapped)") ] # self.N self.cmb_freq_display_items = [ "<span>Select how to display the spectrum.</span>", ("mag", "Magnitude", "<span>Spectral magnitude</span>"), ("mag_phi", "Mag. / Phase", "<span>Magnitude and phase.</span>"), ("re_im", "Re. / Imag.", "<span>Real and imaginary part of spectrum.</span>") ] self._construct_UI() # self._enable_stim_widgets() self.update_N(emit=False) # also updates window function and win_dict # self._update_noi() def _construct_UI(self): # ----------- --------------------------------------------------- # Run control widgets # --------------------------------------------------------------- # self.but_auto_run = QPushButtonRT(text=to_html("Auto", frmt="b"), margin=0) self.but_auto_run = QPushButton(" Auto", self) self.but_auto_run.setObjectName("but_auto_run") self.but_auto_run.setToolTip( "<span>Update response automatically when " "parameters have been changed.</span>") # self.but_auto_run.setMaximumWidth(qtext_width(text=" Auto ")) self.but_auto_run.setCheckable(True) self.but_auto_run.setChecked(True) but_height = self.but_auto_run.sizeHint().height() self.but_run = QPushButton(self) self.but_run.setIcon(QIcon(":/play.svg")) self.but_run.setIconSize(QSize(but_height, but_height)) self.but_run.setFixedSize(QSize(2 * but_height, but_height)) self.but_run.setToolTip("Run simulation") self.but_run.setEnabled(True) self.cmb_sim_select = QComboBox(self) self.cmb_sim_select.addItems(["Float", "Fixpoint"]) qset_cmb_box(self.cmb_sim_select, "Float") self.cmb_sim_select.setToolTip("<span>Simulate floating-point or " "fixpoint response.</span>") self.lbl_N_points = QLabel(to_html("N", frmt='bi') + " =", self) self.led_N_points = QLineEdit(self) self.led_N_points.setText(str(self.N)) self.led_N_points.setToolTip( "<span>Last data point. " "<i>N</i> = 0 tries to choose for you.</span>") self.led_N_points.setMaximumWidth(qtext_width(N_x=8)) self.lbl_N_start = QLabel(to_html("N_0", frmt='bi') + " =", self) self.led_N_start = QLineEdit(self) self.led_N_start.setText(str(self.N_start)) self.led_N_start.setToolTip("<span>First point to plot.</span>") self.led_N_start.setMaximumWidth(qtext_width(N_x=8)) self.lbl_N_frame = QLabel(to_html("ΔN", frmt='bi') + " =", self) self.led_N_frame = QLineEdit(self) self.led_N_frame.setText(str(self.N_frame)) self.led_N_frame.setToolTip( "<span>Frame length; longer frames calculate faster but calculation cannot " "be stopped so quickly. " "<i>ΔN</i> = 0 calculates all samples in one frame.</span>") self.led_N_frame.setMaximumWidth(qtext_width(N_x=8)) self.prg_wdg = QProgressBar(self) self.prg_wdg.setFixedHeight(but_height) self.prg_wdg.setFixedWidth(qtext_width(N_x=6)) self.prg_wdg.setMinimum(0) self.prg_wdg.setValue(0) self.but_toggle_stim_options = PushButton(" Stimuli ", checked=True) self.but_toggle_stim_options.setObjectName("but_stim_options") self.but_toggle_stim_options.setToolTip( "<span>Show / hide stimulus options.</span>") self.lbl_stim_cmplx_warn = QLabel(self) self.lbl_stim_cmplx_warn = QLabel(to_html("Cmplx!", frmt='b'), self) self.lbl_stim_cmplx_warn.setToolTip( '<span>Signal is complex valued; ' 'single-sided and H<sub>id</sub> spectra may be wrong.</span>') self.lbl_stim_cmplx_warn.setStyleSheet("background-color : yellow;" "border : 1px solid grey") self.but_fft_wdg = QPushButton(self) self.but_fft_wdg.setIcon(QIcon(":/fft.svg")) self.but_fft_wdg.setIconSize(QSize(but_height, but_height)) self.but_fft_wdg.setFixedSize(QSize(int(1.5 * but_height), but_height)) self.but_fft_wdg.setToolTip( '<span>Show / hide FFT widget (select window type ' ' and display its properties).</span>') self.but_fft_wdg.setCheckable(True) self.but_fft_wdg.setChecked(False) self.qfft_win_select = QFFTWinSelector(self, self.win_dict) self.but_fx_scale = PushButton(" FX:Int ") self.but_fx_scale.setObjectName("but_fx_scale") self.but_fx_scale.setToolTip( "<span>Display data with integer (fixpoint) scale.</span>") self.but_fx_range = PushButton(" FX:Range") self.but_fx_range.setObjectName("but_fx_limits") self.but_fx_range.setToolTip( "<span>Display limits of fixpoint range.</span>") layH_ctrl_run = QHBoxLayout() layH_ctrl_run.addWidget(self.but_auto_run) layH_ctrl_run.addWidget(self.but_run) layH_ctrl_run.addWidget(self.cmb_sim_select) layH_ctrl_run.addSpacing(10) layH_ctrl_run.addWidget(self.lbl_N_start) layH_ctrl_run.addWidget(self.led_N_start) layH_ctrl_run.addWidget(self.lbl_N_points) layH_ctrl_run.addWidget(self.led_N_points) layH_ctrl_run.addWidget(self.lbl_N_frame) layH_ctrl_run.addWidget(self.led_N_frame) layH_ctrl_run.addWidget(self.prg_wdg) layH_ctrl_run.addSpacing(20) layH_ctrl_run.addWidget(self.but_toggle_stim_options) layH_ctrl_run.addSpacing(5) layH_ctrl_run.addWidget(self.lbl_stim_cmplx_warn) layH_ctrl_run.addSpacing(20) layH_ctrl_run.addWidget(self.but_fft_wdg) layH_ctrl_run.addWidget(self.qfft_win_select) layH_ctrl_run.addSpacing(20) layH_ctrl_run.addWidget(self.but_fx_scale) layH_ctrl_run.addWidget(self.but_fx_range) layH_ctrl_run.addStretch(10) # layH_ctrl_run.setContentsMargins(*params['wdg_margins']) self.wdg_ctrl_run = QWidget(self) self.wdg_ctrl_run.setLayout(layH_ctrl_run) # --- end of run control ---------------------------------------- # ----------- --------------------------------------------------- # Controls for time domain # --------------------------------------------------------------- self.lbl_plt_time_stim = QLabel(to_html("Stim. x", frmt='bi'), self) self.cmb_plt_time_stim = QComboBox(self) qcmb_box_populate(self.cmb_plt_time_stim, self.plot_styles_list, self.plt_time_stim) self.cmb_plt_time_stim.setToolTip( "<span>Plot style for stimulus.</span>") self.lbl_plt_time_stmq = QLabel( to_html(" Fixp. Stim. x_Q", frmt='bi'), self) self.cmb_plt_time_stmq = QComboBox(self) qcmb_box_populate(self.cmb_plt_time_stmq, self.plot_styles_list, self.plt_time_stmq) self.cmb_plt_time_stmq.setToolTip( "<span>Plot style for <em>fixpoint</em> " "(quantized) stimulus.</span>") lbl_plt_time_resp = QLabel(to_html(" Resp. y", frmt='bi'), self) self.cmb_plt_time_resp = QComboBox(self) qcmb_box_populate(self.cmb_plt_time_resp, self.plot_styles_list, self.plt_time_resp) self.cmb_plt_time_resp.setToolTip( "<span>Plot style for response.</span>") self.lbl_win_time = QLabel(to_html(" Win", frmt='bi'), self) self.chk_win_time = QCheckBox(self) self.chk_win_time.setObjectName("chk_win_time") self.chk_win_time.setToolTip( '<span>Plot FFT windowing function.</span>') self.chk_win_time.setChecked(False) line1 = QVLine() line2 = QVLine(width=5) self.but_log_time = PushButton(" dB") self.but_log_time.setObjectName("but_log_time") self.but_log_time.setToolTip( "<span>Logarithmic scale for y-axis.</span>") lbl_plt_time_spgr = QLabel(to_html("Spectrogram", frmt='bi'), self) self.cmb_plt_time_spgr = QComboBox(self) qcmb_box_populate(self.cmb_plt_time_spgr, self.cmb_time_spgr_items, self.plt_time_spgr) spgr_en = self.plt_time_spgr != "none" self.cmb_mode_spgr_time = QComboBox(self) qcmb_box_populate(self.cmb_mode_spgr_time, self.cmb_mode_spgr_time_items, self.mode_spgr_time) self.cmb_mode_spgr_time.setVisible(spgr_en) self.lbl_byfs_spgr_time = QLabel(to_html(" per f_S", frmt='b'), self) self.lbl_byfs_spgr_time.setVisible(spgr_en) self.chk_byfs_spgr_time = QCheckBox(self) self.chk_byfs_spgr_time.setObjectName("chk_log_spgr") self.chk_byfs_spgr_time.setToolTip("<span>Display spectral density " "i.e. scale by f_S</span>") self.chk_byfs_spgr_time.setChecked(True) self.chk_byfs_spgr_time.setVisible(spgr_en) self.but_log_spgr_time = QPushButton("dB") self.but_log_spgr_time.setMaximumWidth(qtext_width(text=" dB")) self.but_log_spgr_time.setObjectName("but_log_spgr") self.but_log_spgr_time.setToolTip( "<span>Logarithmic scale for spectrogram.</span>") self.but_log_spgr_time.setCheckable(True) self.but_log_spgr_time.setChecked(True) self.but_log_spgr_time.setVisible(spgr_en) self.lbl_time_nfft_spgr = QLabel(to_html(" N_FFT =", frmt='bi'), self) self.lbl_time_nfft_spgr.setVisible(spgr_en) self.led_time_nfft_spgr = QLineEdit(self) self.led_time_nfft_spgr.setText(str(self.time_nfft_spgr)) self.led_time_nfft_spgr.setToolTip("<span>Number of FFT points per " "spectrogram segment.</span>") self.led_time_nfft_spgr.setVisible(spgr_en) self.lbl_time_ovlp_spgr = QLabel(to_html(" N_OVLP =", frmt='bi'), self) self.lbl_time_ovlp_spgr.setVisible(spgr_en) self.led_time_ovlp_spgr = QLineEdit(self) self.led_time_ovlp_spgr.setText(str(self.time_ovlp_spgr)) self.led_time_ovlp_spgr.setToolTip( "<span>Number of overlap data points " "between spectrogram segments.</span>") self.led_time_ovlp_spgr.setVisible(spgr_en) self.lbl_log_bottom_time = QLabel(to_html("min =", frmt='bi'), self) self.led_log_bottom_time = QLineEdit(self) self.led_log_bottom_time.setText(str(self.bottom_t)) self.led_log_bottom_time.setMaximumWidth(qtext_width(N_x=8)) self.led_log_bottom_time.setToolTip( "<span>Minimum display value for time and spectrogram plots with log. scale." "</span>") self.lbl_log_bottom_time.setVisible( self.but_log_time.isChecked() or (spgr_en and self.but_log_spgr_time.isChecked())) self.led_log_bottom_time.setVisible( self.lbl_log_bottom_time.isVisible()) # self.lbl_colorbar_time = QLabel(to_html(" Col.bar", frmt='b'), self) # self.lbl_colorbar_time.setVisible(spgr_en) # self.chk_colorbar_time = QCheckBox(self) # self.chk_colorbar_time.setObjectName("chk_colorbar_time") # self.chk_colorbar_time.setToolTip("<span>Enable colorbar</span>") # self.chk_colorbar_time.setChecked(True) # self.chk_colorbar_time.setVisible(spgr_en) layH_ctrl_time = QHBoxLayout() layH_ctrl_time.addWidget(self.lbl_plt_time_stim) layH_ctrl_time.addWidget(self.cmb_plt_time_stim) # layH_ctrl_time.addWidget(self.lbl_plt_time_stmq) layH_ctrl_time.addWidget(self.cmb_plt_time_stmq) # layH_ctrl_time.addWidget(lbl_plt_time_resp) layH_ctrl_time.addWidget(self.cmb_plt_time_resp) # layH_ctrl_time.addWidget(self.lbl_win_time) layH_ctrl_time.addWidget(self.chk_win_time) layH_ctrl_time.addSpacing(5) layH_ctrl_time.addWidget(line1) layH_ctrl_time.addSpacing(5) # layH_ctrl_time.addWidget(self.lbl_log_bottom_time) layH_ctrl_time.addWidget(self.led_log_bottom_time) layH_ctrl_time.addWidget(self.but_log_time) layH_ctrl_time.addSpacing(5) layH_ctrl_time.addWidget(line2) layH_ctrl_time.addSpacing(5) # layH_ctrl_time.addWidget(lbl_plt_time_spgr) layH_ctrl_time.addWidget(self.cmb_plt_time_spgr) layH_ctrl_time.addWidget(self.cmb_mode_spgr_time) layH_ctrl_time.addWidget(self.lbl_byfs_spgr_time) layH_ctrl_time.addWidget(self.chk_byfs_spgr_time) layH_ctrl_time.addWidget(self.but_log_spgr_time) layH_ctrl_time.addWidget(self.lbl_time_nfft_spgr) layH_ctrl_time.addWidget(self.led_time_nfft_spgr) layH_ctrl_time.addWidget(self.lbl_time_ovlp_spgr) layH_ctrl_time.addWidget(self.led_time_ovlp_spgr) layH_ctrl_time.addStretch(10) # layH_ctrl_time.setContentsMargins(*params['wdg_margins']) self.wdg_ctrl_time = QWidget(self) self.wdg_ctrl_time.setLayout(layH_ctrl_time) # ---- end time domain ------------------ # --------------------------------------------------------------- # Controls for frequency domain # --------------------------------------------------------------- self.lbl_plt_freq_stim = QLabel(to_html("Stimulus X", frmt='bi'), self) self.cmb_plt_freq_stim = QComboBox(self) qcmb_box_populate(self.cmb_plt_freq_stim, self.plot_styles_list, self.plt_freq_stim) self.cmb_plt_freq_stim.setToolTip( "<span>Plot style for stimulus.</span>") self.lbl_plt_freq_stmq = QLabel( to_html(" Fixp. Stim. X_Q", frmt='bi'), self) self.cmb_plt_freq_stmq = QComboBox(self) qcmb_box_populate(self.cmb_plt_freq_stmq, self.plot_styles_list, self.plt_freq_stmq) self.cmb_plt_freq_stmq.setToolTip( "<span>Plot style for <em>fixpoint</em> (quantized) stimulus.</span>" ) lbl_plt_freq_resp = QLabel(to_html(" Response Y", frmt='bi'), self) self.cmb_plt_freq_resp = QComboBox(self) qcmb_box_populate(self.cmb_plt_freq_resp, self.plot_styles_list, self.plt_freq_resp) self.cmb_plt_freq_resp.setToolTip( "<span>Plot style for response.</span>") self.but_log_freq = QPushButton("dB") self.but_log_freq.setMaximumWidth(qtext_width(" dB")) self.but_log_freq.setObjectName(".but_log_freq") self.but_log_freq.setToolTip( "<span>Logarithmic scale for y-axis.</span>") self.but_log_freq.setCheckable(True) self.but_log_freq.setChecked(True) self.lbl_log_bottom_freq = QLabel(to_html("min =", frmt='bi'), self) self.lbl_log_bottom_freq.setVisible(self.but_log_freq.isChecked()) self.led_log_bottom_freq = QLineEdit(self) self.led_log_bottom_freq.setText(str(self.bottom_f)) self.led_log_bottom_freq.setMaximumWidth(qtext_width(N_x=8)) self.led_log_bottom_freq.setToolTip( "<span>Minimum display value for log. scale.</span>") self.led_log_bottom_freq.setVisible(self.but_log_freq.isChecked()) if not self.but_log_freq.isChecked(): self.bottom_f = 0 self.cmb_freq_display = QComboBox(self) qcmb_box_populate(self.cmb_freq_display, self.cmb_freq_display_items, self.cmb_freq_display_item) self.cmb_freq_display.setObjectName("cmb_re_im_freq") self.but_Hf = QPushButtonRT(self, to_html("H_id", frmt="bi"), margin=5) self.but_Hf.setObjectName("chk_Hf") self.but_Hf.setToolTip( "<span>Show ideal frequency response, calculated " "from the filter coefficients.</span>") self.but_Hf.setChecked(False) self.but_Hf.setCheckable(True) self.but_freq_norm_impz = QPushButtonRT( text="<b><i>E<sub>X</sub></i> = 1</b>", margin=5) self.but_freq_norm_impz.setToolTip( "<span>Normalize the FFT of the stimulus with <i>N<sub>FFT</sub></i> for " "<i>E<sub>X</sub></i> = 1. For a dirac pulse, this yields " "|<i>Y(f)</i>| = |<i>H(f)</i>|. DC and Noise need to be " "turned off, window should be <b>Rectangular</b>.</span>") self.but_freq_norm_impz.setCheckable(True) self.but_freq_norm_impz.setChecked(True) self.but_freq_norm_impz.setObjectName("freq_norm_impz") self.but_freq_show_info = QPushButton("Info", self) self.but_freq_show_info.setMaximumWidth(qtext_width(" Info ")) self.but_freq_show_info.setObjectName("but_show_info_freq") self.but_freq_show_info.setToolTip( "<span>Show signal power in legend.</span>") self.but_freq_show_info.setCheckable(True) self.but_freq_show_info.setChecked(False) layH_ctrl_freq = QHBoxLayout() layH_ctrl_freq.addWidget(self.lbl_plt_freq_stim) layH_ctrl_freq.addWidget(self.cmb_plt_freq_stim) # layH_ctrl_freq.addWidget(self.lbl_plt_freq_stmq) layH_ctrl_freq.addWidget(self.cmb_plt_freq_stmq) # layH_ctrl_freq.addWidget(lbl_plt_freq_resp) layH_ctrl_freq.addWidget(self.cmb_plt_freq_resp) # layH_ctrl_freq.addSpacing(5) layH_ctrl_freq.addWidget(self.but_Hf) layH_ctrl_freq.addStretch(1) # layH_ctrl_freq.addWidget(self.lbl_log_bottom_freq) layH_ctrl_freq.addWidget(self.led_log_bottom_freq) layH_ctrl_freq.addWidget(self.but_log_freq) layH_ctrl_freq.addStretch(1) layH_ctrl_freq.addWidget(self.cmb_freq_display) layH_ctrl_freq.addStretch(1) layH_ctrl_freq.addWidget(self.but_freq_norm_impz) layH_ctrl_freq.addStretch(1) layH_ctrl_freq.addWidget(self.but_freq_show_info) layH_ctrl_freq.addStretch(10) # layH_ctrl_freq.setContentsMargins(*params['wdg_margins']) self.wdg_ctrl_freq = QWidget(self) self.wdg_ctrl_freq.setLayout(layH_ctrl_freq) # ---- end Frequency Domain ------------------ # ---------------------------------------------------------------------- # GLOBAL SIGNALS & SLOTs # ---------------------------------------------------------------------- # connect FFT widget to qfft_selector and vice versa and to and signals upstream: self.fft_widget.sig_tx.connect(self.process_sig_rx) self.qfft_win_select.sig_tx.connect(self.process_sig_rx) # connect process_sig_rx output to both FFT widgets self.sig_tx_fft.connect(self.fft_widget.sig_rx) self.sig_tx_fft.connect(self.qfft_win_select.sig_rx) # ---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs # ---------------------------------------------------------------------- # --- run control --- self.led_N_start.editingFinished.connect(self.update_N) self.led_N_points.editingFinished.connect(self.update_N) self.led_N_frame.editingFinished.connect(self.update_N) self.but_fft_wdg.clicked.connect(self.toggle_fft_wdg) # ------------------------------------------------------------------------- def update_N(self, emit=True): """ Update values for `self.N` and `self.win_dict['N']`, for `self.N_start` and `self.N_end` from the corresponding QLineEditWidgets. When `emit==True`, fire `'ui_changed': 'N'` to update the FFT window and the `plot_impz` widgets. In contrast to `view_changed`, this also forces a recalculation of the transient response. This method is called by: - `self._construct_ui()` with `emit==False` - `plot_impz()` with `emit==False` when the automatic calculation of N has to be updated (e.g. order of FIR Filter has changed - signal-slot connection when `N_start` or `N_end` QLineEdit widgets have been changed (`emit==True`) """ if not isinstance(emit, bool): logger.error("update N: emit={0}".format(emit)) self.N_start = safe_eval(self.led_N_start.text(), self.N_start, return_type='int', sign='poszero') self.led_N_start.setText(str(self.N_start)) # update widget self.N_user = safe_eval(self.led_N_points.text(), self.N_user, return_type='int', sign='poszero') if self.N_user == 0: # automatic calculation self.N = self.calc_n_points(self.N_user) # widget remains set to 0 self.led_N_points.setText("0") # update widget else: self.N = self.N_user self.led_N_points.setText(str(self.N)) # update widget # total number of points to be calculated: N + N_start self.N_end = self.N + self.N_start self.N_frame_user = safe_eval(self.led_N_frame.text(), self.N_frame_user, return_type='int', sign='poszero') if self.N_frame_user == 0: self.N_frame = self.N_end # use N_end for frame length self.led_N_frame.setText( "0") # update widget with "0" as set by user else: self.N_frame = self.N_frame_user self.led_N_frame.setText(str(self.N_frame)) # update widget # recalculate displayed freq. index values when freq. unit == 'k' if fb.fil[0]['freq_specs_unit'] == 'k': self.update_freqs() if emit: # use `'ui_changed'` as this triggers recalculation of the transient # response self.emit({'ui_changed': 'N'}) # ------------------------------------------------------------------------------ def toggle_fft_wdg(self): """ Show / hide FFT widget depending on the state of the corresponding button When widget is shown, trigger an update of the window function. """ if self.but_fft_wdg.isChecked(): self.fft_widget.show() self.emit({'view_changed': 'fft_win_type'}, sig_name='sig_tx_fft') else: self.fft_widget.hide() # -------------------------------------------------------------------------- def hide_fft_wdg(self): """ The closeEvent caused by clicking the "x" in the FFT widget is caught there and routed here to only hide the window """ self.but_fft_wdg.setChecked(False) self.fft_widget.hide() # ------------------------------------------------------------------------------ def calc_n_points(self, N_user=0): """ Calculate number of points to be displayed, depending on type of filter (FIR, IIR) and user input. If the user selects 0 points, the number is calculated automatically. An improvement would be to calculate the dominant pole and the corresponding settling time. """ if N_user == 0: # set number of data points automatically if fb.fil[0]['ft'] == 'IIR': # IIR: No algorithm yet, set N = 100 N = 100 else: # FIR: N = number of coefficients (max. 100) N = min(len(fb.fil[0]['ba'][0]), 100) else: N = N_user return N
class Firwin(QWidget): FRMT = 'ba' # output format(s) of filter design routines 'zpk' / 'ba' / 'sos' # currently, only 'ba' is supported for firwin routines sig_tx = pyqtSignal( object) # local signal between FFT widget and FFTWin_Selector sig_tx_local = pyqtSignal(object) from pyfda.libs.pyfda_qt_lib import emit def __init__(self): QWidget.__init__(self) self.ft = 'FIR' win_names_list = [ "Boxcar", "Rectangular", "Barthann", "Bartlett", "Blackman", "Blackmanharris", "Bohman", "Cosine", "Dolph-Chebyshev", "Flattop", "General Gaussian", "Gauss", "Hamming", "Hann", "Kaiser", "Nuttall", "Parzen", "Slepian", "Triangular", "Tukey" ] self.cur_win_name = "Kaiser" # set initial window type self.alg = "ichige" # initialize windows dict with the list above for firwin window settings self.win_dict = get_windows_dict(win_names_list=win_names_list, cur_win_name=self.cur_win_name) # get initial / last setting from dictionary, updating self.win_dict self._load_dict() # instantiate FFT window with windows dict self.fft_widget = Plot_FFT_win(self, win_dict=self.win_dict, sym=True, title="pyFDA FIR Window Viewer") # hide window initially, this is modeless i.e. a non-blocking popup window self.fft_widget.hide() c = Common() self.rt_dict = c.rt_base_iir self.rt_dict_add = { 'COM': { 'min': { 'msg': ('a', "<br /><b>Note:</b> Filter order is only a rough " "approximation and most likely far too low!") }, 'man': { 'msg': ('a', "Enter desired filter order <b><i>N</i></b> and " "<b>-6 dB</b> pass band corner " "frequency(ies) <b><i>F<sub>C</sub></i></b> .") }, }, 'LP': { 'man': {}, 'min': {} }, 'HP': { 'man': { 'msg': ('a', r"<br /><b>Note:</b> Order needs to be odd!") }, 'min': {} }, 'BS': { 'man': { 'msg': ('a', r"<br /><b>Note:</b> Order needs to be odd!") }, 'min': {} }, 'BP': { 'man': {}, 'min': {} }, } self.info = """**Windowed FIR filters** are designed by truncating the infinite impulse response of an ideal filter with a window function. The kind of used window has strong influence on ripple etc. of the resulting filter. **Design routines:** ``scipy.signal.firwin()`` """ # self.info_doc = [] is set in self._update_UI() # ------------------- end of static info for filter tree --------------- # ------------------------------------------------------------------------------ def process_sig_rx(self, dict_sig=None): """ Process local signals from / for - FFT window widget - qfft_win_select """ logger.debug("SIG_RX - vis: {0}\n{1}".format(self.isVisible(), pprint_log(dict_sig))) if dict_sig['id'] == id(self): logger.warning(f"Stopped infinite loop:\n{pprint_log(dict_sig)}") # --- signals coming from the FFT window widget or the qfft_win_select if dict_sig['class'] in {'Plot_FFT_win', 'QFFTWinSelector'}: if 'closeEvent' in dict_sig: # hide FFT window windget and return self.hide_fft_wdg() return else: if 'view_changed' in dict_sig and 'fft_win' in dict_sig[ 'view_changed']: # self._update_fft_window() # TODO: needed? # local connection to FFT window widget and qfft_win_select self.emit(dict_sig, sig_name='sig_tx_local') # global connection to upper hierachies # send notification that filter design has changed self.emit({'filt_changed': 'firwin'}) # -------------------------------------------------------------------------- def construct_UI(self): """ Create additional subwidget(s) needed for filter design: These subwidgets are instantiated dynamically when needed in select_filter.py using the handle to the filter object, fb.filObj . """ # Combobox for selecting the algorithm to estimate minimum filter order self.cmb_firwin_alg = QComboBox(self) self.cmb_firwin_alg.setObjectName('wdg_cmb_firwin_alg') self.cmb_firwin_alg.addItems(['ichige', 'kaiser', 'herrmann']) # Minimum size, can be changed in the upper hierarchy levels using layouts: self.cmb_firwin_alg.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.cmb_firwin_alg.hide() self.qfft_win_select = QFFTWinSelector(self, self.win_dict) # Minimum size, can be changed in the upper hierarchy levels using layouts: # self.qfft_win_select.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.but_fft_wdg = QPushButton(self) self.but_fft_wdg.setIcon(QIcon(":/fft.svg")) but_height = self.qfft_win_select.sizeHint().height() self.but_fft_wdg.setIconSize(QSize(but_height, but_height)) self.but_fft_wdg.setFixedSize(QSize(but_height, but_height)) self.but_fft_wdg.setToolTip( '<span>Show / hide FFT widget (select window type ' ' and display its properties).</span>') self.but_fft_wdg.setCheckable(True) self.but_fft_wdg.setChecked(False) self.layHWin1 = QHBoxLayout() # self.layHWin1.addWidget(self.cmb_firwin_win) # self.layHWin1.addWidget(self.but_fft_wdg) self.layHWin1.addWidget(self.cmb_firwin_alg) self.layHWin2 = QHBoxLayout() self.layHWin2.addWidget(self.but_fft_wdg) self.layHWin2.addWidget(self.qfft_win_select) self.layVWin = QVBoxLayout() self.layVWin.addLayout(self.layHWin1) self.layVWin.addLayout(self.layHWin2) self.layVWin.setContentsMargins(0, 0, 0, 0) # Widget containing all subwidgets (cmbBoxes, Labels, lineEdits) self.wdg_fil = QWidget(self) self.wdg_fil.setObjectName('wdg_fil') self.wdg_fil.setLayout(self.layVWin) # ---------------------------------------------------------------------- # GLOBAL SIGNALS & SLOTs # ---------------------------------------------------------------------- # connect FFT widget to qfft_selector and vice versa and to signals upstream: self.fft_widget.sig_tx.connect(self.process_sig_rx) self.qfft_win_select.sig_tx.connect(self.process_sig_rx) # connect process_sig_rx output to both FFT widgets self.sig_tx_local.connect(self.fft_widget.sig_rx) self.sig_tx_local.connect(self.qfft_win_select.sig_rx) # ---------------------------------------------------------------------- # SIGNALS & SLOTs # ---------------------------------------------------------------------- self.cmb_firwin_alg.currentIndexChanged.connect( self._update_fft_window) self.but_fft_wdg.clicked.connect(self.toggle_fft_wdg) # ---------------------------------------------------------------------- # ============================================================================== def _update_fft_window(self): """ Update window type for FirWin - unneeded at the moment """ self.alg = str(self.cmb_firwin_alg.currentText()) self.emit({'filt_changed': 'firwin'}) # -------------------------------------------------------------------------- def _load_dict(self): """ Reload window selection and parameters from filter dictionary and set UI elements accordingly. load_dict() is called upon initialization and when the filter is loaded from disk. """ self.N = fb.fil[0]['N'] # alg_idx = 0 if 'wdg_fil' in fb.fil[0] and 'firwin' in fb.fil[0]['wdg_fil']\ and type(fb.fil[0]['wdg_fil']['firwin']) is dict: self.win_dict = fb.fil[0]['wdg_fil']['firwin'] self.emit({'view_changed': 'fft_win_type'}, sig_name='sig_tx_local') # -------------------------------------------------------------------------- def _store_dict(self): """ Store window and parameter settings using `self.win_dict` in filter dictionary. """ if 'wdg_fil' not in fb.fil[0]: fb.fil[0].update({'wdg_fil': {}}) fb.fil[0]['wdg_fil'].update({'firwin': self.win_dict}) # -------------------------------------------------------------------------- def _get_params(self, fil_dict): """ Translate parameters from the passed dictionary to instance parameters, scaling / transforming them if needed. """ self.N = fil_dict['N'] self.F_PB = fil_dict['F_PB'] self.F_SB = fil_dict['F_SB'] self.F_PB2 = fil_dict['F_PB2'] self.F_SB2 = fil_dict['F_SB2'] self.F_C = fil_dict['F_C'] self.F_C2 = fil_dict['F_C2'] # firwin amplitude specs are linear (not in dBs) self.A_PB = fil_dict['A_PB'] self.A_PB2 = fil_dict['A_PB2'] self.A_SB = fil_dict['A_SB'] self.A_SB2 = fil_dict['A_SB2'] # self.alg = 'ichige' # algorithm for determining the minimum order # self.alg = self.cmb_firwin_alg.currentText() def _test_N(self): """ Warn the user if the calculated order is too high for a reasonable filter design. """ if self.N > 1000: return qfilter_warning(self, self.N, "FirWin") else: return True def _save(self, fil_dict, arg): """ Convert between poles / zeros / gain, filter coefficients (polynomes) and second-order sections and store all available formats in the passed dictionary 'fil_dict'. """ fil_save(fil_dict, arg, self.FRMT, __name__) try: # has the order been calculated by a "min" filter design? fil_dict['N'] = self.N # yes, update filterbroker except AttributeError: pass self._store_dict() # ------------------------------------------------------------------------------ def firwin(self, numtaps, cutoff, window=None, pass_zero=True, scale=True, nyq=1.0, fs=None): """ FIR filter design using the window method. This is more or less the same as `scipy.signal.firwin` with the exception that an ndarray with the window values can be passed as an alternative to the window name. The parameters "width" (specifying a Kaiser window) and "fs" have been omitted, they are not needed here. This function computes the coefficients of a finite impulse response filter. The filter will have linear phase; it will be Type I if `numtaps` is odd and Type II if `numtaps` is even. Type II filters always have zero response at the Nyquist rate, so a ValueError exception is raised if firwin is called with `numtaps` even and having a passband whose right end is at the Nyquist rate. Parameters ---------- numtaps : int Length of the filter (number of coefficients, i.e. the filter order + 1). `numtaps` must be even if a passband includes the Nyquist frequency. cutoff : float or 1D array_like Cutoff frequency of filter (expressed in the same units as `nyq`) OR an array of cutoff frequencies (that is, band edges). In the latter case, the frequencies in `cutoff` should be positive and monotonically increasing between 0 and `nyq`. The values 0 and `nyq` must not be included in `cutoff`. window : ndarray or string string: use the window with the passed name from scipy.signal.windows ndarray: The window values - this is an addition to the original firwin routine. pass_zero : bool, optional If True, the gain at the frequency 0 (i.e. the "DC gain") is 1. Otherwise the DC gain is 0. scale : bool, optional Set to True to scale the coefficients so that the frequency response is exactly unity at a certain frequency. That frequency is either: - 0 (DC) if the first passband starts at 0 (i.e. pass_zero is True) - `nyq` (the Nyquist rate) if the first passband ends at `nyq` (i.e the filter is a single band highpass filter); center of first passband otherwise nyq : float, optional Nyquist frequency. Each frequency in `cutoff` must be between 0 and `nyq`. Returns ------- h : (numtaps,) ndarray Coefficients of length `numtaps` FIR filter. Raises ------ ValueError If any value in `cutoff` is less than or equal to 0 or greater than or equal to `nyq`, if the values in `cutoff` are not strictly monotonically increasing, or if `numtaps` is even but a passband includes the Nyquist frequency. See also -------- scipy.firwin """ cutoff = np.atleast_1d(cutoff) / float(nyq) # Check for invalid input. if cutoff.ndim > 1: raise ValueError("The cutoff argument must be at most " "one-dimensional.") if cutoff.size == 0: raise ValueError("At least one cutoff frequency must be given.") if cutoff.min() <= 0 or cutoff.max() >= 1: raise ValueError( "Invalid cutoff frequency {0}: frequencies must be " "greater than 0 and less than nyq.".format(cutoff)) if np.any(np.diff(cutoff) <= 0): raise ValueError("Invalid cutoff frequencies: the frequencies " "must be strictly increasing.") pass_nyquist = bool(cutoff.size & 1) ^ pass_zero if pass_nyquist and numtaps % 2 == 0: raise ValueError( "A filter with an even number of coefficients must " "have zero response at the Nyquist rate.") # Insert 0 and/or 1 at the ends of cutoff so that the length of cutoff # is even, and each pair in cutoff corresponds to passband. cutoff = np.hstack(([0.0] * pass_zero, cutoff, [1.0] * pass_nyquist)) # `bands` is a 2D array; each row gives the left and right edges of # a passband. bands = cutoff.reshape(-1, 2) # Build up the coefficients. alpha = 0.5 * (numtaps - 1) m = np.arange(0, numtaps) - alpha h = 0 for left, right in bands: h += right * sinc(right * m) h -= left * sinc(left * m) if type(window) == str: # Get and apply the window function. # from scipy.signal.signaltools import get_window win = signaltools.get_window(window, numtaps, fftbins=False) elif type(window) == np.ndarray: win = window else: logger.error( "The 'window' was neither a string nor a numpy array, " "it could not be evaluated.") return None # apply the window function. h *= win # Now handle scaling if desired. if scale: # Get the first passband. left, right = bands[0] if left == 0: scale_frequency = 0.0 elif right == 1: scale_frequency = 1.0 else: scale_frequency = 0.5 * (left + right) c = np.cos(np.pi * m * scale_frequency) s = np.sum(h * c) h /= s return h def _firwin_ord(self, F, W, A, alg): # http://www.mikroe.com/chapters/view/72/chapter-2-fir-filters/ delta_f = abs(F[1] - F[0]) * 2 # referred to f_Ny # delta_A = np.sqrt(A[0] * A[1]) if "Kaiser" in self.win_dict and self.win_dict[ 'cur_win_name'] == "Kaiser": N, beta = sig.kaiserord(20 * np.log10(np.abs(fb.fil[0]['A_SB'])), delta_f) # logger.warning(f"N={N}, beta={beta}, A_SB={fb.fil[0]['A_SB']}") self.win_dict["Kaiser"]["par"][0]["val"] = beta self.qfft_win_select.led_win_par_0.setText(str(beta)) self.qfft_win_select.ui2dict_params( ) # pass changed parameter to other widgets else: N = remezord(F, W, A, fs=1, alg=alg)[0] self.emit({'view_changed': 'fft_win_type'}, sig_name='sig_tx_local') return N def LPmin(self, fil_dict): self._get_params(fil_dict) self.N = self._firwin_ord([self.F_PB, self.F_SB], [1, 0], [self.A_PB, self.A_SB], alg=self.alg) if not self._test_N(): return -1 fil_dict['F_C'] = (self.F_SB + self.F_PB) / 2 # average calculated F_PB and F_SB self._save( fil_dict, self.firwin(self.N, fil_dict['F_C'], nyq=0.5, window=self.qfft_win_select.get_window(self.N, sym=True))) def LPman(self, fil_dict): self._get_params(fil_dict) if not self._test_N(): return -1 logger.warning(self.win_dict["cur_win_name"]) self._save( fil_dict, self.firwin(self.N, fil_dict['F_C'], nyq=0.5, window=self.qfft_win_select.get_window(self.N, sym=True))) def HPmin(self, fil_dict): self._get_params(fil_dict) N = self._firwin_ord([self.F_SB, self.F_PB], [0, 1], [self.A_SB, self.A_PB], alg=self.alg) self.N = round_odd(N) # enforce odd order if not self._test_N(): return -1 fil_dict['F_C'] = (self.F_SB + self.F_PB) / 2 # average calculated F_PB and F_SB self._save( fil_dict, self.firwin(self.N, fil_dict['F_C'], pass_zero=False, nyq=0.5, window=self.qfft_win_select.get_window(self.N, sym=True))) def HPman(self, fil_dict): self._get_params(fil_dict) self.N = round_odd(self.N) # enforce odd order if not self._test_N(): return -1 self._save( fil_dict, self.firwin(self.N, fil_dict['F_C'], pass_zero=False, nyq=0.5, window=self.qfft_win_select.get_window(self.N, sym=True))) # For BP and BS, F_PB and F_SB have two elements each def BPmin(self, fil_dict): self._get_params(fil_dict) self.N = remezord([self.F_SB, self.F_PB, self.F_PB2, self.F_SB2], [0, 1, 0], [self.A_SB, self.A_PB, self.A_SB2], fs=1, alg=self.alg)[0] if not self._test_N(): return -1 fil_dict['F_C'] = (self.F_SB + self.F_PB) / 2 # average calculated F_PB and F_SB fil_dict['F_C2'] = (self.F_SB2 + self.F_PB2) / 2 self._save( fil_dict, self.firwin(self.N, [fil_dict['F_C'], fil_dict['F_C2']], nyq=0.5, pass_zero=False, window=self.qfft_win_select.get_window(self.N, sym=True))) def BPman(self, fil_dict): self._get_params(fil_dict) if not self._test_N(): return -1 self._save( fil_dict, self.firwin(self.N, [fil_dict['F_C'], fil_dict['F_C2']], nyq=0.5, pass_zero=False, window=self.qfft_win_select.get_window(self.N, sym=True))) def BSmin(self, fil_dict): self._get_params(fil_dict) N = remezord([self.F_PB, self.F_SB, self.F_SB2, self.F_PB2], [1, 0, 1], [self.A_PB, self.A_SB, self.A_PB2], fs=1, alg=self.alg)[0] self.N = round_odd(N) # enforce odd order if not self._test_N(): return -1 fil_dict['F_C'] = (self.F_SB + self.F_PB) / 2 # average calculated F_PB and F_SB fil_dict['F_C2'] = (self.F_SB2 + self.F_PB2) / 2 self._save( fil_dict, self.firwin(self.N, [fil_dict['F_C'], fil_dict['F_C2']], window=self.qfft_win_select.get_window(self.N, sym=True), pass_zero=True, nyq=0.5)) def BSman(self, fil_dict): self._get_params(fil_dict) self.N = round_odd(self.N) # enforce odd order if not self._test_N(): return -1 self._save( fil_dict, self.firwin(self.N, [fil_dict['F_C'], fil_dict['F_C2']], window=self.qfft_win_select.get_window(self.N, sym=True), pass_zero=True, nyq=0.5)) # ------------------------------------------------------------------------------ def toggle_fft_wdg(self): """ Show / hide FFT widget depending on the state of the corresponding button When widget is shown, trigger an update of the window function. """ if self.but_fft_wdg.isChecked(): self.fft_widget.show() self.emit({'view_changed': 'fft_win_type'}, sig_name='sig_tx_local') else: self.fft_widget.hide() # -------------------------------------------------------------------------- def hide_fft_wdg(self): """ The closeEvent caused by clicking the "x" in the FFT widget is caught there and routed here to only hide the window """ self.but_fft_wdg.setChecked(False) self.fft_widget.hide()
class Firwin(QWidget): FRMT = 'ba' # output format(s) of filter design routines 'zpk' / 'ba' / 'sos' # currently, only 'ba' is supported for firwin routines sig_tx = pyqtSignal(object) def __init__(self): QWidget.__init__(self) self.ft = 'FIR' self.fft_window = None # dictionary for firwin window settings self.win_dict = fb.fil[0]['win_fir'] c = Common() self.rt_dict = c.rt_base_iir self.rt_dict_add = { 'COM': { 'min': { 'msg': ('a', r"<br /><b>Note:</b> Filter order is only a rough approximation " "and most likely far too low!") }, 'man': { 'msg': ('a', r"Enter desired filter order <b><i>N</i></b> and " "<b>-6 dB</b> pass band corner " "frequency(ies) <b><i>F<sub>C</sub></i></b> .") }, }, 'LP': { 'man': {}, 'min': {} }, 'HP': { 'man': { 'msg': ('a', r"<br /><b>Note:</b> Order needs to be odd!") }, 'min': {} }, 'BS': { 'man': { 'msg': ('a', r"<br /><b>Note:</b> Order needs to be odd!") }, 'min': {} }, 'BP': { 'man': {}, 'min': {} }, } self.info = """**Windowed FIR filters** are designed by truncating the infinite impulse response of an ideal filter with a window function. The kind of used window has strong influence on ripple etc. of the resulting filter. **Design routines:** ``scipy.signal.firwin()`` """ #self.info_doc = [] is set in self._update_UI() #------------------- end of static info for filter tree --------------- #---------------------------------------------------------------------- def construct_UI(self): """ Create additional subwidget(s) needed for filter design: These subwidgets are instantiated dynamically when needed in select_filter.py using the handle to the filter object, fb.filObj . """ # Combobox for selecting the algorithm to estimate minimum filter order self.cmb_firwin_alg = QComboBox(self) self.cmb_firwin_alg.setObjectName('wdg_cmb_firwin_alg') self.cmb_firwin_alg.addItems(['ichige', 'kaiser', 'herrmann']) # Minimum size, can be changed in the upper hierarchy levels using layouts: self.cmb_firwin_alg.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.cmb_firwin_alg.hide() # Combobox for selecting the window used for filter design self.cmb_firwin_win = QComboBox(self) self.cmb_firwin_win.addItems(get_window_names()) self.cmb_firwin_win.setObjectName('wdg_cmb_firwin_win') # Minimum size, can be changed in the upper hierarchy levels using layouts: self.cmb_firwin_win.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.but_fft_win = QPushButton(self) self.but_fft_win.setText("WIN FFT") self.but_fft_win.setToolTip( "Show time and frequency response of FFT Window") self.but_fft_win.setCheckable(True) self.but_fft_win.setChecked(False) self.lblWinPar1 = QLabel("a", self) self.lblWinPar1.setObjectName('wdg_lbl_firwin_1') self.ledWinPar1 = QLineEdit(self) self.ledWinPar1.setText("0.5") self.ledWinPar1.setObjectName('wdg_led_firwin_1') self.lblWinPar1.setVisible(False) self.ledWinPar1.setVisible(False) self.lblWinPar2 = QLabel("b", self) self.lblWinPar2.setObjectName('wdg_lbl_firwin_2') self.ledWinPar2 = QLineEdit(self) self.ledWinPar2.setText("0.5") self.ledWinPar2.setObjectName('wdg_led_firwin_2') self.ledWinPar2.setVisible(False) self.lblWinPar2.setVisible(False) self.layHWin1 = QHBoxLayout() self.layHWin1.addWidget(self.cmb_firwin_win) self.layHWin1.addWidget(self.but_fft_win) self.layHWin1.addWidget(self.cmb_firwin_alg) self.layHWin2 = QHBoxLayout() self.layHWin2.addWidget(self.lblWinPar1) self.layHWin2.addWidget(self.ledWinPar1) self.layHWin2.addWidget(self.lblWinPar2) self.layHWin2.addWidget(self.ledWinPar2) self.layVWin = QVBoxLayout() self.layVWin.addLayout(self.layHWin1) self.layVWin.addLayout(self.layHWin2) self.layVWin.setContentsMargins(0, 0, 0, 0) # Widget containing all subwidgets (cmbBoxes, Labels, lineEdits) self.wdg_fil = QWidget(self) self.wdg_fil.setObjectName('wdg_fil') self.wdg_fil.setLayout(self.layVWin) #---------------------------------------------------------------------- # SIGNALS & SLOTs #---------------------------------------------------------------------- self.cmb_firwin_alg.activated.connect(self._update_win_fft) self.cmb_firwin_win.activated.connect(self._update_win_fft) self.ledWinPar1.editingFinished.connect(self._read_param1) self.ledWinPar2.editingFinished.connect(self._read_param2) self.but_fft_win.clicked.connect(self.show_fft_win) #---------------------------------------------------------------------- self._load_dict() # get initial / last setting from dictionary self._update_win_fft() #============================================================================= # Copied from impz() #============================================================================== def _read_param1(self): """Read out textbox when editing is finished and update dict and fft window""" param = safe_eval(self.ledWinPar1.text(), self.win_dict['par'][0]['val'], sign='pos', return_type='float') if param < self.win_dict['par'][0]['min']: param = self.win_dict['par'][0]['min'] elif param > self.win_dict['par'][0]['max']: param = self.win_dict['par'][0]['max'] self.ledWinPar1.setText(str(param)) self.win_dict['par'][0]['val'] = param self._update_win_fft() def _read_param2(self): """Read out textbox when editing is finished and update dict and fft window""" param = safe_eval(self.ledWinPar2.text(), self.win_dict['par'][1]['val'], return_type='float') if param < self.win_dict['par'][1]['min']: param = self.win_dict['par'][1]['min'] elif param > self.win_dict['par'][1]['max']: param = self.win_dict['par'][1]['max'] self.ledWinPar2.setText(str(param)) self.win_dict['par'][1]['val'] = param self._update_win_fft() def _update_win_fft(self): """ Update window type for FirWin """ self.alg = str(self.cmb_firwin_alg.currentText()) self.fir_window_name = qget_cmb_box(self.cmb_firwin_win, data=False) self.win = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) n_par = self.win_dict['n_par'] self.lblWinPar1.setVisible(n_par > 0) self.ledWinPar1.setVisible(n_par > 0) self.lblWinPar2.setVisible(n_par > 1) self.ledWinPar2.setVisible(n_par > 1) if n_par > 0: self.lblWinPar1.setText( to_html(self.win_dict['par'][0]['name'] + " =", frmt='bi')) self.ledWinPar1.setText(str(self.win_dict['par'][0]['val'])) self.ledWinPar1.setToolTip(self.win_dict['par'][0]['tooltip']) if n_par > 1: self.lblWinPar2.setText( to_html(self.win_dict['par'][1]['name'] + " =", frmt='bi')) self.ledWinPar2.setText(str(self.win_dict['par'][1]['val'])) self.ledWinPar2.setToolTip(self.win_dict['par'][1]['tooltip']) # sig_tx -> select_filter -> filter_specs self.sig_tx.emit({'sender': __name__, 'filt_changed': 'firwin'}) #============================================================================= def _load_dict(self): """ Reload window selection and parameters from filter dictionary and set UI elements accordingly. load_dict() is called upon initialization and when the filter is loaded from disk. """ self.N = fb.fil[0]['N'] win_idx = 0 alg_idx = 0 if 'wdg_fil' in fb.fil[0] and 'firwin' in fb.fil[0]['wdg_fil']: wdg_fil_par = fb.fil[0]['wdg_fil']['firwin'] if 'win' in wdg_fil_par: if np.isscalar( wdg_fil_par['win']): # true for strings (non-vectors) window = wdg_fil_par['win'] else: window = wdg_fil_par['win'][0] self.ledWinPar1.setText(str(wdg_fil_par['win'][1])) if len(wdg_fil_par['win']) > 2: self.ledWinPar2.setText(str(wdg_fil_par['win'][2])) # find index for window string win_idx = self.cmb_firwin_win.findText( window, Qt.MatchFixedString) # case insensitive flag if win_idx == -1: # Key does not exist, use first entry instead win_idx = 0 if 'alg' in wdg_fil_par: alg_idx = self.cmb_firwin_alg.findText(wdg_fil_par['alg'], Qt.MatchFixedString) if alg_idx == -1: # Key does not exist, use first entry instead alg_idx = 0 self.cmb_firwin_win.setCurrentIndex( win_idx) # set index for window and self.cmb_firwin_alg.setCurrentIndex(alg_idx) # and algorithm cmbBox def _store_entries(self): """ Store window and alg. selection and parameter settings (part of self.firWindow, if any) in filter dictionary. """ if not 'wdg_fil' in fb.fil[0]: fb.fil[0].update({'wdg_fil': {}}) fb.fil[0]['wdg_fil'].update( {'firwin': { 'win': self.firWindow, 'alg': self.alg }}) def _get_params(self, fil_dict): """ Translate parameters from the passed dictionary to instance parameters, scaling / transforming them if needed. """ self.N = fil_dict['N'] self.F_PB = fil_dict['F_PB'] self.F_SB = fil_dict['F_SB'] self.F_PB2 = fil_dict['F_PB2'] self.F_SB2 = fil_dict['F_SB2'] self.F_C = fil_dict['F_C'] self.F_C2 = fil_dict['F_C2'] # firwin amplitude specs are linear (not in dBs) self.A_PB = fil_dict['A_PB'] self.A_PB2 = fil_dict['A_PB2'] self.A_SB = fil_dict['A_SB'] self.A_SB2 = fil_dict['A_SB2'] # self.alg = 'ichige' # algorithm for determining the minimum order # self.alg = self.cmb_firwin_alg.currentText() def _test_N(self): """ Warn the user if the calculated order is too high for a reasonable filter design. """ if self.N > 1000: return qfilter_warning(self, self.N, "FirWin") else: return True def _save(self, fil_dict, arg): """ Convert between poles / zeros / gain, filter coefficients (polynomes) and second-order sections and store all available formats in the passed dictionary 'fil_dict'. """ fil_save(fil_dict, arg, self.FRMT, __name__) try: # has the order been calculated by a "min" filter design? fil_dict['N'] = self.N # yes, update filterbroker except AttributeError: pass # self._store_entries() #------------------------------------------------------------------------------ def firwin(self, numtaps, cutoff, window=None, pass_zero=True, scale=True, nyq=1.0, fs=None): """ FIR filter design using the window method. This is more or less the same as `scipy.signal.firwin` with the exception that an ndarray with the window values can be passed as an alternative to the window name. The parameters "width" (specifying a Kaiser window) and "fs" have been omitted, they are not needed here. This function computes the coefficients of a finite impulse response filter. The filter will have linear phase; it will be Type I if `numtaps` is odd and Type II if `numtaps` is even. Type II filters always have zero response at the Nyquist rate, so a ValueError exception is raised if firwin is called with `numtaps` even and having a passband whose right end is at the Nyquist rate. Parameters ---------- numtaps : int Length of the filter (number of coefficients, i.e. the filter order + 1). `numtaps` must be even if a passband includes the Nyquist frequency. cutoff : float or 1D array_like Cutoff frequency of filter (expressed in the same units as `nyq`) OR an array of cutoff frequencies (that is, band edges). In the latter case, the frequencies in `cutoff` should be positive and monotonically increasing between 0 and `nyq`. The values 0 and `nyq` must not be included in `cutoff`. window : ndarray or string string: use the window with the passed name from scipy.signal.windows ndarray: The window values - this is an addition to the original firwin routine. pass_zero : bool, optional If True, the gain at the frequency 0 (i.e. the "DC gain") is 1. Otherwise the DC gain is 0. scale : bool, optional Set to True to scale the coefficients so that the frequency response is exactly unity at a certain frequency. That frequency is either: - 0 (DC) if the first passband starts at 0 (i.e. pass_zero is True) - `nyq` (the Nyquist rate) if the first passband ends at `nyq` (i.e the filter is a single band highpass filter); center of first passband otherwise nyq : float, optional Nyquist frequency. Each frequency in `cutoff` must be between 0 and `nyq`. Returns ------- h : (numtaps,) ndarray Coefficients of length `numtaps` FIR filter. Raises ------ ValueError If any value in `cutoff` is less than or equal to 0 or greater than or equal to `nyq`, if the values in `cutoff` are not strictly monotonically increasing, or if `numtaps` is even but a passband includes the Nyquist frequency. See also -------- scipy.firwin """ cutoff = np.atleast_1d(cutoff) / float(nyq) # Check for invalid input. if cutoff.ndim > 1: raise ValueError("The cutoff argument must be at most " "one-dimensional.") if cutoff.size == 0: raise ValueError("At least one cutoff frequency must be given.") if cutoff.min() <= 0 or cutoff.max() >= 1: raise ValueError( "Invalid cutoff frequency {0}: frequencies must be " "greater than 0 and less than nyq.".format(cutoff)) if np.any(np.diff(cutoff) <= 0): raise ValueError("Invalid cutoff frequencies: the frequencies " "must be strictly increasing.") pass_nyquist = bool(cutoff.size & 1) ^ pass_zero if pass_nyquist and numtaps % 2 == 0: raise ValueError( "A filter with an even number of coefficients must " "have zero response at the Nyquist rate.") # Insert 0 and/or 1 at the ends of cutoff so that the length of cutoff # is even, and each pair in cutoff corresponds to passband. cutoff = np.hstack(([0.0] * pass_zero, cutoff, [1.0] * pass_nyquist)) # `bands` is a 2D array; each row gives the left and right edges of # a passband. bands = cutoff.reshape(-1, 2) # Build up the coefficients. alpha = 0.5 * (numtaps - 1) m = np.arange(0, numtaps) - alpha h = 0 for left, right in bands: h += right * sinc(right * m) h -= left * sinc(left * m) if type(window) == str: # Get and apply the window function. from scipy.signal.signaltools import get_window win = get_window(window, numtaps, fftbins=False) elif type(window) == np.ndarray: win = window else: logger.error( "The 'window' was neither a string nor a numpy array, it could not be evaluated." ) return None # apply the window function. h *= win # Now handle scaling if desired. if scale: # Get the first passband. left, right = bands[0] if left == 0: scale_frequency = 0.0 elif right == 1: scale_frequency = 1.0 else: scale_frequency = 0.5 * (left + right) c = np.cos(np.pi * m * scale_frequency) s = np.sum(h * c) h /= s return h def _firwin_ord(self, F, W, A, alg): #http://www.mikroe.com/chapters/view/72/chapter-2-fir-filters/ delta_f = abs(F[1] - F[0]) * 2 # referred to f_Ny delta_A = np.sqrt(A[0] * A[1]) if self.fir_window_name == 'kaiser': N, beta = sig.kaiserord(20 * np.log10(np.abs(fb.fil[0]['A_SB'])), delta_f) self.ledWinPar1.setText(str(beta)) fb.fil[0]['wdg_fil'][1] = beta self._update_UI() else: N = remezord(F, W, A, fs=1, alg=alg)[0] return N def LPmin(self, fil_dict): self._get_params(fil_dict) self.N = self._firwin_ord([self.F_PB, self.F_SB], [1, 0], [self.A_PB, self.A_SB], alg=self.alg) if not self._test_N(): return -1 self.fir_window = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) fil_dict['F_C'] = (self.F_SB + self.F_PB ) / 2 # use average of calculated F_PB and F_SB self._save( fil_dict, self.firwin(self.N, fil_dict['F_C'], window=self.fir_window, nyq=0.5)) def LPman(self, fil_dict): self._get_params(fil_dict) if not self._test_N(): return -1 self.fir_window = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) self._save( fil_dict, self.firwin(self.N, fil_dict['F_C'], window=self.fir_window, nyq=0.5)) def HPmin(self, fil_dict): self._get_params(fil_dict) N = self._firwin_ord([self.F_SB, self.F_PB], [0, 1], [self.A_SB, self.A_PB], alg=self.alg) self.N = round_odd(N) # enforce odd order if not self._test_N(): return -1 self.fir_window = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) fil_dict['F_C'] = (self.F_SB + self.F_PB ) / 2 # use average of calculated F_PB and F_SB self._save( fil_dict, self.firwin(self.N, fil_dict['F_C'], window=self.fir_window, pass_zero=False, nyq=0.5)) def HPman(self, fil_dict): self._get_params(fil_dict) self.N = round_odd(self.N) # enforce odd order if not self._test_N(): return -1 self.fir_window = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) self._save( fil_dict, self.firwin(self.N, fil_dict['F_C'], window=self.fir_window, pass_zero=False, nyq=0.5)) # For BP and BS, F_PB and F_SB have two elements each def BPmin(self, fil_dict): self._get_params(fil_dict) self.N = remezord([self.F_SB, self.F_PB, self.F_PB2, self.F_SB2], [0, 1, 0], [self.A_SB, self.A_PB, self.A_SB2], fs=1, alg=self.alg)[0] if not self._test_N(): return -1 self.fir_window = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) fil_dict['F_C'] = (self.F_SB + self.F_PB ) / 2 # use average of calculated F_PB and F_SB fil_dict['F_C2'] = (self.F_SB2 + self.F_PB2 ) / 2 # use average of calculated F_PB and F_SB self._save( fil_dict, self.firwin(self.N, [fil_dict['F_C'], fil_dict['F_C2']], window=self.fir_window, pass_zero=False, nyq=0.5)) def BPman(self, fil_dict): self._get_params(fil_dict) if not self._test_N(): return -1 self.fir_window = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) self._save( fil_dict, self.firwin(self.N, [fil_dict['F_C'], fil_dict['F_C2']], window=self.fir_window, pass_zero=False, nyq=0.5)) def BSmin(self, fil_dict): self._get_params(fil_dict) N = remezord([self.F_PB, self.F_SB, self.F_SB2, self.F_PB2], [1, 0, 1], [self.A_PB, self.A_SB, self.A_PB2], fs=1, alg=self.alg)[0] self.N = round_odd(N) # enforce odd order if not self._test_N(): return -1 self.fir_window = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) fil_dict['F_C'] = (self.F_SB + self.F_PB ) / 2 # use average of calculated F_PB and F_SB fil_dict['F_C2'] = (self.F_SB2 + self.F_PB2 ) / 2 # use average of calculated F_PB and F_SB self._save( fil_dict, self.firwin(self.N, [fil_dict['F_C'], fil_dict['F_C2']], window=self.fir_window, pass_zero=True, nyq=0.5)) def BSman(self, fil_dict): self._get_params(fil_dict) self.N = round_odd(self.N) # enforce odd order if not self._test_N(): return -1 self.fir_window = calc_window_function(self.win_dict, self.fir_window_name, N=self.N, sym=True) self._save( fil_dict, self.firwin(self.N, [fil_dict['F_C'], fil_dict['F_C2']], window=self.fir_window, pass_zero=True, nyq=0.5)) #------------------------------------------------------------------------------ def show_fft_win(self): """ Pop-up FFT window """ if self.but_fft_win.isChecked(): qstyle_widget(self.but_fft_win, "changed") else: qstyle_widget(self.but_fft_win, "normal") if self.fft_window is None: # no handle to the window? Create a new instance if self.but_fft_win.isChecked(): # important: Handle to window must be class attribute # pass the name of the dictionary where parameters are stored and # whether a symmetric window or one that can be continued periodically # will be constructed self.fft_window = Plot_FFT_win(self, win_dict=self.win_dict, sym=True, title="pyFDA FIR Window Viewer") self.sig_tx.connect(self.fft_window.sig_rx) self.fft_window.sig_tx.connect(self.close_fft_win) self.fft_window.show( ) # modeless i.e. non-blocking popup window else: if not self.but_fft_win.isChecked(): if self.fft_window is None: logger.warning("FFT window is already closed!") else: self.fft_window.close() def close_fft_win(self): self.fft_window = None self.but_fft_win.setChecked(False) qstyle_widget(self.but_fft_win, "normal")
def _construct_UI(self): """ initialize the User Interface """ butClipboard = QPushButton(self) butClipboard.setIcon(QIcon(':/clipboard.svg')) butClipboard.setToolTip("Copy text to clipboard.") butAbout = QPushButton(self) butAbout.setText("About") butAbout.setToolTip("Display 'About' info") butChangelog = QPushButton(self) butChangelog.setText("Changelog") butChangelog.setToolTip("Display changelog") butLicMIT = QPushButton(self) butLicMIT.setText("MIT License") butLicMIT.setToolTip("MIT License for pyFDA source code") butLicGPLv3 = QPushButton(self) butLicGPLv3.setText("GPLv3 License") butLicGPLv3.setToolTip("GPLv3 License for bundled distribution") butClose = QPushButton(self) butClose.setIcon(QIcon(':/circle-x.svg')) butClose.setToolTip("Close Window.") layGButtons = QGridLayout() layGButtons.addWidget(butClipboard, 0, 0) layGButtons.addWidget(butAbout, 0, 1) layGButtons.addWidget(butChangelog, 0, 2) layGButtons.addWidget(butLicMIT, 0, 3) layGButtons.addWidget(butLicGPLv3, 0, 4) layGButtons.addWidget(butClose, 0, 5) lblInfo = QLabel(self) lblInfo.setText(self.info_str) lblInfo.setFixedHeight(lblInfo.height() * 1.2) #lblInfo.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) lblInfo.setOpenExternalLinks(True) lblIcon = QLabel(self) lblIcon.setPixmap( QPixmap(':/pyfda_icon.svg').scaledToHeight( lblInfo.height(), Qt.SmoothTransformation)) butClipboard.setFixedWidth(lblInfo.height()) butClose.setFixedWidth(lblInfo.height()) layHInfo = QHBoxLayout() layHInfo.addWidget(lblIcon) layHInfo.addWidget(lblInfo) self.txtDisplay = QTextBrowser(self) self.txtDisplay.setOpenExternalLinks(True) self.display_about_str() self.txtDisplay.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) #self.txtDisplay.setFixedHeight(self.txtDisplay.width() * 2) layVMain = QVBoxLayout() # layVMain.setAlignment(Qt.AlignTop) # this affects only the first widget (intended here) layVMain.addLayout(layGButtons) layVMain.addLayout(layHInfo) layVMain.addWidget(self.txtDisplay) layVMain.setContentsMargins(*params['wdg_margins_spc']) self.setLayout(layVMain) #self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) #self.resize(0,0) #self.adjustSize() #QApplication.processEvents() butClipboard.clicked.connect( lambda: self.to_clipboard(self.info_str + self.about_str)) butAbout.clicked.connect(self.display_about_str) butChangelog.clicked.connect(self.display_changelog) butLicMIT.clicked.connect(self.display_MIT_lic) butLicGPLv3.clicked.connect(self.display_GPL_lic) butClose.clicked.connect(self.close)
class Input_Coeffs_UI(QWidget): """ Create the UI for the FilterCoeffs class """ sig_rx = pyqtSignal(dict) # incoming sig_tx = pyqtSignal(dict) # outgoing from pyfda.libs.pyfda_qt_lib import emit def __init__(self, parent=None): super(Input_Coeffs_UI, self).__init__(parent) self.eps = 1.e-6 # initialize tolerance value self._construct_UI() # ------------------------------------------------------------------------------ def process_sig_rx(self, dict_sig=None): """ Process signals coming from the CSV pop-up window """ # logger.debug("PROCESS_SIG_RX:\n{0}".format(pprint_log(dict_sig))) if 'closeEvent' in dict_sig: self._close_csv_win() self.emit({'ui_changed': 'csv'}) return elif 'ui_changed' in dict_sig: self._set_load_save_icons() # update icons file <-> clipboard # inform e.g. the p/z input widget about changes in CSV options self.emit({'ui_changed': 'csv'}) # ------------------------------------------------------------------------------ def _construct_UI(self): """ Intitialize the widget, consisting of: - top chkbox row - coefficient table - two bottom rows with action buttons """ self.bfont = QFont() self.bfont.setBold(True) self.bifont = QFont() self.bifont.setBold(True) self.bifont.setItalic(True) # q_icon_size = QSize(20, 20) # optional, size is derived from butEnable ####################################################################### # frmMain # # This frame contains all the buttons ####################################################################### # --------------------------------------------- # layHDisplay # # UI Elements for controlling the display # --------------------------------------------- self.butEnable = PushButton(self, icon=QIcon(':/circle-check.svg'), checked=True) q_icon_size = self.butEnable.iconSize() # <- uncomment this for manual sizing self.butEnable.setToolTip( "<span>Show / hide filter coefficients in an editable table." " For high order systems, table display might be slow.</span>") fix_formats = ['Dec', 'Hex', 'Bin', 'CSD'] self.cmbFormat = QComboBox(self) model = self.cmbFormat.model() item = QtGui.QStandardItem('Float') item.setData('child', Qt.AccessibleDescriptionRole) model.appendRow(item) item = QtGui.QStandardItem('Fixp.:') item.setData('parent', Qt.AccessibleDescriptionRole) item.setData(0, QtGui.QFont.Bold) item.setFlags(item.flags() & ~Qt.ItemIsEnabled) # | Qt.ItemIsSelectable)) model.appendRow(item) for idx in range(len(fix_formats)): item = QtGui.QStandardItem(fix_formats[idx]) # item.setForeground(QtGui.QColor('red')) model.appendRow(item) self.cmbFormat.insertSeparator(1) qset_cmb_box(self.cmbFormat, 'float') self.cmbFormat.setToolTip('Set the display format.') self.cmbFormat.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.spnDigits = QSpinBox(self) self.spnDigits.setRange(0, 16) self.spnDigits.setValue(params['FMT_ba']) self.spnDigits.setToolTip("Number of digits to display.") self.lblDigits = QLabel("Digits", self) self.lblDigits.setFont(self.bifont) self.cmbQFrmt = QComboBox(self) q_formats = [('Norm. Frac.', 'qnfrac'), ('Integer', 'qint'), ('Fractional', 'qfrac')] for q in q_formats: self.cmbQFrmt.addItem(*q) self.lbl_W = QLabel("W = ", self) self.lbl_W.setFont(self.bifont) self.ledW = QLineEdit(self) self.ledW.setToolTip("Specify total wordlength.") self.ledW.setText("16") self.ledW.setMaxLength(2) # maximum of 2 digits self.ledW.setFixedWidth(30) # width of lineedit in points(?) layHDisplay = QHBoxLayout() layHDisplay.setAlignment(Qt.AlignLeft) layHDisplay.addWidget(self.butEnable) layHDisplay.addWidget(self.cmbFormat) layHDisplay.addWidget(self.spnDigits) layHDisplay.addWidget(self.lblDigits) layHDisplay.addWidget(self.cmbQFrmt) layHDisplay.addWidget(self.lbl_W) layHDisplay.addWidget(self.ledW) layHDisplay.addStretch() ####################################################################### # frmButtonsCoeffs # # This frame contains all buttons for manipulating coefficients ####################################################################### # ----------------------------------------------------------------- # layHButtonsCoeffs1 # # UI Elements for loading / storing / manipulating cells and rows # ----------------------------------------------------------------- self.cmbFilterType = QComboBox(self) self.cmbFilterType.setObjectName("comboFilterType") self.cmbFilterType.setToolTip( "<span>Select between IIR and FIR filter for manual entry." "Changing the type reloads the filter from the filter dict.</span>") self.cmbFilterType.addItems(["FIR", "IIR"]) self.cmbFilterType.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.butAddCells = QPushButton(self) self.butAddCells.setIcon(QIcon(':/row_insert_above.svg')) self.butAddCells.setIconSize(q_icon_size) self.butAddCells.setToolTip( "<span>Select cells to insert a new cell above each selected cell. " "Use <SHIFT> or <CTRL> to select multiple cells. " "When nothing is selected, add a row at the end.</span>") self.butDelCells = QPushButton(self) self.butDelCells.setIcon(QIcon(':/row_delete.svg')) self.butDelCells.setIconSize(q_icon_size) self.butDelCells.setToolTip( "<span>Delete selected cell(s) from the table. " "Use <SHIFT> or <CTRL> to select multiple cells. " "When nothing is selected, delete the last row.</span>") self.butSave = QPushButton(self) self.butSave.setIcon(QIcon(':/upload.svg')) self.butSave.setIconSize(q_icon_size) self.butSave.setToolTip( "<span>Copy coefficient table to filter dict and update all plots" "and widgets.</span>") self.butLoad = QPushButton(self) self.butLoad.setIcon(QIcon(':/download.svg')) self.butLoad.setIconSize(q_icon_size) self.butLoad.setToolTip("Reload coefficient table from filter dict.") self.butClear = QPushButton(self) self.butClear.setIcon(QIcon(':/trash.svg')) self.butClear.setIconSize(q_icon_size) self.butClear.setToolTip("Clear all table entries.") self.butFromTable = QPushButton(self) self.butFromTable.setIconSize(q_icon_size) self.butToTable = QPushButton(self) self.butToTable.setIconSize(q_icon_size) self.but_csv_options = PushButton(self, icon=QIcon(':/settings.svg'), checked=False) self.but_csv_options.setIconSize(q_icon_size) self.but_csv_options.setToolTip( "<span>Select CSV format and whether " "to copy to/from clipboard or file.</span>") self._set_load_save_icons() # initialize icon / button settings layHButtonsCoeffs1 = QHBoxLayout() layHButtonsCoeffs1.addWidget(self.cmbFilterType) layHButtonsCoeffs1.addWidget(self.butAddCells) layHButtonsCoeffs1.addWidget(self.butDelCells) layHButtonsCoeffs1.addWidget(self.butClear) layHButtonsCoeffs1.addWidget(self.butSave) layHButtonsCoeffs1.addWidget(self.butLoad) layHButtonsCoeffs1.addWidget(self.butFromTable) layHButtonsCoeffs1.addWidget(self.butToTable) layHButtonsCoeffs1.addWidget(self.but_csv_options) layHButtonsCoeffs1.addStretch() # ---------------------------------------------------------------------- # layHButtonsCoeffs2 # # Eps / set zero settings # --------------------------------------------------------------------- self.butSetZero = QPushButton("= 0", self) self.butSetZero.setToolTip( "<span>Set selected coefficients = 0 with a magnitude < ε. " "When nothing is selected, test the whole table.</span>") self.butSetZero.setIconSize(q_icon_size) lblEps = QLabel(self) lblEps.setText("<b><i>for b, a</i> <</b>") self.ledEps = QLineEdit(self) self.ledEps.setToolTip("Specify tolerance value.") layHButtonsCoeffs2 = QHBoxLayout() layHButtonsCoeffs2.addWidget(self.butSetZero) layHButtonsCoeffs2.addWidget(lblEps) layHButtonsCoeffs2.addWidget(self.ledEps) layHButtonsCoeffs2.addStretch() # ------------------------------------------------------------------- # Now put the ButtonsCoeffs HBoxes into frmButtonsCoeffs # --------------------------------------------------------------------- layVButtonsCoeffs = QVBoxLayout() layVButtonsCoeffs.addLayout(layHButtonsCoeffs1) layVButtonsCoeffs.addLayout(layHButtonsCoeffs2) layVButtonsCoeffs.setContentsMargins(0, 5, 0, 0) # This frame encompasses all Quantization Settings self.frmButtonsCoeffs = QFrame(self) self.frmButtonsCoeffs.setLayout(layVButtonsCoeffs) # ###################################################################### # frmQSettings # # This frame contains all quantization settings # ###################################################################### # ------------------------------------------------------------------- # layHW_Scale # # QFormat and scale settings # --------------------------------------------------------------------- lbl_Q = QLabel("Q =", self) lbl_Q.setFont(self.bifont) self.ledWI = QLineEdit(self) self.ledWI.setToolTip("Specify number of integer bits.") self.ledWI.setText("0") self.ledWI.setMaxLength(2) # maximum of 2 digits self.ledWI.setFixedWidth(30) # width of lineedit in points(?) self.lblDot = QLabel(".", self) # class attribute, visibility is toggled self.lblDot.setFont(self.bfont) self.ledWF = QLineEdit(self) self.ledWF.setToolTip("Specify number of fractional bits.") self.ledWF.setText("15") self.ledWF.setMaxLength(2) # maximum of 2 digits # self.ledWF.setFixedWidth(30) # width of lineedit in points(?) self.ledWF.setMaximumWidth(30) self.lblScale = QLabel("<b><i>Scale</i> =</b>", self) self.ledScale = QLineEdit(self) self.ledScale.setToolTip( "Set the scale for converting float to fixpoint representation.") self.ledScale.setText(str(1)) self.ledScale.setEnabled(False) layHWI_WF = QHBoxLayout() layHWI_WF.addWidget(lbl_Q) layHWI_WF.addWidget(self.ledWI) layHWI_WF.addWidget(self.lblDot) layHWI_WF.addWidget(self.ledWF) layHWI_WF.addStretch() layHScale = QHBoxLayout() layHScale.addWidget(self.lblScale) layHScale.addWidget(self.ledScale) layHScale.addStretch() layHW_Scale = QHBoxLayout() layHW_Scale.addLayout(layHWI_WF) layHW_Scale.addLayout(layHScale) # ------------------------------------------------------------------- # layGQOpt # # Quantization / Overflow / MSB / LSB settings # --------------------------------------------------------------------- lblQOvfl = QLabel("Ovfl.:", self) lblQOvfl.setFont(self.bifont) lblQuant = QLabel("Quant.:", self) lblQuant.setFont(self.bifont) self.cmbQOvfl = QComboBox(self) qOvfl = ['wrap', 'sat'] self.cmbQOvfl.addItems(qOvfl) qset_cmb_box(self.cmbQOvfl, 'sat') self.cmbQOvfl.setToolTip("Select overflow behaviour.") # ComboBox size is adjusted automatically to fit the longest element self.cmbQOvfl.setSizeAdjustPolicy(QComboBox.AdjustToContents) layHQOvflOpt = QHBoxLayout() layHQOvflOpt.addWidget(lblQOvfl) layHQOvflOpt.addWidget(self.cmbQOvfl) layHQOvflOpt.addStretch() self.cmbQuant = QComboBox(self) qQuant = ['none', 'round', 'fix', 'floor'] self.cmbQuant.addItems(qQuant) qset_cmb_box(self.cmbQuant, 'round') self.cmbQuant.setToolTip("Select the kind of quantization.") self.cmbQuant.setSizeAdjustPolicy(QComboBox.AdjustToContents) layHQuantOpt = QHBoxLayout() layHQuantOpt.addWidget(lblQuant) layHQuantOpt.addWidget(self.cmbQuant) layHQuantOpt.addStretch() self.butQuant = QPushButton(self) self.butQuant.setToolTip( "<span>Quantize selected coefficients / " "whole table with specified settings.</span>") self.butQuant.setIcon(QIcon(':/quantize.svg')) self.butQuant.setIconSize(q_icon_size) self.butQuant.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) lblMSBtxt = QLabel(self) lblMSBtxt.setText("<b><i>MSB</i><sub>10</sub> =</b>") self.lblMSB = QLabel(self) layHMSB = QHBoxLayout() layHMSB.addWidget(lblMSBtxt) layHMSB.addWidget(self.lblMSB) layHMSB.addStretch() lblLSBtxt = QLabel(self) lblLSBtxt.setText("<b><i>LSB</i><sub>10</sub> =</b>") self.lblLSB = QLabel(self) layHLSB = QHBoxLayout() layHLSB.addWidget(lblLSBtxt) layHLSB.addWidget(self.lblLSB) layHLSB.addStretch() layGQOpt = QGridLayout() layGQOpt.addLayout(layHQOvflOpt, 0, 0) layGQOpt.addLayout(layHQuantOpt, 0, 1) layGQOpt.addWidget(self.butQuant, 0, 2, Qt.AlignCenter) layGQOpt.addLayout(layHMSB, 1, 0) layGQOpt.addLayout(layHLSB, 1, 1) # ------------------------------------------------------------------- # Display MAX # --------------------------------------------------------------------- lblMAXtxt = QLabel(self) lblMAXtxt.setText("<b><i>Max =</i></b>") self.lblMAX = QLabel(self) layHCoeffs_MAX = QHBoxLayout() layHCoeffs_MAX.addWidget(lblMAXtxt) layHCoeffs_MAX.addWidget(self.lblMAX) layHCoeffs_MAX.addStretch() ####################################################################### # Now put all the coefficient HBoxes into frmQSettings # --------------------------------------------------------------------- layVButtonsQ = QVBoxLayout() layVButtonsQ.addLayout(layHW_Scale) layVButtonsQ.addLayout(layGQOpt) layVButtonsQ.addLayout(layHCoeffs_MAX) layVButtonsQ.setContentsMargins(0, 0, 0, 0) # This frame encompasses all Quantization Settings self.frmQSettings = QFrame(self) self.frmQSettings.setLayout(layVButtonsQ) ####################################################################### # ######################## Main UI Layout ############################ ####################################################################### # layout for frame (UI widget) layVMainF = QVBoxLayout() layVMainF.addLayout(layHDisplay) layVMainF.addWidget(self.frmQSettings) layVMainF.addWidget(QHLine()) layVMainF.addWidget(self.frmButtonsCoeffs) # This frame encompasses all UI elements frmMain = QFrame(self) frmMain.setLayout(layVMainF) layVMain = QVBoxLayout() # the following affects only the first widget (intended here) layVMain.setAlignment(Qt.AlignTop) layVMain.addWidget(frmMain) layVMain.setContentsMargins(*params['wdg_margins']) self.setLayout(layVMain) ####################################################################### # --- set initial values from dict ------------ self.spnDigits.setValue(params['FMT_ba']) self.ledEps.setText(str(self.eps)) # ---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.but_csv_options.clicked.connect(self._open_csv_win) # -------------------------------------------------------------------------- def _open_csv_win(self): """ Pop-up window for CSV options """ if self.but_csv_options.isChecked(): qstyle_widget(self.but_csv_options, "changed") else: qstyle_widget(self.but_csv_options, "normal") if dirs.csv_options_handle is None: # no handle to the window? Create a new instance if self.but_csv_options.isChecked(): # Important: Handle to window must be class attribute, otherwise it # (and the attached window) is deleted immediately when it goes # out of scope dirs.csv_options_handle = CSV_option_box(self) dirs.csv_options_handle.sig_tx.connect(self.process_sig_rx) dirs.csv_options_handle.show() # modeless i.e. non-blocking popup window else: if not self.but_csv_options.isChecked(): # this should not happen if dirs.csv_options_handle is None: logger.warning("CSV options window is already closed!") else: dirs.csv_options_handle.close() self.emit({'ui_changed': 'csv'}) # ------------------------------------------------------------------------------ def _close_csv_win(self): dirs.csv_options_handle = None self.but_csv_options.setChecked(False) qstyle_widget(self.but_csv_options, "normal") # ------------------------------------------------------------------------------ def _set_load_save_icons(self): """ Set icons / tooltipps for loading and saving data to / from file or clipboard depending on selected options. """ if params['CSV']['clipboard']: self.butFromTable.setIcon(QIcon(':/to_clipboard.svg')) self.butFromTable.setToolTip( "<span>Copy table to clipboard, SELECTED items are copied as " "displayed. When nothing is selected, the whole table " "is copied with full precision in decimal format.</span>") self.butToTable.setIcon(QIcon(':/from_clipboard.svg')) self.butToTable.setToolTip("<span>Copy clipboard to table.</span>") else: self.butFromTable.setIcon(QIcon(':/save.svg')) self.butFromTable.setToolTip( "<span>" "Save table to file, SELECTED items are copied as " "displayed. When nothing is selected, the whole table " "is copied with full precision in decimal format.</span>") self.butToTable.setIcon(QIcon(':/file.svg')) self.butToTable.setToolTip("<span>Load table from file.</span>") if dirs.csv_options_handle is None: qstyle_widget(self.but_csv_options, "normal") self.but_csv_options.setChecked(False) else: qstyle_widget(self.but_csv_options, "changed") self.but_csv_options.setChecked(True)
class Plot_Tran_Stim_UI(QWidget): """ Create the UI for the PlotImpz class """ # incoming: sig_rx = pyqtSignal(object) # outgoing: from various UI elements to PlotImpz ('ui_changed':'xxx') sig_tx = pyqtSignal(object) # outgoing: to fft related widgets (FFT window widget, qfft_win_select) sig_tx_fft = pyqtSignal(object) from pyfda.libs.pyfda_qt_lib import emit # ------------------------------------------------------------------------------ def process_sig_rx(self, dict_sig=None): """ Process signals coming from - FFT window widget - qfft_win_select """ logger.warning("PROCESS_SIG_RX - vis: {0}\n{1}".format( self.isVisible(), pprint_log(dict_sig))) if 'id' in dict_sig and dict_sig['id'] == id(self): logger.warning("Stopped infinite loop:\n{0}".format( pprint_log(dict_sig))) return elif 'view_changed' in dict_sig: if dict_sig['view_changed'] == 'f_S': self.recalc_freqs() # ------------------------------------------------------------------------------ def __init__(self): super().__init__() """ Intitialize the widget, consisting of: - top chkbox row - coefficient table - two bottom rows with action buttons """ # initial settings self.N_FFT = 0 # TODO: FFT value needs to be passed here somehow? # stimuli self.cmb_stim_item = "impulse" self.cmb_stim_periodic_item = 'square' self.stim = "dirac" self.impulse_type = 'dirac' self.sinusoid_type = 'sine' self.chirp_type = 'linear' self.modulation_type = 'am' self.noise = "None" self.f1 = 0.02 self.f2 = 0.03 self.A1 = 1.0 self.A2 = 0.0 self.phi1 = self.phi2 = 0 self.T1 = self.T2 = 0 self.TW1 = self.TW2 = 1 self.BW1 = self.BW2 = 0.5 self.noi = 0.1 self.noise = 'none' self.DC = 0.0 self.stim_formula = "A1 * abs(sin(2 * pi * f1 * n))" self.stim_par1 = 0.5 self.scale_impz = 1 # optional energy scaling for impulses # self.bottom_f = -120 # initial value for log. scale # self.param = None # dictionaries with widgets needed for the various stimuli self.stim_wdg_dict = collections.OrderedDict() self.stim_wdg_dict.update({ "none": {"dc", "noise"}, "dirac": {"dc", "a1", "T1", "norm", "noise"}, "sinc": {"dc", "a1", "a2", "T1", "T2", "f1", "f2", "norm", "noise"}, "gauss": { "dc", "a1", "a2", "T1", "T2", "f1", "f2", "BW1", "BW2", "norm", "noise" }, "rect": {"dc", "a1", "T1", "TW1", "norm", "noise"}, "step": {"a1", "T1", "noise"}, "cos": {"dc", "a1", "a2", "phi1", "phi2", "f1", "f2", "noise"}, "sine": {"dc", "a1", "a2", "phi1", "phi2", "f1", "f2", "noise"}, "exp": {"dc", "a1", "a2", "phi1", "phi2", "f1", "f2", "noise"}, "diric": {"dc", "a1", "phi1", "T1", "TW1", "f1", "noise"}, "chirp": {"dc", "a1", "phi1", "f1", "f2", "T2", "noise"}, "triang": {"dc", "a1", "phi1", "f1", "noise", "bl"}, "saw": {"dc", "a1", "phi1", "f1", "noise", "bl"}, "square": {"dc", "a1", "phi1", "f1", "noise", "bl", "par1"}, "comb": {"dc", "a1", "phi1", "f1", "noise"}, "am": {"dc", "a1", "a2", "phi1", "phi2", "f1", "f2", "noise"}, "pmfm": {"dc", "a1", "a2", "phi1", "phi2", "f1", "f2", "noise"}, "formula": { "dc", "a1", "a2", "phi1", "phi2", "f1", "f2", "BW1", "BW2", "noise" } }) # combobox tooltip + data / text / tooltip for stimulus category items self.cmb_stim_items = [ ("<span>Stimulus category.</span>"), ("none", "None", "<span>Only noise and DC can be selected.</span>"), ("impulse", "Impulse", "<span>Different impulses</span>"), ("step", "Step", "<span>Calculate step response and its error.</span>"), ("sinusoid", "Sinusoid", "<span>Sinusoidal waveforms</span>"), ("chirp", "Chirp", "<span>Different frequency sweeps.</span>"), ("periodic", "Periodic", "<span>Periodic functions with discontinuities, " "either band-limited or with aliasing.</span>"), ("modulation", "Modulat.", "<span>Modulated waveforms.</span>"), ("formula", "Formula", "<span>Formula defined stimulus.</span>") ] # combobox tooltip + data / text / tooltip for periodic signals items self.cmb_stim_periodic_items = [ "<span>Periodic functions with discontinuities.</span>", ("square", "Square", "<span>Square signal with duty cycle α</span>"), ("saw", "Saw", "Sawtooth signal"), ("triang", "Triang", "Triangular signal"), ("comb", "Comb", "Comb signal") ] # combobox tooltip + data / text / tooltip for chirp signals items self.cmb_stim_chirp_items = [ "<span>Type of frequency sweep from <i>f</i><sub>1</sub> @ <i>t</i> = 0 to " "<i>f</i><sub>2</sub> @ t = <i>T</i><sub>2</sub>.</span>", ("linear", "Lin", "Linear frequency sweep"), ("quadratic", "Square", "Quadratic frequency sweep"), ("logarithmic", "Log", "Logarithmic frequency sweep"), ("hyperbolic", "Hyper", "Hyperbolic frequency sweep") ] self.cmb_stim_impulse_items = [ "<span>Different aperiodic impulse forms</span>", ("dirac", "Dirac", "<span>Discrete-time dirac impulse for simulating impulse and " "frequency response.</span>"), ("gauss", "Gauss", "<span>Gaussian pulse with bandpass spectrum and controllable " "relative -6 dB bandwidth.</span>"), ("sinc", "Sinc", "<span>Sinc pulse with rectangular baseband spectrum</span>"), ("rect", "Rect", "<span>Rectangular pulse with sinc-shaped spectrum</span>") ] self.cmb_stim_sinusoid_items = [ "Sinusoidal or similar signals", ("sine", "Sine", "Sine signal"), ("cos", "Cos", "Cosine signal"), ("exp", "Exp", "Complex exponential"), ("diric", "Sinc", "<span>Periodic Sinc (Dirichlet function)</span>") ] # data / text / tooltip for noise stimulus combobox. self.cmb_stim_noise_items = [ "Type of additive noise.", ("none", "None", ""), ("gauss", "Gauss", "<span>Normal- or Gauss-distributed process with std. deviation σ." "</span>"), ("uniform", "Uniform", "<span>Uniformly distributed process in the range ± Δ/2." "</span>"), ("prbs", "PRBS", "<span>Pseudo-Random Binary Sequence with values ± A.</span>" ), ("mls", "MLS", "<span>Maximum Length Sequence with values ± A. The sequence is " "always the same as the state is not stored for the next sequence start." "</span>"), ("brownian", "Brownian", "<span>Brownian (cumulated sum) process based on Gaussian noise with" " std. deviation σ.</span>") ] self._construct_UI() self._enable_stim_widgets() self._update_noi() def _construct_UI(self): # ===================================================================== # Controls for stimuli # ===================================================================== self.cmbStimulus = QComboBox(self) qcmb_box_populate(self.cmbStimulus, self.cmb_stim_items, self.cmb_stim_item) self.lblStimPar1 = QLabel(to_html("α =", frmt='b'), self) self.ledStimPar1 = QLineEdit(self) self.ledStimPar1.setText("0.5") self.ledStimPar1.setToolTip("Duty Cycle, 0 ... 1") self.ledStimPar1.setObjectName("ledStimPar1") self.but_stim_bl = QPushButton(self) self.but_stim_bl.setText("BL") self.but_stim_bl.setToolTip( "<span>Bandlimit the signal to the Nyquist " "frequency to avoid aliasing. However, this is much slower " "to calculate especially for a large number of points.</span>") self.but_stim_bl.setMaximumWidth(qtext_width(text="BL ")) self.but_stim_bl.setCheckable(True) self.but_stim_bl.setChecked(True) self.but_stim_bl.setObjectName("stim_bl") # ------------------------------------- self.cmbChirpType = QComboBox(self) qcmb_box_populate(self.cmbChirpType, self.cmb_stim_chirp_items, self.chirp_type) self.cmbImpulseType = QComboBox(self) qcmb_box_populate(self.cmbImpulseType, self.cmb_stim_impulse_items, self.impulse_type) self.cmbSinusoidType = QComboBox(self) qcmb_box_populate(self.cmbSinusoidType, self.cmb_stim_sinusoid_items, self.sinusoid_type) self.cmbPeriodicType = QComboBox(self) qcmb_box_populate(self.cmbPeriodicType, self.cmb_stim_periodic_items, self.cmb_stim_periodic_item) self.cmbModulationType = QComboBox(self) for t in [("AM", "am"), ("PM / FM", "pmfm")]: # text, data self.cmbModulationType.addItem(*t) qset_cmb_box(self.cmbModulationType, self.modulation_type, data=True) # ------------------------------------- self.chk_step_err = QPushButton("Error", self) self.chk_step_err.setToolTip( "<span>Display the step response error.</span>") self.chk_step_err.setMaximumWidth(qtext_width(text="Error ")) self.chk_step_err.setCheckable(True) self.chk_step_err.setChecked(False) self.chk_step_err.setObjectName("stim_step_err") layHCmbStim = QHBoxLayout() layHCmbStim.addWidget(self.cmbStimulus) layHCmbStim.addWidget(self.cmbImpulseType) layHCmbStim.addWidget(self.cmbSinusoidType) layHCmbStim.addWidget(self.cmbChirpType) layHCmbStim.addWidget(self.cmbPeriodicType) layHCmbStim.addWidget(self.cmbModulationType) layHCmbStim.addWidget(self.but_stim_bl) layHCmbStim.addWidget(self.lblStimPar1) layHCmbStim.addWidget(self.ledStimPar1) layHCmbStim.addWidget(self.chk_step_err) self.lblDC = QLabel(to_html("DC =", frmt='bi'), self) self.ledDC = QLineEdit(self) self.ledDC.setText(str(self.DC)) self.ledDC.setToolTip("DC Level") self.ledDC.setObjectName("stimDC") layHStimDC = QHBoxLayout() layHStimDC.addWidget(self.lblDC) layHStimDC.addWidget(self.ledDC) # ====================================================================== self.lblAmp1 = QLabel(to_html(" A_1", frmt='bi') + " =", self) self.ledAmp1 = QLineEdit(self) self.ledAmp1.setText(str(self.A1)) self.ledAmp1.setToolTip( "Stimulus amplitude, complex values like 3j - 1 are allowed") self.ledAmp1.setObjectName("stimAmp1") self.lblAmp2 = QLabel(to_html(" A_2", frmt='bi') + " =", self) self.ledAmp2 = QLineEdit(self) self.ledAmp2.setText(str(self.A2)) self.ledAmp2.setToolTip( "Stimulus amplitude 2, complex values like 3j - 1 are allowed") self.ledAmp2.setObjectName("stimAmp2") # ---------------------------------------------- self.lblPhi1 = QLabel(to_html(" φ_1", frmt='bi') + " =", self) self.ledPhi1 = QLineEdit(self) self.ledPhi1.setText(str(self.phi1)) self.ledPhi1.setToolTip("Stimulus phase") self.ledPhi1.setObjectName("stimPhi1") self.lblPhU1 = QLabel(to_html("°", frmt='b'), self) self.lblPhi2 = QLabel(to_html(" φ_2", frmt='bi') + " =", self) self.ledPhi2 = QLineEdit(self) self.ledPhi2.setText(str(self.phi2)) self.ledPhi2.setToolTip("Stimulus phase 2") self.ledPhi2.setObjectName("stimPhi2") self.lblPhU2 = QLabel(to_html("°", frmt='b'), self) # ---------------------------------------------- self.lbl_T1 = QLabel(to_html(" T_1", frmt='bi') + " =", self) self.led_T1 = QLineEdit(self) self.led_T1.setText(str(self.T1)) self.led_T1.setToolTip("Time shift") self.led_T1.setObjectName("stimT1") self.lbl_TU1 = QLabel(to_html("T_S", frmt='b'), self) self.lbl_T2 = QLabel(to_html(" T_2", frmt='bi') + " =", self) self.led_T2 = QLineEdit(self) self.led_T2.setText(str(self.T2)) self.led_T2.setToolTip("Time shift 2") self.led_T2.setObjectName("stimT2") self.lbl_TU2 = QLabel(to_html("T_S", frmt='b'), self) # --------------------------------------------- self.lbl_TW1 = QLabel( to_html(" ΔT_1", frmt='bi') + " =", self) self.led_TW1 = QLineEdit(self) self.led_TW1.setText(str(self.TW1)) self.led_TW1.setToolTip("Time width") self.led_TW1.setObjectName("stimTW1") self.lbl_TWU1 = QLabel(to_html("T_S", frmt='b'), self) self.lbl_TW2 = QLabel( to_html(" ΔT_2", frmt='bi') + " =", self) self.led_TW2 = QLineEdit(self) self.led_TW2.setText(str(self.TW2)) self.led_TW2.setToolTip("Time width 2") self.led_TW2.setObjectName("stimTW2") self.lbl_TWU2 = QLabel(to_html("T_S", frmt='b'), self) # ---------------------------------------------- self.txtFreq1_f = to_html(" f_1", frmt='bi') + " =" self.txtFreq1_k = to_html(" k_1", frmt='bi') + " =" self.lblFreq1 = QLabel(self.txtFreq1_f, self) self.ledFreq1 = QLineEdit(self) self.ledFreq1.setText(str(self.f1)) self.ledFreq1.setToolTip("Stimulus frequency") self.ledFreq1.setObjectName("stimFreq1") self.lblFreqUnit1 = QLabel("f_S", self) self.txtFreq2_f = to_html(" f_2", frmt='bi') + " =" self.txtFreq2_k = to_html(" k_2", frmt='bi') + " =" self.lblFreq2 = QLabel(self.txtFreq2_f, self) self.ledFreq2 = QLineEdit(self) self.ledFreq2.setText(str(self.f2)) self.ledFreq2.setToolTip("Stimulus frequency 2") self.ledFreq2.setObjectName("stimFreq2") self.lblFreqUnit2 = QLabel("f_S", self) # ---------------------------------------------- self.lbl_BW1 = QLabel( to_html(self.tr(" BW_1"), frmt='bi') + " =", self) self.led_BW1 = QLineEdit(self) self.led_BW1.setText(str(self.BW1)) self.led_BW1.setToolTip(self.tr("Relative bandwidth")) self.led_BW1.setObjectName("stimBW1") self.lbl_BW2 = QLabel( to_html(self.tr(" BW_2"), frmt='bi') + " =", self) self.led_BW2 = QLineEdit(self) self.led_BW2.setText(str(self.BW2)) self.led_BW2.setToolTip(self.tr("Relative bandwidth 2")) self.led_BW2.setObjectName("stimBW2") # ---------------------------------------------- self.lblNoise = QLabel(to_html(" Noise", frmt='bi'), self) self.cmbNoise = QComboBox(self) qcmb_box_populate(self.cmbNoise, self.cmb_stim_noise_items, self.noise) self.lblNoi = QLabel("not initialized", self) self.ledNoi = QLineEdit(self) self.ledNoi.setText(str(self.noi)) self.ledNoi.setToolTip("not initialized") self.ledNoi.setObjectName("stimNoi") layGStim = QGridLayout() layGStim.addLayout(layHCmbStim, 0, 1) layGStim.addLayout(layHStimDC, 1, 1) layGStim.addWidget(self.lblAmp1, 0, 2) layGStim.addWidget(self.lblAmp2, 1, 2) layGStim.addWidget(self.ledAmp1, 0, 3) layGStim.addWidget(self.ledAmp2, 1, 3) layGStim.addWidget(self.lblPhi1, 0, 4) layGStim.addWidget(self.lblPhi2, 1, 4) layGStim.addWidget(self.ledPhi1, 0, 5) layGStim.addWidget(self.ledPhi2, 1, 5) layGStim.addWidget(self.lblPhU1, 0, 6) layGStim.addWidget(self.lblPhU2, 1, 6) layGStim.addWidget(self.lbl_T1, 0, 7) layGStim.addWidget(self.lbl_T2, 1, 7) layGStim.addWidget(self.led_T1, 0, 8) layGStim.addWidget(self.led_T2, 1, 8) layGStim.addWidget(self.lbl_TU1, 0, 9) layGStim.addWidget(self.lbl_TU2, 1, 9) layGStim.addWidget(self.lbl_TW1, 0, 10) layGStim.addWidget(self.lbl_TW2, 1, 10) layGStim.addWidget(self.led_TW1, 0, 11) layGStim.addWidget(self.led_TW2, 1, 11) layGStim.addWidget(self.lbl_TWU1, 0, 12) layGStim.addWidget(self.lbl_TWU2, 1, 12) layGStim.addWidget(self.lblFreq1, 0, 13) layGStim.addWidget(self.lblFreq2, 1, 13) layGStim.addWidget(self.ledFreq1, 0, 14) layGStim.addWidget(self.ledFreq2, 1, 14) layGStim.addWidget(self.lblFreqUnit1, 0, 15) layGStim.addWidget(self.lblFreqUnit2, 1, 15) layGStim.addWidget(self.lbl_BW1, 0, 16) layGStim.addWidget(self.lbl_BW2, 1, 16) layGStim.addWidget(self.led_BW1, 0, 17) layGStim.addWidget(self.led_BW2, 1, 17) layGStim.addWidget(self.lblNoise, 0, 18) layGStim.addWidget(self.lblNoi, 1, 18) layGStim.addWidget(self.cmbNoise, 0, 19) layGStim.addWidget(self.ledNoi, 1, 19) # ---------------------------------------------- self.lblStimFormula = QLabel(to_html("x =", frmt='bi'), self) self.ledStimFormula = QLineEdit(self) self.ledStimFormula.setText(str(self.stim_formula)) self.ledStimFormula.setToolTip( "<span>Enter formula for stimulus in numexpr syntax.</span>") self.ledStimFormula.setObjectName("stimFormula") layH_stim_formula = QHBoxLayout() layH_stim_formula.addWidget(self.lblStimFormula) layH_stim_formula.addWidget(self.ledStimFormula, 10) # ---------------------------------------------------------------------- # Main Widget # ---------------------------------------------------------------------- layH_stim_par = QHBoxLayout() layH_stim_par.addLayout(layGStim) layV_stim = QVBoxLayout() layV_stim.addLayout(layH_stim_par) layV_stim.addLayout(layH_stim_formula) layH_stim = QHBoxLayout() layH_stim.addLayout(layV_stim) layH_stim.addStretch(10) self.wdg_stim = QWidget(self) self.wdg_stim.setLayout(layH_stim) self.wdg_stim.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum) # ---------------------------------------------------------------------- # Event Filter # ---------------------------------------------------------------------- # frequency related widgets are scaled with f_s, requiring special handling self.ledFreq1.installEventFilter(self) self.ledFreq2.installEventFilter(self) self.led_T1.installEventFilter(self) self.led_T2.installEventFilter(self) self.led_TW1.installEventFilter(self) self.led_TW2.installEventFilter(self) # ---------------------------------------------------------------------- # GLOBAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.sig_rx.connect(self.process_sig_rx) # ---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs # ---------------------------------------------------------------------- # --- stimulus control --- self.but_stim_bl.clicked.connect(self._enable_stim_widgets) self.chk_step_err.clicked.connect(self._enable_stim_widgets) self.cmbStimulus.currentIndexChanged.connect(self._enable_stim_widgets) self.cmbNoise.currentIndexChanged.connect(self._update_noi) self.ledNoi.editingFinished.connect(self._update_noi) self.ledAmp1.editingFinished.connect(self._update_amp1) self.ledAmp2.editingFinished.connect(self._update_amp2) self.ledPhi1.editingFinished.connect(self._update_phi1) self.ledPhi2.editingFinished.connect(self._update_phi2) self.led_BW1.editingFinished.connect(self._update_BW1) self.led_BW2.editingFinished.connect(self._update_BW2) self.cmbImpulseType.currentIndexChanged.connect( self._update_impulse_type) self.cmbSinusoidType.currentIndexChanged.connect( self._update_sinusoid_type) self.cmbChirpType.currentIndexChanged.connect(self._update_chirp_type) self.cmbPeriodicType.currentIndexChanged.connect( self._update_periodic_type) self.cmbModulationType.currentIndexChanged.connect( self._update_modulation_type) self.ledDC.editingFinished.connect(self._update_DC) self.ledStimFormula.editingFinished.connect(self._update_stim_formula) self.ledStimPar1.editingFinished.connect(self._update_stim_par1) # ------------------------------------------------------------------------------ def update_freq_units(self): """ Update labels referrring to frequency specs """ if fb.fil[0]['freq_specs_unit'] == 'k': f_unit = '' t_unit = '' self.lblFreq1.setText(self.txtFreq1_k) self.lblFreq2.setText(self.txtFreq2_k) else: f_unit = fb.fil[0]['plt_fUnit'] t_unit = fb.fil[0]['plt_tUnit'].replace(r"$\mu$", "μ") self.lblFreq1.setText(self.txtFreq1_f) self.lblFreq2.setText(self.txtFreq2_f) if f_unit in {"f_S", "f_Ny"}: unit_frmt = "i" # italic else: unit_frmt = None # don't print units like kHz in italic self.lblFreqUnit1.setText(to_html(f_unit, frmt=unit_frmt)) self.lblFreqUnit2.setText(to_html(f_unit, frmt=unit_frmt)) self.lbl_TU1.setText(to_html(t_unit, frmt=unit_frmt)) self.lbl_TU2.setText(to_html(t_unit, frmt=unit_frmt)) # ------------------------------------------------------------------------------ def eventFilter(self, source, event): """ Filter all events generated by the monitored widgets (ledFreq1 and 2 and T1 / T2). Source and type of all events generated by monitored objects are passed to this eventFilter, evaluated and passed on to the next hierarchy level. - When a QLineEdit widget gains input focus (``QEvent.FocusIn``), display the stored value from filter dict with full precision - When a key is pressed inside the text field, set the `spec_edited` flag to True. - When a QLineEdit widget loses input focus (``QEvent.FocusOut``), store current value normalized to f_S with full precision (only if ``spec_edited == True``) and display the stored value in selected format Emit 'ui_changed':'stim' """ def _reload_entry(source): """ Reload text entry for active line edit field in rounded format """ if source.objectName() == "stimFreq1": source.setText( str(params['FMT'].format(self.f1 * self.f_scale))) elif source.objectName() == "stimFreq2": source.setText( str(params['FMT'].format(self.f2 * self.f_scale))) elif source.objectName() == "stimT1": source.setText( str(params['FMT'].format(self.T1 * self.t_scale))) elif source.objectName() == "stimT2": source.setText( str(params['FMT'].format(self.T2 * self.t_scale))) elif source.objectName() == "stimTW1": source.setText( str(params['FMT'].format(self.TW1 * self.t_scale))) elif source.objectName() == "stimTW2": source.setText( str(params['FMT'].format(self.TW2 * self.t_scale))) def _store_entry(source): if self.spec_edited: if source.objectName() == "stimFreq1": self.f1 = safe_eval(source.text(), self.f1 * self.f_scale, return_type='float') / self.f_scale source.setText( str(params['FMT'].format(self.f1 * self.f_scale))) elif source.objectName() == "stimFreq2": self.f2 = safe_eval(source.text(), self.f2 * self.f_scale, return_type='float') / self.f_scale source.setText( str(params['FMT'].format(self.f2 * self.f_scale))) elif source.objectName() == "stimT1": self.T1 = safe_eval(source.text(), self.T1 * self.t_scale, return_type='float') / self.t_scale source.setText( str(params['FMT'].format(self.T1 * self.t_scale))) elif source.objectName() == "stimT2": self.T2 = safe_eval(source.text(), self.T2 * self.t_scale, return_type='float') / self.t_scale source.setText( str(params['FMT'].format(self.T2 * self.t_scale))) elif source.objectName() == "stimTW1": self.TW1 = safe_eval(source.text(), self.TW1 * self.t_scale, sign='pos', return_type='float') / self.t_scale source.setText( str(params['FMT'].format(self.TW1 * self.t_scale))) elif source.objectName() == "stimTW2": self.TW2 = safe_eval(source.text(), self.TW2 * self.t_scale, sign='pos', return_type='float') / self.t_scale source.setText( str(params['FMT'].format(self.TW2 * self.t_scale))) self.spec_edited = False # reset flag self._update_scale_impz() self.emit({'ui_changed': 'stim'}) # nothing has changed, but display frequencies in rounded format anyway else: _reload_entry(source) # -------------------------------------------------------------------- # if isinstance(source, QLineEdit): # if source.objectName() in {"stimFreq1","stimFreq2"}: if event.type() in {QEvent.FocusIn, QEvent.KeyPress, QEvent.FocusOut}: if event.type() == QEvent.FocusIn: self.spec_edited = False self.update_freqs() elif event.type() == QEvent.KeyPress: self.spec_edited = True # entry has been changed key = event.key() if key in {Qt.Key_Return, Qt.Key_Enter}: _store_entry(source) elif key == Qt.Key_Escape: # revert changes self.spec_edited = False _reload_entry(source) elif event.type() == QEvent.FocusOut: _store_entry(source) # Call base class method to continue normal event processing: return super(Plot_Tran_Stim_UI, self).eventFilter(source, event) # ------------------------------------------------------------- def recalc_freqs(self): """ Update normalized frequencies if required. This is called by via signal ['ui_changed':'f_S'] from plot_impz.process_sig_rx """ if fb.fil[0]['freq_locked']: self.f1 *= fb.fil[0]['f_S_prev'] / fb.fil[0]['f_S'] self.f2 *= fb.fil[0]['f_S_prev'] / fb.fil[0]['f_S'] self.T1 *= fb.fil[0]['f_S'] / fb.fil[0]['f_S_prev'] self.T2 *= fb.fil[0]['f_S'] / fb.fil[0]['f_S_prev'] self.TW1 *= fb.fil[0]['f_S'] / fb.fil[0]['f_S_prev'] self.TW2 *= fb.fil[0]['f_S'] / fb.fil[0]['f_S_prev'] self._update_scale_impz() self.update_freqs() self.emit({'ui_changed': 'f1_f2'}) # ------------------------------------------------------------- def update_freqs(self): """ `update_freqs()` is called: - when one of the stimulus frequencies has changed via eventFilter() - sampling frequency has been changed via signal ['ui_changed':'f_S'] from plot_impz.process_sig_rx -> self.recalc_freqs The sampling frequency is loaded from filter dictionary and stored as `self.f_scale` (except when the frequency unit is k when `f_scale = self.N_FFT`). Frequency field entries are always stored normalized w.r.t. f_S in the dictionary: When the `f_S` lock button is unlocked, only the displayed values for frequency entries are updated with f_S, not the dictionary. When the `f_S` lock button is pressed, the absolute frequency values in the widget fields are kept constant, and the dictionary entries are updated. """ # recalculate displayed freq spec values for (maybe) changed f_S if fb.fil[0]['freq_specs_unit'] == 'k': self.f_scale = self.N_FFT else: self.f_scale = fb.fil[0]['f_S'] self.t_scale = fb.fil[0]['T_S'] if self.ledFreq1.hasFocus(): # widget has focus, show full precision self.ledFreq1.setText(str(self.f1 * self.f_scale)) elif self.ledFreq2.hasFocus(): self.ledFreq2.setText(str(self.f2 * self.f_scale)) elif self.led_T1.hasFocus(): self.led_T1.setText(str(self.T1 * self.t_scale)) elif self.led_T2.hasFocus(): self.led_T2.setText(str(self.T2 * self.t_scale)) elif self.led_TW1.hasFocus(): self.led_TW1.setText(str(self.TW1 * self.t_scale)) elif self.led_TW2.hasFocus(): self.led_TW2.setText(str(self.TW2 * self.t_scale)) else: # widgets have no focus, round the display self.ledFreq1.setText( str(params['FMT'].format(self.f1 * self.f_scale))) self.ledFreq2.setText( str(params['FMT'].format(self.f2 * self.f_scale))) self.led_T1.setText( str(params['FMT'].format(self.T1 * self.t_scale))) self.led_T2.setText( str(params['FMT'].format(self.T2 * self.t_scale))) self.led_TW1.setText( str(params['FMT'].format(self.TW1 * self.t_scale))) self.led_TW2.setText( str(params['FMT'].format(self.TW2 * self.t_scale))) self.update_freq_units( ) # TODO: should only be called at f_S / unit update # ------------------------------------------------------------- def _enable_stim_widgets(self): """ Enable / disable widgets depending on the selected stimulus """ self.cmb_stim = qget_cmb_box(self.cmbStimulus) if self.cmb_stim == "impulse": self.stim = qget_cmb_box(self.cmbImpulseType) # recalculate the energy scaling for impulse functions self._update_scale_impz() elif self.cmb_stim == "sinusoid": self.stim = qget_cmb_box(self.cmbSinusoidType) elif self.cmb_stim == "periodic": self.stim = qget_cmb_box(self.cmbPeriodicType) elif self.cmb_stim == "modulation": self.stim = qget_cmb_box(self.cmbModulationType) else: self.stim = self.cmb_stim # read out which stimulus widgets are enabled stim_wdg = self.stim_wdg_dict[self.stim] self.lblDC.setVisible("dc" in stim_wdg) self.ledDC.setVisible("dc" in stim_wdg) self.chk_step_err.setVisible(self.stim == "step") self.lblStimPar1.setVisible("par1" in stim_wdg) self.ledStimPar1.setVisible("par1" in stim_wdg) self.but_stim_bl.setVisible("bl" in stim_wdg) self.lblAmp1.setVisible("a1" in stim_wdg) self.ledAmp1.setVisible("a1" in stim_wdg) self.lblPhi1.setVisible("phi1" in stim_wdg) self.ledPhi1.setVisible("phi1" in stim_wdg) self.lblPhU1.setVisible("phi1" in stim_wdg) self.lbl_T1.setVisible("T1" in stim_wdg) self.led_T1.setVisible("T1" in stim_wdg) self.lbl_TU1.setVisible("T1" in stim_wdg) self.lbl_TW1.setVisible("TW1" in stim_wdg) self.led_TW1.setVisible("TW1" in stim_wdg) self.lbl_TWU1.setVisible("TW1" in stim_wdg) self.lblFreq1.setVisible("f1" in stim_wdg) self.ledFreq1.setVisible("f1" in stim_wdg) self.lblFreqUnit1.setVisible("f1" in stim_wdg) self.lbl_BW1.setVisible("BW1" in stim_wdg) self.led_BW1.setVisible("BW1" in stim_wdg) self.lblAmp2.setVisible("a2" in stim_wdg) self.ledAmp2.setVisible("a2" in stim_wdg) self.lblPhi2.setVisible("phi2" in stim_wdg) self.ledPhi2.setVisible("phi2" in stim_wdg) self.lblPhU2.setVisible("phi2" in stim_wdg) self.lbl_T2.setVisible("T2" in stim_wdg) self.led_T2.setVisible("T2" in stim_wdg) self.lbl_TU2.setVisible("T2" in stim_wdg) self.lbl_TW2.setVisible("TW2" in stim_wdg) self.led_TW2.setVisible("TW2" in stim_wdg) self.lbl_TWU2.setVisible("TW2" in stim_wdg) self.lblFreq2.setVisible("f2" in stim_wdg) self.ledFreq2.setVisible("f2" in stim_wdg) self.lblFreqUnit2.setVisible("f2" in stim_wdg) self.lbl_BW2.setVisible("BW2" in stim_wdg) self.led_BW2.setVisible("BW2" in stim_wdg) self.lblStimFormula.setVisible(self.stim == "formula") self.ledStimFormula.setVisible(self.stim == "formula") self.cmbImpulseType.setVisible(self.cmb_stim == 'impulse') self.cmbSinusoidType.setVisible(self.cmb_stim == 'sinusoid') self.cmbChirpType.setVisible(self.cmb_stim == 'chirp') self.cmbPeriodicType.setVisible(self.cmb_stim == 'periodic') self.cmbModulationType.setVisible(self.cmb_stim == 'modulation') self.emit({'ui_changed': 'stim'}) # ------------------------------------------------------------- def _update_amp1(self): """ Update value for self.A1 from QLineEditWidget""" self.A1 = safe_eval(self.ledAmp1.text(), self.A1, return_type='cmplx') self.ledAmp1.setText(str(self.A1)) self.emit({'ui_changed': 'a1'}) def _update_amp2(self): """ Update value for self.A2 from the QLineEditWidget""" self.A2 = safe_eval(self.ledAmp2.text(), self.A2, return_type='cmplx') self.ledAmp2.setText(str(self.A2)) self.emit({'ui_changed': 'a2'}) def _update_phi1(self): """ Update value for self.phi1 from QLineEditWidget""" self.phi1 = safe_eval(self.ledPhi1.text(), self.phi1, return_type='float') self.ledPhi1.setText(str(self.phi1)) self.emit({'ui_changed': 'phi1'}) def _update_BW1(self): """ Update value for self.BW1 from QLineEditWidget""" self.BW1 = safe_eval(self.led_BW1.text(), self.BW1, return_type='float', sign='pos') self.led_BW1.setText(str(self.BW1)) self._update_scale_impz() self.emit({'ui_changed': 'BW1'}) def _update_BW2(self): """ Update value for self.BW2 from QLineEditWidget""" self.BW2 = safe_eval(self.led_BW2.text(), self.BW2, return_type='float', sign='pos') self.led_BW2.setText(str(self.BW2)) self.emit({'ui_changed': 'BW2'}) def _update_scale_impz(self): """ recalculate the energy scaling for impulse functions when impulse type or relevant frequency / bandwidth parameter have been updated """ if self.stim == "dirac": self.scale_impz = 1. elif self.stim == "sinc": self.scale_impz = self.f1 * 2 elif self.stim == "gauss": self.scale_impz = self.f1 * 2 * self.BW1 elif self.stim == "rect": self.scale_impz = 1. / self.TW1 def _update_phi2(self): """ Update value for self.phi2 from the QLineEditWidget""" self.phi2 = safe_eval(self.ledPhi2.text(), self.phi2, return_type='float') self.ledPhi2.setText(str(self.phi2)) self.emit({'ui_changed': 'phi2'}) def _update_chirp_type(self): """ Update value for self.chirp_type from data field of ComboBox""" self.chirp_type = qget_cmb_box(self.cmbChirpType) self.emit({'ui_changed': 'chirp_type'}) def _update_impulse_type(self): """ Update value for self.impulse_type from data field of ComboBox""" self.impulse_type = qget_cmb_box(self.cmbImpulseType) self._enable_stim_widgets() def _update_sinusoid_type(self): """ Update value for self.sinusoid_type from data field of ComboBox""" self.sinusoid_type = qget_cmb_box(self.cmbSinusoidType) self._enable_stim_widgets() def _update_periodic_type(self): """ Update value for self.periodic_type from data field of ComboBox""" self.periodic_type = qget_cmb_box(self.cmbPeriodicType) self._enable_stim_widgets() def _update_modulation_type(self): """ Update value for self.modulation_type from from data field of ComboBox""" self.modulation_type = qget_cmb_box(self.cmbModulationType) self._enable_stim_widgets() # ------------------------------------------------------------- def _update_noi(self): """ Update type + value + label for self.noi for noise""" self.noise = qget_cmb_box(self.cmbNoise) self.lblNoi.setVisible(self.noise != 'none') self.ledNoi.setVisible(self.noise != 'none') if self.noise != 'none': self.noi = safe_eval(self.ledNoi.text(), 0, return_type='cmplx') self.ledNoi.setText(str(self.noi)) if self.noise == 'gauss': self.lblNoi.setText(to_html(" σ =", frmt='bi')) self.ledNoi.setToolTip( "<span>Standard deviation of statistical process," "noise power is <i>P</i> = σ<sup>2</sup></span>") elif self.noise == 'uniform': self.lblNoi.setText(to_html(" Δ =", frmt='bi')) self.ledNoi.setToolTip( "<span>Interval size for uniformly distributed process (e.g. " "quantization step size for quantization noise), centered around 0. " "Noise power is <i>P</i> = Δ<sup>2</sup>/12.</span>") elif self.noise == 'prbs': self.lblNoi.setText(to_html(" A =", frmt='bi')) self.ledNoi.setToolTip( "<span>Amplitude of bipolar Pseudorandom Binary Sequence. " "Noise power is <i>P</i> = A<sup>2</sup>.</span>") elif self.noise == 'mls': self.lblNoi.setText(to_html(" A =", frmt='bi')) self.ledNoi.setToolTip( "<span>Amplitude of Maximum Length Sequence. " "Noise power is <i>P</i> = A<sup>2</sup>.</span>") elif self.noise == 'brownian': self.lblNoi.setText(to_html(" σ =", frmt='bi')) self.ledNoi.setToolTip( "<span>Standard deviation of the Gaussian process " "that is cumulated.</span>") self.emit({'ui_changed': 'noi'}) def _update_DC(self): """ Update value for self.DC from the QLineEditWidget""" self.DC = safe_eval(self.ledDC.text(), 0, return_type='cmplx') self.ledDC.setText(str(self.DC)) self.emit({'ui_changed': 'dc'}) def _update_stim_formula(self): """Update string with formula to be evaluated by numexpr""" self.stim_formula = self.ledStimFormula.text().strip() self.ledStimFormula.setText(str(self.stim_formula)) self.emit({'ui_changed': 'stim_formula'}) def _update_stim_par1(self): """ Update value for self.par1 from QLineEditWidget""" self.stim_par1 = safe_eval(self.ledStimPar1.text(), self.stim_par1, sign='pos', return_type='float') self.ledStimPar1.setText(str(self.stim_par1)) self.emit({'ui_changed': 'stim_par1'})
class Input_Fixpoint_Specs(QWidget): """ Create the widget that holds the dynamically loaded fixpoint filter ui """ # sig_resize = pyqtSignal() # emit a signal when the image has been resized sig_rx_local = pyqtSignal(object) # incoming from subwidgets -> process_sig_rx_local sig_rx = pyqtSignal(object) # incoming, connected to input_tab_widget.sig_rx sig_tx = pyqtSignal(object) # outcgoing from pyfda.libs.pyfda_qt_lib import emit def __init__(self, parent=None): super(Input_Fixpoint_Specs, self).__init__(parent) self.tab_label = 'Fixpoint' self.tool_tip = ("<span>Select a fixpoint implementation for the filter," " simulate it or generate a Verilog netlist.</span>") self.parent = parent self.fx_path = os.path.realpath( os.path.join(dirs.INSTALL_DIR, 'fixpoint_widgets')) self.no_fx_filter_img = os.path.join(self.fx_path, "no_fx_filter.png") if not os.path.isfile(self.no_fx_filter_img): logger.error("Image {0:s} not found!".format(self.no_fx_filter_img)) self.default_fx_img = os.path.join(self.fx_path, "default_fx_img.png") if not os.path.isfile(self.default_fx_img): logger.error("Image {0:s} not found!".format(self.default_fx_img)) self._construct_UI() inst_wdg_list = self._update_filter_cmb() if len(inst_wdg_list) == 0: logger.warning("No fixpoint filter found for this type of filter!") else: logger.debug("Imported {0:d} fixpoint filters:\n{1}" .format(len(inst_wdg_list.split("\n"))-1, inst_wdg_list)) self._update_fixp_widget() # ------------------------------------------------------------------------------ def process_sig_rx_local(self, dict_sig: dict = None) -> None: """ Process signals coming in from input and output quantizer subwidget and the dynamically instantiated subwidget and emit {'fx_sim': 'specs_changed'} in the end. """ if dict_sig['id'] == id(self): logger.warning(f'RX_LOCAL - Stopped infinite loop: "{first_item(dict_sig)}"') return elif 'fx_sim' in dict_sig and dict_sig['fx_sim'] == 'specs_changed': self.wdg_dict2ui() # update wordlengths in UI and set RUN button to 'changed' dict_sig.update({'id': id(self)}) # propagate 'specs_changed' with self 'id' self.emit(dict_sig) return # ---- Process input and output quantizer settings ('ui' in dict_sig) -- elif 'ui' in dict_sig: if 'wdg_name' not in dict_sig: logger.warning(f"No key 'wdg_name' in dict_sig:\n{pprint_log(dict_sig)}") return elif dict_sig['wdg_name'] == 'w_input': """ Input fixpoint format has been changed or butLock has been clicked. When I/O lock is active, copy input fixpoint word format to output word format. """ if dict_sig['ui'] == 'butLock'\ and not self.wdg_w_input.butLock.isChecked(): # butLock was deactivitated, don't do anything return elif self.wdg_w_input.butLock.isChecked(): # but lock was activated or wordlength setting have been changed fb.fil[0]['fxqc']['QO']['WI'] = fb.fil[0]['fxqc']['QI']['WI'] fb.fil[0]['fxqc']['QO']['WF'] = fb.fil[0]['fxqc']['QI']['WF'] fb.fil[0]['fxqc']['QO']['W'] = fb.fil[0]['fxqc']['QI']['W'] elif dict_sig['wdg_name'] == 'w_output': """ Output fixpoint format has been changed. When I/O lock is active, copy output fixpoint word format to input word format. """ if self.wdg_w_input.butLock.isChecked(): fb.fil[0]['fxqc']['QI']['WI'] = fb.fil[0]['fxqc']['QO']['WI'] fb.fil[0]['fxqc']['QI']['WF'] = fb.fil[0]['fxqc']['QO']['WF'] fb.fil[0]['fxqc']['QI']['W'] = fb.fil[0]['fxqc']['QO']['W'] elif dict_sig['wdg_name'] in {'q_output', 'q_input'}: pass else: logger.error("Unknown wdg_name '{0}' in dict_sig:\n{1}" .format(dict_sig['wdg_name'], pprint_log(dict_sig))) return if dict_sig['ui'] not in {'WI', 'WF', 'ovfl', 'quant', 'cmbW', 'butLock'}: logger.warning("Unknown value '{0}' for key 'ui'".format(dict_sig['ui'])) self.wdg_dict2ui() # update wordlengths in UI and set RUN button to 'changed' self.emit({'fx_sim': 'specs_changed'}) # propagate 'specs_changed' else: logger.error(f"Unknown key/value in 'dict_sig':\n{pprint_log(dict_sig)}") # ------------------------------------------------------------------------------ def process_sig_rx(self, dict_sig: dict = None) -> None: """ Process signals coming in via `sig_rx` from other widgets. Trigger fx simulation: 1. ``fx_sim': 'init'``: Start fixpoint simulation by sending 'fx_sim':'start_fx_response_calculation' 2. ``fx_sim_calc_response()``: Receive stimulus from widget in 'fx_sim':'calc_frame_fx_response' and pass it to fixpoint simulation method 3. Store fixpoint response in `fb.fx_result` and return to initiating routine """ # logger.info( # "SIG_RX(): vis={0}\n{1}".format(self.isVisible(), pprint_log(dict_sig))) # logger.debug(f'SIG_RX(): "{first_item(dict_sig)}"') if dict_sig['id'] == id(self): # logger.warning(f'Stopped infinite loop: "{first_item(dict_sig)}"') return elif 'data_changed' in dict_sig and dict_sig['data_changed'] == "filter_designed": # New filter has been designed, update list of available filter topologies self._update_filter_cmb() return elif 'data_changed' in dict_sig or\ ('view_changed' in dict_sig and dict_sig['view_changed'] == 'q_coeff'): # Filter data has changed (but not the filter type) or the coefficient # format / wordlength have been changed in `input_coeffs`. The latter means # the view / display has been changed (wordlength) but not the actual # coefficients in the `input_coeffs` widget. However, the wordlength setting # is copied to the fxqc dict and from there to the fixpoint widget. # - update fields in the fixpoint filter widget - wordlength may have # been changed. # - Set RUN button to "changed" in wdg_dict2ui() self.wdg_dict2ui() # --------------- FX Simulation ------------------------------------------- elif 'fx_sim' in dict_sig: if dict_sig['fx_sim'] == 'init': # fixpoint simulation has been started externally, e.g. by # `impz.impz_init()`, return a handle to the fixpoint filter function # via signal-slot connection if not self.fx_wdg_found: logger.error("No fixpoint widget found!") qstyle_widget(self.butSimFx, "error") self.emit({'fx_sim': 'error'}) elif self.fx_sim_init() != 0: # returned an error qstyle_widget(self.butSimFx, "error") self.emit({'fx_sim': 'error'}) else: self.emit({'fx_sim': 'start_fx_response_calculation', 'fxfilter_func': self.fx_filt_ui.fxfilter}) elif dict_sig['fx_sim'] == 'calc_frame_fx_response': self.fx_sim_calc_response(dict_sig) # return to the routine collecting the response frame by frame return elif dict_sig['fx_sim'] == 'specs_changed': # fixpoint specification have been changed somewhere, update ui # and set run button to "changed" in wdg_dict2ui() self.wdg_dict2ui() elif dict_sig['fx_sim'] == 'finish': qstyle_widget(self.butSimFx, "normal") else: logger.error('Unknown "fx_sim" command option "{0}"\n' '\treceived from "{1}".' .format(dict_sig['fx_sim'], dict_sig['class'])) # ---- resize image when "Fixpoint" tab is selected or widget size is changed: elif 'ui_changed' in dict_sig and dict_sig['ui_changed'] in {'resized', 'tab'}\ and self.isVisible(): self.resize_img() # ------------------------------------------------------------------------------ def _construct_UI(self) -> None: """ Intitialize the main GUI, consisting of: - A combo box to select the filter topology and an image of the topology - The input quantizer - The UI of the fixpoint filter widget - Simulation and export buttons """ # ------------------------------------------------------------------------------ # Define frame and layout for the dynamically updated filter widget # The actual filter widget is instantiated in self.set_fixp_widget() later on self.layH_fx_wdg = QHBoxLayout() # self.layH_fx_wdg.setContentsMargins(*params['wdg_margins']) frmHDL_wdg = QFrame(self) frmHDL_wdg.setLayout(self.layH_fx_wdg) # frmHDL_wdg.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) # ------------------------------------------------------------------------------ # Initialize fixpoint filter combobox, title and description # ------------------------------------------------------------------------------ self.cmb_fx_wdg = QComboBox(self) self.cmb_fx_wdg.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.lblTitle = QLabel("not set", self) self.lblTitle.setWordWrap(True) self.lblTitle.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) layHTitle = QHBoxLayout() layHTitle.addWidget(self.cmb_fx_wdg) layHTitle.addWidget(self.lblTitle) self.frmTitle = QFrame(self) self.frmTitle.setLayout(layHTitle) self.frmTitle.setContentsMargins(*params['wdg_margins']) # ------------------------------------------------------------------------------ # Input and Output Quantizer # ------------------------------------------------------------------------------ # - instantiate widgets for input and output quantizer # - pass the quantization (sub-?) dictionary to the constructor # ------------------------------------------------------------------------------ self.wdg_w_input = UI_W(self, q_dict=fb.fil[0]['fxqc']['QI'], wdg_name='w_input', label='', lock_visible=True) self.wdg_w_input.sig_tx.connect(self.process_sig_rx_local) cmb_q = ['round', 'floor', 'fix'] self.wdg_w_output = UI_W(self, q_dict=fb.fil[0]['fxqc']['QO'], wdg_name='w_output', label='') self.wdg_w_output.sig_tx.connect(self.process_sig_rx_local) self.wdg_q_output = UI_Q(self, q_dict=fb.fil[0]['fxqc']['QO'], wdg_name='q_output', label='Output Format <i>Q<sub>Y </sub></i>:', cmb_q=cmb_q, cmb_ov=['wrap', 'sat']) self.wdg_q_output.sig_tx.connect(self.sig_rx_local) if HAS_DS: cmb_q.append('dsm') self.wdg_q_input = UI_Q(self, q_dict=fb.fil[0]['fxqc']['QI'], wdg_name='q_input', label='Input Format <i>Q<sub>X </sub></i>:', cmb_q=cmb_q) self.wdg_q_input.sig_tx.connect(self.sig_rx_local) # Layout and frame for input quantization layVQiWdg = QVBoxLayout() layVQiWdg.addWidget(self.wdg_q_input) layVQiWdg.addWidget(self.wdg_w_input) frmQiWdg = QFrame(self) # frmBtns.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken) frmQiWdg.setLayout(layVQiWdg) frmQiWdg.setContentsMargins(*params['wdg_margins']) # Layout and frame for output quantization layVQoWdg = QVBoxLayout() layVQoWdg.addWidget(self.wdg_q_output) layVQoWdg.addWidget(self.wdg_w_output) frmQoWdg = QFrame(self) # frmBtns.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken) frmQoWdg.setLayout(layVQoWdg) frmQoWdg.setContentsMargins(*params['wdg_margins']) # ------------------------------------------------------------------------------ # Dynamically updated image of filter topology (label as placeholder) # ------------------------------------------------------------------------------ # allow setting background color # lbl_fixp_img_palette = QPalette() # lbl_fixp_img_palette.setColor(QPalette(window, Qt: white)) # lbl_fixp_img_palette.setBrush(self.backgroundRole(), QColor(150, 0, 0)) # lbl_fixp_img_palette.setColor(QPalette: WindowText, Qt: blue) self.lbl_fixp_img = QLabel("img not set", self) self.lbl_fixp_img.setAutoFillBackground(True) # self.lbl_fixp_img.setPalette(lbl_fixp_img_palette) # self.lbl_fixp_img.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.embed_fixp_img(self.no_fx_filter_img) layHImg = QHBoxLayout() layHImg.setContentsMargins(0, 0, 0, 0) layHImg.addWidget(self.lbl_fixp_img) # , Qt.AlignCenter) self.frmImg = QFrame(self) self.frmImg.setLayout(layHImg) self.frmImg.setContentsMargins(*params['wdg_margins']) # ------------------------------------------------------------------------------ # Simulation and export Buttons # ------------------------------------------------------------------------------ self.butExportHDL = QPushButton(self) self.butExportHDL.setToolTip( "Create Verilog or VHDL netlist for fixpoint filter.") self.butExportHDL.setText("Create HDL") self.butSimFx = QPushButton(self) self.butSimFx.setToolTip("Start fixpoint simulation.") self.butSimFx.setText("Sim. FX") self.layHHdlBtns = QHBoxLayout() self.layHHdlBtns.addWidget(self.butSimFx) self.layHHdlBtns.addWidget(self.butExportHDL) # This frame encompasses the HDL buttons sim and convert frmHdlBtns = QFrame(self) # frmBtns.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken) frmHdlBtns.setLayout(self.layHHdlBtns) frmHdlBtns.setContentsMargins(*params['wdg_margins']) # ------------------------------------------------------------------- # Top level layout # ------------------------------------------------------------------- splitter = QSplitter(self) splitter.setOrientation(Qt.Vertical) splitter.addWidget(frmHDL_wdg) splitter.addWidget(frmQoWdg) splitter.addWidget(self.frmImg) # setSizes uses absolute pixel values, but can be "misused" by specifying values # that are way too large: in this case, the space is distributed according # to the _ratio_ of the values: splitter.setSizes([3000, 3000, 5000]) layVMain = QVBoxLayout() layVMain.addWidget(self.frmTitle) layVMain.addWidget(frmHdlBtns) layVMain.addWidget(frmQiWdg) layVMain.addWidget(splitter) layVMain.addStretch() layVMain.setContentsMargins(*params['wdg_margins']) self.setLayout(layVMain) # ---------------------------------------------------------------------- # GLOBAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.sig_rx.connect(self.process_sig_rx) self.sig_rx_local.connect(self.process_sig_rx_local) # dynamic connection in `self._update_fixp_widget()`: # ----- # if hasattr(self.fx_filt_ui, "sig_rx"): # self.sig_rx.connect(self.fx_filt_ui.sig_rx) # if hasattr(self.fx_filt_ui, "sig_tx"): # self.fx_filt_ui.sig_tx.connect(self.sig_rx_local) # ---- # ---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.cmb_fx_wdg.currentIndexChanged.connect(self._update_fixp_widget) self.butExportHDL.clicked.connect(self.exportHDL) self.butSimFx.clicked.connect(lambda x: self.emit({'fx_sim': 'start'})) # ---------------------------------------------------------------------- # EVENT FILTER # ---------------------------------------------------------------------- # # monitor events and generate sig_resize event when resized # self.lbl_fixp_img.installEventFilter(self) # # ... then redraw image when resized # self.sig_resize.connect(self.resize_img) # ------------------------------------------------------------------------------ def _update_filter_cmb(self) -> str: """ (Re-)Read list of available fixpoint filters for a given filter design every time a new filter design is selected. Then try to import the fixpoint designs in the list and populate the fixpoint implementation combo box `self.cmb_fx_wdg` when successfull. Returns ------- inst_wdg_str: str string with all fixpoint widgets that could be instantiated successfully """ inst_wdg_str = "" # full names of successfully instantiated widgets for logging # remember last fx widget setting: last_fx_wdg = qget_cmb_box(self.cmb_fx_wdg, data=False) self.cmb_fx_wdg.clear() fc = fb.fil[0]['fc'] if 'fix' in fb.filter_classes[fc]: self.cmb_fx_wdg.blockSignals(True) for class_name in fb.filter_classes[fc]['fix']: # get class name try: # construct module + class name ... mod_class_name = fb.fixpoint_classes[class_name]['mod'] + '.'\ + class_name # ... and display name disp_name = fb.fixpoint_classes[class_name]['name'] self.cmb_fx_wdg.addItem(disp_name, mod_class_name) inst_wdg_str += '\t' + class_name + ' : ' + mod_class_name + '\n' except AttributeError as e: logger.warning('Widget "{0}":\n{1}'.format(class_name, e)) self.embed_fixp_img(self.no_fx_filter_img) continue # with next `class_name` of for loop except KeyError as e: logger.warning("No fixpoint filter for filter type {0} available." .format(e)) self.embed_fixp_img(self.no_fx_filter_img) continue # with next `class_name` of for loop # restore last fx widget if possible idx = self.cmb_fx_wdg.findText(last_fx_wdg) # set to idx 0 if not found (returned -1) self.cmb_fx_wdg.setCurrentIndex(max(idx, 0)) self.cmb_fx_wdg.blockSignals(False) else: # no fixpoint widget self.embed_fixp_img(self.no_fx_filter_img) self._update_fixp_widget() return inst_wdg_str # # ------------------------------------------------------------------------------ # def eventFilter(self, source, event): # """ # Filter all events generated by monitored QLabel, only resize events are # processed here, generating a `sig_resize` signal. All other events # are passed on to the next hierarchy level. # """ # if event.type() == QEvent.Resize: # logger.warning("resize event!") # self.sig_resize.emit() # # Call base class method to continue normal event processing: # return super(Input_Fixpoint_Specs, self).eventFilter(source, event) # ------------------------------------------------------------------------------ def embed_fixp_img(self, img_file: str) -> QPixmap: """ Embed `img_file` in png format as `self.img_fixp` Parameters ---------- img_file: str path and file name to image file Returns ------- self.img_fixp: QPixmap object pixmap containing the passed img_file """ if not os.path.isfile(img_file): logger.warning("Image file {0} doesn't exist.".format(img_file)) img_file = self.default_fx_img _, file_extension = os.path.splitext(img_file) if file_extension != '.png': logger.error('Unknown file extension "{0}"!'.format(file_extension)) img_file = self.default_fx_img self.img_fixp = QPixmap(img_file) # logger.warning(f"img_fixp = {img_file}") # logger.warning(f"_embed_fixp_img(): {self.img_fixp.__class__.__name__}") return self.img_fixp # ------------------------------------------------------------------------------ def resize_img(self) -> None: """ Triggered when `self` (the widget) is selected or resized. The method resizes the image inside QLabel to completely fill the label while keeping the aspect ratio. An offset of some pixels is needed, otherwise the image is clipped. """ # logger.warning(f"resize_img(): img_fixp = {self.img_fixp.__class__.__name__}") if self.parent is None: # parent is QApplication, has no width or height par_w, par_h = 300, 700 # fixed size for module level test else: # widget parent is InputTabWidget() par_w, par_h = self.parent.width(), self.parent.height() img_w, img_h = self.img_fixp.width(), self.img_fixp.height() if img_w > 10: max_h = int(max(np.floor(img_h * par_w/img_w) - 5, 20)) else: max_h = 200 logger.debug("img size: {0},{1}, frm size: {2},{3}, max_h: {4}" .format(img_w, img_h, par_w, par_h, max_h)) # The following doesn't work because the width of the parent widget can grow # with the image size # img_scaled = self.img_fixp.scaled(self.lbl_fixp_img.size(), # Qt.KeepAspectRatio, Qt.SmoothTransformation) img_scaled = self.img_fixp.scaledToHeight(max_h, Qt.SmoothTransformation) self.lbl_fixp_img.setPixmap(img_scaled) # ------------------------------------------------------------------------------ def _update_fixp_widget(self): """ This method is called at the initialization of the widget and when a new fixpoint filter implementation is selected from the combo box: - Destruct old instance of fixpoint filter widget `self.fx_filt_ui` - Import and instantiate new fixpoint filter widget e.g. after changing the filter topology as - Try to load image for filter topology - Update the UI of the widget - Try to instantiate HDL filter as `self.fx_filt_ui.fixp_filter` with dummy data - emit {'fx_sim': 'specs_changed'} when successful """ def _disable_fx_wdg(self) -> None: if hasattr(self, "fx_filt_ui") and self.fx_filt_ui is not None: # is a fixpoint widget loaded? try: # try to remove widget from layout self.layH_fx_wdg.removeWidget(self.fx_filt_ui) # delete QWidget when scope has been left self.fx_filt_ui.deleteLater() except AttributeError as e: logger.error("Destructing UI failed!\n{0}".format(e)) self.fx_wdg_found = False self.butSimFx.setEnabled(False) self.butExportHDL.setVisible(False) # self.layH_fx_wdg.setVisible(False) self.img_fixp = self.embed_fixp_img(self.no_fx_filter_img) self.resize_img() self.lblTitle.setText("") self.fx_filt_ui = None # ----------------------------------------------------------- _disable_fx_wdg(self) # destruct old fixpoint widget instance: # instantiate new fixpoint widget class as self.fx_filt_ui cmb_wdg_fx_cur = qget_cmb_box(self.cmb_fx_wdg, data=False) if cmb_wdg_fx_cur: # at least one valid fixpoint widget found self.fx_wdg_found = True # get list [module name and path, class name] fx_mod_class_name = qget_cmb_box(self.cmb_fx_wdg, data=True).rsplit('.', 1) fx_mod = importlib.import_module(fx_mod_class_name[0]) # get module fx_filt_ui_class = getattr(fx_mod, fx_mod_class_name[1]) # get class logger.info("Instantiating new FX widget\n\t" f"{fx_mod.__name__}.{fx_filt_ui_class.__name__}") # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ self.fx_filt_ui = fx_filt_ui_class() # instantiate the fixpoint widget # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # and add it to layout: self.layH_fx_wdg.addWidget(self.fx_filt_ui, stretch=1) self.fx_filt_ui.setVisible(True) self.wdg_dict2ui() # initialize the fixpoint subwidgets from the fxqc_dict # ---- connect signals to fx_filt_ui ---- if hasattr(self.fx_filt_ui, "sig_rx"): self.sig_rx.connect(self.fx_filt_ui.sig_rx) if hasattr(self.fx_filt_ui, "sig_tx"): self.fx_filt_ui.sig_tx.connect(self.sig_rx_local) # ---- get name of new fixpoint filter image ---- if not (hasattr(self.fx_filt_ui, "img_name") and self.fx_filt_ui.img_name): # no image name defined, use default image img_file = self.default_fx_img else: # get path of imported fixpoint widget ... file_path = os.path.dirname(fx_mod.__file__) # ... and construct full image name from it img_file = os.path.join(file_path, self.fx_filt_ui.img_name) # ---- instantiate and scale graphic of filter topology ---- self.embed_fixp_img(img_file) self.resize_img() # ---- set title and description for filter self.lblTitle.setText(self.fx_filt_ui.title) # Check which methods the fixpoint widget provides and enable # corresponding buttons: self.butExportHDL.setVisible(hasattr(self.fx_filt_ui, "to_hdl")) self.butSimFx.setEnabled(hasattr(self.fx_filt_ui, "fxfilter")) self.update_fxqc_dict() self.emit({'fx_sim': 'specs_changed'}) # ------------------------------------------------------------------------------ def wdg_dict2ui(self): """ Trigger an update of the fixpoint widget UI when view (i.e. fixpoint coefficient format) or data have been changed outside this class. Additionally, pass the fixpoint quantization widget to update / restore other subwidget settings. Set the RUN button to "changed". """ # fb.fil[0]['fxqc']['QCB'].update({'scale':(1 << fb.fil[0]['fxqc']['QCB']['W'])}) self.wdg_q_input.dict2ui(fb.fil[0]['fxqc']['QI']) self.wdg_q_output.dict2ui(fb.fil[0]['fxqc']['QO']) self.wdg_w_input.dict2ui(fb.fil[0]['fxqc']['QI']) self.wdg_w_output.dict2ui(fb.fil[0]['fxqc']['QO']) if self.fx_wdg_found and hasattr(self.fx_filt_ui, "dict2ui"): self.fx_filt_ui.dict2ui() # dict_sig = {'fx_sim':'specs_changed'} # self.emit(dict_sig) qstyle_widget(self.butSimFx, "changed") # ------------------------------------------------------------------------------ def update_fxqc_dict(self): """ Update the fxqc dictionary before simulation / HDL generation starts. """ if self.fx_wdg_found: # get a dict with the coefficients and fixpoint settings from fixpoint widget if hasattr(self.fx_filt_ui, "ui2dict"): fb.fil[0]['fxqc'].update(self.fx_filt_ui.ui2dict()) logger.debug("update fxqc: \n{0}".format(pprint_log(fb.fil[0]['fxqc']))) else: logger.error("No fixpoint widget found!") # ------------------------------------------------------------------------------ def exportHDL(self): """ Synthesize HDL description of filter """ dlg = QFD(self) # instantiate file dialog object file_types = "Verilog (*.v)" # needed for overwrite confirmation when name is entered without suffix: dlg.setDefaultSuffix('v') dlg.setWindowTitle('Export Verilog') dlg.setNameFilter(file_types) dlg.setDirectory(dirs.save_dir) # set mode "save file" instead "open file": dlg.setAcceptMode(QFD.AcceptSave) dlg.setOption(QFD.DontConfirmOverwrite, False) if dlg.exec_() == QFD.Accepted: hdl_file = qstr(dlg.selectedFiles()[0]) # hdl_type = extract_file_ext(qstr(dlg.selectedNameFilter()))[0] # ============================================================================= # # static method getSaveFileName_() is simple but unflexible # hdl_file, hdl_filter = dlg.getSaveFileName_( # caption="Save Verilog netlist as (this also defines the module name)", # directory=dirs.save_dir, filter=file_types) # hdl_file = qstr(hdl_file) # if hdl_file != "": # "operation cancelled" returns an empty string # # return '.v' or '.vhd' depending on filetype selection: # # hdl_type = extract_file_ext(qstr(hdl_filter))[0] # # sanitized dir + filename + suffix. The filename suffix is replaced # # by `v` later. # hdl_file = os.path.normpath(hdl_file) # complete path + file name # ============================================================================= hdl_dir_name = os.path.dirname(hdl_file) # extract the directory path if not os.path.isdir(hdl_dir_name): # create directory if it doesn't exist os.mkdir(hdl_dir_name) dirs.save_dir = hdl_dir_name # make this directory the new default / base dir hdl_file_name = os.path.splitext(os.path.basename(hdl_file))[0] hdl_full_name = os.path.join(hdl_dir_name, hdl_file_name + ".v") # remove all non-alphanumeric chars: vlog_mod_name = re.sub(r'\W+', '', hdl_file_name).lower() logger.info('Creating hdl_file "{0}"\n\twith top level module "{1}"' .format(hdl_full_name, vlog_mod_name)) try: self.update_fxqc_dict() self.fx_filt_ui.construct_fixp_filter() code = self.fx_filt_ui.to_hdl(name=vlog_mod_name) # logger.info(str(code)) # print verilog code to console with io.open(hdl_full_name, 'w', encoding="utf8") as f: f.write(str(code)) logger.info("HDL conversion finished!") except (IOError, TypeError) as e: logger.warning(e) # -------------------------------------------------------------------------- def fx_sim_init(self): """ Initialize fix-point simulation: - Update the `fxqc_dict` containing all quantization information - Setup a filter instance for fixpoint simulation - Request a stimulus signal Returns ------- error: int 0 for sucessful fx widget construction, -1 for error """ try: self.update_fxqc_dict() self.fx_filt_ui.init_filter() # setup filter instance return 0 except ValueError as e: logger.error('Fixpoint stimulus generation failed during "init"' '\nwith "{0} "'.format(e)) return -1 # ------------------------------------------------------------------------------ def fx_sim_calc_response(self, dict_sig) -> None: """ - Read fixpoint stimulus from `dict_sig` in integer format - Pass it to the fixpoint filter which calculates the fixpoint response - Store the result in `fb.fx_results` and return. In case of an error, `fb.fx_results == None` Returns ------- None """ try: # logger.info( # 'Simulate fixpoint frame with "{0}" stimulus:\n\t{1}'.format( # dict_sig['class'], # pprint_log(dict_sig['fx_stimulus'], tab=" "), # )) # Run fixpoint simulation and store the results as integer values: fb.fx_results = self.fx_filt_ui.fxfilter(dict_sig['fx_stimulus']) if len(fb.fx_results) == 0: logger.error("Fixpoint simulation returned empty results!") # else: # # logger.debug("fx_results: {0}"\ # # .format(pprint_log(fb.fx_results, tab= " "))) # logger.info( # f'Fixpoint simulation successful for dict\n{pprint_log(dict_sig)}' # f'\tStimuli: Shape {np.shape(dict_sig["fx_stimulus"])}' # f' of type "{dict_sig["fx_stimulus"].dtype}"' # f'\n\tResponse: Shape {np.shape(fb.fx_results)}' # f' of type "{type(fb.fx_results).__name__} "' # f' ("{type(fb.fx_results[0]).__name__}")' # ) except ValueError as e: logger.error("Simulator error {0}".format(e)) fb.fx_results = None except AssertionError as e: logger.error('Fixpoint simulation failed for dict\n{0}' '\twith msg. "{1}"\n\tStimuli: Shape {2} of type "{3}"' '\n\tResponse: Shape {4} of type "{5}"'.format( pprint_log(dict_sig), e, np.shape(dict_sig['fx_stimulus']), dict_sig['fx_stimulus'].dtype, np.shape(fb.fx_results), type(fb.fx_results) )) fb.fx_results = None if fb.fx_results is None: qstyle_widget(self.butSimFx, "error") else: pass # everything ok, return # logger.debug("Sending fixpoint results") return
def about_window(self): """ Display an "About" window with copyright and version infos """ def to_clipboard(my_string): """ Copy version info to clipboard """ mapping = [ ('<br>', '\n'), ('<br />', '\n'), ('</tr>', '\n'), ('</th>', '\n==============\n'), ('</table>', '\n'), ('<hr>', '\n---------\n'), ('<b>', ''), ('</b>', ''), ('<tr>', ''), ('<td>', ''), ('</td>', '\t'), ('<th>', ''), (' ', ' '), ('<table>', ''), # ('</a>',''), ("<th style='font-size:large;'>", "\n") ] for k, v in mapping: my_string = my_string.replace(k, v) fb.clipboard.setText(my_string) user_dirs_str = "" if dirs.USER_DIRS: for d in dirs.USER_DIRS: user_dirs_str += d + '<br />' info_string = ("<b><a href=https://www.github.com/chipmuenk/pyfda>pyfda</a> " "Version {0} (c) 2013 - 2020 Christian Münker</b><br />" "Design, analyze and synthesize digital filters. Docs @ " "<a href=https://pyfda.rtfd.org>pyfda.rtfd.org</a>" " (<a href=https://media.readthedocs.org/pdf/pyfda/latest/pyfda.pdf>pdf</a>)<hr>"\ .format(version.__version__)) versions_string = ( "<b>OS:</b> {0} {1}<br><b>User Name:</b> {2}<br>".format( dirs.OS, dirs.OS_VER, dirs.USER_NAME)) # dir_string = ("<table><th style='font-size:large;'>Imported Modules</th>" # "<tr><td>  {0}</td></tr>"\ # .format( pyfda_lib.mod_version().replace("\n", "<br>  "))) dir_string = ( "<table><th style='font-size:large;'>Software Versions</th>") dir_string += pyfda_lib.mod_version() dir_string += "</table>" dir_string += ("<table><th style='font-size:large;'>Directories</th>" "<tr><td><b>Home:</b></td><td>{0}</td></tr>" "<tr><td><b>Install: </b></td><td>{1}</td></tr>" "<tr><td><b>Config: </b></td><td>{2}</td></tr>" "<tr><td><b>User: </b></td><td>{3}</td></tr>" "<tr><td><b>Temp:</b></td><td>{4}</td></tr>"\ .format( dirs.HOME_DIR, dirs.INSTALL_DIR, dirs.CONF_DIR, user_dirs_str[:-6], dirs.TEMP_DIR)) dir_string += ("<th style='font-size:large;'>Logging Files</th>" "<tr><td><b>Config:</b></td><td>{0}</td></tr>" "<tr><td><b>Output: </b></td><td>{1}</td></tr>" "</table>"\ .format(dirs.USER_LOG_CONF_DIR_FILE, dirs.LOG_DIR_FILE)) about_string = info_string + versions_string + dir_string #msg = QMessageBox.about(self, "About pyFDA", info_string) butClipboard = QPushButton(self) butClipboard.setIcon(QIcon(':/clipboard.svg')) butClipboard.setToolTip("Copy text to clipboard.") # butClipboard.adjustSize() # butClipboard.setFixedSize(self.checkLayout.sizeHint()) msg = QMessageBox(self) msg.setIconPixmap( QPixmap(':/pyfda_icon.svg').scaledToHeight( 32, Qt.SmoothTransformation)) msg.addButton(butClipboard, QMessageBox.ActionRole) msg.setText(about_string) # msg.setInformativeText("This is additional information") #msg.setDetailedText(versions_string) # adds a button that opens another textwindow msg.setWindowTitle("About pyFDA") msg.setStandardButtons(QMessageBox.Ok) # | QMessageBox.Cancel # close Message box with close event triggered by "x" icon msg.closeEvent = self.closeEvent butClipboard.clicked.connect(lambda: to_clipboard(about_string)) retval = msg.exec_()
class Input_PZ_UI(QWidget): """ Create the UI for the FilterPZ class """ sig_rx = pyqtSignal(object) # incoming sig_tx = pyqtSignal(object) # outgoing def __init__(self, parent): """ Pass instance `parent` of parent class (FilterCoeffs) """ super(Input_PZ_UI, self).__init__(parent) # self.parent = parent # instance of the parent (not the base) class self.eps = 1.e-4 # # tolerance value for e.g. setting P/Z to zero self._construct_UI() #------------------------------------------------------------------------------ def process_sig_rx(self, dict_sig=None): """ Process signals coming from the CSV pop-up window """ logger.debug("PROCESS_SIG_RX\n{0}".format(pprint_log(dict_sig))) if 'closeEvent' in dict_sig: self._close_csv_win() self.sig_tx.emit({'sender':__name__, 'ui_changed': 'csv'}) return # probably not needed elif 'ui_changed' in dict_sig: self._set_load_save_icons() # update icons file <-> clipboard # inform e.g. the p/z input widget about changes in CSV options self.sig_tx.emit({'sender':__name__, 'ui_changed': 'csv'}) #------------------------------------------------------------------------------ def _construct_UI(self): """ Intitialize the widget, consisting of: - top chkbox row - coefficient table - two bottom rows with action buttons """ self.bfont = QFont() self.bfont.setBold(True) self.bifont = QFont() self.bifont.setBold(True) self.bifont.setItalic(True) # q_icon_size = QSize(20, 20) # optional, size is derived from butEnable # --------------------------------------------- # UI Elements for controlling the display # --------------------------------------------- self.butEnable = QPushButton(self) self.butEnable.setIcon(QIcon(':/circle-x.svg')) q_icon_size = self.butEnable.iconSize() # <- set this for manual icon sizing self.butEnable.setIconSize(q_icon_size) self.butEnable.setCheckable(True) self.butEnable.setChecked(True) self.butEnable.setToolTip("<span>Show / hide poles and zeros in an editable table." " For high order systems, the table display might be slow.</span>") self.cmbPZFrmt = QComboBox(self) pz_formats = [('Cartesian', 'cartesian'), ('Polar (rad)', 'polar_rad'), ('Polar (pi)', 'polar_pi'), ('Polar (°)', 'polar_deg')] # display text, data # π: u'3C0, °: u'B0, ∠: u'2220 for pz in pz_formats: self.cmbPZFrmt.addItem(*pz) self.cmbPZFrmt.setSizeAdjustPolicy(QComboBox.AdjustToContents) # self.cmbPZFrmt.setEnabled(False) self.cmbPZFrmt.setToolTip("<span>Set display format for poles and zeros to" " either cartesian (x + jy) or polar (r * ∠ Ω)." " Type 'o' for '°', '<' for '∠' and 'pi' for 'π'.</span>") self.spnDigits = QSpinBox(self) self.spnDigits.setRange(0,16) self.spnDigits.setToolTip("Number of digits to display.") self.lblDigits = QLabel("Digits", self) self.lblDigits.setFont(self.bifont) self.cmbCausal = QComboBox(self) causal_types = ['Causal', 'Acausal', 'Anticausal'] for cs in causal_types: self.cmbCausal.addItem(cs) qset_cmb_box(self.cmbCausal, 'Causal') self.cmbCausal.setToolTip('<span>Set the system type. Not implemented yet.</span>') self.cmbCausal.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.cmbCausal.setEnabled(False) layHDisplay = QHBoxLayout() layHDisplay.setAlignment(Qt.AlignLeft) layHDisplay.addWidget(self.butEnable) layHDisplay.addWidget(self.cmbPZFrmt) layHDisplay.addWidget(self.spnDigits) layHDisplay.addWidget(self.lblDigits) layHDisplay.addWidget(self.cmbCausal) layHDisplay.addStretch() # --------------------------------------------- # UI Elements for setting the gain # --------------------------------------------- self.lblNorm = QLabel(to_html("Normalize:", frmt='bi'), self) self.cmbNorm = QComboBox(self) self.cmbNorm.addItems(["None", "1", "Max"]) self.cmbNorm.setToolTip("<span>Set the gain <i>k</i> so that H(f)<sub>max</sub> is " "either 1 or the max. of the previous system.</span>") self.lblGain = QLabel(to_html("k =", frmt='bi'), self) self.ledGain = QLineEdit(self) self.ledGain.setToolTip("<span>Specify gain factor <i>k</i>" " (only possible for Normalize = 'None').</span>") self.ledGain.setText(str(1.)) self.ledGain.setObjectName("ledGain") layHGain = QHBoxLayout() layHGain.addWidget(self.lblNorm) layHGain.addWidget(self.cmbNorm) layHGain.addWidget(self.lblGain) layHGain.addWidget(self.ledGain) layHGain.addStretch() # --------------------------------------------- # UI Elements for loading / storing / manipulating cells and rows # --------------------------------------------- # self.cmbFilterType = QComboBox(self) # self.cmbFilterType.setObjectName("comboFilterType") # self.cmbFilterType.setToolTip("Select between IIR and FIR filte for manual entry.") # self.cmbFilterType.addItems(["FIR","IIR"]) # self.cmbFilterType.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.butAddCells = QPushButton(self) self.butAddCells.setIcon(QIcon(':/row_insert_above.svg')) self.butAddCells.setIconSize(q_icon_size) self.butAddCells.setToolTip("<SPAN>Select cells to insert a new cell above each selected cell. " "Use <SHIFT> or <CTRL> to select multiple cells. " "When nothing is selected, add a row at the end.</SPAN>") self.butDelCells = QPushButton(self) self.butDelCells.setIcon(QIcon(':/row_delete.svg')) self.butDelCells.setIconSize(q_icon_size) self.butDelCells.setToolTip("<SPAN>Delete selected cell(s) from the table. " "Use <SHIFT> or <CTRL> to select multiple cells. " "When nothing is selected, delete the last row.</SPAN>") self.butSave = QPushButton(self) self.butSave.setIcon(QIcon(':/upload.svg')) self.butSave.setIconSize(q_icon_size) self.butSave.setToolTip("<span>Copy P/Z table to filter dict and update all plots and widgets.</span>") self.butLoad = QPushButton(self) self.butLoad.setIcon(QIcon(':/download.svg')) self.butLoad.setIconSize(q_icon_size) self.butLoad.setToolTip("Reload P/Z table from filter dict.") self.butClear = QPushButton(self) self.butClear.setIcon(QIcon(':/trash.svg')) self.butClear.setIconSize(q_icon_size) self.butClear.setToolTip("Clear all table entries.") self.butFromTable = QPushButton(self) self.butFromTable.setIconSize(q_icon_size) self.butToTable = QPushButton(self) self.butToTable.setIconSize(q_icon_size) self.but_csv_options = QPushButton(self) self.but_csv_options.setIcon(QIcon(':/settings.svg')) self.but_csv_options.setIconSize(q_icon_size) self.but_csv_options.setToolTip("<span>Select CSV format and whether " "to copy to/from clipboard or file.</span>") self.but_csv_options.setCheckable(True) self.but_csv_options.setChecked(False) self._set_load_save_icons() # initialize icon / button settings layHButtonsCoeffs1 = QHBoxLayout() # layHButtonsCoeffs1.addWidget(self.cmbFilterType) layHButtonsCoeffs1.addWidget(self.butAddCells) layHButtonsCoeffs1.addWidget(self.butDelCells) layHButtonsCoeffs1.addWidget(self.butClear) layHButtonsCoeffs1.addWidget(self.butSave) layHButtonsCoeffs1.addWidget(self.butLoad) layHButtonsCoeffs1.addWidget(self.butFromTable) layHButtonsCoeffs1.addWidget(self.butToTable) layHButtonsCoeffs1.addWidget(self.but_csv_options) layHButtonsCoeffs1.addStretch() #------------------------------------------------------------------- # Eps / set zero settings # --------------------------------------------------------------------- self.butSetZero = QPushButton("= 0", self) self.butSetZero.setToolTip("<span>Set selected poles / zeros = 0 with a magnitude < ε. " "When nothing is selected, test the whole table.</span>") self.butSetZero.setIconSize(q_icon_size) lblEps = QLabel(self) lblEps.setText("<b><i>for ε</i> <</b>") self.ledEps = QLineEdit(self) self.ledEps.setToolTip("Specify tolerance value.") layHButtonsCoeffs2 = QHBoxLayout() layHButtonsCoeffs2.addWidget(self.butSetZero) layHButtonsCoeffs2.addWidget(lblEps) layHButtonsCoeffs2.addWidget(self.ledEps) layHButtonsCoeffs2.addStretch() # ######################## Main UI Layout ############################ # layout for frame (UI widget) layVMainF = QVBoxLayout() layVMainF.addLayout(layHDisplay) layVMainF.addLayout(layHGain) layVMainF.addLayout(layHButtonsCoeffs1) layVMainF.addLayout(layHButtonsCoeffs2) # This frame encompasses all UI elements frmMain = QFrame(self) frmMain.setLayout(layVMainF) layVMain = QVBoxLayout() layVMain.setAlignment(Qt.AlignTop) # this affects only the first widget (intended here) layVMain.addWidget(frmMain) layVMain.setContentsMargins(*params['wdg_margins']) self.setLayout(layVMain) #--- set initial values from dict ------------ self.spnDigits.setValue(params['FMT_pz']) self.ledEps.setText(str(self.eps)) #---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs #---------------------------------------------------------------------- self.but_csv_options.clicked.connect(self._open_csv_win) #------------------------------------------------------------------------------ def _open_csv_win(self): """ Pop-up window for CSV options """ if self.but_csv_options.isChecked(): qstyle_widget(self.but_csv_options, "changed") else: qstyle_widget(self.but_csv_options, "normal") if dirs.csv_options_handle is None: # no handle to the window? Create a new instance if self.but_csv_options.isChecked(): # Important: Handle to window must be class attribute otherwise it # (and the attached window) is deleted immediately when it goes out of scope dirs.csv_options_handle = CSV_option_box(self) dirs.csv_options_handle.sig_tx.connect(self.process_sig_rx) dirs.csv_options_handle.show() # modeless i.e. non-blocking popup window else: if not self.but_csv_options.isChecked(): # this should not happen if dirs.csv_options_handle is None: logger.warning("CSV options window is already closed!") else: dirs.csv_options_handle.close() self.sig_tx.emit({'sender':__name__, 'ui_changed': 'csv'}) #------------------------------------------------------------------------------ def _close_csv_win(self): dirs.csv_options_handle = None self.but_csv_options.setChecked(False) qstyle_widget(self.but_csv_options, "normal") #------------------------------------------------------------------------------ def _set_load_save_icons(self): """ Set icons / tooltipps for loading and saving data to / from file or clipboard depending on selected options. """ if params['CSV']['clipboard']: self.butFromTable.setIcon(QIcon(':/to_clipboard.svg')) self.butFromTable.setToolTip("<span>" "Copy table to clipboard, SELECTED items are copied as " "displayed. When nothing is selected, the whole table " "is copied with full precision in decimal format.</span>") self.butToTable.setIcon(QIcon(':/from_clipboard.svg')) self.butToTable.setToolTip("<span>Copy clipboard to table.</span>") else: self.butFromTable.setIcon(QIcon(':/save.svg')) self.butFromTable.setToolTip("<span>" "Save table to file, SELECTED items are copied as " "displayed. When nothing is selected, the whole table " "is copied with full precision in decimal format.</span>") self.butToTable.setIcon(QIcon(':/file.svg')) self.butToTable.setToolTip("<span>Load table from file.</span>") if dirs.csv_options_handle is None: qstyle_widget(self.but_csv_options, "normal") self.but_csv_options.setChecked(False) else: qstyle_widget(self.but_csv_options, "changed") self.but_csv_options.setChecked(True)
class Plot_FFT_win(QDialog): """ Create a pop-up widget for displaying time and frequency view of an FFT window. Data is passed via the dictionary `win_dict` that is specified during construction. Available windows, parameters, tooltipps etc are provided by the widget `pyfda_fft_windows_lib.QFFTWinSelection` Parameters ---------- parent : class instance reference to parent win_dict : dict dictionary derived from `pyfda_fft_windows_lib.all_windows_dict` with valid and available windows and their current settings (if applicable) sym : bool Passed to `get_window()`: When True, generate a symmetric window for use in filter design. When False (default), generate a periodic window for use in spectral analysis. title : str Title text for Qt Window ignore_close_event : bool Disable close event when True (Default) Methods ------- - `self.calc_N()` - `self.update_view()`: - `self.draw()`: calculate window and FFT and draw both - `get_win(N)` : Get the window array """ sig_rx = pyqtSignal(object) # incoming sig_tx = pyqtSignal(object) # outgoing from pyfda.libs.pyfda_qt_lib import emit def __init__(self, parent, win_dict, sym=False, title='pyFDA Window Viewer', ignore_close_event=True): super(Plot_FFT_win, self).__init__(parent) # make window stay on top qwindow_stay_on_top(self, True) self.win_dict = win_dict self.sym = sym self.ignore_close_event = ignore_close_event self.setWindowTitle(title) self.needs_calc = True self.bottom_f = -80 # min. value for dB display self.bottom_t = -60 # initial number of data points for visualization self.N_view = 32 self.pad = 16 # zero padding factor for smooth FFT plot # initial settings for checkboxes self.tbl_sel = [True, True, False, False] # False, False, False, False] self.tbl_cols = 6 self.tbl_rows = len(self.tbl_sel) // (self.tbl_cols // 3) self._construct_UI() self.calc_win_draw() # ------------------------------------------------------------------------------ def closeEvent(self, event): """ Catch `closeEvent` (user has tried to close the FFT window) and send a signal to parent to decide how to proceed. This can be disabled by setting `self.ignore_close_event = False` e.g. for instantiating the widget as a standalone window. """ if self.ignore_close_event: event.ignore() self.emit({'closeEvent': ''}) # ------------------------------------------------------------------------------ def process_sig_rx(self, dict_sig=None): """ Process signals coming from the navigation toolbar and from sig_rx: - `self.calc_N` - `self.update_view`: - `self.draw`: calculate window and FFT and draw both """ # logger.debug("PROCESS_SIG_RX - vis={0}, needs_calc={1}\n{2}" # .format(self.isVisible(), self.needs_calc, pprint_log(dict_sig))) if dict_sig['id'] == id(self): logger.warning("Stopped infinite loop:\n{0}".format( pprint_log(dict_sig))) return elif not self.isVisible(): self.needs_calc = True elif 'view_changed' in dict_sig and 'fft_win' in dict_sig['view_changed']\ or self.needs_calc: self.calc_win_draw() self.needs_calc = False elif 'home' in dict_sig: self.update_view() else: logger.error("Unknown content of dict_sig: {0}".format(dict_sig)) # ------------------------------------------------------------------------------ def _construct_UI(self): """ Intitialize the widget, consisting of: - Matplotlib widget with NavigationToolbar - Frame with control elements """ self.bfont = QFont() self.bfont.setBold(True) self.qfft_win_select = QFFTWinSelector(self, self.win_dict) self.lbl_N = QLabel(to_html("N =", frmt='bi')) self.led_N = QLineEdit(self) self.led_N.setText(str(self.N_view)) self.led_N.setMaximumWidth(qtext_width(N_x=8)) self.led_N.setToolTip( "<span>Number of window data points to display.</span>") # By default, the enter key triggers the default 'dialog action' in QDialog # widgets. This activates one of the pushbuttons. self.but_log_t = QPushButton("dB", default=False, autoDefault=False) self.but_log_t.setMaximumWidth(qtext_width(" dB ")) self.but_log_t.setObjectName("chk_log_time") self.but_log_t.setCheckable(True) self.but_log_t.setChecked(False) self.but_log_t.setToolTip("Display in dB") self.led_log_bottom_t = QLineEdit(self) self.led_log_bottom_t.setVisible(self.but_log_t.isChecked()) self.led_log_bottom_t.setText(str(self.bottom_t)) self.led_log_bottom_t.setMaximumWidth(qtext_width(N_x=6)) self.led_log_bottom_t.setToolTip( "<span>Minimum display value for log. scale.</span>") self.lbl_log_bottom_t = QLabel(to_html("min =", frmt='bi'), self) self.lbl_log_bottom_t.setVisible(self.but_log_t.isChecked()) self.but_norm_f = QPushButton("Max=1", default=False, autoDefault=False) self.but_norm_f.setCheckable(True) self.but_norm_f.setChecked(True) self.but_norm_f.setMaximumWidth(qtext_width(text=" Max=1 ")) self.but_norm_f.setToolTip( "Normalize window spectrum for a maximum of 1.") self.but_half_f = QPushButton("0...½", default=False, autoDefault=False) self.but_half_f.setCheckable(True) self.but_half_f.setChecked(True) self.but_half_f.setMaximumWidth(qtext_width(text=" 0...½ ")) self.but_half_f.setToolTip( "Display window spectrum in the range 0 ... 0.5 f_S.") # By default, the enter key triggers the default 'dialog action' in QDialog # widgets. This activates one of the pushbuttons. self.but_log_f = QPushButton("dB", default=False, autoDefault=False) self.but_log_f.setMaximumWidth(qtext_width(" dB ")) self.but_log_f.setObjectName("chk_log_freq") self.but_log_f.setToolTip("<span>Display in dB.</span>") self.but_log_f.setCheckable(True) self.but_log_f.setChecked(True) self.lbl_log_bottom_f = QLabel(to_html("min =", frmt='bi'), self) self.lbl_log_bottom_f.setVisible(self.but_log_f.isChecked()) self.led_log_bottom_f = QLineEdit(self) self.led_log_bottom_f.setVisible(self.but_log_t.isChecked()) self.led_log_bottom_f.setText(str(self.bottom_f)) self.led_log_bottom_f.setMaximumWidth(qtext_width(N_x=6)) self.led_log_bottom_f.setToolTip( "<span>Minimum display value for log. scale.</span>") # ---------------------------------------------------------------------- # ### frmControls ### # # This widget encompasses all control subwidgets # ---------------------------------------------------------------------- layH_win_select = QHBoxLayout() layH_win_select.addWidget(self.qfft_win_select) layH_win_select.setContentsMargins(0, 0, 0, 0) layH_win_select.addStretch(1) frmQFFT = QFrame(self) frmQFFT.setObjectName("frmQFFT") frmQFFT.setLayout(layH_win_select) hline = QHLine() layHControls = QHBoxLayout() layHControls.addWidget(self.lbl_N) layHControls.addWidget(self.led_N) layHControls.addStretch(1) layHControls.addWidget(self.lbl_log_bottom_t) layHControls.addWidget(self.led_log_bottom_t) layHControls.addWidget(self.but_log_t) layHControls.addStretch(5) layHControls.addWidget(QVLine(width=2)) layHControls.addStretch(5) layHControls.addWidget(self.but_norm_f) layHControls.addStretch(1) layHControls.addWidget(self.but_half_f) layHControls.addStretch(1) layHControls.addWidget(self.lbl_log_bottom_f) layHControls.addWidget(self.led_log_bottom_f) layHControls.addWidget(self.but_log_f) layVControls = QVBoxLayout() layVControls.addWidget(frmQFFT) layVControls.addWidget(hline) layVControls.addLayout(layHControls) frmControls = QFrame(self) frmControls.setObjectName("frmControls") frmControls.setLayout(layVControls) # ---------------------------------------------------------------------- # ### mplwidget ### # # Layout layVMainMpl (VBox) is defined within MplWidget, additional # widgets can be added below the matplotlib widget (here: frmControls) # # ---------------------------------------------------------------------- self.mplwidget = MplWidget(self) self.mplwidget.layVMainMpl.addWidget(frmControls) self.mplwidget.layVMainMpl.setContentsMargins(0, 0, 0, 0) # ---------------------------------------------------------------------- # ### frmInfo ### # # This widget encompasses the text info box and the table with window # parameters. # ---------------------------------------------------------------------- self.tbl_win_props = QTableWidget(self.tbl_rows, self.tbl_cols, self) self.tbl_win_props.setAlternatingRowColors(True) # Auto-resize of table can be set using the header (although it is invisible) self.tbl_win_props.verticalHeader().setSectionResizeMode( QHeaderView.Stretch) # Only the columns with data are stretched, the others are minimum size self.tbl_win_props.horizontalHeader().setSectionResizeMode( 1, QHeaderView.Stretch) self.tbl_win_props.horizontalHeader().setSectionResizeMode( 4, QHeaderView.Stretch) self.tbl_win_props.verticalHeader().setVisible(False) self.tbl_win_props.horizontalHeader().setVisible(False) self.tbl_win_props.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) self.tbl_win_props.setFixedHeight( self.tbl_win_props.rowHeight(0) * self.tbl_rows + self.tbl_win_props.frameWidth() * 2) # self.tbl_win_props.setVerticalScrollBarPolicy( # Qt.ScrollBarAlwaysOff) # self.tbl_win_props.setHorizontalScrollBarPolicy( # Qt.ScrollBarAlwaysOff) self._construct_table(self.tbl_rows, self.tbl_cols, " ") self.txtInfoBox = QTextBrowser(self) layVInfo = QVBoxLayout(self) layVInfo.addWidget(self.tbl_win_props) layVInfo.addWidget(self.txtInfoBox) frmInfo = QFrame(self) frmInfo.setObjectName("frmInfo") frmInfo.setLayout(layVInfo) # ---------------------------------------------------------------------- # ### splitter ### # # This widget encompasses all subwidgets # ---------------------------------------------------------------------- splitter = QSplitter(self) splitter.setOrientation(Qt.Vertical) splitter.addWidget(self.mplwidget) splitter.addWidget(frmInfo) # setSizes uses absolute pixel values, but can be "misused" by # specifying values that are way too large: in this case, the space # is distributed according to the _ratio_ of the values: splitter.setSizes([3000, 800]) layVMain = QVBoxLayout() layVMain.addWidget(splitter) self.setLayout(layVMain) # ---------------------------------------------------------------------- # Set subplots # self.ax = self.mplwidget.fig.subplots(nrows=1, ncols=2) self.ax_t = self.ax[0] self.ax_f = self.ax[1] self.calc_win_draw() # initial calculation and drawing # ---------------------------------------------------------------------- # GLOBAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.sig_rx.connect(self.process_sig_rx) self.sig_rx.connect(self.qfft_win_select.sig_rx) # ---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs # ---------------------------------------------------------------------- self.but_log_f.clicked.connect(self.update_view) self.but_log_t.clicked.connect(self.update_view) self.led_log_bottom_t.editingFinished.connect(self.update_bottom) self.led_log_bottom_f.editingFinished.connect(self.update_bottom) self.led_N.editingFinished.connect(self.calc_win_draw) self.but_norm_f.clicked.connect(self.calc_win_draw) self.but_half_f.clicked.connect(self.update_view) self.mplwidget.mplToolbar.sig_tx.connect(self.process_sig_rx) self.tbl_win_props.itemClicked.connect(self._handle_item_clicked) self.qfft_win_select.sig_tx.connect(self.update_fft_win) # ------------------------------------------------------------------------------ def _construct_table(self, rows, cols, val): """ Create a table with `rows` and `cols`, organized in sets of 3: Name (with a checkbox) - value - unit each item. Parameters ---------- rows : int number of rows cols : int number of columns (must be multiple of 3) val : str initialization value for the table Returns ------- None """ for r in range(rows): for c in range(cols): item = QTableWidgetItem(val) if c % 3 == 0: item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) if self.tbl_sel[r * 2 + c % 3]: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) self.tbl_win_props.setItem(r, c, item) # https://stackoverflow.com/questions/12366521/pyqt-checkbox-in-qtablewidget # ------------------------------------------------------------------------------ def update_fft_win(self, dict_sig=None): """ Update FFT window when window or parameters have changed and pass thru 'view_changed':'fft_win_type' or 'fft_win_par' """ self.calc_win_draw() self.emit(dict_sig) # ------------------------------------------------------------------------------ def calc_win_draw(self): """ (Re-)Calculate the window, its FFT and some characteristic values and update the plot of the window and its FFT. This should be triggered when the window type or length or a parameters has been changed. Returns ------- None Attributes ---------- """ self.N_view = safe_eval(self.led_N.text(), self.N_view, sign='pos', return_type='int') self.led_N.setText(str(self.N_view)) self.n = np.arange(self.N_view) self.win_view = self.qfft_win_select.get_window(self.N_view, sym=self.sym) if self.qfft_win_select.err: self.qfft_win_select.dict2ui() self.nenbw = self.N_view * np.sum(np.square(self.win_view))\ / np.square(np.sum(self.win_view)) self.cgain = np.sum(self.win_view) / self.N_view # coherent gain # calculate the FFT of the window with a zero padding factor # of `self.pad` self.F = fftfreq(self.N_view * self.pad, d=1. / fb.fil[0]['f_S']) self.Win = np.abs(fft(self.win_view, self.N_view * self.pad)) # Correct gain for periodic signals (coherent gain) if self.but_norm_f.isChecked(): self.Win /= (self.N_view * self.cgain) # calculate frequency of first zero and maximum sidelobe level first_zero = argrelextrema(self.Win[:(self.N_view * self.pad) // 2], np.less) if np.shape(first_zero)[1] > 0: first_zero = first_zero[0][0] self.first_zero_f = self.F[first_zero] self.sidelobe_level = np.max( self.Win[first_zero:(self.N_view * self.pad) // 2]) else: self.first_zero_f = np.nan self.sidelobe_level = 0 self.update_view() # ------------------------------------------------------------------------------ def _set_table_item(self, row, col, val, font=None, sel=None): """ Set the table item with the index `row, col` and the value val """ item = self.tbl_win_props.item(row, col) item.setText(str(val)) if font: self.tbl_win_props.item(row, col).setFont(font) if sel is True: item.setCheckState(Qt.Checked) if sel is False: item.setCheckState(Qt.Unchecked) # when sel is not specified, don't change anything # ------------------------------------------------------------------------------ def _handle_item_clicked(self, item): if item.column() % 3 == 0: # clicked on checkbox num = item.row() * 2 + item.column() // 3 if item.checkState() == Qt.Checked: self.tbl_sel[num] = True logger.debug('"{0}:{1}" Checked'.format(item.text(), num)) else: self.tbl_sel[num] = False logger.debug('"{0}:{1}" Unchecked'.format(item.text(), num)) elif item.column() % 3 == 1: # clicked on value field logger.info("{0:s} copied to clipboard.".format(item.text())) fb.clipboard.setText(item.text()) self.update_view() # ------------------------------------------------------------------------------ def update_bottom(self): """ Update log bottom settings """ self.bottom_t = safe_eval(self.led_log_bottom_t.text(), self.bottom_t, sign='neg', return_type='float') self.led_log_bottom_t.setText(str(self.bottom_t)) self.bottom_f = safe_eval(self.led_log_bottom_f.text(), self.bottom_f, sign='neg', return_type='float') self.led_log_bottom_f.setText(str(self.bottom_f)) self.update_view() # ------------------------------------------------------------------------------ def update_view(self): """ Draw the figure with new limits, scale, lin/log etc without recalculating the window or its FFT. """ # suppress "divide by zero in log10" warnings old_settings_seterr = np.seterr() np.seterr(divide='ignore') self.ax_t.cla() self.ax_f.cla() self.ax_t.set_xlabel(fb.fil[0]['plt_tLabel']) self.ax_t.set_ylabel(r'$w[n] \; \rightarrow$') self.ax_f.set_xlabel(fb.fil[0]['plt_fLabel']) self.ax_f.set_ylabel(r'$W(f) \; \rightarrow$') if self.but_log_t.isChecked(): self.ax_t.plot( self.n, np.maximum(20 * np.log10(np.abs(self.win_view)), self.bottom_t)) else: self.ax_t.plot(self.n, self.win_view) if self.but_half_f.isChecked(): F = self.F[:len(self.F * self.pad) // 2] Win = self.Win[:len(self.F * self.pad) // 2] else: F = fftshift(self.F) Win = fftshift(self.Win) if self.but_log_f.isChecked(): self.ax_f.plot( F, np.maximum(20 * np.log10(np.abs(Win)), self.bottom_f)) self.nenbw_disp = 10 * np.log10(self.nenbw) self.cgain_disp = 20 * np.log10(self.cgain) self.sidelobe_level_disp = 20 * np.log10(self.sidelobe_level) self.nenbw_unit = "dB" self.cgain_unit = "dB" else: self.ax_f.plot(F, Win) self.nenbw_disp = self.nenbw self.cgain_disp = self.cgain self.sidelobe_level_disp = self.sidelobe_level self.nenbw_unit = "bins" self.cgain_unit = "" self.led_log_bottom_t.setVisible(self.but_log_t.isChecked()) self.lbl_log_bottom_t.setVisible(self.but_log_t.isChecked()) self.led_log_bottom_f.setVisible(self.but_log_f.isChecked()) self.lbl_log_bottom_f.setVisible(self.but_log_f.isChecked()) cur = self.win_dict['cur_win_name'] cur_win_d = self.win_dict[cur] param_txt = "" if cur_win_d['n_par'] > 0: if type(cur_win_d['par'][0]['val']) in {str}: p1 = cur_win_d['par'][0]['val'] else: p1 = "{0:.3g}".format(cur_win_d['par'][0]['val']) param_txt = " ({0:s} = {1:s})".format( cur_win_d['par'][0]['name_tex'], p1) if self.win_dict[cur]['n_par'] > 1: if type(cur_win_d['par'][1]['val']) in {str}: p2 = cur_win_d['par'][1]['val'] else: p2 = "{0:.3g}".format(cur_win_d['par'][1]['val']) param_txt = param_txt[:-1]\ + ", {0:s} = {1:s})".format(cur_win_d['par'][1]['name_tex'], p2) self.mplwidget.fig.suptitle(r'{0} Window'.format(cur) + param_txt) # plot a line at the max. sidelobe level if self.tbl_sel[3]: self.ax_f.axhline(self.sidelobe_level_disp, ls='dotted', c='b') patch = mpl_patches.Rectangle((0, 0), 1, 1, fc="white", ec="white", lw=0, alpha=0) # Info legend for time domain window labels_t = [] labels_t.append("$N$ = {0:d}".format(self.N_view)) self.ax_t.legend([patch], labels_t, loc='best', fontsize='small', fancybox=True, framealpha=0.7, handlelength=0, handletextpad=0) # Info legend for frequency domain window labels_f = [] N_patches = 0 if self.tbl_sel[0]: labels_f.append("$NENBW$ = {0:.4g} {1}".format( self.nenbw_disp, self.nenbw_unit)) N_patches += 1 if self.tbl_sel[1]: labels_f.append("$CGAIN$ = {0:.4g} {1}".format( self.cgain_disp, self.cgain_unit)) N_patches += 1 if self.tbl_sel[2]: labels_f.append("1st Zero = {0:.4g}".format(self.first_zero_f)) N_patches += 1 if N_patches > 0: self.ax_f.legend([patch] * N_patches, labels_f, loc='best', fontsize='small', fancybox=True, framealpha=0.7, handlelength=0, handletextpad=0) np.seterr(**old_settings_seterr) self.update_info() self.redraw() # ------------------------------------------------------------------------------ def update_info(self): """ Update the text info box for the window """ cur = self.win_dict['cur_win_name'] if 'info' in self.win_dict[cur]: self.txtInfoBox.setText(self.win_dict[cur]['info']) else: self.txtInfoBox.clear() self._set_table_item(0, 0, "NENBW", font=self.bfont) # , sel=True) self._set_table_item(0, 1, "{0:.5g}".format(self.nenbw_disp)) self._set_table_item(0, 2, self.nenbw_unit) self._set_table_item(0, 3, "Scale", font=self.bfont) # , sel=True) self._set_table_item(0, 4, "{0:.5g}".format(self.cgain_disp)) self._set_table_item(0, 5, self.cgain_unit) self._set_table_item(1, 0, "1st Zero", font=self.bfont) # , sel=True) self._set_table_item(1, 1, "{0:.5g}".format(self.first_zero_f)) self._set_table_item(1, 2, "f_S") self._set_table_item(1, 3, "Sidelobes", font=self.bfont) # , sel=True) self._set_table_item(1, 4, "{0:.5g}".format(self.sidelobe_level_disp)) self._set_table_item(1, 5, self.cgain_unit) self.tbl_win_props.resizeColumnsToContents() self.tbl_win_props.resizeRowsToContents() # ----------------------------------------------------------------------------- def redraw(self): """ Redraw the canvas when e.g. the canvas size has changed """ self.mplwidget.redraw()
class EllipZeroPhz(QWidget): # Since we are also using poles/residues -> let's force zpk FRMT = 'zpk' info = """ **Elliptic filters with zero phase** (also known as Cauer filters) have the steepest rate of transition between the frequency response’s passband and stopband of all IIR filters. This comes at the expense of a constant ripple (equiripple) :math:`A_PB` and :math:`A_SB` in both pass and stop band. Ringing of the step response is increased in comparison to Chebychev filters. As the passband ripple :math:`A_PB` approaches 0, the elliptical filter becomes a Chebyshev type II filter. As the stopband ripple :math:`A_SB` approaches 0, it becomes a Chebyshev type I filter. As both approach 0, becomes a Butterworth filter (butter). For the filter design, the order :math:`N`, minimum stopband attenuation :math:`A_SB` and the critical frequency / frequencies :math:`F_PB` where the gain first drops below the maximum passband ripple :math:`-A_PB` have to be specified. The ``ellipord()`` helper routine calculates the minimum order :math:`N` and critical passband frequency :math:`F_C` from pass and stop band specifications. The Zero Phase Elliptic Filter squares an elliptic filter designed in a way to produce the required Amplitude specifications. So initially the amplitude specs design an elliptic filter with the square root of the amp specs. The filter is then squared to produce a zero phase filter. The filter coefficients are applied to the signal data in a backward and forward time fashion. This filter can only be applied to stored signal data (not real-time streaming data that comes in a forward time order). We are forcing the order N of the filter to be even. This simplifies the poles/zeros to be complex (no real values). **Design routines:** ``scipy.signal.ellip()``, ``scipy.signal.ellipord()`` """ sig_tx = pyqtSignal(object) def __init__(self): QWidget.__init__(self) self.ft = 'IIR' c = Common() self.rt_dict = c.rt_base_iir self.rt_dict_add = { 'COM': { 'man': { 'msg': ('a', "Enter the filter order <b><i>N</i></b>, the minimum stop " "band attenuation <b><i>A<sub>SB</sub></i></b> and frequency or " "frequencies <b><i>F<sub>C</sub></i></b> where gain first drops " "below the max passband ripple <b><i>-A<sub>PB</sub></i></b> ." ) } }, 'LP': { 'man': {}, 'min': {} }, 'HP': { 'man': {}, 'min': {} }, 'BS': { 'man': {}, 'min': {} }, 'BP': { 'man': {}, 'min': {} }, } self.info_doc = [] self.info_doc.append('ellip()\n========') self.info_doc.append(sig.ellip.__doc__) self.info_doc.append('ellipord()\n==========') self.info_doc.append(ellipord.__doc__) #-------------------------------------------------------------------------- def construct_UI(self): """ Create additional subwidget(s) needed for filter design: These subwidgets are instantiated dynamically when needed in select_filter.py using the handle to the filter instance, fb.fil_inst. """ # ============================================================================= # self.chkComplex = QCheckBox("ComplexFilter", self) # self.chkComplex.setToolTip("Designs BP or BS Filter for complex data.") # self.chkComplex.setObjectName('wdg_lbl_el') # self.chkComplex.setChecked(False) # # ============================================================================= self.butSave = QPushButton(self) self.butSave.setText("SAVE") self.butSave.setToolTip("Save filter in proprietary format") #-------------------------------------------------- # Layout for filter optional subwidgets self.layHWin = QHBoxLayout() self.layHWin.setObjectName('wdg_layGWin') #self.layHWin.addWidget(self.chkComplex) self.layHWin.addWidget(self.butSave) self.layHWin.addStretch() self.layHWin.setContentsMargins(0, 0, 0, 0) # Widget containing all subwidgets self.wdg_fil = QWidget(self) self.wdg_fil.setObjectName('wdg_fil') self.wdg_fil.setLayout(self.layHWin) self.butSave.clicked.connect(self.save_filter) def _get_params(self, fil_dict): """ Translate parameters from the passed dictionary to instance parameters, scaling / transforming them if needed. For zero phase filter, we take square root of amplitude specs since we later square filter. Define design around smallest amp spec """ # Frequencies are normalized to f_Nyq = f_S/2, ripple specs are in dB self.analog = False # set to True for analog filters self.manual = False # default is normal design self.N = int(fil_dict['N']) # force N to be even if (self.N % 2) == 1: self.N += 1 self.F_PB = fil_dict['F_PB'] * 2 self.F_SB = fil_dict['F_SB'] * 2 self.F_PB2 = fil_dict['F_PB2'] * 2 self.F_SB2 = fil_dict['F_SB2'] * 2 self.F_PBC = None # find smallest spec'd linear value and rewrite dictionary ampPB = fil_dict['A_PB'] ampSB = fil_dict['A_SB'] # take square roots of amp specs so resulting squared # filter will meet specifications if (ampPB < ampSB): ampSB = sqrt(ampPB) ampPB = sqrt(1 + ampPB) - 1 else: ampPB = sqrt(1 + ampSB) - 1 ampSB = sqrt(ampSB) self.A_PB = lin2unit(ampPB, 'IIR', 'A_PB', unit='dB') self.A_SB = lin2unit(ampSB, 'IIR', 'A_SB', unit='dB') #logger.warning("design with "+str(self.A_PB)+","+str(self.A_SB)) # ellip filter routines support only one amplitude spec for # pass- and stop band each if str(fil_dict['rt']) == 'BS': fil_dict['A_PB2'] = self.A_PB elif str(fil_dict['rt']) == 'BP': fil_dict['A_SB2'] = self.A_SB # partial fraction expansion to define residue vector def _partial(self, k, p, z, norder): # create diff array diff = p - z # now compute residual vector cone = complex(1., 0.) residues = zeros(norder, complex) for i in range(norder): residues[i] = k * (diff[i] / p[i]) for j in range(norder): if (j != i): residues[i] = residues[i] * (cone + diff[j] / (p[i] - p[j])) # now compute DC term for new expansion sumRes = 0. for i in range(norder): sumRes = sumRes + residues[i].real dc = k - sumRes return (dc, residues) # # Take a causal filter and square it. The result has the square # of the amplitude response of the input, and zero phase. Filter # is noncausal. # Input: # k - gain in pole/zero form # p - numpy array of poles # z - numpy array of zeros # g - gain in pole/residue form # r - numpy array of residues # nn- order of filter # Output: # kn - new gain (pole/zero) # pn - new poles # zn - new zeros (numpy array) # gn - new gain (pole/residue) # rn - new residues def _sqCausal(self, k, p, z, g, r, nn): # Anticausal poles have conjugate-reciprocal symmetry # Starting anticausal residues are conjugates (adjusted below) pA = conj(1. / p) # antiCausal poles zA = conj(z) # antiCausal zeros (store reciprocal) rA = conj(r) # antiCausal residues (to start) rC = zeros(nn, complex) # Adjust residues. Causal part first. for j in range(nn): # Evaluate the anticausal filter at each causal pole tmpx = rA / (1. - p[j] / pA) ztmp = g + sum(tmpx) # Adjust residue rC[j] = r[j] * ztmp # anticausal residues are just conjugates of causal residues # r3 = np.conj(r2) # Compute the constant term dc2 = (g + sum(r)) * g - sum(rC) # Populate output (2nn elements) gn = dc2.real # Drop complex poles/residues in LHP, keep only UHP pA = conj(p) #store AntiCasual pole (reciprocal) p0 = zeros(int(nn / 2), complex) r0 = zeros(int(nn / 2), complex) cnt = 0 for j in range(nn): if (p[j].imag > 0.0): p0[cnt] = p[j] r0[cnt] = rC[j] cnt = cnt + 1 # Let operator know we squared filter # logger.info('After squaring filter, order: '+str(nn*2)) # For now and our case, only store causal residues # Filters are symmetric and can generate antiCausal residues return (pA, zA, gn, p0, r0) def _test_N(self): """ Warn the user if the calculated order is too high for a reasonable filter design. """ if self.N > 30: return qfilter_warning(self, self.N, "Zero-phase Elliptic") else: return True # custom save of filter dictionary def _save(self, fil_dict, arg): """ First design initial elliptic filter meeting sqRoot Amp specs; - Then create residue vector from poles/zeros; - Then square filter (k,p,z and dc,p,r) to get zero phase filter; - Then Convert results of filter design to all available formats (pz, pr, ba, sos) and store them in the global filter dictionary. Corner frequencies and order calculated for minimum filter order are also stored to allow for an easy subsequent manual filter optimization. """ fil_save(fil_dict, arg, self.FRMT, __name__) # For min. filter order algorithms, update filter dict with calculated # new values for filter order N and corner frequency(s) F_PBC fil_dict['N'] = self.N if str(fil_dict['fo']) == 'min': if str(fil_dict['rt']) == 'LP' or str(fil_dict['rt']) == 'HP': # HP or LP - single corner frequency fil_dict['F_PB'] = self.F_PBC / 2. else: # BP or BS - two corner frequencies fil_dict['F_PB'] = self.F_PBC[0] / 2. fil_dict['F_PB2'] = self.F_PBC[1] / 2. # Now generate poles/residues for custom file save of new parameters if (not self.manual): z = fil_dict['zpk'][0] p = fil_dict['zpk'][1] k = fil_dict['zpk'][2] n = len(z) gain, residues = self._partial(k, p, z, n) pA, zA, gn, pC, rC = self._sqCausal(k, p, z, gain, residues, n) fil_dict['rpk'] = [rC, pC, gn] # save antiCausal b,a (nonReciprocal) also [easier to compute h(n) try: fil_dict['baA'] = sig.zpk2tf(zA, pA, k) except Exception as e: logger.error(e) # 'rpk' is our signal that this is a non-Causal filter with zero phase # inserted into fil dictionary after fil_save and convert # sig_tx -> select_filter -> filter_specs self.sig_tx.emit({'sender': __name__, 'filt_changed': 'ellip_zero'}) #------------------------------------------------------------------------------ def save_filter(self): file_filters = ("Text file pole/residue (*.txt_rpk)") dlg = QFD(self) # return selected file name (with or without extension) and filter (Linux: full text) file_name, file_type = dlg.getSaveFileName_(caption="Save filter as", directory=dirs.save_dir, filter=file_filters) file_name = str(file_name) # QString -> str() needed for Python 2.x # Qt5 has QFileDialog.mimeTypeFilters(), but under Qt4 the mime type cannot # be extracted reproducibly across file systems, so it is done manually: for t in extract_file_ext( file_filters): # get a list of file extensions if t in str(file_type): file_type = t # return the last matching extension if file_name != "": # cancelled file operation returns empty string # strip extension from returned file name (if any) + append file type: file_name = os.path.splitext(file_name)[0] + file_type file_type_err = False try: # save as a custom residue/pole text output for apply with custom tool # make sure we have the residues if 'rpk' in fb.fil[0]: with io.open(file_name, 'w', encoding="utf8") as f: self.file_dump(f) else: file_type_err = True logger.error( 'Filter has no residues/poles, cannot save as *.txt_rpk file' ) if not file_type_err: logger.info('Successfully saved filter as\n\t"{0}"'.format( file_name)) dirs.save_dir = os.path.dirname(file_name) # save new dir except IOError as e: logger.error('Failed saving "{0}"!\n{1}'.format(file_name, e)) #------------------------------------------------------------------------------ def file_dump(self, fOut): """ Dump file out in custom text format that apply tool can read to know filter coef's """ # Fixed format widths for integers and doubles intw = '10' dblW = 27 frcW = 20 # Fill up character string with filter output filtStr = '# IIR filter\n' # parameters that made filter (choose smallest eps) # Amp is stored in Volts (linear units) # the second amp terms aren't really used (for ellip filters) FA_PB = fb.fil[0]['A_PB'] FA_SB = fb.fil[0]['A_SB'] FAmp = min(FA_PB, FA_SB) # Freq terms in radians so move from -1:1 to -pi:pi f_lim = fb.fil[0]['freqSpecsRange'] f_unit = fb.fil[0]['freq_specs_unit'] F_S = fb.fil[0]['f_S'] if fb.fil[0]['freq_specs_unit'] == 'f_S': F_S = F_S * 2 F_SB = fb.fil[0]['F_SB'] * F_S * np.pi F_SB2 = fb.fil[0]['F_SB2'] * F_S * np.pi F_PB = fb.fil[0]['F_PB'] * F_S * np.pi F_PB2 = fb.fil[0]['F_PB2'] * F_S * np.pi # Determine pass/stop bands depending on filter response type passMin = [] passMax = [] stopMin = [] stopMax = [] if fb.fil[0]['rt'] == 'LP': passMin = [-F_PB, 0, 0] passMax = [F_PB, 0, 0] stopMin = [-np.pi, F_SB, 0] stopMax = [-F_SB, np.pi, 0] f1 = F_PB f2 = F_SB f3 = f4 = 0 Ftype = 1 Fname = 'Low_Pass' if fb.fil[0]['rt'] == 'HP': passMin = [-np.pi, F_PB, 0] passMax = [-F_PB, np.pi, 0] stopMin = [-F_SB, 0, 0] stopMax = [F_SB, 0, 0] f1 = F_SB f2 = F_PB f3 = f4 = 0 Ftype = 2 Fname = 'Hi_Pass' if fb.fil[0]['rt'] == 'BS': passMin = [-np.pi, -F_PB, F_PB2] passMax = [-F_PB2, F_PB, np.pi] stopMin = [-F_SB2, F_SB, 0] stopMax = [-F_SB, F_SB2, 0] f1 = F_PB f2 = F_SB f3 = F_SB2 f4 = F_PB2 Ftype = 4 Fname = 'Band_Stop' if fb.fil[0]['rt'] == 'BP': passMin = [-F_PB2, F_PB, 0] passMax = [-F_PB, F_PB2, 0] stopMin = [-np.pi, -F_SB, F_SB2] stopMax = [-F_SB2, F_SB, np.pi] f1 = F_SB f2 = F_PB f3 = F_PB2 f4 = F_SB2 Ftype = 3 Fname = 'Band_Pass' filtStr = filtStr + '{:{align}{width}}'.format( '10', align='>', width=intw) + ' IIRFILT_4SYM\n' filtStr = filtStr + '{:{align}{width}}'.format( str(Ftype), align='>', width=intw) + ' ' + Fname + '\n' filtStr = filtStr + '{:{d}.{p}f}'.format(FAmp, d=dblW, p=frcW) + '\n' filtStr = filtStr + '{: {d}.{p}f}'.format(passMin[0], d=dblW, p=frcW) filtStr = filtStr + '{: {d}.{p}f}'.format(passMax[0], d=dblW, p=frcW) + '\n' filtStr = filtStr + '{: {d}.{p}f}'.format(passMin[1], d=dblW, p=frcW) filtStr = filtStr + '{: {d}.{p}f}'.format(passMax[1], d=dblW, p=frcW) + '\n' filtStr = filtStr + '{: {d}.{p}f}'.format(passMin[2], d=dblW, p=frcW) filtStr = filtStr + '{: {d}.{p}f}'.format(passMax[2], d=dblW, p=frcW) + '\n' filtStr = filtStr + '{: {d}.{p}f}'.format(stopMin[0], d=dblW, p=frcW) filtStr = filtStr + '{: {d}.{p}f}'.format(stopMax[0], d=dblW, p=frcW) + '\n' filtStr = filtStr + '{: {d}.{p}f}'.format(stopMin[1], d=dblW, p=frcW) filtStr = filtStr + '{: {d}.{p}f}'.format(stopMax[1], d=dblW, p=frcW) + '\n' filtStr = filtStr + '{: {d}.{p}f}'.format(stopMin[2], d=dblW, p=frcW) filtStr = filtStr + '{: {d}.{p}f}'.format(stopMax[2], d=dblW, p=frcW) + '\n' filtStr = filtStr + '{: {d}.{p}f}'.format(f1, d=dblW, p=frcW) filtStr = filtStr + '{: {d}.{p}f}'.format(f2, d=dblW, p=frcW) + '\n' filtStr = filtStr + '{: {d}.{p}f}'.format(f3, d=dblW, p=frcW) filtStr = filtStr + '{: {d}.{p}f}'.format(f4, d=dblW, p=frcW) + '\n' # move pol/res/gain into terms we need Fdc = fb.fil[0]['rpk'][2] rC = fb.fil[0]['rpk'][0] pC = fb.fil[0]['rpk'][1] Fnum = len(pC) # Gain term filtStr = filtStr + '{: {d}.{p}e}'.format(Fdc, d=dblW, p=frcW) + '\n' # Real pole count inside the unit circle (none of these) filtStr = filtStr + '{:{align}{width}}'.format( str(0), align='>', width=intw) + '\n' # Complex pole/res count inside the unit circle filtStr = filtStr + '{:{i}d}'.format(Fnum, i=intw) + '\n' # Now dump poles/residues for j in range(Fnum): filtStr = filtStr + '{:{i}d}'.format(j, i=intw) + ' ' filtStr = filtStr + '{: {d}.{p}e}'.format( rC[j].real, d=dblW, p=frcW) + ' ' filtStr = filtStr + '{: {d}.{p}e}'.format( rC[j].imag, d=dblW, p=frcW) + ' ' filtStr = filtStr + '{: {d}.{p}e}'.format( pC[j].real, d=dblW, p=frcW) + ' ' filtStr = filtStr + '{: {d}.{p}e}'.format( pC[j].imag, d=dblW, p=frcW) + '\n' # Real pole count outside the unit circle (none of these) filtStr = filtStr + '{:{align}{width}}'.format( str(0), align='>', width=intw) + '\n' # Complex pole count outside the unit circle (none of these) filtStr = filtStr + '{:{align}{width}}'.format( str(0), align='>', width=intw) + '\n' # Now write huge text string to file fOut.write(filtStr) #------------------------------------------------------------------------------ # # DESIGN ROUTINES # #------------------------------------------------------------------------------ # LP: F_PB < F_stop ------------------------------------------------------- def LPmin(self, fil_dict): """Elliptic LP filter, minimum order""" self._get_params(fil_dict) self.N, self.F_PBC = ellipord(self.F_PB, self.F_SB, self.A_PB, self.A_SB, analog=self.analog) # force even N if (self.N % 2) == 1: self.N += 1 if not self._test_N(): return -1 #logger.warning("and "+str(self.F_PBC) + " " + str(self.N)) self._save( fil_dict, sig.ellip(self.N, self.A_PB, self.A_SB, self.F_PBC, btype='low', analog=self.analog, output=self.FRMT)) def LPman(self, fil_dict): """Elliptic LP filter, manual order""" self._get_params(fil_dict) if not self._test_N(): return -1 self._save( fil_dict, sig.ellip(self.N, self.A_PB, self.A_SB, self.F_PB, btype='low', analog=self.analog, output=self.FRMT)) # HP: F_stop < F_PB ------------------------------------------------------- def HPmin(self, fil_dict): """Elliptic HP filter, minimum order""" self._get_params(fil_dict) self.N, self.F_PBC = ellipord(self.F_PB, self.F_SB, self.A_PB, self.A_SB, analog=self.analog) # force even N if (self.N % 2) == 1: self.N += 1 if not self._test_N(): return -1 self._save( fil_dict, sig.ellip(self.N, self.A_PB, self.A_SB, self.F_PBC, btype='highpass', analog=self.analog, output=self.FRMT)) def HPman(self, fil_dict): """Elliptic HP filter, manual order""" self._get_params(fil_dict) if not self._test_N(): return -1 self._save( fil_dict, sig.ellip(self.N, self.A_PB, self.A_SB, self.F_PB, btype='highpass', analog=self.analog, output=self.FRMT)) # For BP and BS, F_XX have two elements each, A_XX has only one # BP: F_SB[0] < F_PB[0], F_SB[1] > F_PB[1] -------------------------------- def BPmin(self, fil_dict): """Elliptic BP filter, minimum order""" self._get_params(fil_dict) self.N, self.F_PBC = ellipord([self.F_PB, self.F_PB2], [self.F_SB, self.F_SB2], self.A_PB, self.A_SB, analog=self.analog) #logger.warning(" "+str(self.F_PBC) + " " + str(self.N)) if (self.N % 2) == 1: self.N += 1 if not self._test_N(): return -1 #logger.warning("-"+str(self.F_PBC) + " " + str(self.A_SB)) self._save( fil_dict, sig.ellip(self.N, self.A_PB, self.A_SB, self.F_PBC, btype='bandpass', analog=self.analog, output=self.FRMT)) def BPman(self, fil_dict): """Elliptic BP filter, manual order""" self._get_params(fil_dict) if not self._test_N(): return -1 self._save( fil_dict, sig.ellip(self.N, self.A_PB, self.A_SB, [self.F_PB, self.F_PB2], btype='bandpass', analog=self.analog, output=self.FRMT)) # BS: F_SB[0] > F_PB[0], F_SB[1] < F_PB[1] -------------------------------- def BSmin(self, fil_dict): """Elliptic BP filter, minimum order""" self._get_params(fil_dict) self.N, self.F_PBC = ellipord([self.F_PB, self.F_PB2], [self.F_SB, self.F_SB2], self.A_PB, self.A_SB, analog=self.analog) # force even N if (self.N % 2) == 1: self.N += 1 if not self._test_N(): return -1 self._save( fil_dict, sig.ellip(self.N, self.A_PB, self.A_SB, self.F_PBC, btype='bandstop', analog=self.analog, output=self.FRMT)) def BSman(self, fil_dict): """Elliptic BS filter, manual order""" self._get_params(fil_dict) if not self._test_N(): return -1 self._save( fil_dict, sig.ellip(self.N, self.A_PB, self.A_SB, [self.F_PB, self.F_PB2], btype='bandstop', analog=self.analog, output=self.FRMT))
class Input_Fixpoint_Specs(QWidget): """ Create the widget that holds the dynamically loaded fixpoint filter ui """ # emit a signal when the image has been resized sig_resize = pyqtSignal() # incoming from subwidgets -> process_sig_rx_local sig_rx_local = pyqtSignal(object) # incoming, connected to input_tab_widget.sig_rx sig_rx = pyqtSignal(object) # outcgoing sig_tx = pyqtSignal(object) def __init__(self, parent): super(Input_Fixpoint_Specs, self).__init__(parent) self.tab_label = 'Fixpoint' self.tool_tip = ( "<span>Select a fixpoint implementation for the filter," " simulate it or generate a Verilog netlist.</span>") self.parent = parent self.fx_path = os.path.realpath( os.path.join(dirs.INSTALL_DIR, 'fixpoint_widgets')) self.no_fx_filter_img = os.path.join(self.fx_path, "no_fx_filter.png") if not os.path.isfile(self.no_fx_filter_img): logger.error("Image {0:s} not found!".format( self.no_fx_filter_img)) self.default_fx_img = os.path.join(self.fx_path, "default_fx_img.png") if not os.path.isfile(self.default_fx_img): logger.error("Image {0:s} not found!".format(self.default_fx_img)) if HAS_MIGEN: self._construct_UI() else: self.state = "deactivated" # "invisible", "disabled" #------------------------------------------------------------------------------ def process_sig_rx(self, dict_sig=None): """ Process signals coming in via subwidgets and sig_rx Play PingPong with a stimulus & plot widget: 2. ``fx_sim_init()``: Request stimulus by sending 'fx_sim':'get_stimulus' 3. ``fx_sim_set_stimulus()``: Receive stimulus from widget in 'fx_sim':'send_stimulus' and pass it to HDL object for simulation 4. Send back HDL response to widget via 'fx_sim':'set_response' """ logger.debug("process_sig_rx(): vis={0}\n{1}"\ .format(self.isVisible(), pprint_log(dict_sig))) if dict_sig['sender'] == __name__: logger.debug("Stopped infinite loop\n{0}".format( pprint_log(dict_sig))) return elif 'data_changed' in dict_sig and dict_sig[ 'data_changed'] == "filter_designed": # New filter has been designed, update list of available filter topologies here self._update_filter_cmb() return elif 'data_changed' in dict_sig or\ ('view_changed' in dict_sig and dict_sig['view_changed'] == 'q_coeff'): # update fields in the filter topology widget - wordlength may have # been changed. Also set RUN button to "changed" in wdg_dict2ui() self.wdg_dict2ui() #self.sig_tx.emit({'sender':__name__, 'fx_sim':'specs_changed'}) elif 'fx_sim' in dict_sig: if dict_sig['fx_sim'] == 'init': if self.fx_wdg_found: self.fx_sim_init() else: logger.error("No fixpoint widget found!") qstyle_widget(self.butSimHDL, "error") self.sig_tx.emit({'sender': __name__, 'fx_sim': 'error'}) elif dict_sig['fx_sim'] == 'send_stimulus': self.fx_sim_set_stimulus(dict_sig) elif dict_sig['fx_sim'] == 'specs_changed': # fixpoint specification have been changed somewhere, update ui # and set run button to "changed" in wdg_dict2ui() self.wdg_dict2ui() elif dict_sig['fx_sim'] == 'finish': qstyle_widget(self.butSimHDL, "normal") logger.info('Fixpoint simulation [{0:5.3g} ms]: Plotting finished'\ .format((time.process_time() - self.t_resp)*1000)) else: logger.error('Unknown "fx_sim" command option "{0}"\n' '\treceived from "{1}".'.format( dict_sig['fx_sim'], dict_sig['sender'])) # ---- Process local widget signals elif 'ui' in dict_sig: if 'id' in dict_sig and dict_sig['id'] == 'w_input': """ Input fixpoint format has been changed or butLock has been clicked. When I/O lock is active, copy input fixpoint word format to output word format. """ if dict_sig[ 'ui'] == 'butLock' and not self.wdg_w_input.butLock.isChecked( ): # butLock was deactivitated, don't do anything return elif self.wdg_w_input.butLock.isChecked(): # but lock was activated or wordlength setting have been changed fb.fil[0]['fxqc']['QO']['WI'] = fb.fil[0]['fxqc']['QI'][ 'WI'] fb.fil[0]['fxqc']['QO']['WF'] = fb.fil[0]['fxqc']['QI'][ 'WF'] fb.fil[0]['fxqc']['QO']['W'] = fb.fil[0]['fxqc']['QI']['W'] elif 'id' in dict_sig and dict_sig['id'] == 'w_output': """ Output fixpoint format has been changed. When I/O lock is active, copy output fixpoint word format to input word format. """ if self.wdg_w_input.butLock.isChecked(): fb.fil[0]['fxqc']['QI']['WI'] = fb.fil[0]['fxqc']['QO'][ 'WI'] fb.fil[0]['fxqc']['QI']['WF'] = fb.fil[0]['fxqc']['QO'][ 'WF'] fb.fil[0]['fxqc']['QI']['W'] = fb.fil[0]['fxqc']['QO']['W'] elif 'id' in dict_sig and dict_sig['id'] in \ {'w_coeff', 'q_input', 'q_output', 'w_accu', 'q_accu'}: pass # nothing to do for now else: if not "id" in dict_sig: logger.warning("No id in dict_sig:\n{0}".format( pprint_log(dict_sig))) else: logger.warning('Unknown id "{0}" in dict_sig:\n{1}'\ .format(dict_sig['id'], pprint_log(dict_sig))) if not dict_sig['ui'] in { 'WI', 'WF', 'ovfl', 'quant', 'cmbW', 'butLock' }: logger.warning("Unknown value '{0}' for key 'ui'".format( dict_sig['ui'])) self.wdg_dict2ui( ) # update wordlengths in UI and set RUN button to 'changed' self.sig_tx.emit({'sender': __name__, 'fx_sim': 'specs_changed'}) return #------------------------------------------------------------------------------ def _construct_UI(self): """ Intitialize the main GUI, consisting of: - A combo box to select the filter topology and an image of the topology - The input quantizer - The UI of the fixpoint filter widget - Simulation and export buttons """ #------------------------------------------------------------------------------ # Define frame and layout for the dynamically updated filter widget # The actual filter widget is instantiated in self.set_fixp_widget() later on self.layH_fx_wdg = QHBoxLayout() #self.layH_fx_wdg.setContentsMargins(*params['wdg_margins']) frmHDL_wdg = QFrame(self) frmHDL_wdg.setLayout(self.layH_fx_wdg) #frmHDL_wdg.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) #------------------------------------------------------------------------------ # Initialize fixpoint filter combobox, title and description #------------------------------------------------------------------------------ self.cmb_wdg_fixp = QComboBox(self) self.cmb_wdg_fixp.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.lblTitle = QLabel("not set", self) self.lblTitle.setWordWrap(True) self.lblTitle.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) layHTitle = QHBoxLayout() layHTitle.addWidget(self.cmb_wdg_fixp) layHTitle.addWidget(self.lblTitle) self.frmTitle = QFrame(self) self.frmTitle.setLayout(layHTitle) self.frmTitle.setContentsMargins(*params['wdg_margins']) #------------------------------------------------------------------------------ # Input and Output Quantizer #------------------------------------------------------------------------------ # - instantiate widgets for input and output quantizer # - pass the quantization (sub-?) dictionary to the constructor #------------------------------------------------------------------------------ self.wdg_w_input = UI_W(self, q_dict=fb.fil[0]['fxqc']['QI'], id='w_input', label='', lock_visible=True) self.wdg_w_input.sig_tx.connect(self.process_sig_rx) cmb_q = ['round', 'floor', 'fix'] self.wdg_w_output = UI_W(self, q_dict=fb.fil[0]['fxqc']['QO'], id='w_output', label='') self.wdg_w_output.sig_tx.connect(self.process_sig_rx) self.wdg_q_output = UI_Q( self, q_dict=fb.fil[0]['fxqc']['QO'], id='q_output', label='Output Format <i>Q<sub>Y </sub></i>:', cmb_q=cmb_q, cmb_ov=['wrap', 'sat']) self.wdg_q_output.sig_tx.connect(self.sig_rx) if HAS_DS: cmb_q.append('dsm') self.wdg_q_input = UI_Q( self, q_dict=fb.fil[0]['fxqc']['QI'], id='q_input', label='Input Format <i>Q<sub>X </sub></i>:', cmb_q=cmb_q) self.wdg_q_input.sig_tx.connect(self.sig_rx) # Layout and frame for input quantization layVQiWdg = QVBoxLayout() layVQiWdg.addWidget(self.wdg_q_input) layVQiWdg.addWidget(self.wdg_w_input) frmQiWdg = QFrame(self) #frmBtns.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken) frmQiWdg.setLayout(layVQiWdg) frmQiWdg.setContentsMargins(*params['wdg_margins']) # Layout and frame for output quantization layVQoWdg = QVBoxLayout() layVQoWdg.addWidget(self.wdg_q_output) layVQoWdg.addWidget(self.wdg_w_output) frmQoWdg = QFrame(self) #frmBtns.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken) frmQoWdg.setLayout(layVQoWdg) frmQoWdg.setContentsMargins(*params['wdg_margins']) #------------------------------------------------------------------------------ # Dynamically updated image of filter topology #------------------------------------------------------------------------------ # label is a placeholder for image self.lbl_fixp_img = QLabel("img not set", self) #self.lbl_fixp_img.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.embed_fixp_img(self.no_fx_filter_img) layHImg = QHBoxLayout() layHImg.setContentsMargins(0, 0, 0, 0) layHImg.addWidget(self.lbl_fixp_img) #, Qt.AlignCenter) self.frmImg = QFrame(self) self.frmImg.setLayout(layHImg) self.frmImg.setContentsMargins(*params['wdg_margins']) self.resize_img() #------------------------------------------------------------------------------ # Simulation and export Buttons #------------------------------------------------------------------------------ self.butExportHDL = QPushButton(self) self.butExportHDL.setToolTip( "Export fixpoint filter in Verilog format.") self.butExportHDL.setText("Create HDL") self.butSimHDL = QPushButton(self) self.butSimHDL.setToolTip("Start migen fixpoint simulation.") self.butSimHDL.setText("Sim. HDL") self.butSimFxPy = QPushButton(self) self.butSimFxPy.setToolTip("Simulate filter with fixpoint effects.") self.butSimFxPy.setText("Sim. FixPy") self.layHHdlBtns = QHBoxLayout() self.layHHdlBtns.addWidget(self.butSimFxPy) self.layHHdlBtns.addWidget(self.butSimHDL) self.layHHdlBtns.addWidget(self.butExportHDL) # This frame encompasses the HDL buttons sim and convert frmHdlBtns = QFrame(self) #frmBtns.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken) frmHdlBtns.setLayout(self.layHHdlBtns) frmHdlBtns.setContentsMargins(*params['wdg_margins']) # ------------------------------------------------------------------- # Top level layout # ------------------------------------------------------------------- splitter = QSplitter(self) splitter.setOrientation(Qt.Vertical) splitter.addWidget(frmHDL_wdg) splitter.addWidget(frmQoWdg) splitter.addWidget(self.frmImg) # setSizes uses absolute pixel values, but can be "misused" by specifying values # that are way too large: in this case, the space is distributed according # to the _ratio_ of the values: splitter.setSizes([3000, 3000, 5000]) layVMain = QVBoxLayout() layVMain.addWidget(self.frmTitle) layVMain.addWidget(frmHdlBtns) layVMain.addWidget(frmQiWdg) layVMain.addWidget(splitter) layVMain.addStretch() layVMain.setContentsMargins(*params['wdg_margins']) self.setLayout(layVMain) #---------------------------------------------------------------------- # GLOBAL SIGNALS & SLOTs #---------------------------------------------------------------------- self.sig_rx.connect(self.process_sig_rx) #---------------------------------------------------------------------- # LOCAL SIGNALS & SLOTs & EVENTFILTERS #---------------------------------------------------------------------- # monitor events and generate sig_resize event when resized self.lbl_fixp_img.installEventFilter(self) # ... then redraw image when resized self.sig_resize.connect(self.resize_img) self.cmb_wdg_fixp.currentIndexChanged.connect(self._update_fixp_widget) self.butExportHDL.clicked.connect(self.exportHDL) self.butSimHDL.clicked.connect(self.fx_sim_init) #---------------------------------------------------------------------- inst_wdg_list = self._update_filter_cmb() if len(inst_wdg_list) == 0: logger.warning("No fixpoint filters found!") else: logger.debug("Imported {0:d} fixpoint filters:\n{1}".format( len(inst_wdg_list.split("\n")) - 1, inst_wdg_list)) self._update_fixp_widget() #------------------------------------------------------------------------------ def _update_filter_cmb(self): """ (Re-)Read list of available fixpoint filters for a given filter design every time a new filter design is selected. Then try to import the fixpoint designs in the list and populate the fixpoint implementation combo box `self.cmb_wdg_fixp` when successfull. """ inst_wdg_str = "" # full names of successfully instantiated widgets for logging last_fx_wdg = qget_cmb_box( self.cmb_wdg_fixp, data=False) # remember last fx widget setting self.cmb_wdg_fixp.clear() fc = fb.fil[0]['fc'] if 'fix' in fb.filter_classes[fc]: for class_name in fb.filter_classes[fc]['fix']: # get class name try: # construct module + class name mod_class_name = fb.fixpoint_classes[class_name][ 'mod'] + '.' + class_name disp_name = fb.fixpoint_classes[class_name][ 'name'] # # and display name self.cmb_wdg_fixp.addItem(disp_name, mod_class_name) inst_wdg_str += '\t' + class_name + ' : ' + mod_class_name + '\n' except AttributeError as e: logger.warning('Widget "{0}":\n{1}'.format(class_name, e)) self.embed_fixp_img(self.no_fx_filter_img) continue except KeyError as e: logger.warning( "No fixpoint filter for filter type {0} available.". format(e)) self.embed_fixp_img(self.no_fx_filter_img) continue # restore last fxp widget if possible idx = self.cmb_wdg_fixp.findText(last_fx_wdg) # set to idx 0 if not found (returned -1) self.cmb_wdg_fixp.setCurrentIndex(max(idx, 0)) else: # no fixpoint widget self.embed_fixp_img(self.no_fx_filter_img) return inst_wdg_str #------------------------------------------------------------------------------ def eventFilter(self, source, event): """ Filter all events generated by monitored QLabel, only resize events are processed here, generating a `sig_resize` signal. All other events are passed on to the next hierarchy level. """ if event.type() == QEvent.Resize: self.sig_resize.emit() # Call base class method to continue normal event processing: return super(Input_Fixpoint_Specs, self).eventFilter(source, event) #------------------------------------------------------------------------------ def embed_fixp_img(self, img_file): """ Embed image as self.img_fixp, either in png or svg format Parameters: img_file: str path and file name to image file """ if not os.path.isfile(img_file): logger.warning("Image file {0} doesn't exist.".format(img_file)) img_file = self.default_fx_img # _, file_extension = os.path.splitext(self.fx_wdg_inst.img_name) _, file_extension = os.path.splitext(img_file) if file_extension == '.png': self.img_fixp = QPixmap(img_file) #self.lbl_fixp_img.setPixmap(QPixmap(self.img_fixp)) # fixed size # elif file_extension == '.svg': # self.img_fixp = QtSvg.QSvgWidget(img_file) else: logger.error( 'Unknown file extension "{0}"!'.format(file_extension)) self.resize_img() #------------------------------------------------------------------------------ def resize_img(self): """ Triggered when self (the widget) is resized, consequently the image inside QLabel is resized to completely fill the label while keeping the aspect ratio. This doesn't really work at the moment. """ if hasattr(self.parent, "width"): # needed for module test par_w, par_h = self.parent.width(), self.parent.height() else: par_w, par_h = 300, 700 # fixed size for module testself.lbl_img_fixp lbl_w, lbl_h = self.lbl_fixp_img.width(), self.lbl_fixp_img.height() img_w, img_h = self.img_fixp.width(), self.img_fixp.height() if img_w > 10: max_h = int(max(np.floor(img_h * par_w / img_w) - 15, 20)) else: max_h = 200 logger.debug("img size: {0},{1}, frm size: {2},{3}, max_h: {4}".format( img_w, img_h, par_w, par_h, max_h)) # The following doesn't work because the width of the parent widget can grow # with the image size # img_scaled = self.img_fixp.scaled(self.lbl_fixp_img.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation) img_scaled = self.img_fixp.scaledToHeight(max_h, Qt.SmoothTransformation) #img_scaled = self.img_fixp.scaledToHeight(max_h) self.lbl_fixp_img.setPixmap(img_scaled) #------------------------------------------------------------------------------ def _update_fixp_widget(self): """ This method is called at the initialization of the widget and when a new fixpoint filter implementation is selected from the combo box: - Destruct old instance of fixpoint filter widget `self.fx_wdg_inst` - Import and instantiate new fixpoint filter widget e.g. after changing the filter topology as - Try to load image for filter topology - Update the UI of the widget - Try to instantiate HDL filter as `self.fx_wdg_inst.fixp_filter` with dummy data """ def _disable_fx_wdg(self): if hasattr( self, "fx_wdg_inst" ) and self.fx_wdg_inst is not None: # is a fixpoint widget loaded? try: self.layH_fx_wdg.removeWidget( self.fx_wdg_inst) # remove widget from layout self.fx_wdg_inst.deleteLater( ) # delete QWidget when scope has been left except AttributeError as e: logger.error("Destructing UI failed!\n{0}".format(e)) self.fx_wdg_found = False self.butSimFxPy.setVisible(False) self.butSimHDL.setEnabled(False) self.butExportHDL.setEnabled(False) #self.layH_fx_wdg.setVisible(False) self.img_fixp = self.embed_fixp_img(self.no_fx_filter_img) self.lblTitle.setText("") self.fx_wdg_inst = None # destruct old fixpoint widget instance _disable_fx_wdg(self) # instantiate new fixpoint widget class as self.fx_wdg_inst cmb_wdg_fx_cur = qget_cmb_box(self.cmb_wdg_fixp, data=False) if cmb_wdg_fx_cur: # at least one valid fixpoint widget found self.fx_wdg_found = True # get list [module name and path, class name] fx_mod_class_name = qget_cmb_box(self.cmb_wdg_fixp, data=True).rsplit('.', 1) fx_mod = importlib.import_module( fx_mod_class_name[0]) # get module fx_wdg_class = getattr(fx_mod, fx_mod_class_name[1]) # get class #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ self.fx_wdg_inst = fx_wdg_class( self) # instantiate the fixpoint widget #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ self.layH_fx_wdg.addWidget(self.fx_wdg_inst, stretch=1) # and add it to layout self.fx_wdg_inst.setVisible(True) # Doesn't work at the moment, combo box becomes inaccessible # try: # self.fx_wdg_inst = fx_wdg_class(self) # instantiate the widget # self.layH_fx_wdg.addWidget(self.fx_wdg_inst, stretch=1) # and add it to layout # except KeyError as e: # logger.warning('Key Error {0} in fixpoint filter \n{1}'\ # .format(e, fx_mod_name + "." + cmb_wdg_fx_cur)) # _disable_fx_wdg(self) # return self.wdg_dict2ui( ) # initialize the fixpoint subwidgets from the fxqc_dict #---- connect signals to fx_wdg_inst ---- if hasattr(self.fx_wdg_inst, "sig_rx"): self.sig_rx.connect(self.fx_wdg_inst.sig_rx) if hasattr(self.fx_wdg_inst, "sig_tx"): self.fx_wdg_inst.sig_tx.connect(self.sig_rx) #---- get name of new fixpoint filter image ---- if not (hasattr(self.fx_wdg_inst, "img_name") and self.fx_wdg_inst.img_name): # is an image name defined? img_file = self.default_fx_img else: file_path = os.path.dirname( fx_mod.__file__ ) # get path of imported fixpoint widget and img_file = os.path.join(file_path, self.fx_wdg_inst.img_name ) # construct full image name from it #---- instantiate and scale graphic of filter topology ---- self.embed_fixp_img(img_file) #---- set title and description for filter self.lblTitle.setText(self.fx_wdg_inst.title) #--- try to reference Python fixpoint filter instance ----- # if hasattr(self.fx_wdg_inst,'fxpy_filter'): # self.fxpy_filter_inst = self.fx_wdg_inst.fxpy_filter # self.butSimFxPy.setEnabled(True) # else: # self.butSimFxPy.setVisible(False) #--- Check whether fixpoint widget contains HDL filters ----- if hasattr(self.fx_wdg_inst, 'fixp_filter'): self.butExportHDL.setEnabled( hasattr(self.fx_wdg_inst, "to_verilog")) self.butSimHDL.setEnabled(hasattr(self.fx_wdg_inst, "run_sim")) self.update_fxqc_dict() self.sig_tx.emit({ 'sender': __name__, 'fx_sim': 'specs_changed' }) else: self.butSimHDL.setEnabled(False) self.butExportHDL.setEnabled(False) else: _disable_fx_wdg(self) #------------------------------------------------------------------------------ def wdg_dict2ui(self): """ Trigger an update of the fixpoint widget UI when view (i.e. fixpoint coefficient format) or data have been changed outside this class. Additionally, pass the fixpoint quantization widget to update / restore other subwidget settings. Set the RUN button to "changed". """ # fb.fil[0]['fxqc']['QCB'].update({'scale':(1 << fb.fil[0]['fxqc']['QCB']['W'])}) self.wdg_q_input.dict2ui(fb.fil[0]['fxqc']['QI']) self.wdg_q_output.dict2ui(fb.fil[0]['fxqc']['QO']) self.wdg_w_input.dict2ui(fb.fil[0]['fxqc']['QI']) self.wdg_w_output.dict2ui(fb.fil[0]['fxqc']['QO']) if self.fx_wdg_found and hasattr(self.fx_wdg_inst, "dict2ui"): self.fx_wdg_inst.dict2ui() # dict_sig = {'sender':__name__, 'fx_sim':'specs_changed'} # self.sig_tx.emit(dict_sig) qstyle_widget(self.butSimHDL, "changed") #------------------------------------------------------------------------------ def update_fxqc_dict(self): """ Update the fxqc dictionary before simulation / HDL generation starts. """ if self.fx_wdg_found: # get a dict with the coefficients and fixpoint settings from fixpoint widget if hasattr(self.fx_wdg_inst, "ui2dict"): fb.fil[0]['fxqc'].update(self.fx_wdg_inst.ui2dict()) logger.debug("update fxqc: \n{0}".format( pprint_log(fb.fil[0]['fxqc']))) else: logger.error("No fixpoint widget found!") #------------------------------------------------------------------------------ def exportHDL(self): """ Synthesize HDL description of filter """ if not hasattr(self.fx_wdg_inst, 'construct_fixp_filter'): logger.warning( 'Fixpoint widget has no method "construct_fixp_filter", aborting.' ) return dlg = QFD(self) # instantiate file dialog object file_types = "Verilog (*.v)" dlg.setDefaultSuffix( 'v' ) # needed for overwrite confirmation when name is entered without suffix dlg.setWindowTitle('Export Vlog') dlg.setNameFilter(file_types) dlg.setDirectory(dirs.save_dir) dlg.setAcceptMode( QFD.AcceptSave) # set mode "save file" instead "open file" dlg.setOption(QFD.DontConfirmOverwrite, False) if dlg.exec_() == QFD.Accepted: hdl_file = qstr(dlg.selectedFiles()[0]) # hdl_type = extract_file_ext(qstr(dlg.selectedNameFilter()))[0] # ============================================================================= # # static method getSaveFileName_() is simple but unflexible # hdl_file, hdl_filter = dlg.getSaveFileName_( # caption="Save Verilog netlist as (this also defines the module name)", # directory=dirs.save_dir, filter=file_types) # hdl_file = qstr(hdl_file) # if hdl_file != "": # "operation cancelled" returns an empty string # # return '.v' or '.vhd' depending on filetype selection: # # hdl_type = extract_file_ext(qstr(hdl_filter))[0] # # sanitized dir + filename + suffix. The filename suffix is replaced # # by `v` later. # hdl_file = os.path.normpath(hdl_file) # complete path + file name # ============================================================================= hdl_dir_name = os.path.dirname( hdl_file) # extract the directory path if not os.path.isdir( hdl_dir_name): # create directory if it doesn't exist os.mkdir(hdl_dir_name) dirs.save_dir = hdl_dir_name # make this directory the new default / base dir hdl_file_name = os.path.splitext(os.path.basename(hdl_file))[0] hdl_full_name = os.path.join(hdl_dir_name, hdl_file_name + ".v") vlog_mod_name = re.sub( r'\W+', '', hdl_file_name).lower() # remove all non-alphanumeric chars logger.info( 'Creating hdl_file "{0}"\n\twith top level module "{1}"'. format(hdl_full_name, vlog_mod_name)) try: self.update_fxqc_dict() self.fx_wdg_inst.construct_fixp_filter() code = self.fx_wdg_inst.to_verilog(name=vlog_mod_name) #logger.info(str(code)) # print verilog code to console with io.open(hdl_full_name, 'w', encoding="utf8") as f: f.write(str(code)) logger.info("HDL conversion finished!") except (IOError, TypeError) as e: logger.warning(e) ##------------------------------------------------------------------------------ # def fx_sim_py(self): # """ # Start fix-point simulation: Send the ``fxqc_dict`` # containing all quantization information and request a stimulus signal # Not implemented yet # """ # try: # logger.info("Started python fixpoint simulation") # self.update_fxqc_dict() # self.fxpyfilter.setup(fb.fil[0]['fxqc']) # setup filter instance # dict_sig = {'sender':__name__, 'fx_sim':'get_stimulus'} # self.sig_tx.emit(dict_sig) # # except AttributeError as e: # logger.warning("Fixpoint stimulus generation failed:\n{0}".format(e)) # return #------------------------------------------------------------------------------ def fx_sim_init(self): """ Initialize fix-point simulation: - Update the `fxqc_dict` containing all quantization information - Setup a filter instance for migen simulation - Request a stimulus signal """ if not hasattr(self.fx_wdg_inst, 'construct_fixp_filter'): logger.error( 'Fixpoint widget has no method "construct_fixp_filter", aborting.' ) self.sig_tx.emit({'sender': __name__, 'fx_sim': 'error'}) return try: logger.info("Fixpoint simulation started") self.t_start = time.process_time() self.update_fxqc_dict() self.fx_wdg_inst.construct_fixp_filter() # setup filter instance dict_sig = {'sender': __name__, 'fx_sim': 'get_stimulus'} self.sig_tx.emit(dict_sig) except ValueError as e: # exception logger.error( 'Fixpoint stimulus generation failed during "init" for dict\n{0}' '\nwith "{1} "'.format(pprint_log(dict_sig), e)) return #------------------------------------------------------------------------------ def fx_sim_set_stimulus(self, dict_sig): """ - Get fixpoint stimulus from `dict_sig` in integer format - Pass it to the fixpoint filter and calculate the fixpoint response - Send the reponse to the plotting widget """ try: logger.debug( 'Starting fixpoint simulation with stimulus from "{0}":\n\tfx_stimulus:{1}' '\n\tStimuli: Shape {2} of type "{3}"'.format( dict_sig['sender'], pprint_log(dict_sig['fx_stimulus'], tab=" "), np.shape(dict_sig['fx_stimulus']), dict_sig['fx_stimulus'].dtype, )) self.t_stim = time.process_time() logger.info("Fixpoint simulation [{0:5.3g} ms]: Stimuli generated"\ .format((self.t_stim-self.t_start)*1000)) # Run fixpoint simulation and return the results as integer values: self.fx_results = self.fx_wdg_inst.run_sim( dict_sig['fx_stimulus']) # Run the simulation self.t_resp = time.process_time() if len(self.fx_results) == 0: logger.warning("Fixpoint simulation returned empty results!") else: #logger.debug("fx_results: {0}"\ # .format(pprint_log(self.fx_results, tab= " "))) logger.debug('Fixpoint simulation successful for dict\n{0}' '\tStimuli: Shape {1} of type "{2}"' '\n\tResponse: Shape {3} of type "{4}"'\ .format(pprint_log(dict_sig), np.shape(dict_sig['fx_stimulus']), dict_sig['fx_stimulus'].dtype, np.shape(self.fx_results), type(self.fx_results) )) logger.info('Fixpoint simulation [{0:5.3g} ms]: Response calculated'\ .format((self.t_resp - self.t_stim)*1000)) #TODO: fixed point / integer to float conversion? #TODO: color push-button to show state of simulation #TODO: add QTimer single shot # self.timer_id = QtCore.QTimer() # self.timer_id.setSingleShot(True) # # kill simulation after some idle time, also add a button for this # self.timer_id.timeout.connect(self.kill_sim) except ValueError as e: logger.error("Simulator error {0}".format(e)) self.fx_results = None qstyle_widget(self.butSimHDL, "error") self.sig_tx.emit({'sender': __name__, 'fx_sim': 'error'}) return except AssertionError as e: logger.error('Fixpoint simulation failed for dict\n{0}' '\twith msg. "{1}"\n\tStimuli: Shape {2} of type "{3}"' '\n\tResponse: Shape {4} of type "{5}"'\ .format(pprint_log(dict_sig), e, np.shape(dict_sig['fx_stimulus']), dict_sig['fx_stimulus'].dtype, np.shape(self.fx_results), type(self.fx_results) )) self.fx_results = None qstyle_widget(self.butSimHDL, "error") self.sig_tx.emit({'sender': __name__, 'fx_sim': 'error'}) return logger.debug("Sending fixpoint results") dict_sig = { 'sender': __name__, 'fx_sim': 'set_results', 'fx_results': self.fx_results } self.sig_tx.emit(dict_sig) qstyle_widget(self.butSimHDL, "normal") return