def extractSignalsForOperation(values, storedSignals): selectedGraphs = values['selectedGraphs'] if len(selectedGraphs) != 2: sg.Popup('Error!', 'Select 2 graphs to add') return (None, None) return storedSignals[getSelectedGraphIndex( selectedGraphs[0])], storedSignals[getSelectedGraphIndex( selectedGraphs[1])]
def onShowGraph(window, values, storedSignals): if (len(values['selectedGraphs']) == 0): sg.Popup('Error!', "Select at least one signal") return selectedGraphs = values["selectedGraphs"] data = [] layout = plotly_layout for x in selectedGraphs: graph = storedSignals[getSelectedGraphIndex(x)] isCoercedToContinuous = None try: isCoercedToContinuous = graph['displayContinuous'] except KeyError: isCoercedToContinuous = False data.append( go.Scatter( x=graph['x'], y=graph['y'], mode="markers" if graph['isDiscrete'] and not isCoercedToContinuous else 'lines', )) figure = go.Figure(data=data, layout=layout) py.plot(figure, filename='graph')
def onComputeErrorParameters(window, values, storedSignals): selectedGraphs = values['selectedGraphs'] if len(selectedGraphs) != 1: sg.Popup('Error!', 'Select 1 graph for error parameters computation!') return None selectedSignal = storedSignals[getSelectedGraphIndex(selectedGraphs[0])] name, x, y, params = pluck(selectedSignal, 'name', 'x', 'y', 'params') actualValuesForSignal = computeSignal(selectedSignal, x) #test newSignal = { **selectedSignal, 'displayName': f"computed: {selectedSignal['displayName']}", 'y': actualValuesForSignal } addToSelectionList(window, newSignal, storedSignals) #test errors = calculateErrorStatistics(selectedSignal['y'], actualValuesForSignal) MSE, SNR, PSNR, MD, ENOB = pluck(errors, 'MSE', 'SNR', 'PSNR', 'MD', 'ENOB') sg.Popup( 'Signal properties', f""" MSE: {MSE} SNR: {SNR} PSNR: {PSNR} MD: {MD} ENOB: {ENOB} """)
def onGenerateSpectrum(window, selectedSignals, storedSignals): if (len(selectedSignals) != 1): sg.Popup('Error!', "Select single signal") else: selectedSignal = storedSignals[getSelectedGraphIndex( selectedSignals[0])] spectrum = ops.calculateSpectrum(selectedSignal) addToSelectionList(window, spectrum, storedSignals)
def onReconstructSignal(window, values, storedSignals): selectedGraphs = values['selectedGraphs'] if len(selectedGraphs) != 1: sg.Popup('Error!', 'Select 1 graph to reconstruct!') return None reconstructed = reconstruct(storedSignals[getSelectedGraphIndex( selectedGraphs[0])], params=values) addToSelectionList(window, reconstructed, storedSignals)
def saveFile(window, values, storedSignals): selected = values["selectedGraphs"] fileName = values['saveFile'] saveToBin = values['saveToBin'] if len(selected) != 1: sg.Popup("Error!", "You have to select 1 signal to save.") return signalToSave = storedSignals[getSelectedGraphIndex(selected[0])] fileOps.saveFile(signalToSave, fileName, saveToBin)
def removeFromSelectionList(window, selectedToRemove, storedSignals): selectionList = window.FindElement('selectedGraphs') alreadyRemoved = 0 for x in selectedToRemove: number = getSelectedGraphIndex(x) storedSignals.pop(number - alreadyRemoved) alreadyRemoved += 1 currentList = [] for i in range(len(storedSignals)): currentList.append( f"{i}. {storedSignals[i]['displayName']} {storedSignals[i]['params']}" ) selectionList.Update(currentList)
def onSignalProperties(window, selectedSignals, storedSignals): if (len(selectedSignals) != 1): sg.Popup('Error!', "Select single signal") else: selectedSignal = storedSignals[getSelectedGraphIndex( selectedSignals[0])] data = calculateStatistics(selectedSignal) sg.Popup( 'Signal properties', f""" Average value: {data['average']} Average absolute value: {data['averageAbsolute']} Average power: {data['averagePower']} Standard deviation: {data['standardDeviation']} Effective value: {data['effectiveValue']} """)
def onQuantizeSignal(window, values, storedSignals): selectedGraphs = values['selectedGraphs'] if len(selectedGraphs) != 1: sg.Popup('Error!', 'Select 1 graph to quantize!') return None steps = try_int(values['quantizationSteps']) if steps == None: sg.Popup('Error!', 'Specify amount of quantization levels!') return None selectedGraph = storedSignals[getSelectedGraphIndex(selectedGraphs[0])] quantized = quantize(selectedGraph, steps) addToSelectionList(window, quantized, storedSignals)
def onSampleSignal(window, values, storedSignals): selectedGraphs = values['selectedGraphs'] if len(selectedGraphs) != 1: sg.Popup('Error!', 'Select 1 graph to sample!') return None fp = try_float(values['samplingFrequency']) if fp == None or fp == 0: sg.Popup('Error!', 'Specify correct sampling frequency!') return None selectedGraph = storedSignals[getSelectedGraphIndex(selectedGraphs[0])] sampled = sample(selectedGraph, fp) addToSelectionList(window, sampled, storedSignals)
def onShowHistogram(window, values, storedSignals): if (len(values['selectedGraphs']) != 1): sg.Popup('Error!', "Select only one signal") return selectedGraphs = values["selectedGraphs"] ranges = int(values['ranges']) data = [] for x in selectedGraphs: graph = storedSignals[getSelectedGraphIndex(x)] data.append( go.Histogram( x=graph['y'], xbins={ 'size': (max(graph['y']) - min(graph['y'])) / (ranges - 1) }, )) py.plot(data, filename='graph')
def onFilter(window, values, storedSignals): errors, params = assembleFilterParams(values) if errors != "": sg.Popup('Invalid parameters', errors) return None selectedSignal = values['selectedGraphs'] if len(selectedSignal) != 1: sg.Popup('Error!', 'Select 1 graph to filter!') return None selectedSignal = storedSignals[getSelectedGraphIndex(selectedSignal[0])] if not values["useK?"]: params["K"] = selectedSignal["params"]["fp"] / params["K"] filter, filteredSignal = filterSignal(selectedSignal, **params) filteredSignal = { 'name': selectedSignal["name"], 'displayName': f"filtered {selectedSignal['displayName']}", 'isDiscrete': False, 'isComplex': False, 'isPeriodic': False, 'x': selectedSignal["x"], 'y': filteredSignal, "params": selectedSignal['params'] } filterTransmittance = operations.calculateFilterTransmittance( selectedSignal, filteredSignal) impulseResponse = { 'name': selectedSignal["name"], 'displayName': f"filter", 'isDiscrete': False, 'isComplex': False, 'isPeriodic': False, 'x': np.linspace(0, len(filter) - 1, len(filter)), 'y': filter, "params": selectedSignal['params'] } addToSelectionList(window, filteredSignal, storedSignals) addToSelectionList(window, impulseResponse, storedSignals) addToSelectionList(window, filterTransmittance, storedSignals)