コード例 #1
0
ファイル: analyse.py プロジェクト: dboyliao/pyaudio_wrapper
    def plot_spectrum(self, colors = None):
        """
        params:
            `colors`: any interable of symbols of color `supported` by matplotlib
        """

        if colors is None:
            colors = _cycle(["#FF6600", "#0033CC"])
        else:
            colors = _cycle(colors)

        N = len(self.audio_data.data) if self.audio_data.CHANNELS == 1 else len(self.audio_data.data[0])

        if self.__cached_fft is None:
            self.fft()

        coefs = self.__cached_fft

        fig, axs = _plt.subplots(len(coefs) + 1, 1, figsize = (20, 7))
        zoom_ax = axs[-1]
        zoom_ax.set_axis_bgcolor("#E6E6B8")
        zoom_ax.title.set_text("Selected Segment")
        zoom_line, = zoom_ax.plot([], [], "#008A2E")

        for i in range(len(coefs)):
            coef = coefs[i]
            y = _np.abs(coef[1:N/2])
            x = _np.arange(len(y))
            color = colors.next()
            ax = axs[i]

            ax.set_axis_bgcolor("#E6E6B8")
            ax.title.set_text("Channel {}".format(i+1))
            ax.plot(x, y, color)

            def onselect_callback(xmin, xmax):

                indmin, indmax = _np.searchsorted(x, (xmin, xmax))
                indmax = min(len(y) - 1, indmax)

                x_segment = x[indmin:indmax]
                y_segment = y[indmin:indmax]
                zoom_line.set_data(x_segment, y_segment)
                zoom_ax.set_xlim(x_segment[0], x_segment[-1])
                zoom_ax.set_ylim(y_segment.min(), y_segment.max())
                zoom_ax.title.set_text("Selected Segment: {} to {}".format(indmin, indmax))
                
                _plt.draw()

            span_selector = _SpanSelector(ax, onselect_callback, 'horizontal', span_stays = True, 
                                          rectprops = dict(alpha = 0.5, facecolor = 'cyan'))
            self.__current_widgets["span_selectors"].append(span_selector)

        self.__current_figs.append(fig)
        fig.subplots_adjust(top = 0.9, hspace = 0.3)
    def plot_spectrum(self, colors=None):
        """
        params:
            `colors`: any interable of symbols of color `supported` by matplotlib
        """

        if colors is None:
            colors = _cycle(["#FF6600", "#0033CC"])
        else:
            colors = _cycle(colors)

        N = len(
            self.audio_data.data) if self.audio_data.CHANNELS == 1 else len(
                self.audio_data.data[0])

        if self.__cached_fft is None:
            self.fft()

        coefs = self.__cached_fft

        fig, axs = _plt.subplots(len(coefs) + 1, 1, figsize=(20, 7))
        zoom_ax = axs[-1]
        zoom_ax.set_axis_bgcolor("#E6E6B8")
        zoom_ax.title.set_text("Selected Segment")
        zoom_line, = zoom_ax.plot([], [], "#008A2E")

        for i in range(len(coefs)):
            coef = coefs[i]
            y = _np.abs(coef[1:N / 2])
            x = _np.arange(len(y))
            color = colors.next()
            ax = axs[i]

            ax.set_axis_bgcolor("#E6E6B8")
            ax.title.set_text("Channel {}".format(i + 1))
            ax.plot(x, y, color)

            def onselect_callback(xmin, xmax):

                indmin, indmax = _np.searchsorted(x, (xmin, xmax))
                indmax = min(len(y) - 1, indmax)

                x_segment = x[indmin:indmax]
                y_segment = y[indmin:indmax]
                zoom_line.set_data(x_segment, y_segment)
                zoom_ax.set_xlim(x_segment[0], x_segment[-1])
                zoom_ax.set_ylim(y_segment.min(), y_segment.max())
                zoom_ax.title.set_text("Selected Segment: {} to {}".format(
                    indmin, indmax))

                _plt.draw()

            span_selector = _SpanSelector(ax,
                                          onselect_callback,
                                          'horizontal',
                                          span_stays=True,
                                          rectprops=dict(alpha=0.5,
                                                         facecolor='cyan'))
            self.__current_widgets["span_selectors"].append(span_selector)

        self.__current_figs.append(fig)
        fig.subplots_adjust(top=0.9, hspace=0.3)
コード例 #3
0
ファイル: analyse.py プロジェクト: dboyliao/pyaudio_wrapper
    def plot(self, by_sec = True, colors = None):
        """
        Plot the audio data.

        params:
            `by_sec`: if True, it will plot the audio in second or it will plot
                      the audio data in the number of frames.
            `colors`: Any iterable of symbols of color supported by matplotlib.
        """
        if colors is None:
            colors = _cycle(["#FF6600", "#0033CC"])
        else:
            colors = _cycle(colors)
        
        num_subplots = self.audio_data.CHANNELS + 1
        fig, axs = _plt.subplots(num_subplots, 1, figsize = (20, 7))
        fig.suptitle(repr(self.audio_data), fontsize = 14, y = 1)
        frames = [self.audio_data.data[i] for i in range(2)] if self.audio_data.CHANNELS == 2 else [self.audio_data.data]

        if by_sec:
            x = _np.linspace(0, len(frames[0])/self.audio_data.SAMPLE_RATE, num = len(frames[0]))
            x_label = "Time (Seconds)"
        else:
            x = _np.linspace(0, len(frames[0]), num = len(frames[0]))
            x_label = "Frame"

        zoom_ax = axs[-1]
        zoom_ax.set_axis_bgcolor("#E6E6B8")
        zoom_ax.title.set_text("Selected Segment")
        zoom_ax.xaxis.set_label_text(x_label)
        zoom_ax.yaxis.set_label_text("Altitude")
        zoom_line, = zoom_ax.plot([], [], "#008A2E")

        temp = []
        for i, (ax, frame) in enumerate(zip(axs[:-1], frames)):
            color = colors.next()

            ax.title.set_text("Channel {}".format(i+1))
            ax.set_axis_bgcolor("#E6E6B8")
            ax.plot(x, frame, color)
            ax.set_xlim(x[0], x[-1])
            ax.xaxis.set_label_text(x_label)
            ax.yaxis.set_label_text("Altitude")

            def onselect_callback(xmin, xmax):

                indmin, indmax = _np.searchsorted(x, (xmin, xmax))
                indmax = min(len(frames[0]) - 1, indmax)

                x_segment = x[indmin:indmax]
                y_segment = frame[indmin:indmax]
                zoom_line.set_data(x_segment, y_segment)
                zoom_ax.set_xlim(x_segment[0], x_segment[-1])
                zoom_ax.set_ylim(y_segment.min(), y_segment.max())

                if by_sec:
                    x_start = int(self.audio_data.duration * float(indmin) / len(x))
                    x_stop = int(self.audio_data.duration * float(indmax) / len(x))
                else:
                    x_start = indmin
                    x_stop = indmax
                
                start_time = self.audio_data.duration * float(indmin) / len(x)
                stop_time = self.audio_data.duration * float(indmax) / len(x)
                zoom_ax.title.set_text("Selected Segment: {} to {}".format(x_start, x_stop))
                _plt.draw()

                self.audio_data.play(start_time, stop_time)
            
            span_selector = _SpanSelector(ax, onselect_callback, 'horizontal', span_stays = True, 
                                          rectprops = dict(alpha = 0.5, facecolor = 'cyan'))
            self.__current_widgets["span_selectors"].append(span_selector)
        fig.subplots_adjust(top = 0.9, hspace = 0.7)
        self.__current_figs.append(fig)
    def plot(self, by_sec=True, colors=None):
        """
        Plot the audio data.

        params:
            `by_sec`: if True, it will plot the audio in second or it will plot
                      the audio data in the number of frames.
            `colors`: Any iterable of symbols of color supported by matplotlib.
        """
        if colors is None:
            colors = _cycle(["#FF6600", "#0033CC"])
        else:
            colors = _cycle(colors)

        num_subplots = self.audio_data.CHANNELS + 1
        fig, axs = _plt.subplots(num_subplots, 1, figsize=(20, 7))
        fig.suptitle(repr(self.audio_data), fontsize=14, y=1)
        frames = [
            self.audio_data.data[i] for i in range(2)
        ] if self.audio_data.CHANNELS == 2 else [self.audio_data.data]

        if by_sec:
            x = _np.linspace(0,
                             len(frames[0]) / self.audio_data.SAMPLE_RATE,
                             num=len(frames[0]))
            x_label = "Time (Seconds)"
        else:
            x = _np.linspace(0, len(frames[0]), num=len(frames[0]))
            x_label = "Frame"

        zoom_ax = axs[-1]
        zoom_ax.set_axis_bgcolor("#E6E6B8")
        zoom_ax.title.set_text("Selected Segment")
        zoom_ax.xaxis.set_label_text(x_label)
        zoom_ax.yaxis.set_label_text("Altitude")
        zoom_line, = zoom_ax.plot([], [], "#008A2E")

        temp = []
        for i, (ax, frame) in enumerate(zip(axs[:-1], frames)):
            color = colors.next()

            ax.title.set_text("Channel {}".format(i + 1))
            ax.set_axis_bgcolor("#E6E6B8")
            ax.plot(x, frame, color)
            ax.set_xlim(x[0], x[-1])
            ax.xaxis.set_label_text(x_label)
            ax.yaxis.set_label_text("Altitude")

            def onselect_callback(xmin, xmax):

                indmin, indmax = _np.searchsorted(x, (xmin, xmax))
                indmax = min(len(frames[0]) - 1, indmax)

                x_segment = x[indmin:indmax]
                y_segment = frame[indmin:indmax]
                zoom_line.set_data(x_segment, y_segment)
                zoom_ax.set_xlim(x_segment[0], x_segment[-1])
                zoom_ax.set_ylim(y_segment.min(), y_segment.max())

                if by_sec:
                    x_start = int(self.audio_data.duration * float(indmin) /
                                  len(x))
                    x_stop = int(self.audio_data.duration * float(indmax) /
                                 len(x))
                else:
                    x_start = indmin
                    x_stop = indmax

                start_time = self.audio_data.duration * float(indmin) / len(x)
                stop_time = self.audio_data.duration * float(indmax) / len(x)
                zoom_ax.title.set_text("Selected Segment: {} to {}".format(
                    x_start, x_stop))
                _plt.draw()

                self.audio_data.play(start_time, stop_time)

            span_selector = _SpanSelector(ax,
                                          onselect_callback,
                                          'horizontal',
                                          span_stays=True,
                                          rectprops=dict(alpha=0.5,
                                                         facecolor='cyan'))
            self.__current_widgets["span_selectors"].append(span_selector)
        fig.subplots_adjust(top=0.9, hspace=0.7)
        self.__current_figs.append(fig)