示例#1
0
    def draw(self, context):
        """Redraw the drawing area.

        Keywords:
            context surface used for drawing actions.
        """
        context.set_source_rgb(0, 0, 0)
        context.rectangle(-1, -1, 2, 2)
        context.fill()

        fill = self.fill
        data = self.data

        group = len(data) / 256
        data = [sum(seq) / len(seq) for seq in batch(data, group)]

        # color stuff.
        context.set_source_rgb(.8, .8, .8)

        # actual rendering.
        width = 2 / len(data)
        context.set_line_width(width)

        x = -1 + width / 2
        context.move_to(x, 0)
        for value in data:
            y = -value
            context.line_to(x, y)
            x += width
        context.line_to(1 - width / 2, 0)
        if fill:
            context.fill()
        else:
            context.stroke()
示例#2
0
    def draw(self, context):
        """Redraw the drawing area.

        Keywords:
            context surface used for drawing actions.
        """
        context.set_source_rgb(0, 0, 0)
        context.rectangle(-1, -1, 2, 2)
        context.fill()

        threshold = self.threshold
        bands = self.bands
        data = self.data

        # compute the fft and trasform it in decibel notation:
        # - add 1e-15 in order to prevent log10(0).
        # - we divide by 1 because it is supposed to be the highest peak.
        data = 20 * log10(abs(fft(data) + 1e-15) / 1)

        # how many values we have to merge in order to achieve the desired
        # number of bands
        group = len(data) / bands
        data = [sum(seq) / len(seq) for seq in batch(data, group)]

        # is it possible to obtain more values that needed; we simply ignore
        # them (they refer to high value of frequencies.
        data[:] = data[:bands]

        ## data = 20 * log10(abs(fft(data[:512]) + 1e-15) / 1)
        ## temp = []
        ## data = [max(data[2 ** i:2 ** (i + 1)]) for i in xrange(8)]

        # color stuff.
        context.set_source_rgb(.8, .8, .8)

        # actual rendering.
        width = 2 / len(data)
        context.set_line_width(width)

        x = -1 + width / 2
        for value in data:
            context.move_to(x, 1)
            if value < threshold:
                value = threshold
            y = 1 - (value - threshold) / (-threshold) * 2
            context.line_to(x, y)

            context.stroke()
            x += width