示例#1
0
def test_phase_rad(sine_plus_impulse):
    """Test the function returning the phase of a signal in radians."""
    phase = dsp.phase(sine_plus_impulse, deg=False, unwrap=False)
    truth = np.angle(sine_plus_impulse.freq)
    npt.assert_allclose(phase, truth, rtol=1e-10)
示例#2
0
def _phase(signal, deg=False, unwrap=False, xscale='log', ax=None, **kwargs):
    """Plot the phase of the spectrum on the positive frequency axis."""

    # check input
    if not isinstance(signal, (Signal, FrequencyData)):
        raise TypeError(
            'Input data has to be of type: Signal or FrequencyData.')
    _check_axis_scale(xscale)

    # prepare figure
    _, ax = _prepare_plot(ax)

    # prepare input
    kwargs = _return_default_colors_rgb(**kwargs)
    phase_data = dsp.phase(signal, deg=deg, unwrap=unwrap)
    # Construct the correct label string:
    ylabel_string = 'Phase '
    if unwrap == '360':
        ylabel_string += '(wrapped to 360) '
    elif unwrap is True:
        ylabel_string += '(unwrapped) '
    elif not isinstance(unwrap, bool):
        raise ValueError(f"unwrap is {unwrap} but must be True, False, or 360")

    if deg:
        ylabel_string += 'in degree'
        y_margin = 5
    else:
        ylabel_string += 'in radians'
        # nice tick formatting is not done for unwrap=True. In this case
        # it can create 1000 or more ticks.
        if not unwrap or unwrap == "360":
            ax.yaxis.set_major_locator(MultipleFractionLocator(np.pi, 2))
            ax.yaxis.set_minor_locator(MultipleFractionLocator(np.pi, 6))
            ax.yaxis.set_major_formatter(
                MultipleFractionFormatter(nominator=1,
                                          denominator=2,
                                          base=np.pi,
                                          base_str=r'\pi'))
        y_margin = np.radians(5)
    ymin = np.nanmin(phase_data) - y_margin  # more elegant solution possible?
    ymax = np.nanmax(phase_data) + y_margin

    # prepare figure
    ax.set_xlabel("Frequency in Hz")
    ax.set_ylabel(ylabel_string)
    ax.grid(True, 'both')
    _set_axlim(ax, ax.set_xlim, _lower_frequency_limit(signal),
               signal.frequencies[-1], ax.get_xlim())
    _set_axlim(ax, ax.set_ylim, ymin, ymax, ax.get_ylim())

    # plot data
    if xscale == 'log':
        ax.semilogx(signal.frequencies, phase_data.T, **kwargs)
    else:
        ax.plot(signal.frequencies, phase_data.T, **kwargs)
    plt.tight_layout()

    # set and format ticks
    if xscale == 'log':
        ax.xaxis.set_major_locator(LogLocatorITAToolbox())
    ax.xaxis.set_major_formatter(LogFormatterITAToolbox())

    return ax
示例#3
0
def test_phase_deg_unwrap(sine_plus_impulse):
    """Test the function returning the unwrapped phase of a signal in deg."""
    phase = dsp.phase(sine_plus_impulse, deg=True, unwrap=True)
    truth = np.degrees(np.unwrap(np.angle(sine_plus_impulse.freq)))
    npt.assert_allclose(phase, truth, rtol=1e-10)