Ejemplo n.º 1
0
    def get_waveforms(self):
        """The waveforms generated using the current parameters.

        If the waveforms haven't been generated yet, they will be generated,
        resized to the same length as the data, and cached. If the
        ``highpass_waveforms`` attribute is set, a highpass filter will
        also be applied to the waveforms.

        Returns
        -------
        dict :
            Dictionary of detector names -> FrequencySeries.
        """
        if self._current_wfs is None:
            params = self.current_params
            wfs = self.waveform_generator.generate(**params)
            for det, h in wfs.items():
                # make the same length as the data
                h.resize(len(self.data[det]))
                # apply high pass
                if self.highpass_waveforms:
                    h = highpass(h.to_timeseries(),
                                 frequency=self.highpass_waveforms
                                 ).to_frequencyseries()
                wfs[det] = h
            self._current_wfs = wfs
        return self._current_wfs
Ejemplo n.º 2
0
    def setUp(self):
        ###### Get data for references analysis of 170817
        m = Merger("GW170817")
        ifos = ['H1', 'V1', 'L1']
        self.psds = {}
        self.data = {}

        for ifo in ifos:
            print("Processing {} data".format(ifo))

            # Download the gravitational wave data for GW170817
            url = "https://dcc.ligo.org/public/0146/P1700349/001/"
            url += "{}-{}1_LOSC_CLN_4_V1-1187007040-2048.gwf"
            fname = download_file(url.format(ifo[0], ifo[0]), cache=True)
            ts = read_frame(fname,
                            "{}:LOSC-STRAIN".format(ifo),
                            start_time=int(m.time - 260),
                            end_time=int(m.time + 40))
            ts = highpass(ts, 15.0)
            ts = resample_to_delta_t(ts, 1.0 / 2048)
            ts = ts.time_slice(m.time - 112, m.time + 16)
            self.data[ifo] = ts.to_frequencyseries()

            psd = interpolate(ts.psd(4), ts.delta_f)
            psd = inverse_spectrum_truncation(psd,
                                              int(4 * psd.sample_rate),
                                              trunc_method='hann',
                                              low_frequency_cutoff=20.0)
            self.psds[ifo] = psd

        self.static = {
            'mass1': 1.3757,
            'mass2': 1.3757,
            'f_lower': 20.0,
            'approximant': "TaylorF2",
            'polarization': 0,
            'ra': 3.44615914,
            'dec': -0.40808407,
            'tc': 1187008882.42840,
        }
        self.variable = (
            'distance',
            'inclination',
        )
        self.flow = {'H1': 25, 'L1': 25, 'V1': 25}
        inclination_prior = SinAngle(inclination=None)
        distance_prior = Uniform(distance=(10, 100))
        tc_prior = Uniform(tc=(m.time - 0.1, m.time + 0.1))
        self.prior = JointDistribution(self.variable, inclination_prior,
                                       distance_prior)

        ###### Expected answers
        # Answer taken from marginalized gaussian model
        self.q1 = {'distance': 42.0, 'inclination': 2.5}
        self.a1 = 541.8235746138382

        # answer taken from brute marginize pol + phase
        self.a2 = 542.581
        self.pol_samples = 200
Ejemplo n.º 3
0
 def get_waveforms(self):
     if self._current_wfs is None:
         params = self.current_params
         wfs = self.waveform_generator.generate(**params)
         for det, (hp, hc) in wfs.items():
             # make the same length as the data
             hp.resize(len(self.data[det]))
             hc.resize(len(self.data[det]))
             # apply high pass
             if self.highpass_waveforms:
                 hp = highpass(hp.to_timeseries(),
                               frequency=self.highpass_waveforms
                               ).to_frequencyseries()
                 hc = highpass(hc.to_timeseries(),
                               frequency=self.highpass_waveforms
                               ).to_frequencyseries()
             wfs[det] = (hp, hc)
         self._current_wfs = wfs
     return self._current_wfs
Ejemplo n.º 4
0
def get_GWSC_Full_strain(pth, tstart, tend):
    """This function, require the strain .gwf name, without the detector definition 
    (H-H1,L-L1), and loads the entire frame file downloaded from GW OSC,
    applies a high-pass filter at 15 Hz, then return the whole data without cutting that
    """
    strain = {}
    for ifo in ifos:
        fname = '%s-%s_%s' % (ifo[0], ifo, pth)
        channel_name = '%s:GWOSC-4KHZ_R1_STRAIN' % ifo
        strain[ifo] = read_frame(fname, channel_name, tstart, tend)
        strain[ifo] = highpass(strain[ifo], 15.0)
    return strain
Ejemplo n.º 5
0
def get_raw_strain(window=512):
    """This function loads the entire frame file downloaded from GW OSC,
    applies a high-pass filter at 15 Hz, then keeps the data around
    ``event_time`` +/-``window``. The ``event_time`` is from above; the
    default window is 512 (s).
    """
    strain = {}
    for ifo in ifos:
        fname = '%s-%s_LOSC_4_V2-1126257414-4096.gwf' % (ifo[0], ifo)
        channel_name = '%s:LOSC-STRAIN' % ifo
        strain[ifo] = read_frame(fname, channel_name)
        strain[ifo] = highpass(strain[ifo], 15.0)
        strain[ifo] = strain[ifo].time_slice(event_time-window/2, event_time+window/2)
    return strain
Ejemplo n.º 6
0
def get_LSC_NoNan_strain(pth, tev, tstart, tend, window):
    """This function, require the strain .gwf name, without the detector definition 
    (H-H1,L-L1), and loads the entire frame file downloaded from GW OSC,
    applies a high-pass filter at 15 Hz, then keeps the data around
    ``event_time`` +/-``window``. The ``event_time`` is from above; the
    default window is 512 (s).
    """
    strain = {}
    for ifo in ifos:
        fname = '%s-%s_%s' % (ifo[0], ifo, pth)
        channel_name = '%s:LOSC-STRAIN' % ifo
        strain[ifo] = read_frame(fname, channel_name, tstart, tend)
        strain[ifo] = highpass(strain[ifo], 15.0)
        strain[ifo] = strain[ifo].time_slice(tev - window / 2,
                                             tev + window / 2)
    return strain
Ejemplo n.º 7
0
def from_cli(opt, dyn_range_fac=1, precision='single'):
    """Parses the CLI options related to strain data reading and conditioning.

    Parameters
    ----------
    opt : object
        Result of parsing the CLI with OptionParser, or any object with the
        required attributes  (gps-start-time, gps-end-time, strain-high-pass, 
        pad-data, sample-rate, (frame-cache or frame-files), channel-name, 
        fake-strain, fake-strain-seed, gating_file).

    dyn_range_fac: {float, 1}, optional
        A large constant to reduce the dynamic range of the strain.

    Returns
    -------
    strain : TimeSeries
        The time series containing the conditioned strain data.
    """
    if opt.frame_cache or opt.frame_files:
        if opt.frame_cache:
            frame_source = opt.frame_cache
        if opt.frame_files:
            frame_source = opt.frame_files

        logging.info("Reading Frames")
        strain = read_frame(frame_source, opt.channel_name,
                            start_time=opt.gps_start_time-opt.pad_data,
                            end_time=opt.gps_end_time+opt.pad_data)

        if opt.zpk_z and opt.zpk_p and opt.zpk_k:
            logging.info("Highpass Filtering")
            strain = highpass(strain, frequency=opt.strain_high_pass)

            logging.info("Applying zpk filter")
            z = numpy.array(opt.zpk_z)
            p = numpy.array(opt.zpk_p)
            k = float(opt.zpk_k)
            strain = filter_zpk(strain.astype(numpy.float64), z, p, k)

        if opt.normalize_strain:
            logging.info("Dividing strain by constant")
            l = opt.normalize_strain
            strain = strain / l

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2])

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2])

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (strain * dyn_range_fac).astype(float32)

        if opt.gating_file is not None:
            logging.info("Gating glitches")
            gate_params = numpy.loadtxt(opt.gating_file)
            if len(gate_params.shape) == 1:
                gate_params = [gate_params]
            strain = gate_data(
                strain, gate_params,
                data_start_time=(opt.gps_start_time - opt.pad_data))

        logging.info("Resampling data")
        strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas')

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        logging.info("Remove Padding")
        start = opt.pad_data*opt.sample_rate
        end = len(strain)-opt.sample_rate*opt.pad_data
        strain = strain[start:end]

    if opt.fake_strain:
        logging.info("Generating Fake Strain")
        duration = opt.gps_end_time - opt.gps_start_time
        tlen = duration * opt.sample_rate
        pdf = 1.0/128
        plen = int(opt.sample_rate / pdf) / 2 + 1

        logging.info("Making PSD for strain")
        strain_psd = psd.from_string(opt.fake_strain, plen,
                                     pdf, opt.low_frequency_cutoff)

        logging.info("Making colored noise")
        strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate,
                                            strain_psd,
                                            seed=opt.fake_strain_seed)
        strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time)

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2])

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2])

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (dyn_range_fac * strain).astype(float32)

    if opt.injection_file:
        strain.injections = injections

    return strain
Ejemplo n.º 8
0
# Check that the median bias is the same
print "%s segments" % nsegs
print "BIAS pycbc", pycbc.psd.median_bias(nsegs)
print "BIAS lal", lal.RngMedBias(nsegs)

# Check the psd norm
print "PYCBC psd norm", 2.0 / float(sample_rate) / (
    w_pycbc.squared_norm().sum()) / pycbc.psd.median_bias(nsegs)

# Same strain preparation for lal and pycbc psd estimation
strain = read_frame("LER2.lcf",
                    "H1:LDAS-STRAIN",
                    start_time=start_time - pad_data,
                    duration=duration + pad_data * 2)
strain = highpass(strain, frequency=30)
strain *= pycbc.DYN_RANGE_FAC
strain = TimeSeries(strain,
                    delta_t=strain.delta_t,
                    epoch=strain.start_time,
                    dtype=float32)
strain = resample_to_delta_t(strain, 1.0 / sample_rate)
strain = strain[int(pad_data * 1.0 / strain.delta_t):len(strain) -
                int(1.0 / strain.delta_t * pad_data)]

psd_pycbc = pycbc.psd.welch(strain, seg_len=seg_len, seg_stride=stride)
psd_lal = lal_psd_estimate(seg_len, strain)

print psd_pycbc[-1]
print psd_lal[-1]
Ejemplo n.º 9
0
    def mathched_filtering(self,m1,m2,f_highPass = 15,\
                                      fft_crop = 2,\
                                      psd_interval = 4,\
                                      genWave_f_lowerbound = 20,\
                                      snrCrop = 4):
        #done to avoid loading the data every time when used in a loop
        if self.mergerStrain == None:
            #this methode takes in a duration instead of a time interval
            #This automatically pulls strain data centered around the
            #gps time stamp instead of you specifing it yourself.
            self.mergerStrain = self.getMergerStrain()

        merger = Merger(self.event_id)
        '''
        There is an issue for how the strain data is read using this methode
        when being used with the filter.highpass methode

        Need to find a conversion so that a custome time interval can be used
        when selecting a data set
        '''

        #changing from the class wide strain array to a local one at the same
        #time of performing the highpass filtering.
        strain = filter.highpass(self.mergerStrain, f_highPass)
        strain = filter.resample_to_delta_t(strain, 1.0 / 2048)

        #removing discontinuities errors that form at the end due to resampling
        conditioned = strain.crop(fft_crop, fft_crop)
        #crops off the first
        #and last two seconds
        #generating the psd, thats used in the matched filtering methode
        #the psd is used to weight "the frequency components of the
        #potential signal and data by the noise amplitude"
        psd = conditioned.psd(psd_interval)
        #this matches the psd to our conditioned strain data
        psd = pycbc.psd.interpolate(psd, conditioned.delta_f)
        #this generated a 1/psd that is used to further filter the data
        psd = pycbc.psd.inverse_spectrum_truncation(psd,\
                                        psd_interval*conditioned.sample_rate,\
                                        low_frequency_cutoff=f_highPass)

        #Generating matched filtering waveform
        hp, hc = get_td_waveform(approximant="SEOBNRv4_opt",
                                 mass1=m1,
                                 mass2=m2,
                                 delta_t=conditioned.delta_t,
                                 f_lower=genWave_f_lowerbound)

        #Resizing the matched filtering wave form to the size of the our data
        hp.resize(len(conditioned))
        #shifting the moldeled wave form to the aproximant location of the
        #merger event
        template = hp.cyclic_time_shift(hp.start_time)
        #generating the signal to noise ratio data set
        snr = filter.matched_filter(template,
                                    conditioned,
                                    psd=psd,
                                    low_frequency_cutoff=genWave_f_lowerbound)

        #cropping out the problamatic data points. There are discontinuitie
        #errors at the ends of the interval
        snr = snr.crop(snrCrop + psd_interval, snrCrop)
        snrPeakIndex = abs(snr).numpy().argmax()
        snrPeak = abs(snr)[snrPeakIndex]
        snrPeakTime = snr.sample_times[snrPeakIndex]

        # # Shift the template to the peak time
        # dt = snrPeakTime - conditioned.start_time
        # aligned = template.cyclic_time_shift(dt)
        #
        # # scale the template so that it would have SNR 1 in this data
        # aligned /= sigma(aligned, psd=psd, low_frequency_cutoff=20.0)
        #
        # # Scale the template amplitude and phase to the peak value
        # aligned = (aligned.to_frequencyseries() * snrPeak).to_timeseries()
        # aligned.start_time = conditioned.start_time

        # We do it this way so that we can whiten both the template and the data
        white_data = (conditioned.to_frequencyseries() /
                      psd**0.5).to_timeseries()
        white_data = white_data.highpass_fir(f_highPass * 2,
                                             512).lowpass_fir(230, 512)

        # Select the time around the merger
        white_data = white_data.time_slice(merger.time - .1, merger.time + .1)


        outputFormater = namedtuple('signal_to_noise_ratio_data',\
            ['snr','snrPeakIndex','snrPeak','snrPeakTime','white_data'])
        #returning the signal to noise ratio
        return outputFormater(snr, snrPeakIndex, snrPeak, snrPeakTime,
                              white_data)
Ejemplo n.º 10
0
def highpass_raw_strain(strain2):
    for ifo in ifos:
        strain2[ifo] = highpass(strain2[ifo], 15.0)
    return strain2
Ejemplo n.º 11
0
pylab.xlabel('Time (s)')
pylab.xlim(params.tc - params.plotzoom, params.tc + params.plotzoom)
pylab.ylim([0, 30])

for filename in infile:

    # Load in data files
    file = cwd + '/' + filename
    hoft = pycbc.types.timeseries.load_timeseries(file)

    # =========================
    # = Precondition the data =
    # =========================

    # Remove low frequency noise
    hoft = highpass(hoft, 15.0)

    hoft = resample_to_delta_t(hoft, 1./2048.)

    # Remove 2 seconds from the start and the end to protect against edge effects
    conditioned = hoft.crop(2, 2)

    # Calculate PSD of data
    psd = conditioned.psd(4)
    psd = interpolate(psd, conditioned.delta_f)
    psd = inverse_spectrum_truncation(psd, 4 * conditioned.sample_rate, low_frequency_cutoff=15)

    psdlist[i] = psd


Ejemplo n.º 12
0
def from_cli(opt, dyn_range_fac=1, precision='single'):
    """Parses the CLI options related to strain data reading and conditioning.

    Parameters
    ----------
    opt : object
        Result of parsing the CLI with OptionParser, or any object with the
        required attributes  (gps-start-time, gps-end-time, strain-high-pass, 
        pad-data, sample-rate, (frame-cache or frame-files), channel-name, 
        fake-strain, fake-strain-seed, gating_file).

    dyn_range_fac: {float, 1}, optional
        A large constant to reduce the dynamic range of the strain.

    Returns
    -------
    strain : TimeSeries
        The time series containing the conditioned strain data.
    """

    gating_info = {}

    if opt.frame_cache or opt.frame_files or opt.frame_type:
        if opt.frame_cache:
            frame_source = opt.frame_cache
        if opt.frame_files:
            frame_source = opt.frame_files

        logging.info("Reading Frames")
        
        if opt.frame_type:
            strain = query_and_read_frame(opt.frame_type, opt.channel_name,
                                          start_time=opt.gps_start_time-opt.pad_data,
                                          end_time=opt.gps_end_time+opt.pad_data)
        else:
            strain = read_frame(frame_source, opt.channel_name,
                            start_time=opt.gps_start_time-opt.pad_data,
                            end_time=opt.gps_end_time+opt.pad_data)

        if opt.zpk_z and opt.zpk_p and opt.zpk_k:
            logging.info("Highpass Filtering")
            strain = highpass(strain, frequency=opt.strain_high_pass)

            logging.info("Applying zpk filter")
            z = numpy.array(opt.zpk_z)
            p = numpy.array(opt.zpk_p)
            k = float(opt.zpk_k)
            strain = filter_zpk(strain.astype(numpy.float64), z, p, k)

        if opt.normalize_strain:
            logging.info("Dividing strain by constant")
            l = opt.normalize_strain
            strain = strain / l

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (strain * dyn_range_fac).astype(float32)

        if opt.gating_file is not None:
            logging.info("Gating glitches")
            gate_params = numpy.loadtxt(opt.gating_file)
            if len(gate_params.shape) == 1:
                gate_params = [gate_params]
            strain = gate_data(strain, gate_params)
            gating_info['file'] = \
                    [gp for gp in gate_params \
                     if (gp[0] + gp[1] + gp[2] >= strain.start_time) \
                     and (gp[0] - gp[1] - gp[2] <= strain.end_time)]

        if opt.autogating_threshold is not None:
            # the + 0 is for making a copy
            glitch_times = detect_loud_glitches(
                    strain + 0., threshold=opt.autogating_threshold,
                    cluster_window=opt.autogating_cluster,
                    low_freq_cutoff=opt.strain_high_pass,
                    high_freq_cutoff=opt.sample_rate/2,
                    corrupted_time=opt.pad_data+opt.autogating_pad)
            gate_params = [[gt, opt.autogating_width, opt.autogating_taper] \
                           for gt in glitch_times]
            if len(glitch_times) > 0:
                logging.info('Autogating at %s',
                             ', '.join(['%.3f' % gt for gt in glitch_times]))
            strain = gate_data(strain, gate_params)
            gating_info['auto'] = gate_params

        logging.info("Resampling data")
        strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas')

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        logging.info("Remove Padding")
        start = opt.pad_data*opt.sample_rate
        end = len(strain)-opt.sample_rate*opt.pad_data
        strain = strain[start:end]

    if opt.fake_strain:
        logging.info("Generating Fake Strain")
        duration = opt.gps_end_time - opt.gps_start_time
        tlen = duration * opt.sample_rate
        pdf = 1.0/128
        plen = int(opt.sample_rate / pdf) / 2 + 1

        logging.info("Making PSD for strain")
        strain_psd = pycbc.psd.from_string(opt.fake_strain, plen, pdf,
                                           opt.low_frequency_cutoff)

        logging.info("Making colored noise")
        strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate,
                                            strain_psd,
                                            seed=opt.fake_strain_seed)
        strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time)

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (dyn_range_fac * strain).astype(float32)

    if opt.injection_file or opt.sgburst_injection_file:
        strain.injections = injections

    strain.gating_info = gating_info

    return strain
Ejemplo n.º 13
0
w_pycbc = Array(numpy.hanning(seg_len).astype(float32))
w_lal = lal.CreateHannREAL4Window(seg_len)
print "hann props pycbc", w_pycbc.sum(), w_pycbc.squared_norm().sum()
print "hann props lal", w_lal.sum, w_lal.sumofsquares

# Check that the median bias is the same
print "%s segments" % nsegs
print "BIAS pycbc", pycbc.psd.median_bias(nsegs)
print "BIAS lal", lal.RngMedBias(nsegs)

# Check the psd norm
print "PYCBC psd norm", 2.0 / float(sample_rate) / (w_pycbc.squared_norm().sum()) / pycbc.psd.median_bias(nsegs)

# Same strain preparation for lal and pycbc psd estimation
strain = read_frame("LER2.lcf", "H1:LDAS-STRAIN", start_time=start_time-pad_data, duration=duration+pad_data*2)
strain = highpass(strain, frequency=30)
strain *= pycbc.DYN_RANGE_FAC
strain = TimeSeries(strain, delta_t=strain.delta_t, epoch=strain.start_time, dtype=float32)
strain = resample_to_delta_t(strain, 1.0/sample_rate)
strain = strain[int(pad_data*1.0/strain.delta_t):len(strain)-int(1.0/strain.delta_t*pad_data)]

psd_pycbc = pycbc.psd.welch(strain, seg_len=seg_len, seg_stride=stride)
psd_lal = lal_psd_estimate(seg_len, strain)

print psd_pycbc[-1]
print psd_lal[-1]

pylab.figure(1)
pylab.loglog(psd_pycbc.sample_frequencies.numpy(), psd_pycbc.numpy(), label="PyCBC")
pylab.loglog(psd_lal.sample_frequencies.numpy(), psd_lal.numpy(), label="LAL")
pylab.xlabel("Frequency (Hz)")
"""Cleaning and applying highpass filter. Downsample to 2048 Hz to make the data analysis more convenient. Power density of the noise is higher than the signal. At higher frequency the amplitude of the noise is lower. And then graph."""

from pycbc.catalog import Merger
from pycbc.filter import resample_to_delta_t, highpass

from pycbc.catalog import Merger
from pycbc.filter import resample_to_delta_t, highpass

merger = Merger("GW170817")
strain, stilde = {}, {} # tuple of directories: strain and stilde=cleaned data
for ifo in ['L1', 'H1']:
    # We'll download the data and select 256 seconds that includes the event time
    ts = merger.strain(ifo)
    # Remove the low frequency content and downsample the data to 2048Hz
    strain[ifo] = resample_to_delta_t(highpass(ts, 15.0), 1.0/2048)
    #remove the begining and ending spikes from the data
    strain[ifo] = strain[ifo].crop(2, 2) # remove 2 first and 2 last seconds
    # Also create a frequency domain version of the data
    stilde[ifo] = strain[ifo].to_frequencyseries()

#print (strain.delta_t)
pylab.plot(strain['L1'].sample_times, strain['L1'])
pylab.xlabel('Time (s)')
pylab.show()

"""***part 2***

Then the power spectral density is plotted.
"""
Ejemplo n.º 15
0
def from_cli(opt, dyn_range_fac=1, precision='single'):
    """Parses the CLI options related to strain data reading and conditioning.

    Parameters
    ----------
    opt : object
        Result of parsing the CLI with OptionParser, or any object with the
        required attributes  (gps-start-time, gps-end-time, strain-high-pass, 
        pad-data, sample-rate, (frame-cache or frame-files), channel-name, 
        fake-strain, fake-strain-seed, gating_file).

    dyn_range_fac: {float, 1}, optional
        A large constant to reduce the dynamic range of the strain.

    Returns
    -------
    strain : TimeSeries
        The time series containing the conditioned strain data.
    """

    gating_info = {}

    if opt.frame_cache or opt.frame_files or opt.frame_type:
        if opt.frame_cache:
            frame_source = opt.frame_cache
        if opt.frame_files:
            frame_source = opt.frame_files

        logging.info("Reading Frames")
        
        if opt.frame_type:
            strain = query_and_read_frame(opt.frame_type, opt.channel_name,
                                          start_time=opt.gps_start_time-opt.pad_data,
                                          end_time=opt.gps_end_time+opt.pad_data)
        else:
            strain = read_frame(frame_source, opt.channel_name,
                            start_time=opt.gps_start_time-opt.pad_data,
                            end_time=opt.gps_end_time+opt.pad_data)

        if opt.zpk_z and opt.zpk_p and opt.zpk_k:
            logging.info("Highpass Filtering")
            strain = highpass(strain, frequency=opt.strain_high_pass)

            logging.info("Applying zpk filter")
            z = numpy.array(opt.zpk_z)
            p = numpy.array(opt.zpk_p)
            k = float(opt.zpk_k)
            strain = filter_zpk(strain.astype(numpy.float64), z, p, k)

        if opt.normalize_strain:
            logging.info("Dividing strain by constant")
            l = opt.normalize_strain
            strain = strain / l

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (strain * dyn_range_fac).astype(pycbc.types.float32)

        if opt.gating_file is not None:
            logging.info("Gating glitches")
            gate_params = numpy.loadtxt(opt.gating_file)
            if len(gate_params.shape) == 1:
                gate_params = [gate_params]
            strain = gate_data(strain, gate_params)
            gating_info['file'] = \
                    [gp for gp in gate_params \
                     if (gp[0] + gp[1] + gp[2] >= strain.start_time) \
                     and (gp[0] - gp[1] - gp[2] <= strain.end_time)]

        if opt.autogating_threshold is not None:
            # the + 0 is for making a copy
            glitch_times = detect_loud_glitches(
                    strain + 0., threshold=opt.autogating_threshold,
                    cluster_window=opt.autogating_cluster,
                    low_freq_cutoff=opt.strain_high_pass,
                    high_freq_cutoff=opt.sample_rate/2,
                    corrupt_time=opt.pad_data+opt.autogating_pad)
            gate_params = [[gt, opt.autogating_width, opt.autogating_taper] \
                           for gt in glitch_times]
            if len(glitch_times) > 0:
                logging.info('Autogating at %s',
                             ', '.join(['%.3f' % gt for gt in glitch_times]))
            strain = gate_data(strain, gate_params)
            gating_info['auto'] = gate_params

        logging.info("Resampling data")
        strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas')

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        logging.info("Remove Padding")
        start = opt.pad_data*opt.sample_rate
        end = len(strain)-opt.sample_rate*opt.pad_data
        strain = strain[start:end]

    if opt.fake_strain:
        logging.info("Generating Fake Strain")
        duration = opt.gps_end_time - opt.gps_start_time
        tlen = duration * opt.sample_rate
        pdf = 1.0/128
        plen = int(opt.sample_rate / pdf) / 2 + 1

        logging.info("Making PSD for strain")
        strain_psd = pycbc.psd.from_string(opt.fake_strain, plen, pdf,
                                           opt.low_frequency_cutoff)

        logging.info("Making colored noise")
        strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate,
                                            strain_psd,
                                            seed=opt.fake_strain_seed)
        strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time)

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (dyn_range_fac * strain).astype(pycbc.types.float32)

    if opt.injection_file or opt.sgburst_injection_file:
        strain.injections = injections

    if opt.taper_data:
        logging.info("Tapering data")
        # Use auto-gating stuff for this, a one-sided gate is a taper
        pd_taper_window = opt.taper_data
        gate_params = [(strain.start_time, 0., pd_taper_window)]
        gate_params.append( (strain.end_time, 0.,
                             pd_taper_window) )
        gate_data(strain, gate_params)


    strain.gating_info = gating_info

    return strain
Ejemplo n.º 16
0
plt.figure()
plt.plot(random_waveform.sample_times, random_waveform, label='wf injection location')
plt.legend()
plt.xlabel('Time (s)')
plt.grid()
plt.show()
    
injected_array = np.array(merged_noise_tsc) + np.array(random_waveform)
signal_and_noise = types.timeseries.TimeSeries(injected_array, delta_t=merged_noise_tsc.delta_t)
    
## 6 - Matched filter - condition the noise curve and prepare psd -----------------------------------
    
# # 6.1 Prepatory steps and creating the match template-------------------------------------------------
    
#Highpass the noise curve with injected waveform above 1e-2 Hz
injected_ts_highpass = highpass(signal_and_noise, 0.01)
    
#crop to avoid filter wraparound
conditioned = injected_ts_highpass.crop(2,2)
    
#template
template = hp_ts.copy()
template.resize(np.size(conditioned))
#template = template.cyclic_time_shift(conditioned.duration)
# template = np.roll(template, (np.size(template) - np.size(hp_ts) ))
# template = types.timeseries.TimeSeries(template, delta_t=dt)
    
plt.figure()
plt.plot(template.sample_times, template, label='template rotated for filter')
plt.grid()
plt.legend()
Ejemplo n.º 17
0
# %matplotlib inline
import pylab
from pycbc.filter import highpass
from pycbc.catalog import Merger
from pycbc.frame import read_frame

merger = Merger("GW170817")
import numpy as np
import pycbc.types 

strain, stilde = {}, {}
for ifo in['H1','L1']:
  # We'll download the data and select 256 secondsthat includes the event time
  ts =read_frame("{}-{}_LOSC_CLN_4_V1-1187007040-2048.gwf".format(ifo[0],ifo),'{}:LOSC-STRAIN'.format(ifo),start_time=merger.time -224,end_time=merger.time +32,check_integrity=False)
  # Read the detector data and remove low frequencycontent
  strain[ifo] = highpass(ts,15)
  # Remove time corrupted by the high pass filter
  strain[ifo] = strain[ifo].crop(4,4)
  # Also create a frequency domain version of the data
  stilde[ifo] = strain[ifo].to_frequencyseries()
  #print (strain.delta_t)
  pylab.plot(strain['H1'].sample_times, strain['H1'])
  pylab.xlabel('Time (s)')
  pylab.show()

from pycbc.psd import interpolate, inverse_spectrum_truncation
psds = {}
for ifo in ['L1','H1']:
# Calculate a psd from the data. We'll use 2s segments in a median -welch style estimate# We then interpolate the PSD to the desired frequencystep.
  psds[ifo] = interpolate(strain[ifo].psd(2), stilde[ifo].delta_f)
# We explicitly control how much data will be corruptedbyoverwhitening the data later on# In this case we choose 2 seconds.