Ejemplo n.º 1
0
def plot_two_level_threshold(results, fig=100, plot_initial_estimate=False):
    plt.figure(fig)
    plt.clf()
    bin_centres = results['histogram']['bin_centres']
    counts = results['histogram']['counts']
    plt.bar(bin_centres,
            counts,
            width=bin_centres[1] - bin_centres[0],
            label='histogram')
    plt.ylabel('Counts')
    plt.xlabel('Signal [a.u.]')
    plot_vertical_line(results['signal_threshold'], label='threshold')
    plot_double_gaussian_fit(results['double_gaussian_fit'], bin_centres)
    plt.title('Result of two level threshold processing')

    if plot_initial_estimate:
        xdata = np.linspace(bin_centres[0], bin_centres[-1], 300)
        initial_estimate = results['double_gaussian_fit'][
            'parameters initial guess']
        left0 = initial_estimate[::2][::-1]
        right0 = initial_estimate[1::2][::-1]
        plt.plot(xdata,
                 gaussian(xdata, *left0),
                 ':g',
                 label='initial estimate left')
        plt.plot(xdata,
                 gaussian(xdata, *right0),
                 ':r',
                 label='initial estimate right')
Ejemplo n.º 2
0
def estimate_dominant_frequency(signal, sample_rate=1, remove_dc=True, fig=None):
    """ Estimate dominant frequency in a signal

    Args:
        signal (array): Input data
        sample_rate (float): Sample rate of the data
        remove_dc (bool): If True, then do not estimate the DC component
        fig (int or None): Optionally plot the estimated frequency
    Returns:
        Estimated dominant frequency
    """
    w = np.fft.fft(signal)
    freqs = np.fft.fftfreq(len(signal), d=1. / sample_rate)

    if remove_dc:
        w[0] = 0
    w[freqs < 0] = 0

    dominant_idx = np.argmax(np.abs(w))
    dominant_frequency = freqs[dominant_idx]

    if 0 < dominant_idx < freqs.size / 2 - 1:
        dominant_idx_subpixel, _ = subpixelmax(np.abs(w), [dominant_idx])
        dominant_frequency = np.interp(dominant_idx_subpixel, np.arange(freqs.size), freqs)[0]

    if fig:
        plt.figure(fig)
        plt.clf()
        plt.plot(freqs, np.abs(w), '.b')
        plt.xlabel('Frequency')
        plt.ylabel('Abs of fft')
        plot_vertical_line(dominant_frequency, label='Dominant frequency')
    return dominant_frequency
def plot_allxy(dataset: DataSet,
               result: Dict[str, Any],
               fig: int = 1,
               plot_initial_estimate: bool = False):
    """ Plot the results of an AllXY fit

    Args:
        dataset: Dataset containing the measurement data
        result: Fitting result of fit_allxy
        int: Figure handle
        plot_initial_guess: If True, then plot the initial estimate of the model
    """
    allxy_data = _default_measurement_array(dataset)
    xy_pairs = generate_allxy_combinations()
    x_data = np.arange(21)

    plt.figure(fig)
    plt.clf()
    fitted_parameters = result['fitted_parameters']
    xfine = np.arange(0, 21, 1e-3)
    plt.plot(xfine,
             allxy_model(xfine, *fitted_parameters),
             'm',
             label='fitted allxy',
             alpha=.5)

    plt.plot(x_data, allxy_data, '.b', label='allxy data')

    if plot_initial_estimate:
        p = [0, 0, .5, 0, 1, 0]
        plt.plot(xfine,
                 allxy_model(xfine, *p),
                 'c',
                 label='baseline',
                 alpha=.5)

        initial_params = _estimate_allxy_parameters(allxy_data)
        plt.plot(xfine,
                 allxy_model(xfine, *initial_params),
                 'g',
                 label='initial estimate',
                 alpha=.35)

        initial_parameters = result['initial_parameters']
        plt.plot(xfine,
                 allxy_model(xfine, *initial_parameters),
                 ':g',
                 label='initial estimate',
                 alpha=.35)

    plt.xticks(x_data, [v[0] + "," + v[1] for v in xy_pairs],
               rotation='vertical')
    vl = plot_vertical_line(4.5)
    vl.set_linestyle(':')
    vl = plot_vertical_line(16.5)
    vl.set_linestyle(':')
    plt.title('AllXY')

    plt.legend()
Ejemplo n.º 4
0
def plot_allxy(dataset: DataSet, result: Dict[str, Any], fig: Union[int, Axes, None] = 1, plot_initial_estimate: bool = False):
    """ Plot the results of an AllXY fit

    Args:
        dataset: Dataset containing the measurement data
        result: Fitting result of fit_allxy
        fig: Figure or axis handle. Is passed on to `get_axis`
        plot_initial_guess: If True, then plot the initial estimate of the model
    """
    allxy_data = _default_measurement_array(dataset)
    xy_pairs = generate_allxy_combinations()
    x_data = np.arange(21)

    ax = get_axis(fig)
    fitted_parameters = result['fitted_parameters']
    xfine = np.arange(0, 21, 1e-3)
    ax.plot(xfine, allxy_model(xfine, *fitted_parameters), 'm', label='fitted allxy', alpha=.5)

    ax.plot(x_data, allxy_data, '.b', label='allxy data')

    if plot_initial_estimate:
        p = [0, 0, .5, 0, 1, 0]
        ax.plot(xfine, allxy_model(xfine, *p), 'c', label='baseline', alpha=.5)

        initial_parameters = result['initial_parameters']
        ax.plot(xfine, allxy_model(xfine, *initial_parameters), ':g', label='initial estimate', alpha=.35)

    ax.set_xticks(x_data)
    ax.set_xticklabels([v[0] + "," + v[1] for v in xy_pairs], rotation='vertical')

    vl = plot_vertical_line(4.5)
    vl.set_linestyle(':')
    vl = plot_vertical_line(16.5)
    vl.set_linestyle(':')
    ax.set_title(f'AllXY: {dataset.location}')

    ax.legend()
Ejemplo n.º 5
0
def plot_two_level_threshold(results: dict,
                             fig: int = 100,
                             plot_initial_estimate: bool = False):
    separation = results['separation']
    threshold = results['signal_threshold']

    ax = get_axis(fig)
    bin_centres = results['histogram']['bin_centres']
    counts = results['histogram']['counts']
    ax.bar(bin_centres,
           counts,
           width=bin_centres[1] - bin_centres[0],
           label='histogram')
    ax.set_ylabel('Counts')
    ax.set_xlabel('Signal [a.u.]')
    plot_vertical_line(threshold, label='threshold')
    plot_double_gaussian_fit(results['double_gaussian_fit'], bin_centres)

    ax.set_title(
        f'Two-level signal: separation {separation:.3f}, threshold {threshold:.3g}'
    )

    if plot_initial_estimate:
        xdata = np.linspace(bin_centres[0], bin_centres[-1], 300)
        initial_estimate = results['double_gaussian_fit'][
            'parameters initial guess']
        left0 = initial_estimate[::2][::-1]
        right0 = initial_estimate[1::2][::-1]
        ax.plot(xdata,
                gaussian(xdata, *left0),
                ':g',
                label='initial estimate left')
        ax.plot(xdata,
                gaussian(xdata, *right0),
                ':r',
                label='initial estimate right')