Exemple #1
0
def time_offset(time_v, signal, high_th, low_th, offset):
    """ 
    shift a trace so that the steepest point is at time 0
    steepest points must exist within discriminator window
    """
    # start by finding the max of the derivative
    dt = np.diff(time_v)[0]

    # derivative of the signal, smoothed
    d_signal = savgol_filter(np.diff(signal), 301, 3)
    # ax = plt.gca()
    # ax2 = ax.twinx()
    # ax2.plot(d_signal/np.max(d_signal), color='green')

    # use peaks only if they are legitimate edges identified by the set-reset switch 
    mask = pu.disc_peak_full(signal, high_th, low_th, offset)
    # print np.where(mask==True)
    # print peakutils.indexes(d_signal, .5, 3000)

    # find peaks in the derivative to find the steepest point
    idx = np.array([i for i in peakutils.indexes(d_signal, .5, 3000) if mask[i]==True])
    # print idx

    if len(idx) == 0:
        return [np.nan for _ in time_v]

    idx_s = np.flipud(idx[d_signal[idx].argsort()])[0]
    try:
        time_p = peakutils.interpolate(time_v[:-1], d_signal, [idx_s])[0]
    except (RuntimeError, ValueError) as e:
        time_p = time_v[idx_s]

    n_shift = int(time_p / dt)
    return pa.shift(signal, - n_shift+int(len(signal)/2))
Exemple #2
0
def disc_shift(time_s, signal, height_th):
    """ 
    use the set reset switch to locate the pulse edge and shift it to the center of the acquisition window.
    if all traces have the same acquisition window, they will be synchronised.
    """
    center = int(len(time_s) / 2)

    [mask, clamp, edges, left_edges,
     right_edges] = pd.discriminator(time_s,
                                     signal,
                                     height_th=height_th,
                                     method=3)
    n_shift = left_edges[0]
    print n_shift
    return pa.shift(signal, -n_shift + center)
Exemple #3
0
def fit_shift(time_s, signal, fit_model, high_th, low_th, offset):
    """ 
    fit the trace with a sample pulse and shift it to match the staritngtime
    """
    dt = np.diff(time_s)[0]
    d_signal = savgol_filter(np.diff(signal), 301, 3)
    # ax = plt.gca()
    # ax2 = ax.twinx()
    # ax2.plot(d_signal/np.max(d_signal), color='green')

    # use peaks only if they are legitimate edges identified by the set-reset switch 
    mask = pu.disc_peak_full(signal, high_th, low_th, offset)

    # find peaks in the derivative to find the steepest point
    idx = np.array([i for i in peakutils.indexes(d_signal, .5, 3000) if mask[i]==True])
    # print(time_s[idx])
    # print time_v[left_edges], time_v[idx]

    if len(idx) == 0:
        return [np.nan for _ in time_s]

    idx_s = np.flipud(idx[d_signal[idx].argsort()])
    # print(time_s[idx_s[0]])

    p = Parameters()
    p.add('x_offset', time_s[np.argmax(signal)])
    if len(idx_s) > 0:
        p.add('x_offset', time_s[idx_s[0]])
    p.add('amplitude', 1, vary=1)

    result = fit_model.fit(signal,
                           x=time_s,
                           params=p,
                           # weights=1 / 0.001
                           )

    n_shift = int(result.best_values['x_offset'] / dt)

    # print(result.fit_report())
    # result.plot_fit()
    # print n_shift*dt
    return pa.shift(signal, -n_shift+int(len(signal)/2))
Exemple #4
0
def fit_shift(time_s, signal, fit_model, height_th):
    """ fit the trace with a sample pulse and shift it to match the staritng
    time
    """
    zero = int(len(time_s) / 4) * 0
    dt = np.diff(time_s)[0]
    d_signal = savgol_filter(np.diff(signal), 301, 1)
    # ax = plt.gca()
    # ax2 = ax.twinx()
    # ax2.plot(np.array(time_v)[1:],d_signal*100)

    # use peaks only if they are legitimate edges identified by the set-reset switch
    [mask, clamp, edges, left_edges,
     right_edges] = pd.discriminator(time_s,
                                     signal,
                                     dt_left=100e-9,
                                     dt_right=0,
                                     height_th=height_th,
                                     method=2)

    # find peaks in the derivative to find the steepest point
    idx = left_edges[0] + peakutils.indexes(d_signal[(mask & clamp)[1:]], .5,
                                            3000)
    # print time_v[left_edges], time_v[idx]

    if len(idx) == 0:
        return [np.nan for _ in time_v]
    # else:
    #     idx=(mask&clamp)[idx]*idx

    idx_s = np.flipud(idx[d_signal[idx].argsort()])

    p = Parameters()
    p.add('x_offset', time_s[np.argmax(signal)] - 4e-7 - zero * dt)
    if len(idx_s) > 0:
        p.add('x_offset', time_s[idx_s[0]] - zero * dt)
    p.add('amplitude', 1, vary=1)
    result = fit_model.fit(signal, x=time_s, params=p, weights=1 / 0.001)
    n_shift = int(result.best_values['x_offset'] / dt)
    # print n_shift*dt
    return pa.shift(signal, -n_shift)
Exemple #5
0
def time_offset(time_v, trace, height_th):
    """ shift a trace so that the steepest point is at time 0
    """
    # start by finding the max of the derivative
    zero = int(len(time_v) / 4) * 0
    signal = trace
    dt = np.diff(time_v)[0]

    # derivative of the signal, smoothed
    d_signal = savgol_filter(np.diff(signal), 301, 1)
    # ax = plt.gca()
    # ax2 = ax.twinx()
    # ax2.plot(np.array(time_v)[1:],d_signal*100)

    # use peaks only if they are legitimate edges identified by the set-reset switch
    [mask, clamp, edges, left_edges,
     right_edges] = pd.discriminator(time_v,
                                     signal,
                                     dt_left=100e-9,
                                     dt_right=0,
                                     height_th=height_th,
                                     method=2)

    # find peaks in the derivative to find the steepest point
    idx = left_edges[0] + peakutils.indexes(d_signal[(mask & clamp)[1:]], .5,
                                            3000)
    # print time_v[left_edges], time_v[idx]

    if len(idx) == 0:
        return [np.nan for _ in time_v]
    # else:
    #     idx=(mask&clamp)[idx]*idx

    idx_s = np.flipud(idx[d_signal[idx].argsort()])[0]
    try:
        time_p = peakutils.interpolate(time_v[:-1], d_signal, [idx_s])[0]
    except (RuntimeError, ValueError) as e:
        time_p = time_v[idx_s]

    n_shift = int(time_p / dt)
    return pa.shift(trace, -n_shift + zero)