Esempio n. 1
0
class HistPlot(QtWidgets.QWidget):

    def __init__(self, parent):
        super(HistPlot, self).__init__()

        self.verticalScaleDivision = ScaleDivision(-140, 0, 100)
        self.verticalScaleTransform = CoordinateTransform(-140, 0, 100, 0, 0)

        self.verticalScale = VerticalScaleWidget(self, self.verticalScaleDivision, self.verticalScaleTransform)
        self.verticalScale.setTitle("PSD (dB A)")

        self.horizontalScaleDivision = ScaleDivision(44, 22000, 100)
        self.horizontalScaleTransform = CoordinateTransform(44, 22000, 100, 0, 0)

        self.horizontalScale = HorizontalScaleWidget(self, self.horizontalScaleDivision, self.horizontalScaleTransform)
        self.horizontalScale.setTitle("Frequency (Hz)")

        self.canvasWidget = CanvasWidget(self, self.verticalScaleTransform, self.horizontalScaleTransform)
        self.canvasWidget.setTrackerFormatter(lambda x, y: "%d Hz, %.1f dB" % (x, y))

        plot_layout = QtWidgets.QGridLayout()
        plot_layout.setSpacing(0)
        plot_layout.setContentsMargins(0, 0, 0, 0)
        plot_layout.addWidget(self.verticalScale, 0, 0)
        plot_layout.addWidget(self.canvasWidget, 0, 1)
        plot_layout.addWidget(self.horizontalScale, 1, 1)

        self.setLayout(plot_layout)

        self.needfullreplot = False

        self.horizontalScaleTransform.setScale(fscales.Logarithmic)
        self.horizontalScaleDivision.setScale(fscales.Logarithmic)

        # insert an additional plot item for the peak bar
        self.bar_peak = HistogramPeakBarItem()
        self.canvasWidget.attach(self.bar_peak)
        self.peak = zeros((1,))
        self.peak_int = 0
        self.peak_decay = PEAK_DECAY_RATE

        self.histogram = HistogramItem()
        self.histogram.set_color(Qt.Qt.darkGreen)
        self.canvasWidget.attach(self.histogram)

        # need to replot here for the size Hints to be computed correctly (depending on axis scales...)
        self.update()

    def setdata(self, fl, fh, fc, y):
        self.histogram.setData(fl, fh, fc, y)

        self.compute_peaks(y)
        self.bar_peak.setData(fl, fh, self.peak, self.peak_int, y)

        # only draw on demand
        # self.draw()

    def draw(self):
        if self.needfullreplot:
            self.needfullreplot = False

            self.verticalScaleDivision.setLength(self.canvasWidget.height())
            self.verticalScaleTransform.setLength(self.canvasWidget.height())

            start_border, end_border = self.verticalScale.spacingBorders()
            self.verticalScaleTransform.setBorders(start_border, end_border)

            self.verticalScale.update()

            self.horizontalScaleDivision.setLength(self.canvasWidget.width())
            self.horizontalScaleTransform.setLength(self.canvasWidget.width())

            start_border, end_border = self.horizontalScale.spacingBorders()
            self.horizontalScaleTransform.setBorders(start_border, end_border)

            self.horizontalScale.update()

            x_major_tick = self.horizontalScaleDivision.majorTicks()
            x_minor_tick = self.horizontalScaleDivision.minorTicks()
            y_major_tick = self.verticalScaleDivision.majorTicks()
            y_minor_tick = self.verticalScaleDivision.minorTicks()
            self.canvasWidget.setGrid(array(x_major_tick),
                                      array(x_minor_tick),
                                      array(y_major_tick),
                                      array(y_minor_tick))

        self.canvasWidget.update()

    # redraw when the widget is resized to update coordinates transformations
    def resizeEvent(self, event):
        self.needfullreplot = True
        self.draw()

    def compute_peaks(self, y):
        if len(self.peak) != len(y):
            y_ones = ones(y.shape)
            self.peak = y_ones * (-500.)
            self.peak_int = zeros(y.shape)
            self.peak_decay = y_ones * 20. * log10(PEAK_DECAY_RATE) * 5000

        mask1 = (self.peak < y)
        mask2 = (~mask1)
        mask2_a = mask2 * (self.peak_int < 0.2)
        mask2_b = mask2 * (self.peak_int >= 0.2)

        self.peak[mask1] = y[mask1]
        self.peak[mask2_a] = self.peak[mask2_a] + self.peak_decay[mask2_a]

        self.peak_decay[mask1] = 20. * log10(PEAK_DECAY_RATE) * 5000
        self.peak_decay[mask2_a] += 20. * log10(PEAK_DECAY_RATE) * 5000

        self.peak_int[mask1] = 1.
        self.peak_int[mask2_b] *= 0.975

    def setspecrange(self, spec_min, spec_max):
        self.verticalScaleTransform.setRange(spec_min, spec_max)
        self.verticalScaleDivision.setRange(spec_min, spec_max)

        # notify that sizeHint has changed (this should be done with a signal emitted from the scale division to the scale bar)
        self.verticalScale.scaleBar.updateGeometry()

        self.needfullreplot = True
        self.update()

    def setweighting(self, weighting):
        if weighting is 0:
            title = "PSD (dB)"
        elif weighting is 1:
            title = "PSD (dB A)"
        elif weighting is 2:
            title = "PSD (dB B)"
        else:
            title = "PSD (dB C)"

        self.verticalScale.setTitle(title)
Esempio n. 2
0
class ImagePlot(QtWidgets.QWidget):
    def __init__(self, parent):
        super(ImagePlot, self).__init__(parent)

        self.verticalScaleDivision = ScaleDivision(20, 20000, 100)
        self.verticalScaleTransform = CoordinateTransform(20, 20000, 100, 0, 0)

        self.verticalScale = VerticalScaleWidget(self,
                                                 self.verticalScaleDivision,
                                                 self.verticalScaleTransform)
        self.verticalScale.setTitle("Frequency (Hz)")
        self.verticalScale.scaleBar.setTickFormatter(tickFormatter)

        self.horizontalScaleDivision = ScaleDivision(0, 10, 100)
        self.horizontalScaleTransform = CoordinateTransform(0, 10, 100, 0, 0)

        self.horizontalScale = HorizontalScaleWidget(
            self, self.horizontalScaleDivision, self.horizontalScaleTransform)
        self.horizontalScale.setTitle("Time (s)")

        self.colorScaleDivision = ScaleDivision(-140, 0, 100)
        self.colorScaleTransform = CoordinateTransform(-140, 0, 100, 0, 0)

        self.colorScale = ColorScaleWidget(self, self.colorScaleDivision,
                                           self.colorScaleTransform)
        self.colorScale.setTitle("PSD (dB A)")

        self.canvasWidget = CanvasWidget(self, self.verticalScaleTransform,
                                         self.horizontalScaleTransform)
        self.canvasWidget.setTrackerFormatter(lambda x, y: "%.2f s, %d Hz" %
                                              (x, y))

        plotLayout = QtWidgets.QGridLayout()
        plotLayout.setSpacing(0)
        plotLayout.setContentsMargins(0, 0, 0, 0)
        plotLayout.addWidget(self.verticalScale, 0, 0)
        plotLayout.addWidget(self.canvasWidget, 0, 1)
        plotLayout.addWidget(self.colorScale, 0, 2)
        plotLayout.addWidget(self.horizontalScale, 1, 1)

        self.setLayout(plotLayout)

        self.needfullreplot = False

        # attach a plot image
        self.plotImage = PlotImage()
        self.canvasWidget.attach(self.plotImage)

        self.setfreqscale(fscales.Linear)

        self.setspecrange(-140., 0.)

        # need to replot here for the size Hints to be computed correctly (depending on axis scales...)
        self.update()

    def addData(self, freq, xyzs, last_data_time):
        self.plotImage.addData(freq, xyzs, self.freqscale, last_data_time)

    def draw(self):
        if self.needfullreplot:
            self.needfullreplot = False

            self.verticalScaleDivision.setLength(self.canvasWidget.height())
            self.verticalScaleTransform.setLength(self.canvasWidget.height())
            startBorder, endBorder = self.verticalScale.spacingBorders()
            self.verticalScaleTransform.setBorders(startBorder, endBorder)

            self.verticalScale.update()

            self.horizontalScaleDivision.setLength(self.canvasWidget.width())
            self.horizontalScaleTransform.setLength(self.canvasWidget.width())
            startBorder, endBorder = self.horizontalScale.spacingBorders()
            self.horizontalScaleTransform.setBorders(startBorder, endBorder)

            self.horizontalScale.update()

            self.colorScaleDivision.setLength(self.canvasWidget.height())
            self.colorScaleTransform.setLength(self.canvasWidget.height())
            startBorder, endBorder = self.colorScale.spacingBorders()
            self.colorScaleTransform.setBorders(startBorder, endBorder)

            self.colorScale.update()

        self.canvasWidget.update()

    # redraw when the widget is resized to update coordinates transformations
    def resizeEvent(self, event):
        self.needfullreplot = True
        self.draw()

    def pause(self):
        self.plotImage.pause()

    def restart(self):
        self.plotImage.restart()

    def setfreqscale(self, scale):
        self.freqscale = scale

        self.plotImage.erase()
        self.plotImage.setfreqscale(scale)

        self.verticalScaleTransform.setScale(scale)
        self.verticalScaleDivision.setScale(scale)

        # notify that sizeHint has changed (this should be done with a signal emitted from the scale division to the scale bar)
        self.verticalScale.scaleBar.updateGeometry()

        self.needfullreplot = True
        self.update()

    def settimerange(self, timerange_seconds, dT_seconds):
        self.plotImage.settimerange(timerange_seconds, dT_seconds)

        self.horizontalScaleTransform.setRange(0, timerange_seconds)
        self.horizontalScaleDivision.setRange(0, timerange_seconds)

        # notify that sizeHint has changed (this should be done with a signal emitted from the scale division to the scale bar)
        self.horizontalScale.scaleBar.updateGeometry()

        self.needfullreplot = True
        self.update()

    def set_sfft_rate(self, rate_frac):
        self.plotImage.set_sfft_rate(rate_frac)

    def setfreqrange(self, minfreq, maxfreq):
        self.plotImage.setfreqrange(minfreq, maxfreq)

        self.verticalScaleTransform.setRange(minfreq, maxfreq)
        self.verticalScaleDivision.setRange(minfreq, maxfreq)

        # notify that sizeHint has changed (this should be done with a signal emitted from the scale division to the scale bar)
        self.verticalScale.scaleBar.updateGeometry()

        self.needfullreplot = True
        self.update()

    def setspecrange(self, spec_min, spec_max):
        self.colorScaleTransform.setRange(spec_min, spec_max)
        self.colorScaleDivision.setRange(spec_min, spec_max)

        # notify that sizeHint has changed (this should be done with a signal emitted from the scale division to the scale bar)
        self.colorScale.scaleBar.updateGeometry()

        self.needfullreplot = True
        self.update()

    def setweighting(self, weighting):
        if weighting is 0:
            title = "PSD (dB)"
        elif weighting is 1:
            title = "PSD (dB A)"
        elif weighting is 2:
            title = "PSD (dB B)"
        else:
            title = "PSD (dB C)"

        self.colorScale.setTitle(title)
Esempio n. 3
0
class SpectrumPlotWidget(QtWidgets.QWidget):
    def __init__(self, parent):
        super(SpectrumPlotWidget, self).__init__(parent)

        self.x1 = array([0.1, 0.5, 1.])
        self.x2 = array([0.5, 1., 2.])

        self.needtransform = False

        self.paused = False

        self.peaks_enabled = True
        self.peak = zeros((3, ))
        self.peak_int = zeros((3, ))
        self.peak_decay = ones((3, )) * PEAK_DECAY_RATE

        self.verticalScaleDivision = ScaleDivision(0, 1, 100)
        self.verticalScaleTransform = CoordinateTransform(0, 1, 100, 0, 0)

        self.verticalScale = VerticalScaleWidget(self,
                                                 self.verticalScaleDivision,
                                                 self.verticalScaleTransform)
        self.verticalScale.setTitle("PSD (dB)")

        self.horizontalScaleDivision = ScaleDivision(0, 22000, 100)
        self.horizontalScaleTransform = CoordinateTransform(
            0, 22000, 100, 0, 0)

        self.horizontalScale = HorizontalScaleWidget(
            self, self.horizontalScaleDivision, self.horizontalScaleTransform)
        self.horizontalScale.setTitle("Frequency (Hz)")

        self.canvasWidget = GlCanvasWidget(self, self.verticalScaleTransform,
                                           self.horizontalScaleTransform)
        self.canvasWidget.setTrackerFormatter(lambda x, y: "%d Hz, %.1f dB" %
                                              (x, y))
        self.canvasWidget.resized.connect(self.canvasResized)

        def r_peak(p):
            return 1. + 0. * p

        def g_peak(p):
            return 1. - p

        def b_peak(p):
            return 1. - p

        self.peakQuadsItem = QuadsItem(r_peak, g_peak, b_peak)
        self.canvasWidget.attach(self.peakQuadsItem)

        def r_signal(p):
            return 0. * p

        def g_signal(p):
            return 0.3 + 0.5 * p

        def b_signal(p):
            return 0. * p

        self.quadsItem = QuadsItem(r_signal, g_signal, b_signal)
        self.canvasWidget.attach(self.quadsItem)

        plotLayout = QtWidgets.QGridLayout()
        plotLayout.setSpacing(0)
        plotLayout.setContentsMargins(0, 0, 0, 0)
        plotLayout.addWidget(self.verticalScale, 0, 0)
        plotLayout.addWidget(self.canvasWidget, 0, 1)
        plotLayout.addWidget(self.horizontalScale, 1, 1)

        self.setLayout(plotLayout)

    def setfreqscale(self, scale):
        self.horizontalScaleTransform.setScale(scale)
        self.horizontalScaleDivision.setScale(scale)

        self.needtransform = True
        self.draw()

    def setfreqrange(self, minfreq, maxfreq):
        self.xmin = minfreq
        self.xmax = maxfreq

        self.horizontalScaleTransform.setRange(minfreq, maxfreq)
        self.horizontalScaleDivision.setRange(minfreq, maxfreq)

        # notify that sizeHint has changed (this should be done with a signal emitted from the scale division to the scale bar)
        self.horizontalScale.scaleBar.updateGeometry()

        self.needtransform = True
        self.draw()

    def setspecrange(self, spec_min, spec_max):
        if spec_min > spec_max:
            spec_min, spec_max = spec_max, spec_min

        self.verticalScaleTransform.setRange(spec_min, spec_max)
        self.verticalScaleDivision.setRange(spec_min, spec_max)

        # notify that sizeHint has changed (this should be done with a signal emitted from the scale division to the scale bar)
        self.verticalScale.scaleBar.updateGeometry()

        self.needtransform = True
        self.draw()

    def setweighting(self, weighting):
        if weighting is 0:
            title = "PSD (dB)"
        elif weighting is 1:
            title = "PSD (dB A)"
        elif weighting is 2:
            title = "PSD (dB B)"
        else:
            title = "PSD (dB C)"

        self.verticalScale.setTitle(title)
        self.needtransform = True
        self.draw()

    def setShowFreqLabel(self, showFreqLabel):
        self.canvasWidget.setShowFreqLabel(showFreqLabel)

    def set_peaks_enabled(self, enabled):
        self.peaks_enabled = enabled

        self.canvasWidget.detachAll()
        if enabled:
            self.canvasWidget.attach(self.peakQuadsItem)
        self.canvasWidget.attach(self.quadsItem)

    def set_baseline_displayUnits(self, baseline):
        self.quadsItem.set_baseline_displayUnits(baseline)

    def set_baseline_dataUnits(self, baseline):
        self.quadsItem.set_baseline_dataUnits(baseline)

    def setdata(self, x, y, fmax):
        x1 = zeros(x.shape)
        x2 = zeros(x.shape)
        x1[0] = 1e-10
        x1[1:] = (x[1:] + x[:-1]) / 2.
        x2[:-1] = x1[1:]
        x2[-1] = float(SAMPLING_RATE / 2)

        if len(x1) != len(self.x1):
            self.needtransform = True
            self.x1 = x1
            self.x2 = x2

        if not self.paused:
            self.canvasWidget.setfmax(fmax)

            M = np.max(y)
            m = self.verticalScaleTransform.coord_min
            y_int = (y - m) / (np.abs(M - m) + 1e-3)

            self.quadsItem.setData(x1, x2, y, y_int)

            if self.peaks_enabled:
                self.compute_peaks(y)
                self.peakQuadsItem.setData(x1, x2, self.peak, self.peak_int)

    def draw(self):
        if self.needtransform:
            self.verticalScaleDivision.setLength(self.canvasWidget.height())
            self.verticalScaleTransform.setLength(self.canvasWidget.height())
            startBorder, endBorder = self.verticalScale.spacingBorders()
            self.verticalScaleTransform.setBorders(startBorder, endBorder)

            self.verticalScale.update()

            self.horizontalScaleDivision.setLength(self.canvasWidget.width())
            self.horizontalScaleTransform.setLength(self.canvasWidget.width())
            startBorder, endBorder = self.horizontalScale.spacingBorders()
            self.horizontalScaleTransform.setBorders(startBorder, endBorder)

            self.horizontalScale.update()

            self.canvasWidget.setGrid(
                array(self.horizontalScaleDivision.majorTicks()),
                array(self.horizontalScaleDivision.minorTicks()),
                array(self.verticalScaleDivision.majorTicks()),
                array(self.verticalScaleDivision.minorTicks()))

            self.quadsItem.transformUpdate()
            self.peakQuadsItem.transformUpdate()

            self.needtransform = False

    def pause(self):
        self.paused = True
        self.canvasWidget.pause()

    def restart(self):
        self.paused = False
        self.canvasWidget.restart()

    # redraw when the widget is resized to update coordinates transformations
    # QOpenGlWidget does not like that we override resizeEvent
    def canvasResized(self, canvasWidth, canvasHeight):
        self.needtransform = True
        self.draw()

    def canvasUpdate(self):
        self.canvasWidget.update()

    def compute_peaks(self, y):
        if len(self.peak) != len(y):
            y_ones = ones(y.shape)
            self.peak = y_ones * (-500.)
            self.peak_int = zeros(y.shape)
            self.peak_decay = y_ones * 20. * log10(PEAK_DECAY_RATE) * 5000

        mask1 = (self.peak < y)
        mask2 = (~mask1)
        mask2_a = mask2 * (self.peak_int < 0.2)
        mask2_b = mask2 * (self.peak_int >= 0.2)

        self.peak[mask1] = y[mask1]
        self.peak[mask2_a] = self.peak[mask2_a] + self.peak_decay[mask2_a]

        self.peak_decay[mask1] = 20. * log10(PEAK_DECAY_RATE) * 5000
        self.peak_decay[mask2_a] += 20. * log10(PEAK_DECAY_RATE) * 5000

        self.peak_int[mask1] = 1.
        self.peak_int[mask2_b] *= 0.975