Exemple #1
0
def sinegaussian_example(axes1, axes2, e, pol):
    axes1.grid(True)
    axes2.grid(True)

    hp, hc = lalsimulation.SimBurstSineGaussian(8.89, 250., 1., e, pol,
                                                1. / 16384)
    print("measured hrss = %.17g" % lalsimulation.MeasureHrss(hp, hc))
    t = float(hp.epoch) + numpy.arange(0., len(hp.data.data)) * hp.deltaT
    axes1.plot(t, hp.data.data, label=r"$h_{+}$")
    t = float(hc.epoch) + numpy.arange(0., len(hc.data.data)) * hc.deltaT
    axes1.plot(t, hc.data.data, label=r"$h_{\times}$")
    axes1.set_ylim((-15, +15))
    axes1.set_xlabel(r"Time (seconds)")
    axes1.set_ylabel(r"Strain")
    axes1.set_title(
        r"$f_{0} = 250\,\mathrm{Hz}$, $Q = 8.89$, $h_{\mathrm{rss}} = 1$, $\epsilon = %g$, $\phi = %g \pi$"
        % (e, pol / math.pi))
    axes1.legend()
    axes2.plot(hc.data.data, hp.data.data, color="k")
    axes2.set_xlim((-15, +15))
    axes2.set_ylim((-15, +15))
    axes2.set_xlabel(r"$h_{\times}$")
    axes2.set_ylabel(r"$h_{+}$")
    axes2.set_title(
        r"$f_{0} = 250\,\mathrm{Hz}$, $Q = 8.89$, $h_{\mathrm{rss}} = 1$, $\epsilon = %g$, $\phi = %g \pi$"
        % (e, pol / math.pi))
    axes2.set_aspect(1., adjustable="box")
Exemple #2
0
def _lalsim_sgburst_waveform(**p):
    hp, hc = lalsimulation.SimBurstSineGaussian(float(p['q']),
               float(p['frequency']),
               float(p['hrss']),
               float(p['eccentricity']),
               float(p['polarization']),
               float(p['delta_t']))

    hp = TimeSeries(hp.data.data[:], delta_t=hp.deltaT, epoch=hp.epoch)
    hc = TimeSeries(hc.data.data[:], delta_t=hc.deltaT, epoch=hc.epoch)

    return hp, hc
Exemple #3
0
def SG_template(frequency,
                quality,
                delta_t=1. / 16384,
                epoch=0.0,
                datalen=16384):
    """
    Return a pycbc timeseries object with a sine-Gaussian with hrss=1,
    ellipticity=1, polar angle=0
    """

    hp, _ = lalsim.SimBurstSineGaussian(quality, frequency, 1.0 / np.sqrt(2),
                                        1.0, 0, delta_t)

    tmparr = np.zeros(datalen)
    tmparr[:hp.data.length] = hp.data.data[:]

    return pycbc.types.TimeSeries(tmparr, delta_t=delta_t, epoch=epoch)
Exemple #4
0
    def apply(self, strain, detector_name, f_lower=None, distance_scale=1):
        """Add injections (as seen by a particular detector) to a time series.

        Parameters
        ----------
        strain : TimeSeries
            Time series to inject signals into, of type float32 or float64.
        detector_name : string
            Name of the detector used for projecting injections.
        f_lower : {None, float}, optional
            Low-frequency cutoff for injected signals. If None, use value
            provided by each injection.
        distance_scale: {1, foat}, optional
            Factor to scale the distance of an injection with. The default is
            no scaling.

        Returns
        -------
        None

        Raises
        ------
        TypeError
            For invalid types of `strain`.
        """
        if strain.dtype not in (float32, float64):
            raise TypeError("Strain dtype must be float32 or float64, not " \
                    + str(strain.dtype))

        lalstrain = strain.lal()
        detector = Detector(detector_name)
        earth_travel_time = lal.REARTH_SI / lal.C_SI
        t0 = float(strain.start_time) - earth_travel_time
        t1 = float(strain.end_time) + earth_travel_time

        # pick lalsimulation injection function
        add_injection = injection_func_map[strain.dtype]

        for inj in self.table:
            # roughly estimate if the injection may overlap with the segment
            end_time = inj.get_time_geocent()
            #CHECK: This is a hack (10.0s); replace with an accurate estimate
            inj_length = 10.0
            eccentricity = 0.0
            polarization = 0.0
            start_time = end_time - 2 * inj_length
            if end_time < t0 or start_time > t1:
                continue

            # compute the waveform time series
            hp, hc = sim.SimBurstSineGaussian(float(inj.q),
                                              float(inj.frequency),
                                              float(inj.hrss),
                                              float(eccentricity),
                                              float(polarization),
                                              float(strain.delta_t))
            hp = TimeSeries(hp.data.data[:], delta_t=hp.deltaT, epoch=hp.epoch)
            hc = TimeSeries(hc.data.data[:], delta_t=hc.deltaT, epoch=hc.epoch)
            hp._epoch += float(end_time)
            hc._epoch += float(end_time)
            if float(hp.start_time) > t1:
                continue

            # compute the detector response, taper it if requested
            # and add it to the strain
            strain = wfutils.taper_timeseries(strain, inj.taper)
            signal_lal = hp.astype(strain.dtype).lal()
            add_injection(lalstrain, signal_lal, None)

        strain.data[:] = lalstrain.data.data[:]
Exemple #5
0
def damped_chirp_tmplt(det_data, int_params, ext_params):
    """
    Build a template for the detector in det_data from the intrinsic and
    extrinsic parameteres in int_params and ext_params
    """

    #
    # Compute polarisations for damped chirp
    #

    #   _, hp = damped_chirp(int_params.tmplt_len,
    #           int_params.Amp, int_params.f0, int_params.tau, int_params.df)
    #
    #   _, hc = damped_chirp(int_params.tmplt_len,
    #           int_params.Amp, int_params.f0, int_params.tau, int_params.df,
    #           phi0=90.0)
    #
    #   # Get the epoch for the start of the time series
    epoch = ext_params.geocent_peak_time  # - \
    #           #np.argmax(hp)*det_data.td_response.delta_t
    #           # XXX: why don't we need to subtract this bit...?
    #
    #
    #   # --- Put the polarisations into LAL TimeSeries
    Q = 2.0 * np.pi * int_params.tau * int_params.f0

    hp, hc = lalsim.SimBurstSineGaussian(Q, int_params.f0, int_params.Amp, 0,
                                         0, 1.0 / 16384)

    # zero-out...
    hp.data.data[0:hp.data.length / 2] = 0.0
    hc.data.data[0:hp.data.length / 2] = 0.0

    hplus = lal.CreateREAL8TimeSeries('hplus', epoch, 0,
                                      det_data.td_noise.delta_t,
                                      lal.StrainUnit, hp.data.length)
    hplus.data.data = np.copy(hp.data.data)
    del hp

    hcross = lal.CreateREAL8TimeSeries('hcross', epoch, 0,
                                       det_data.td_noise.delta_t,
                                       lal.StrainUnit, hc.data.length)
    hcross.data.data = np.copy(hc.data.data)
    del hc

    #
    #   # --- Put the polarisations into LAL TimeSeries
    #   hplus = lal.CreateREAL8TimeSeries('hplus', epoch, 0,
    #           det_data.td_noise.delta_t, lal.StrainUnit, len(hp))
    #   hplus.data.data=np.copy(hp)
    #   del hp
    #
    #   hcross = lal.CreateREAL8TimeSeries('hcross', epoch, 0,
    #           det_data.td_noise.delta_t, lal.StrainUnit, len(hc))
    #   hcross.data.data=np.copy(hc)
    #   del hc

    #
    # Project polarisations down to detector
    #
    tmplt = lalsim.SimDetectorStrainREAL8TimeSeries(hplus, hcross,
                                                    ext_params.ra,
                                                    ext_params.dec,
                                                    ext_params.polarization,
                                                    det_data.det_site)
    del hplus, hcross

    # Scale for distance (waveforms extracted at 20 Mpc)
    tmplt.data.data *= 20.0 / ext_params.distance

    #
    # Finally make this the same size as the data (useful for fitting)
    #
    #lal.ResizeREAL8TimeSeries(tmplt, 0, len(det_data.td_response.data))
    no_noise = lal.CreateREAL8TimeSeries(
        'blah', det_data.td_noise.start_time, 0, det_data.td_noise.delta_t,
        lal.StrainUnit,
        int(det_data.td_noise.duration / det_data.td_noise.delta_t))
    no_noise.data.data = np.zeros(
        int(det_data.td_noise.duration / det_data.td_noise.delta_t))

    tmplt = lal.AddREAL8TimeSeries(no_noise, tmplt)

    return pycbc.types.timeseries.TimeSeries(\
            initial_array=np.copy(tmplt.data.data), delta_t=tmplt.deltaT,
            epoch=tmplt.epoch)
Exemple #6
0
def reconstructed_SineGaussianF(posterior, waveform, flow=1000, fupp=4096,
        nrec=100):
    """
    return the reconstructed F-domain sine-Gaussians from the posterior samples
    in posterior, as well as the max-posterior reconstruction and the matches
    with the target waveform
    """
    wlen=16384


    # Get a zero-padded version of the target waveform
    htarget=np.zeros(wlen)
    htarget[0:len(waveform.hplus)]=waveform.hplus.data
    htarget=pycbc.types.TimeSeries(htarget, delta_t = waveform.hplus.delta_t)

    # Normalise so that the target has hrss=1
    #hrssTarget = pycbc.filter.sigma(htarget, low_frequency_cutoff=flow,
    #        high_frequency_cutoff=fupp)
    #htarget.data /= hrssTarget

    # Get frequency series of target waveform
    H_target = htarget.to_frequencyseries()

    # Make psd for matches
    flen = len(H_target)
    delta_f = np.diff(H_target.sample_frequencies)[0]
    psd = aLIGOZeroDetHighPower(flen, H_target.delta_f, low_freq_cutoff=flow)

    # -----------
    # MAP waveform
    # XXX: Time-domain is a little easier, since we don't have to figure out
    # which frequencies to populate in a pycbc object

    hp, _ = lalsim.SimBurstSineGaussian(posterior.maxP[1]['quality'],
            posterior.maxP[1]['frequency'], posterior.maxP[1]['hrss'], 0.0, 0.0,
            waveform.hplus.delta_t)
    #hp, _ = lalsim.SimBurstSineGaussian(posterior.maxP[1]['quality'],
    #        posterior.maxP[1]['frequency'], 1.0, 0.0, 0.0,
    #        waveform.hplus.delta_t)

    # zero-pad
    h_MAP = np.zeros(wlen)

    # populate
    h_MAP[:hp.data.length]=hp.data.data

    # pycbc objects
    h_MAP_ts = pycbc.types.TimeSeries(h_MAP,waveform.hplus.delta_t)
    H_MAP = h_MAP_ts.to_frequencyseries()

    MAP_match = pycbc.filter.match(H_target, H_MAP, low_frequency_cutoff=flow,
            high_frequency_cutoff=fupp)[0]

    # -------------------------
    # Waveforms for all samples
    # Pre-allocate:

    #nrec=500
    if len(posterior['frequency'].samples)>nrec:
        decidx = np.random.randint(0, len(posterior['frequency'].samples),
                nrec)
    else:
        decidx = xrange(nrec)

    reconstructions = np.zeros(shape=(nrec, len(H_target)), dtype=complex)
    matches = np.zeros(nrec)

    # All samples!
    for idx in xrange(nrec):


        # let's just use hplus for now...
        hp, _ = lalsim.SimBurstSineGaussian(
                np.squeeze(posterior['quality'].samples)[idx],
                np.squeeze(posterior['frequency'].samples)[idx],
                np.squeeze(posterior['hrss'].samples)[idx], 0.0, 0.0,
                waveform.hplus.delta_t)
       #hp, _ = lalsim.SimBurstSineGaussian(
       #        np.squeeze(posterior['quality'].samples)[idx],
       #        np.squeeze(posterior['frequency'].samples)[idx],
       #        1.0, 0.0, 0.0,
       #        waveform.hplus.delta_t)

        # zero pad
        hcurrent = np.zeros(wlen)

        # populate
        hcurrent[:hp.data.length] = hp.data.data

        # pycbc object
        hcurrent_ts = pycbc.types.TimeSeries(hcurrent, delta_t=hp.deltaT)

        # populate array of all reconstructions
        reconstructions[idx, :] = hcurrent_ts.to_frequencyseries().data

        # compute match for this sample
        matches[idx] = pycbc.filter.match(H_target,
                hcurrent_ts.to_frequencyseries(), low_frequency_cutoff=flow,
                high_frequency_cutoff=fupp)[0]

    # -----------
    reconstruction = {}
    reconstruction['FrequencyAxis'] = H_MAP.sample_frequencies
    reconstruction['TargetSpectrum'] = H_target
    reconstruction['MAPSpectrum'] = H_MAP
    reconstruction['MAPMatch'] = MAP_match
    reconstruction['SampledReconstructions'] = reconstructions
    reconstruction['SampledMatches'] = matches

    return reconstruction