48 import lalsimulation
as lalsim
51 from gstlal
import chirptime
54 __author__ =
"Kipp Cannon <kipp.cannon@ligo.org>, Chad Hanna <chad.hanna@ligo.org>, Drew Keppel <drew.keppel@ligo.org>"
68 gstlal_FD_approximants = set((
71 'SEOBNRv2_ROM_DoubleSpin',
74 'TaylorF2RedSpinTidal'
76 gstlal_TD_approximants = set((
83 gstlal_IMR_approximants = set((
87 'SEOBNRv2_ROM_DoubleSpin'
89 gstlal_approximants = gstlal_FD_approximants | gstlal_TD_approximants
91 assert gstlal_IMR_approximants <= gstlal_approximants,
"gstlal_IMR_approximants contains values not listed in gstlal_approximants"
94 def gstlal_valid_approximant(appx_str):
96 lalsim.GetApproximantFromString(str(appx_str))
98 raise ValueError(
"Approximant not supported by lalsimulation: %s" % appx_str)
99 if appx_str
not in gstlal_approximants:
100 raise ValueError(
"Approximant not currently supported by gstlal, but is supported by lalsimulation: %s. Please consider preparing a patch" % appx_str)
105 A tool for generating the quadrature phase of a real-valued
112 >>> q = QuadraturePhase(128) # initialize for 128-sample templates
113 >>> inseries = lal.CreateREAL8TimeSeries(deltaT = 1.0 / 128, length = 128)
114 >>> inseries.data.data = numpy.cos(numpy.arange(128, dtype = "double") * 2 * numpy.pi / 128) # one cycle of cos(t)
115 >>> outseries = q(inseries) # output has cos(t) in real part, sin(t) in imaginary part
119 Initialize. n is the size, in samples, of the templates to
120 be processed. This is used to pre-allocate work space.
123 self.
fwdplan = lal.CreateForwardREAL8FFTPlan(n, 1)
124 self.
revplan = lal.CreateReverseCOMPLEX16FFTPlan(n, 1)
125 self.
in_fseries = lalfft.prepare_fseries_for_real8tseries(lal.CreateREAL8TimeSeries(deltaT = 1.0, length = n))
131 From the Fourier transform of a real-valued function of
132 time, compute and return the Fourier transform of the
133 complex-valued function of time whose real component is the
134 original time series and whose imaginary component is the
135 quadrature phase of the real part. fseries is a LAL
136 COMPLEX16FrequencySeries and n is the number of samples in
137 the original time series.
143 have_nyquist =
not (n % 2)
149 positive_frequencies = numpy.array(fseries.data.data)
150 positive_frequencies[0] = 0
151 zeros = numpy.zeros((len(positive_frequencies),), dtype =
"cdouble")
154 positive_frequencies = positive_frequencies[:-1]
160 out_fseries = lal.CreateCOMPLEX16FrequencySeries(
162 epoch = fseries.epoch,
164 deltaF = fseries.deltaF,
165 sampleUnits = fseries.sampleUnits,
166 length = len(zeros) + len(positive_frequencies) - 1
168 out_fseries.data.data = numpy.concatenate((zeros, 2 * positive_frequencies[1:]))
175 Transform the real-valued time series stored in tseries
176 into a complex-valued time series. The return value is a
177 newly-allocated complex time series. The input time series
178 is stored in the real part of the output time series, and
179 the complex part stores the quadrature phase.
191 tseries = lal.CreateCOMPLEX16TimeSeries(length = self.
n)
201 def normalized_autocorrelation(fseries, revplan):
202 data = fseries.data.data
203 fseries = lal.CreateCOMPLEX16FrequencySeries(
205 epoch = fseries.epoch,
207 deltaF = fseries.deltaF,
208 sampleUnits = fseries.sampleUnits,
211 fseries.data.data = data * numpy.conj(data)
212 tseries = lal.CreateCOMPLEX16TimeSeries(
214 epoch = fseries.epoch,
216 deltaT = 1. / (len(data)*fseries.deltaF),
218 sampleUnits = lal.DimensionlessUnit
220 tseries.data.data = numpy.empty((len(data),), dtype =
"cdouble")
221 lal.COMPLEX16FreqTimeFFT(tseries, fseries, revplan)
222 data = tseries.data.data
223 tseries.data.data = data / data[0]
229 x = int(math.ceil(x))
232 while n
and (x & (x + 1)):
244 samples_max_256 = 1024,
245 samples_max_64 = 2048,
251 The function time_frequency_boundaries splits a template bank up by
252 times for which different sampling rates are appropriate.
254 The function returns a list of 3-tuples of the form (rate,begin,end)
255 where rate is the sampling rate and begin/end mark the boundaries
256 during which the given rate is guaranteed to be appropriate (no
257 template exceeds a frequency of Nyquist/padding during these times).
259 @param sngl_inspiral_rows The snigle inspiral rows for this sub bank
260 @param flow The low frequency to generate the waveform from
261 @param fhigh The maximum frequency for generating the waveform
262 @param padding Allow a template to be generated up to Nyquist / padding for any slice
263 @param samples_min The minimum number of samples to use in any time slice
264 @param samples_max_256 The maximum number of samples to have in any time slice greater than or equal to 256 Hz
265 @param samples_max_64 The maximum number of samples to have in any time slice greater than or equal to 64 Hz
266 @param samples_max The maximum number of samples in any time slice below 64 Hz
267 @param verbose Be verbose
277 allowed_rates = [16384,8192,4096,2048,1024,512,256,128,64,32]
280 sample_rate_min = ceil_pow_2( 2 * padding * flow )
281 if sample_rate
is None:
282 sample_rate_max = ceil_pow_2( 2 * fhigh )
285 sample_rate_max = sample_rate
286 allowed_rates = [rate
for rate
in allowed_rates
if sample_rate_min <= rate <= sample_rate_max]
299 segment_samples_min = max(ceil_pow_2( 2*len(sngl_inspiral_rows) ),samples_min)
307 time_freq_boundaries = []
316 rates_below = [r/(2.*padding)
for r
in allowed_rates[1:]] + [flow]
318 for this_flow, rate
in zip(rates_below, allowed_rates):
320 segment_samples_max = samples_max_256
322 segment_samples_max = samples_max_64
324 segment_samples_max = samples_max
326 if segment_samples_min > segment_samples_max:
327 raise ValueError(
"The input template bank must have fewer than %d templates, but had %d." % (segment_samples_max, 2 * len(sngl_inspiral_rows)))
329 longest_chirp = max(
chirptime.imr_time(this_flow, row.mass1 * lal.MSUN_SI, row.mass2 * lal.MSUN_SI, row.spin1z, row.spin2z)
for row
in sngl_inspiral_rows)
334 if longest_chirp > accum_time:
336 number_large_blocks = int(numpy.floor( rate*(longest_chirp-accum_time) / segment_samples_max ))
337 number_small_blocks = int(numpy.ceil( rate*(longest_chirp-accum_time) / segment_samples_min )
338 - number_large_blocks*segment_samples_max/segment_samples_min)
344 while number_small_blocks > 0:
345 if number_small_blocks % 2 == 1:
346 time_freq_boundaries.append((rate,
348 accum_time+float(2**exponent)*segment_samples_min/rate))
349 accum_time += float(2**exponent)*segment_samples_min/rate
351 number_small_blocks = number_small_blocks >> 1
355 for idx
in range(number_large_blocks):
356 time_freq_boundaries.append((rate,
358 accum_time+(1./rate)*segment_samples_max))
359 accum_time += (1./rate)*segment_samples_max
362 print(
"Time freq boundaries: ", file=sys.stderr)
363 print(time_freq_boundaries, file=sys.stderr)
365 return numpy.array(time_freq_boundaries,dtype=[(
'rate',
'int'),(
'begin',
'float'),(
'end',
'float')])
368 def hplus_of_f(m1, m2, s1, s2, f_min, f_max, delta_f):
369 farr = numpy.linspace(0, f_max, f_max / delta_f + delta_f)
370 spin1 = [0., 0., s1]; spin2 = [0., 0., s2]
371 hp, hc = lalsim.SimInspiralFD(
372 m1 * lal.MSUN_SI, m2 * lal.MSUN_SI,
373 spin1[0], spin1[1], spin1[2],
374 spin2[0], spin2[1], spin2[2],
386 lalsim.GetApproximantFromString(
"IMRPhenomD")
388 assert hp.data.length > 0,
"huh!? h+ has zero length!"
390 return hp.data.data[:len(farr)]
392 def fn(farr, h, n, psd):
393 df = farr[1] - farr[0]
394 return 4 * numpy.sum(numpy.abs(h)**2 * farr**n * df / psd(farr))
396 def moment(farr, h, n, psd):
397 return fn(farr, h, n, psd) / fn(farr, h, 0, psd)
399 def bandwidth(m1, m2, s1, s2, f_min, f_max, delta_f, psd):
400 h = hplus_of_f(m1, m2, s1, s2, f_min, f_max, delta_f)
401 farr = numpy.linspace(0, f_max, f_max / delta_f + delta_f)
402 return (moment(farr, h, 2, psd) - moment(farr, h, 1, psd)**2)**.5