def createWindow(widget): ''' Example on creating a new plot window in the main window MDI-Area ''' import plotWidget from PySide import QtGui from numpy import linspace from scipy.special import jn from chaco.api import ArrayPlotData, Plot window = widget.createNewWindow() container = plotWidget.plotContainer(window) plotWidget = plotWidget.PlotWidget(container) container.setPlotWidget(plotWidget) x = linspace(-2.0, 10.0, 100) pd = ArrayPlotData(index=x) for i in range(5): pd.set_data("y" + str(i), jn(i, x)) plot = Plot(pd, title=None, padding_left=60, padding_right=5, padding_top=5, padding_bottom=30, border_visible=True) plot.plot(("index", "y0", "y1", "y2"), name="j_n, n<3", color="red") plotWidget.setPlot(plot) layout = QtGui.QBoxLayout(QtGui.QBoxLayout.TopToBottom) layout.addWidget(container) window.setLayout(layout) window.show()
def plotFFT(widget): """ Determine fft of signals in selected widget """ print("plotFFT") model = "" for (modelNumber, modelName, variableName), data in widget.getData(): # print "var:", var, "data: ", data minVal = (0, float("inf")) maxVal = (0, float("-inf")) # for time, value in data: # Get data from data array: time = numpy.array(list((x for x, _ in data))) # data[0] values = numpy.array(list((x for _, x in data))) (Tmin, Tmax, N) = getFFTtimeRange(time) (timeInRange, valuesInRange) = getValuesInRange(time, values, Tmin, Tmax) # Compute fft: A=A(f) (f, A) = fft(timeInRange, valuesInRange, N) # Open new plot tab: import plotWidget window = widget.createNewWindow() container = plotWidget.plotContainer(window) plotWidget = plotWidget.PlotWidget(container) container.setPlotWidget(plotWidget) # Create the plot plotdata = ArrayPlotData(x=f, y=A, border_visible=True, overlay_border=True) plot = Plot(plotdata, title="FFT") # Plot(plotdata, title="FFT") barPlot = plot.plot(("x", "y"), type="bar", bar_width=0.1, color="blue")[0] # scatterPlot = plot.plot(("x", "y"), type="scatter", color="blue")[0] # Attach some tools to the plot plot.tools.append(PanTool(plot)) plot.overlays.append(ZoomTool(plot)) # Activate Plot: plotWidget.setPlot(plot) container.setPlotWidget(plotWidget) layout = QtGui.QBoxLayout(QtGui.QBoxLayout.TopToBottom) layout.addWidget(container) window.setLayout(layout) window.show()
def plotFFT(widget): """ Determine fft of signals in selected widget """ print("plotFFT") model = "" for (modelNumber, modelName, variableName), data in widget.getData(): # print "var:", var, "data: ", data minVal = (0, float("inf")) maxVal = (0, float("-inf")) # for time, value in data: # Get data from data array: time = numpy.array(list((x for x, _ in data))) # data[0] values = numpy.array(list((x for _ , x in data))) (Tmin, Tmax, N) = getFFTtimeRange(time) (timeInRange, valuesInRange) = getValuesInRange(time, values, Tmin, Tmax) # Compute fft: A=A(f) import Algorithms (f, A) = Algorithms.fft(timeInRange, valuesInRange, N) # Open new plot tab: import plotWidget window = widget.createNewWindow() container = plotWidget.plotContainer(window) plotWidget = plotWidget.PlotWidget(container) container.setPlotWidget(plotWidget) # Create the plot plotdata = ArrayPlotData(x=f, y=A, border_visible=True, overlay_border=True) plot = Plot(plotdata, title="FFT") # Plot(plotdata, title="FFT") barPlot = plot.plot(("x", "y"), type="bar", bar_width=0.1, color="blue")[0] # scatterPlot = plot.plot(("x", "y"), type="scatter", color="blue")[0] # Attach some tools to the plot plot.tools.append(PanTool(plot)) plot.overlays.append(ZoomTool(plot)) # Activate Plot: plotWidget.setPlot(plot) container.setPlotWidget(plotWidget) layout = QtGui.QBoxLayout(QtGui.QBoxLayout.TopToBottom) layout.addWidget(container) window.setLayout(layout) window.show()
def plotFFTPlusTHD(widget): """ Determine fft of signals in selected widget and calculate Total harmonic disturbance""" print("plotFFT and Total Harmonic Disturbance") model = "" for (modelNumber, modelName, variableName), data in widget.getData(): # print "var:", var, "data: ", data minVal = (0, float("inf")) maxVal = (0, float("-inf")) # for time, value in data: # Get data from data array: time = numpy.array(list((x for x, _ in data))) # data[0] values = numpy.array(list((x for _ , x in data))) unit = "" (Tmin, Tmax, N) = getFFTtimeRange(time) (timeInRange, valuesInRange) = getValuesInRange(time, values, Tmin, Tmax) # Compute fft: A=A(f) (f, A) = fft(timeInRange, valuesInRange, N) #******* START THD CALCULATION ************* # Estimate fundamental frequency: maxindex = A.argmax() estimation = f[maxindex] def getExFreq(estimation): return estimation """ Inquire im measured fundamental frequency is correct: """ ''' import guidata guidata.qapplication() import guidata.dataset.dataitems as di import guidata.dataset.datatypes as dt class Processing(dt.DataSet): """ Fundamental Frequency """ correctedFreq = di.FloatItem("fundamental frequency [Hz]", default=estimation) param = Processing() okPressed = param.edit() if okPressed: return param.correctedFreq else: # Cancel button pressed return estimation ''' # Ask for a better fundamental frequency exFreq = max(0, min(f.max(), getExFreq(estimation))) # Check if we have at least one harmonic: if exFreq > 0.5 * f.max(): print "THD calculation not possible, extend frequency window to at least 2*fundamental frequency" THD = 999 else: # Get 5% window around fundamental frequency and calculate power: mask = (f > exFreq * 0.975) & (f < exFreq * 1.025) print "Calculating fundamental energy from points: frequency=%s, Amplitude=%s" % (f[mask], A[mask]) P1 = numpy.vdot(A[mask], A[mask]) # squared amplitude PH = 0 # Sum up the Power of all harmonic frequencies in spectrum: noHarmonics = numpy.int(numpy.floor(f.max() / exFreq)) for i in range(noHarmonics - 1): mask = (f > (i + 2) * exFreq * 0.975) & (f < (i + 2) * exFreq * 1.025) PH = PH + (numpy.vdot(A[mask], A[mask])) # squared amplitude THD = PH / P1 * 100 #******* END THD CALCULATION ************* # Open new plot tab: import plotWidget window = widget.createNewWindow() container = plotWidget.plotContainer(window) plotWidget = plotWidget.PlotWidget(container) container.setPlotWidget(plotWidget) # Plot data plotdata = ArrayPlotData(x=f, y=A, border_visible=True, overlay_border=True) plot = Plot(plotdata, title="FFT") # Plot(plotdata, title="FFT") barPlot = plot.plot(("x", "y"), type="bar", bar_width=0.3, color="blue")[0] # Attach some tools to the plot plot.tools.append(PanTool(plot)) plot.overlays.append(ZoomTool(plot)) # Activate Plot: plotWidget.setPlot(plot) if THD != 999: thdLabel = DataLabel(component=plotWidget.plot, data_point=(f[A.argmax()], A.max()), label_position="bottom right", padding_bottom=20, marker_color="transparent", marker_size=8, marker="circle", arrow_visible=False, label_format=str('THD = %.4g percent based on %d harmonics of the %.4g Hz frequency' % (THD, noHarmonics, exFreq))) plotWidget.plot.overlays.append(thdLabel) container.setPlotWidget(plotWidget) layout = QtGui.QBoxLayout(QtGui.QBoxLayout.TopToBottom) layout.addWidget(container) window.setLayout(layout) window.show()
def plotFFTPlusTHD(widget): """ Determine fft of signals in selected widget and calculate Total harmonic disturbance""" print("plotFFT and Total Harmonic Disturbance") model = "" for (modelNumber, modelName, variableName), data in widget.getData(): # print "var:", var, "data: ", data minVal = (0, float("inf")) maxVal = (0, float("-inf")) # for time, value in data: # Get data from data array: time = numpy.array(list((x for x, _ in data))) # data[0] values = numpy.array(list((x for _, x in data))) unit = "" (Tmin, Tmax, N) = getFFTtimeRange(time) (timeInRange, valuesInRange) = getValuesInRange(time, values, Tmin, Tmax) # Compute fft: A=A(f) (f, A) = fft(timeInRange, valuesInRange, N) #******* START THD CALCULATION ************* # Estimate fundamental frequency: maxindex = A.argmax() estimation = f[maxindex] def getExFreq(estimation): return estimation """ Inquire im measured fundamental frequency is correct: """ ''' import guidata guidata.qapplication() import guidata.dataset.dataitems as di import guidata.dataset.datatypes as dt class Processing(dt.DataSet): """ Fundamental Frequency """ correctedFreq = di.FloatItem("fundamental frequency [Hz]", default=estimation) param = Processing() okPressed = param.edit() if okPressed: return param.correctedFreq else: # Cancel button pressed return estimation ''' # Ask for a better fundamental frequency exFreq = max(0, min(f.max(), getExFreq(estimation))) # Check if we have at least one harmonic: if exFreq > 0.5 * f.max(): print "THD calculation not possible, extend frequency window to at least 2*fundamental frequency" THD = 999 else: # Get 5% window around fundamental frequency and calculate power: mask = (f > exFreq * 0.975) & (f < exFreq * 1.025) print "Calculating fundamental energy from points: frequency=%s, Amplitude=%s" % ( f[mask], A[mask]) P1 = numpy.vdot(A[mask], A[mask]) # squared amplitude PH = 0 # Sum up the Power of all harmonic frequencies in spectrum: noHarmonics = numpy.int(numpy.floor(f.max() / exFreq)) for i in range(noHarmonics - 1): mask = (f > (i + 2) * exFreq * 0.975) & (f < (i + 2) * exFreq * 1.025) PH = PH + (numpy.vdot(A[mask], A[mask])) # squared amplitude THD = PH / P1 * 100 #******* END THD CALCULATION ************* # Open new plot tab: import plotWidget window = widget.createNewWindow() container = plotWidget.plotContainer(window) plotWidget = plotWidget.PlotWidget(container) container.setPlotWidget(plotWidget) # Plot data plotdata = ArrayPlotData(x=f, y=A, border_visible=True, overlay_border=True) plot = Plot(plotdata, title="FFT") # Plot(plotdata, title="FFT") barPlot = plot.plot(("x", "y"), type="bar", bar_width=0.3, color="blue")[0] # Attach some tools to the plot plot.tools.append(PanTool(plot)) plot.overlays.append(ZoomTool(plot)) # Activate Plot: plotWidget.setPlot(plot) if THD != 999: thdLabel = DataLabel( component=plotWidget.plot, data_point=(f[A.argmax()], A.max()), label_position="bottom right", padding_bottom=20, marker_color="transparent", marker_size=8, marker="circle", arrow_visible=False, label_format=str( 'THD = %.4g percent based on %d harmonics of the %.4g Hz frequency' % (THD, noHarmonics, exFreq))) plotWidget.plot.overlays.append(thdLabel) container.setPlotWidget(plotWidget) layout = QtGui.QBoxLayout(QtGui.QBoxLayout.TopToBottom) layout.addWidget(container) window.setLayout(layout) window.show()