コード例 #1
0
def Matched_Filter(fp, fc, data_p, data_c, flen, delta_f, flow):
    p_s_d = psd.aLIGOZeroDetHighPower(flen, delta_f, flow)
    SNRp = filter.matched_filter(fp,
                                 data_p,
                                 psd=p_s_d,
                                 low_frequency_cutoff=flow)
    SNRc = filter.matched_filter(fc,
                                 data_c,
                                 psd=p_s_d,
                                 low_frequency_cutoff=flow)
    return SNRp, SNRc
コード例 #2
0
    def next(self):
        nseries = []
        stats = []
        for i in range(self.batch):
            stat = {}
            n = self.noise.next()
            p = self.param.draw()
            hp, _ = get_fd_waveform(p, delta_f=n.delta_f,
                                    f_lower=self.noise.flow, **self.param.static)
            hp.resize(len(n))
            sg = sigma(hp, psd=self.noise.psd, low_frequency_cutoff=self.noise.flow)
            n += hp.cyclic_time_shift(p.tc) / sg * p.snr

            # Collect some standard stats we might want
            msnr = matched_filter(hp, n, psd=self.noise.psd,
                                  low_frequency_cutoff=self.noise.flow)
            msnr = msnr.time_slice(p.tc - 1, p.tc + 1)       
            
            params = {k: p[k] for k in p.dtype.names}
            idx = msnr.abs_arg_max()
            csnr = msnr[idx]
            params['csnr'] = csnr
            params['rsnr'] = csnr.real
            params['isnr'] = csnr.imag
            params['snr'] = abs(csnr)
            params['time'] = float(msnr.start_time + idx * msnr.delta_t)

            nseries.append(n)
            stats.append(params)

        self.current_params = stats
        return nseries, self.current_params
コード例 #3
0
    def cache_segment_snrs(self, template, stilde, psd):
        key = (template.params.template_hash, stilde._epoch, hash(psd))
        # key = (template.params.template_hash, hash(stilde), hash(psd))
        if key not in self._segment_snrs_cache:

            mat = pf.matched_filter(template, stilde, psd, self.f_low)
            self._segment_snrs_cache[key] = mat
        return self._segment_snrs_cache[key]
コード例 #4
0
ファイル: utils.py プロジェクト: a-r-williamson/pycbc
def coalign_waveforms(h1, h2, psd=None,
                      low_frequency_cutoff=None,
                      high_frequency_cutoff=None,
                      resize=True):
    """ Return two time series which are aligned in time and phase.

    The alignment is only to the nearest sample point and all changes to the
    phase are made to the first input waveform. Waveforms should not be split
    accross the vector boundary. If it is, please use roll or cyclic time shift
    to ensure that the entire signal is contiguous in the time series.

    Parameters
    ----------
    h1: pycbc.types.TimeSeries
        The first waveform to align.
    h2: pycbc.types.TimeSeries
        The second waveform to align.
    psd: {None, pycbc.types.FrequencySeries}
        A psd to weight the alignment
    low_frequency_cutoff: {None, float}
        The low frequency cutoff to weight the matching in Hz.
    high_frequency_cutoff: {None, float}
        The high frequency cutoff to weight the matching in Hz.
    resize: Optional, {True, boolean}
        If true, the vectors will be resized to match each other. If false,
        they must be the same length and even in length

    Returns
    -------
    h1: pycbc.types.TimeSeries
        The shifted waveform to align with h2
    h2: pycbc.type.TimeSeries
        The resized (if necessary) waveform to align with h1.
    """
    from pycbc.filter import matched_filter
    mlen = ceilpow2(max(len(h1), len(h2)))

    h1 = h1.copy()
    h2 = h2.copy()

    if resize:
        h1.resize(mlen)
        h2.resize(mlen)
    elif len(h1) != len(h2) or len(h2) % 2 != 0:
        raise ValueError("Time series must be the same size and even if you do "
                         "not allow resizing")

    snr = matched_filter(h1, h2, psd=psd,
                         low_frequency_cutoff=low_frequency_cutoff,
                         high_frequency_cutoff=high_frequency_cutoff)

    _, l =  snr.abs_max_loc()
    rotation =  snr[l] / abs(snr[l])
    h1 = (h1.to_frequencyseries() * rotation).to_timeseries()
    h1.roll(l)

    h1 = TimeSeries(h1, delta_t=h2.delta_t, epoch=h2.start_time)
    return h1, h2
コード例 #5
0
ファイル: gw_matched_filter.py プロジェクト: jprieto343/CCSN
def make(conditioned, template, fc, mc, hc, grafica):

    #	We use 4 second samles of our time series in Welch method.
    psd = conditioned.psd(4)

    #	Now that we have the psd we need to interpolate it to match our data
    #	and then limit the filter length of 1 / PSD. After this, we can
    #	directly use this PSD to filter the data in a controlled manner

    psd = interpolate(psd, conditioned.delta_f)

    psd = inverse_spectrum_truncation(psd,
                                      4 * conditioned.sample_rate,
                                      low_frequency_cutoff=fc)

    psd_o = psd

    snr = matched_filter(template,
                         conditioned,
                         psd=psd,
                         low_frequency_cutoff=fc)
    sigmasq = sigma(template, psd, low_frequency_cutoff=fc)

    #print 'Rho_0'
    #print sigmasq

    #	Remove time corrupted by the template filter and the psd filter
    #	We remove 4 seonds at the beginning and end for the PSD filtering
    #	And we remove 4 additional seconds at the beginning to account for
    #	the template length (this is somewhat generous for
    #	so short a template). A longer signal such as from a BNS, would
    #	require much more padding at the beginning of the vector.
    snr = snr.crop(4, 4)

    if grafica == 1:

        pylab.figure('Matched_filter_output')
        pylab.title('Matched filter output')
        pylab.grid()
        pylab.plot(snr.sample_times, abs(snr))

        pylab.ylabel('Signal-to-noise ratio')
        pylab.xlabel('Time (s)')
        pylab.show()

    peak = abs(snr).numpy().argmax()
    snrp = snr[peak]
    time = snr.sample_times[peak]

    #print 'Tiempo de SNR máximo'
    #print time
    #print 'SNR recuperado'
    #print abs(snrp)

    return snr, psd, peak, time, snrp, psd_o, sigmasq
コード例 #6
0
    def next(self):
        images = []
        targets = []
        for i in range(self.batch):
            n = self.noise.next()
            p = self.param.draw()
            hp, _ = get_fd_waveform(p, delta_f=n.delta_f,
                                    f_lower=self.noise.flow, **self.param.static)
            hp.resize(len(n))
            sg = sigma(hp, psd=self.noise.psd, low_frequency_cutoff=self.noise.flow)
            n += hp.cyclic_time_shift(p.tc) / sg * p.snr
            
            msnr = matched_filter(hp, n, psd=self.noise.psd,
                                  low_frequency_cutoff=self.noise.flow)
            snr = abs(msnr.crop(self.whitening_truncation,
                                self.whitening_truncation)).max()

            n = n.to_timeseries()
            w = n.whiten(self.whitening_truncation, self.whitening_truncation)

            dt = self.duration / float(self.image_dim[0])
            fhigh = self.noise.sample_rate * 0.3
            t, f, p = w.qtransform(dt, logfsteps=self.image_dim[1],
                                       frange=(self.noise.flow, fhigh),
                                       qrange=(self.q, self.q),
                                       return_complex=True)

            kmin = int((w.duration / 2 - self.duration / 2) / dt)
            kmax = kmin + int(self.duration / dt)
            p = p[:, kmin:kmax].transpose()
            
            amp = numpy.abs(p)
            p = numpy.stack([p.real, p.imag, amp], axis=2)
            images.append(p)
            targets.append(snr)
        
        return numpy.stack(images, axis=0), numpy.array(targets)
コード例 #7
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)
コード例 #8
0
ファイル: pycbc-snr-5.py プロジェクト: gwpy/gwpy.github.io
import numpy
from pycbc.filter import matched_filter
snr = matched_filter(hp, zoom.to_pycbc(), psd=psd.to_pycbc(),
                     low_frequency_cutoff=15)
snrts = TimeSeries.from_pycbc(snr).abs()
コード例 #9
0
    def run(self):

        proc_name = self.name

        while True:

            # Get next task to be completed from the queue
            next_task = self._task_queue.get()
            if next_task is None:
                # This poison pil means shutdown
                LOGGER.info("{}: Exiting".format(proc_name))
                break

            results = list()

            # Initialise parameters from task queue to generate SNR time-series
            mass1 = next_task["mass1"]
            mass2 = next_task["mass2"]
            spin1z = next_task["spin1z"]
            spin2z = next_task["spin2z"]
            ra = next_task["ra"]
            dec = next_task["dec"]
            coa_phase = next_task["coa_phase"]
            inclination = next_task["inclination"]
            polarization = next_task["polarization"]
            injection_snr = next_task["injection_snr"]
            f_low = next_task["f_low"]
            approximant = next_task["approximant"]
            delta_t = next_task["delta_t"]
            index = next_task["index"]
            det_string = next_task["det_string"]
            strain_sample = next_task["strain_sample"]

            print("Generating optimal SNR time series: " + det_string +
                  " - sample" + str(index))

            # Convert sample to PyCBC time series
            strain_time_series = TimeSeries(strain_sample,
                                            delta_t=delta_t,
                                            epoch=0,
                                            dtype=None,
                                            copy=True)

            # Convert sample to PyCBC frequency series
            strain_freq_series = strain_time_series.to_frequencyseries()

            # Generate optimal matched filtering template
            template_hp, template_hc = get_td_waveform(
                approximant=approximant,
                mass1=mass1,
                mass2=mass2,
                spin1z=spin1z,
                spin2z=spin2z,
                ra=ra,
                dec=dec,
                coa_phase=coa_phase,
                inclination=inclination,
                f_lower=f_low,
                delta_t=delta_t,
            )

            # Convert template to PyCBC frequency series
            template_freq_series_hp = template_hp.to_frequencyseries(
                delta_f=strain_freq_series.delta_f)

            # Resize template to work with the sample
            template_freq_series_hp.resize(len(strain_freq_series))

            # Time shift the template so that the SNR peak matches the merger time
            template_freq_series_hp = template_freq_series_hp.cyclic_time_shift(
                template_freq_series_hp.start_time)

            # Compute SNR time-series from optimal matched filtering template
            snr_series = matched_filter(template_freq_series_hp,
                                        strain_freq_series.astype(complex),
                                        psd=None,
                                        low_frequency_cutoff=f_low)

            results.append({
                "snr_strain": np.array(abs(snr_series)),
                "mass1": mass1,
                "mass2": mass2,
                "spin1z": spin1z,
                "spin2z": spin2z,
                "ra": ra,
                "dec": dec,
                "coa_phase": coa_phase,
                "inclination": inclination,
                "polarization": polarization,
                "injection_snr": injection_snr,
                "index": index,
                "det_string":
                det_string,  # Unsure I need this for when I store files, using it and index to provide store locations later on
            })

            # Put the results on the results queue
            if len(results) >= 1:
                for result in results:
                    self._result_queue.put(result)

        # Add a poison pill
        self._result_queue.put(None)
コード例 #10
0
    def run(self):

        proc_name = self.name

        while True:

            next_task = self._template_task_queue.get()
            if next_task is None:
                # This poison pil means shutdown
                LOGGER.info("{}: Exiting".format(proc_name))
                break

            template_results = list()

            f_low = next_task["f_low"]
            delta_t = next_task["delta_t"]
            template = next_task["template"]
            sample_index = next_task["sample_index"]
            template_index = next_task["template_index"]
            det_string = next_task["det_string"]
            strain_sample = next_task["strain_sample"]
            sample_type = next_task["sample_type"]
            delta_f = next_task["delta_f"]
            template_start_time = next_task["template_start_time"]

            print("Generating SNR time series: " + det_string + " - sample" +
                  str(sample_index) + ", template" + str(template_index))

            template_time_series = TimeSeries(template,
                                              delta_t=delta_t,
                                              epoch=0,
                                              dtype=None,
                                              copy=True)
            template_freq_series = template_time_series.to_frequencyseries(
                delta_f=delta_f)

            strain_sample_time_series = TimeSeries(strain_sample,
                                                   delta_t=delta_t,
                                                   epoch=0,
                                                   dtype=None,
                                                   copy=True)
            strain_freq_series = strain_sample_time_series.to_frequencyseries(
                delta_f=delta_f)

            template_freq_series.resize(len(strain_freq_series))

            # Time shift the template so that the SNR peak matches the merger time
            template_freq_series = template_freq_series.cyclic_time_shift(
                template_start_time)

            # Compute SNR time-series from optimal matched filtering template
            snr_series = matched_filter(template_freq_series,
                                        strain_freq_series.astype(complex),
                                        psd=None,
                                        low_frequency_cutoff=f_low)

            template_results.append({
                "snr_strain": np.array(abs(snr_series)),
                "sample_index": sample_index,
                "template_index": template_index,
                "det_string": det_string,
                "sample_type": sample_type,
            })

            if len(template_results) >= 1:
                for result in template_results:
                    self._template_result_queue.put(result)

        # Add a poison pill
        self._template_result_queue.put(None)
コード例 #11
0
ファイル: gw150914_h1_snr.py プロジェクト: bema-ligo/pycbc
import urllib

# Read data and remove low frequency content
fname = 'H-H1_LOSC_4_V2-1126259446-32.gwf'
url = "https://losc.ligo.org/s/events/GW150914/" + fname
urllib.urlretrieve(url, filename=fname)
h1 = read_frame('H-H1_LOSC_4_V2-1126259446-32.gwf', 'H1:LOSC-STRAIN')
h1 = highpass_fir(h1, 15, 8)

# Calculate the noise spectrum
psd = interpolate(welch(h1), 1.0 / 32)

# Generate a template to filter with
hp, hc = get_fd_waveform(approximant="IMRPhenomD", mass1=40, mass2=32, 
                         f_lower=20, delta_f=1.0/32)
hp.resize(len(h1) / 2 + 1)

# Calculate the complex (two-phase SNR)
snr = matched_filter(hp, h1, psd=psd, low_frequency_cutoff=20.0)

# Remove regions corrupted by filter wraparound 
snr = snr[len(snr) / 4: len(snr) * 3 / 4]

import pylab
pylab.plot(snr.sample_times, abs(snr))
pylab.ylabel('signal-to-noise')
pylab.xlabel('GPS Time (s)')
pylab.show()


コード例 #12
0
Snr_list = []
for i in range(len(mass1)):
    #Generate a waveform with a given component mass; assumed equal mass, nonspinning
 
    m = mass1[i] # Solar masses
    hp, hc = get_td_waveform(approximant="TaylorT4",
                     mass1=m,
                     mass2=m,
                     delta_t=strain[ifo].delta_t,
                     f_lower=25.0)

    # We will resize the vector to match our data
    hp.resize(len(strain[ifo]))
    template = hp.cyclic_time_shift(hp.start_time)
    
    snr = matched_filter(template, strain[ifo],
                     psd=psds[ifo], low_frequency_cutoff=15.0)

    # Remove time corrupted by the template filter and the psd filter
    # We remove 4 seonds at the beginning and end for the PSD filtering
    # And we remove 4 additional seconds at the beginning to account for
    # the template length (this is somewhat generous for 
    # so short a template). A longer signal such as from a BNS, would 
    # require much more padding at the beginning of the vector.
    snr_H1 = snr.crop(4 + 4, 4)


    Snr_list.append(max(abs(snr_H1)))
snrs[ifo] = Snr_list


コード例 #13
0

#grace_psd = psd.inverse_spectrum_truncation(noise_psd, int((dt*seg_size) * conditioned.sample_rate), low_frequency_cutoff=0.01)
grace_psd = psd.interpolate(noise_psd, conditioned.delta_f)

plt.figure()
plt.loglog(noise_psd.sample_frequencies, np.sqrt(noise_psd), label='noise model asd')
plt.loglog(grace_psd.sample_frequencies, np.sqrt(grace_psd), label='interpolated noise curve')

plt.grid()
plt.legend()
plt.show()


#matched_filter
snr1 = matched_filter(template, conditioned,  psd=grace_psd, low_frequency_cutoff=0.01) 
    
snr1 = snr1.crop(10, 10)
    
#Viewing matched filter snr timeseries
plt.figure()
plt.plot(snr1.sample_times, abs(snr1), label='abs snr')
plt.ylabel('Signal-to-noise')
plt.xlabel('Time (s)')
plt.grid()
plt.legend()
plt.show()
    
# print('hp_ts.size' , np.size(hp_ts))

# if seg_size > np.size(hp_ts):
コード例 #14
0
def coalign_waveforms(h1,
                      h2,
                      psd=None,
                      low_frequency_cutoff=None,
                      high_frequency_cutoff=None,
                      resize=True):
    """ Return two time series which are aligned in time and phase.

    The alignment is only to the nearest sample point and all changes to the
    phase are made to the first input waveform. Waveforms should not be split
    accross the vector boundary. If it is, please use roll or cyclic time shift
    to ensure that the entire signal is contiguous in the time series.

    Parameters
    ----------
    h1: pycbc.types.TimeSeries
        The first waveform to align.
    h2: pycbc.types.TimeSeries
        The second waveform to align.
    psd: {None, pycbc.types.FrequencySeries}
        A psd to weight the alignment
    low_frequency_cutoff: {None, float}
        The low frequency cutoff to weight the matching in Hz.
    high_frequency_cutoff: {None, float}
        The high frequency cutoff to weight the matching in Hz.
    resize: Optional, {True, boolean}
        If true, the vectors will be resized to match each other. If false,
        they must be the same length and even in length

    Returns
    -------
    h1: pycbc.types.TimeSeries
        The shifted waveform to align with h2
    h2: pycbc.type.TimeSeries
        The resized (if necessary) waveform to align with h1.
    """
    from pycbc.filter import matched_filter
    mlen = ceilpow2(max(len(h1), len(h2)))

    h1 = h1.copy()
    h2 = h2.copy()

    if resize:
        h1.resize(mlen)
        h2.resize(mlen)
    elif len(h1) != len(h2) or len(h2) % 2 != 0:
        raise ValueError(
            "Time series must be the same size and even if you do "
            "not allow resizing")

    snr = matched_filter(h1,
                         h2,
                         psd=psd,
                         low_frequency_cutoff=low_frequency_cutoff,
                         high_frequency_cutoff=high_frequency_cutoff)

    _, l = snr.abs_max_loc()
    rotation = snr[l] / abs(snr[l])
    h1 = (h1.to_frequencyseries() * rotation).to_timeseries()
    h1.roll(l)

    h1 = TimeSeries(h1, delta_t=h2.delta_t, epoch=h2.start_time)
    return h1, h2
コード例 #15
0
    print "Match between map and injected is %.2f" % m
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ylim)
    ax.text(xmin,
            ylim[0],
            'Match=%.2f' % m,
            ha='left',
            va='bottom',
            fontsize=12)
    ax.set_ylabel('{} whitened strain'.format(ifo))
    if ii == 2:
        ax.set_xlabel('GPS time - {} (s)'.format(gps_time))

    ## Find and save SNR
    snr = matched_filter(
        ti, y, psd=psd, low_frequency_cutoff=20.0
    )  ## Should be SNR between injected waveform and whitened data
    snr_map = matched_filter(ts, y, psd=psd, low_frequency_cutoff=20.0
                             )  ## SNR between MAP waveform and whitened data

    # Remove regions corrupted by filter wraparound
    snr = snr[len(snr) / 4:len(snr) * 3 / 4]
    snr_map = snr[len(snr_map) / 4:len(snr_map) * 3 / 4]
    snr_list.append(snr)
    snr_list.append(snr_map)
'''
savename=opts.output_file
print savename
figname=savename[:-2]+"_SNR"
## Plot and save SNRs
print "saving SNR figures"
コード例 #16
0
def fake_noisyGW(tab_waveform, seed, run, fs):
    nbr_sec = 15
    nbr_sec_begin = 7
    nbr_sec_end = 8
    NFFT = 1 * fs
    fmin = 15
    fmax = 200000
    print("########################")
    print("creation of noisy signal")
    tab_whiten = []
    tab_snr = []
    random.seed(seed)
    for i in tqdm(range(0, len(tab_waveform))):
        #creation of the fake noise
        seed_local = random.randint(1, 1000)
        fake_noise = fake_gaussian_noise(nbr_sec, seed_local, fs, fmin)
        # injection of GW into the noise
        noisyGW = np.empty([len(fake_noise)])
        # creation of the template (for SNR)
        template = np.zeros([len(fake_noise)
                             ])  # idem pure_GW but with GW in the end(for SNR)
        # creation of pureGW (no use exept plot)
        pureGW = np.zeros([len(fake_noise)])  #pure GW in nbr_sec of 0
        # injection of GW into noise
        k = 0
        for j in range(0, len(fake_noise)):
            if j >= fs * nbr_sec_begin and j < fs * nbr_sec_end:
                noisyGW[j] = fake_noise[j] + tab_waveform[i][0][k]
                pureGW[j] = tab_waveform[i][0][k]
                k = k + 1
            else:
                noisyGW[j] = fake_noise[j]
                pureGW[j] = 0
        # creation of the template
        k = 0
        for j in range(0, len(fake_noise)):
            if j < len(fake_noise) - len(tab_waveform[i][0]):
                template[j] = 0
            else:
                template[j] = tab_waveform[i][0][k]
                k += 1

        ########## the calcule of the SNR ##############
        # First we have to calculate the SNR (the following step is neccessary)
        # we calculate the SNR of noisy GW
        noisyGW_timeseries = TimeSeries(noisyGW, delta_t=tab_waveform[i][17])
        # we need to calculate again the psd
        # we use just 4 sec of data to calulate psd (weltch method)
        psd = noisyGW_timeseries.psd(4)
        # we need to interpolate our psd on all data
        psd = interpolate(psd, noisyGW_timeseries.delta_f)
        # we need to inform that the noise was creat with a lowfrequency cutoff
        psd = inverse_spectrum_truncation(psd,
                                          4 * noisyGW_timeseries.sample_rate,
                                          low_frequency_cutoff=fmin)

        #Second we calcule the SNR
        # we calculate the SNR
        template_timeseries = TimeSeries(template, delta_t=tab_waveform[i][17])
        noisyGW_timeseries = TimeSeries(noisyGW, delta_t=tab_waveform[i][17])
        if template.max(0) == 0:
            snrp = 0
            tab_snr = tab_snr + [snrp]
        else:
            snr = matched_filter(template_timeseries,
                                 noisyGW_timeseries,
                                 psd=psd,
                                 low_frequency_cutoff=fmin)
            #  4+1 sec remove at the begining of SNR and 4 seconde remove at the end
            # +1 is because the length of the template is one sec
            snr = snr.crop(4 + 1, 4)
            peak = abs(snr).numpy().argmax()
            snrp = snr[peak]
            tab_snr = tab_snr + [abs(snrp)]

        # on fait le withening
        # I) methode pycbc
        # data and then flattening the frequency response.
        # (1) The first option sets the duration in seconds of each
        #     sample of the data used as part of the PSD estimate.
        # (2) The second option sets the duration of the filter to apply
        #whitened = noisyGW_timeseries.whiten(15, 4)
        # on fait aussi un bandpass
        #whitened_bp = whitened.highpass_fir(30, 512).lowpass_fir(250, 512)
        # le whitening pycbc donne meme chose mais modifie la longueur
        # par facilite je reprends l autre whitening

        # II) method numpy
        Pxx, freqs = mlab.psd(noisyGW,
                              Fs=freq,
                              NFFT=NFFT,
                              noverlap=NFFT / 2,
                              window=np.blackman(NFFT))
        # We will use interpolations of the ASDs computed above for whitening:
        psd_numpy = interp1d(freqs, Pxx)
        whitened = whitening(noisyGW, psd_numpy, tab_waveform[i][17])
        # We need to suppress the high frequencies with some bandpassing:
        high_freq = 600.
        low_freq = fmin
        bb, ab = butter(4, [low_freq * 2. / fs, high_freq * 2. / fs],
                        btype='band')
        whitened_bp = filtfilt(bb, ab, whitened)

        # Select the secon containing the GW
        withen_bp_onesec = np.empty([fs])
        for j in range(0, len(withen_bp_onesec)):
            withen_bp_onesec[j] = whitened_bp[nbr_sec_begin * fs + j]
        tab_whiten = tab_whiten + [withen_bp_onesec]
        '''
		######################################################################
		### PLOT this is no necessary is just to be sure everithing is OK ###
		# just some  plot to see if everything looks fine
		temps = np.empty([len(fake_noise)])
		for tps in range(0, len(fake_noise)):
			temps[tps] = tps

		temps_onesec = np.empty([len(tab_waveform[i][0])])
		for tps in range(0, len(tab_waveform[i][0])):
			temps_onesec[tps] = tps
		
		# noisy GW
		plt.figure("noisyGW")
		plt.plot(temps, noisyGW, 'b')
		plt.plot(temps, pureGW, 'r')
		plt.show()
		# template 
		plt.figure("template")
		plt.plot(temps, template, 'r')
		plt.show()
		# the GW
		plt.figure("template")
		plt.plot(temps_onesec, tab_waveform[i][0], 'r')
		plt.show()	
		# psd 
		plt.figure("psd")
		plt.loglog(psd.sample_frequencies, psd)
		pylab.xlim(10, 2048)
		pylab.show()
		# SNR
		plt.figure("SNR")
		pylab.plot(snr.sample_times, abs(snr))
		pylab.grid()
		pylab.show()
		# Whitening
		whitened_bp = TimeSeries(whitened_bp, delta_t=tab_waveform[i][17])
		plt.figure("whitening")
		pylab.plot(whitened_bp.sample_times, whitened_bp)
		#pylab.xlim(7,8)
		pylab.grid()
		pylab.show()
		#whitening onesec
		whiten_bp_onesec_timeseries = TimeSeries(withen_bp_onesec, delta_t=tab_waveform[i][17])
		plt.figure("whitening_one_sec")
		pylab.plot(whiten_bp_onesec_timeseries.sample_times, whiten_bp_onesec_timeseries)
		pylab.xlim(0.6, 1.0)
		pylab.grid()
		pylab.show()
		###################################################################
		'''
    tab_snr = np.asarray(tab_snr)
    tab_whiten = np.asarray(tab_whiten)
    # we create the big array (with all the parameters,...)
    tab_waveformnoisy = copy.deepcopy(tab_waveform)
    for i in range(0, len(tab_waveformnoisy)):
        tab_waveformnoisy[i][0] = tab_whiten[i]

    return tab_waveformnoisy, tab_snr
コード例 #17
0
pylab.xlabel('Time (s)')
pylab.ylabel('Strain')
pylab.title('Injected Waveform')

zoom_signal = signal.time_slice(merger_time - 0.5, merger_time + 0.5)
zoom_waveform = waveform.time_slice(merger_time - 0.5, merger_time + 0.5)
pylab.figure(figsize=(10, 5))
pylab.plot(zoom_signal.sample_times, zoom_signal, label='Signal')
pylab.plot(zoom_waveform.sample_times, zoom_waveform, label='Waveform')
pylab.xlabel('Time (s)')
pylab.ylabel('Strain')
pylab.title('Zoomed View')
pylab.legend()

hp.resize(time_duration * f_sample)
snr = matched_filter(hp, signal, psd=psd, low_frequency_cutoff=20)

pylab.figure(figsize=[10, 4])
pylab.plot(snr.sample_times, abs(snr))
pylab.ylabel('Signal-to-noise')
pylab.xlabel('Time (s)')
pylab.show()

peak = abs(snr).numpy().argmax()
snrp = snr[peak]
time = snr.sample_times[peak]

print("We found a signal at {}s with SNR {}".format(time, abs(snrp)))
''''
from synthesizer import GWsynthesizer
hp, hc = get_td_waveform(approximant='SEOBNRv4_opt', mass1=mass, mass2=mass, delta_t= delta_t, f_lower = 25, distance=100)
コード例 #18
0
# match_roll = np.roll(match_template, -np.size(match_template)/4 )
# match_template = types.timeseries.TimeSeries(match_roll, delta_t=conditioned.delta_t)

# plt.figure()
# plt.plot(match_template.sample_times, match_template, label='match template rolled')
# plt.xlabel('time (s)')
# plt.legend()
# plt.show()

#Check properties and Perform the Matched filtering
# print('match template, conditioned data, grace_psd:', 'sizes:', np.size(match_template), np.size(conditioned), np.size(grace_psd),
#       'dt:', match_template.delta_t, conditioned.delta_t, grace_psd.delta_t, 
#       'df:', match_template.delta_f, conditioned.delta_f, grace_psd.delta_f)

# 6.2 - Perform the matched filter via pycbc's filter module-------------------------------------------------------------------------
snr1 = matched_filter(match_template, conditioned, psd=grace_psd) #psd=signal_and_noise_psd)

snr1 = snr1.crop(10, 10)

#Viewing matched filter snr timeseries
#plt.figure()
#plt.plot(snr1.sample_times, abs(snr1), label='abs snr')
#plt.ylabel('Signal-to-noise')
#plt.xlabel('Time (s)')
#plt.grid()
#plt.legend()
#plt.show()



#Below here is my shit doesn't work and I'm trying to see why section :)
コード例 #19
0
# Read data and remove low frequency content
fname = 'H-H1_LOSC_4_V2-1126259446-32.gwf'
url = "https://www.gw-openscience.org/GW150914data/" + fname
urllib.urlretrieve(url, filename=fname)
h1 = read_frame('H-H1_LOSC_4_V2-1126259446-32.gwf', 'H1:LOSC-STRAIN')
h1 = highpass_fir(h1, 15, 8)

# Calculate the noise spectrum
psd = interpolate(welch(h1), 1.0 / h1.duration)

# Generate a template to filter with
hp, hc = get_fd_waveform(approximant="IMRPhenomD",
                         mass1=40,
                         mass2=32,
                         f_lower=20,
                         delta_f=1.0 / h1.duration)
hp.resize(len(h1) / 2 + 1)

# Calculate the complex (two-phase SNR)
snr = matched_filter(hp, h1, psd=psd, low_frequency_cutoff=20.0)

# Remove regions corrupted by filter wraparound
snr = snr[len(snr) / 4:len(snr) * 3 / 4]

import pylab
pylab.plot(snr.sample_times, abs(snr))
pylab.ylabel('signal-to-noise')
pylab.xlabel('GPS Time (s)')
pylab.show()
コード例 #20
0
def worker(kwargs):
    #print("Worker here!")
    #print("Args: {}".format(kwargs))
    full_kwargs = dict(kwargs)
    kwargs = dict(kwargs)

    opt_arg = {}
    opt_keys = [
        'snr', 'gw_prob', 'random_starting_time', 'resample_delta_t', 't_len',
        'resample_t_len', 'time_offset', 'whiten_len', 'whiten_cutoff',
        't_from_right', 'no_gw_snr'
    ]

    for key in opt_keys:
        try:
            opt_arg[key] = kwargs.get(key)
            del kwargs[key]
        except KeyError:
            print("The necessary argument '%s' was not supplied in '%s'" %
                  (key, str(__file__)))

    projection_arg = {}
    projection_arg['end_time'] = 1337 * 137 * 42
    projection_arg['declination'] = 0.0
    projection_arg['right_ascension'] = 0.0
    projection_arg['polarization'] = 0.0
    projection_arg['detectors'] = ['L1', 'H1']

    projection_arg, kwargs = filter_keys(projection_arg, kwargs)

    T_SAMPLES = int(opt_arg['t_len'] / kwargs['delta_t'])
    DELTA_F = 1.0 / opt_arg['t_len']
    F_LEN = int(2.0 / (DELTA_F * kwargs['delta_t']))

    gw_present = bool(random() < opt_arg['gw_prob'])

    psd = generate_psd(**full_kwargs)
    #TODO: Generate the seed for this prior to parallelizing
    noise_list = [
        noise_from_psd(length=T_SAMPLES,
                       delta_t=kwargs['delta_t'],
                       psd=psd,
                       seed=randint(0, 100000))
        for d in projection_arg['detectors']
    ]

    #print("Pre GW generation")

    if gw_present:
        #Generate waveform
        #print("Pre waveform")
        hp, hc = get_td_waveform(**kwargs)

        #Project it onto the considered detectors (This could be handeled using)
        #a list, to make room for more detectors
        #print("Pre projection")
        strain_list = detector_projection(TimeSeries(hp), TimeSeries(hc),
                                          **projection_arg)

        #Enlarge the signals bya adding zeros infront and after. Take care of a
        #random timeshift while still keeping the relative timeshift between
        #detectors
        #TODO: This should also be set outside
        if opt_arg['random_starting_time']:
            t_offset = opt_arg['time_offset']
        else:
            t_offset = 0.0

        #print("Pre embedding in zero")
        set_temp_offset(strain_list, opt_arg['t_len'], t_offset,
                        opt_arg['t_from_right'])

        #Rescale the templates to match wanted SNR
        strain_list = rescale_to_snr(strain_list, opt_arg['snr'], psd,
                                     kwargs['f_lower'])
    else:
        strain_list = [
            TimeSeries(np.zeros(len(n)), n.delta_t) for n in noise_list
        ]
        opt_arg['snr'] = opt_arg['no_gw_snr']

    #print("post generating")
    total_white = []
    matched_snr_sq = []

    #print("Pre loop")
    tmp_white = []
    for i, noise in enumerate(noise_list):
        #print("Loop i: {}".format(i))
        #Add strain to noise
        noise._epoch = strain_list[i]._epoch
        #print("Post epoch, pre adding")
        total = TimeSeries(noise + strain_list[i])

        #print("Post adding, pre whiten")

        #Whiten the total data, downsample and crop the data
        total_white.append(
            total.whiten(opt_arg['whiten_len'],
                         opt_arg['whiten_cutoff'],
                         low_frequency_cutoff=kwargs['f_lower']))
        #print("Post whiten and appending, pre resampling")
        if isinstance(opt_arg['resample_delta_t'], tuple):
            if isinstance(opt_arg['resample_t_len'], tuple) and len(
                    opt_arg['resample_delta_t']) == len(
                        opt_arg['resample_t_len']):
                rdt = opt_arg['resample_delta_t']
                resample_samples = [
                    int(opt_arg['resample_t_len'][idx] / kwargs['delta_t'])
                    for idx in range(len(rdt))
                ]
                for idx, sam in enumerate(resample_samples):
                    #print("Sam: {}".format(sam))
                    #print("Len list: {}".format(len(total_white[i])))
                    #print(i)
                    #print("Resample delta t: {}".format(rdt[idx]))
                    #print("resample_t_len: {}".format(opt_arg['resample_t_len'][idx]))
                    #print("")
                    tmp_white.append(
                        resample_to_delta_t(
                            total_white[i][len(total_white[i]) - sam:],
                            rdt[idx]))
            else:
                raise ValueError(
                    "The options 'resample_delta_t' and 'resample_t_len' have to either both be floats or tuples of floats of the same length."
                )
                #Error or handle
        else:
            total_white[i] = resample_to_delta_t(total_white[i],
                                                 opt_arg['resample_delta_t'])
            #print("Post resampling, pre cropping")
            mid_point = (total_white[i].end_time +
                         total_white[i].start_time) / 2
            total_white[i] = total_white[i].time_slice(
                mid_point - opt_arg['resample_t_len'] / 2,
                mid_point + opt_arg['resample_t_len'] / 2)
        #print("Post cropping, pre matched filtering")
        #print("Strain list: {}\ntotal: {}\nPSD: {}".format(strain_list[i], total, psd))

        #test = matched_filter(strain_list[i], total, psd=psd, low_frequency_cutoff=kwargs['f_lower'])
        #print("Can calc")

        #Calculate matched filter snr
        if gw_present:
            matched_snr_sq.append(
                max(
                    abs(
                        matched_filter(
                            strain_list[i],
                            total,
                            psd=psd,
                            low_frequency_cutoff=kwargs['f_lower'])))**2)
        else:
            #TODO: Implement matched filtering against template bank
            matched_snr_sq.append(opt_arg['no_gw_snr']**2 / len(noise_list))
        #print("Post matched filtering, WTF!")

    if not len(tmp_white) == 0:
        total_white = tmp_white

    del total
    del strain_list

    #Calculate the total SNR of all detectors
    calc_snr = np.sqrt(sum(matched_snr_sq))
    del matched_snr_sq

    #out_wav = []
    #for i, dat in enumerate(total_white):
    #print("Length of total_white[{}]: {}".format(i, len(dat)))
    #for i in range(len(total_white[0])):
    ##print("Length of {}: {}".format(i, len(total_white[i])))
    #tmp = []
    #for j, dat in enumerate(total_white):
    #sys.stdout.write("\ri = {}| j = {}  ".format(i, j))
    #sys.stdout.flush()
    #tmp.append(dat[i])
    #sys.stdout.write('\n')
    #sys.stdout.flush()
    ##print("\n")
    #out_wav.append(tmp)

    out_wav = [[dat[i] for dat in total_white]
               for i in range(len(total_white[0]))]

    #print("Pre return")

    return ((np.array(out_wav), np.array([opt_arg['snr'],
                                          int(gw_present)
                                          ]), np.array(calc_snr),
             np.array(str(kwargs)), np.array(str(opt_arg))))
コード例 #21
0
ファイル: pycbc-snr.py プロジェクト: rngeorge/gwpy
from pycbc.waveform import get_fd_waveform
hp, _ = get_fd_waveform(approximant="IMRPhenomD",
                        mass1=40,
                        mass2=32,
                        f_lower=20,
                        f_final=2048,
                        delta_f=psd.df.value)

# At this point we are ready to calculate the SNR, so we import the
# :func:`pycbc.filter.matched_filter
# <pycbc.filter.matchedfilter.matched_filter>` method, and pass it
# our template, the data, and the PSD:

from pycbc.filter import matched_filter
snr = matched_filter(hp,
                     zoom.to_pycbc(),
                     psd=psd.to_pycbc(),
                     low_frequency_cutoff=15)
snrts = TimeSeries.from_pycbc(snr).abs()

# .. note::
#
#    Here we have used the :meth:`~TimeSeries.to_pycbc` methods of the
#    `~gwpy.timeseries.TimeSeries` and `~gwpy.frequencyseries.FrequencySeries`
#    objects to convert from GWpy objects to something that PyCBC functions
#    can understand, and then used the :meth:`~TimeSeries.from_pycbc` method
#    to convert back to a GWpy object.

# We can plot the SNR `TimeSeries` around the region of interest:
plot = snrts.plot()
ax = plot.gca()
ax.set_xlim(1126259461, 1126259463)
コード例 #22
0
# We will try different component masses and see whichgives us thelargest
masses = numpy.arange(1.3,1.5,.01)
# Variables to store when we've found the max
hmax, smax, tmax, mmax, nsnr = None, {}, {},0,0
snrs = []
for m in masses:
  #Generate a waveform with a given component mass;assumed equal mass,nonspinning
  hp, hc = get_fd_waveform(approximant="TaylorF2",
                           mass1=m, mass2=m,
                           f_lower=20, 
                           delta_f=stilde[ifo].delta_f)
  hp.resize(len(stilde[ifo]))
  # Matched filter the data and find the peak
  max_snr, max_time = {}, {}
  for ifo in ['L1','H1']:
    snr = matched_filter(hp, stilde[ifo], psd=psds[ifo],low_frequency_cutoff=20.0)
    # The complex SNR at the peak
    snr = snr.time_slice(merger.time -1, merger.time+1)
    _, idx = snr.abs_max_loc()
    max_snr[ifo] = snr[idx]
    # The time of the peak
    max_time[ifo] = float(idx) / snr.sample_rate+ snr.start_time
  network_snr = (abs(numpy.array(list(max_snr.values())))**2.0).sum()**0.5
  snrs.append(max_snr)
  # Keep track of only the loudest peak
  if network_snr > nsnr:
    tmax, hmax, mmax, smax = max_time, hp, m, max_snr
    nsnr = network_snr
    # See the SNR as a function of the component mass.Notice where this peaksas it gives us
    # an estimate of what the parameters of the sourcesystem are. Note thatmasses
    # here are in the *detector* frame, so if the sourceis located far away,it will in# fact correspond to a lighter system due to cosmologicalredshift.