Beispiel #1
0
def shift_waveform_phase_time(hp,
                              hc,
                              t_shift,
                              ph_shift,
                              shift_epochs_only=True,
                              trim_leading=False,
                              trim_trailing=True,
                              verbose=False):
    """
    Input:  hp, hc, where h = hp(t) + i hx(t) = Amp(t) * exp(-i * Phi(t))
    Output: hp, hc, where h = Amp(t - t_c) * exp( -i * [Phi(t - t_c) + phi_c] )
    """
    hpnew = TimeSeries(hp,
                       epoch=hp._epoch,
                       delta_t=hp.delta_t,
                       dtype=hp.dtype,
                       copy=True)
    hcnew = TimeSeries(hc,
                       epoch=hc._epoch,
                       delta_t=hc.delta_t,
                       dtype=hc.dtype,
                       copy=True)
    # First apply phase shift
    if ph_shift != 0.:
        amplitude = amplitude_from_polarizations(hpnew, hcnew)
        phase = phase_from_polarizations(hpnew, hcnew)
        if verbose:
            print(("shifting by %f radians" % ph_shift))
        phase = phase + ph_shift
        hpnew = TimeSeries(amplitude * np.cos(phase + np.pi),
                           epoch=hpnew._epoch,
                           delta_t=hpnew.delta_t,
                           dtype=hpnew.dtype)
        hcnew = TimeSeries(amplitude * np.sin(phase + np.pi),
                           epoch=hcnew._epoch,
                           delta_t=hcnew.delta_t,
                           dtype=hcnew.dtype)
    # Now apply time shift
    # Only positive time shifts can be applied by rolling forward data
    # if negative time shifts are asked for, we can only shift epochs
    if shift_epochs_only or t_shift <= 0:
        hpnew._epoch += t_shift
        hcnew._epoch += t_shift
    else:
        hpnewI = InterpolatedUnivariateSpline(hpnew.get_sample_times(),
                                              hpnew.data)
        hcnewI = InterpolatedUnivariateSpline(hcnew.get_sample_times(),
                                              hcnew.data)

        time_vals = hpnew.get_sample_times()
        shifted_time_vals = np.arange(
            time_vals[0],  # start of new timeseries
            time_vals[-1] + t_shift + 0 * hpnew.delta_t,  # end of it
            hpnew.delta_t)
        mask_times_to_reevaluate = [
            (shifted_time_vals - t_shift >= time_vals[0]) &
            (shifted_time_vals - t_shift <= time_vals[-1])
        ]
        hp_shifted = np.zeros(len(shifted_time_vals))
        hp_shifted[mask_times_to_reevaluate] = hpnewI(
            shifted_time_vals[mask_times_to_reevaluate] - t_shift)
        hpnew = TimeSeries(hp_shifted,
                           epoch=hpnew._epoch + t_shift,
                           delta_t=hpnew.delta_t)

        hc_shifted = np.zeros(len(shifted_time_vals))
        hc_shifted[mask_times_to_reevaluate] = hcnewI(
            shifted_time_vals[mask_times_to_reevaluate] - t_shift)
        hcnew = TimeSeries(hc_shifted,
                           epoch=hcnew._epoch + t_shift,
                           delta_t=hcnew.delta_t)

    if trim_trailing:
        hpnew = trim_trailing_zeros(hpnew)
        hcnew = trim_trailing_zeros(hcnew)
    if trim_leading:
        hpnew = trim_leading_zeros(hpnew)
        hcnew = trim_leading_zeros(hcnew)
    return hpnew, hcnew