Ejemplo n.º 1
0
def _plot_filter(b, a, sampling_rate=1000., nfreqs=4096, ax=None):
    """Compute and plot the frequency response of a digital filter.

    Parameters
    ----------
    b : array
        Numerator coefficients.
    a : array
        Denominator coefficients.
    sampling_rate : int, float, optional
        Sampling frequency (Hz).
    nfreqs : int, optional
        Number of frequency points to compute.
    ax : axis, optional
        Plot Axis to use.

    Returns
    -------
    fig : Figure
        Figure object.

    """

    # compute frequency response
    freqs, resp = st._filter_resp(b,
                                  a,
                                  sampling_rate=sampling_rate,
                                  nfreqs=nfreqs)

    # plot
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    else:
        fig = ax.figure

    # amplitude
    pwr = 20. * np.log10(np.abs(resp))
    ax.semilogx(freqs, pwr, 'b', linewidth=MAJOR_LW)
    ax.set_ylabel('Amplitude (dB)', color='b')
    ax.set_xlabel('Frequency (Hz)')

    # phase
    angles = np.unwrap(np.angle(resp))
    ax2 = ax.twinx()
    ax2.semilogx(freqs, angles, 'g', linewidth=MAJOR_LW)
    ax2.set_ylabel('Angle (radians)', color='g')

    ax.grid()

    return fig
Ejemplo n.º 2
0
def _plot_filter(b, a, sampling_rate=1000., nfreqs=512, ax=None):
    """Compute and plot the frequency response of a digital filter.

    Parameters
    ----------
    b : array
        Numerator coefficients.
    a : array
        Denominator coefficients.
    sampling_rate : int, float, optional
        Sampling frequency (Hz).
    nfreqs : int, optional
        Number of frequency points to compute.
    ax : axis, optional
        Plot Axis to use.

    Returns
    -------
    fig : Figure
        Figure object.

    """

    # compute frequency response
    freqs, resp = st._filter_resp(b, a,
                                  sampling_rate=sampling_rate,
                                  nfreqs=nfreqs)

    # plot
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    else:
        fig = ax.figure

    # amplitude
    ax.semilogy(freqs, np.abs(resp), 'b', linewidth=MAJOR_LW)
    ax.set_ylabel('Amplitude (dB)', color='b')
    ax.set_xlabel('Frequency (Hz)')

    # phase
    angles = np.unwrap(np.angle(resp))
    ax2 = ax.twinx()
    ax2.plot(freqs, angles, 'g', linewidth=MAJOR_LW)
    ax2.set_ylabel('Angle (radians)', color='g')

    ax.grid()

    return fig