Skip to content

Strain injection examples

After producing detector strain (e.g. with Waveforms, Continuous waves and Detector projection), you often need to embed that strain into a longer segment aligned to a science run—typically starting from zeros or from a noise realization (noise generation can live in a separate package). This step is central to software injections, end-to-end mock challenges, and pipeline validation.

This page is examples only. Signatures, defaults, and raised exceptions for inject_strain / inject_strains_sequential are documented only under API → Injection.

API reference

Use API → Injection for the authoritative behavior description; examples below illustrate common patterns.

Example 1 — Inject one CBC into a zero-filled segment

import numpy as np

from gwmock_signal import TimeSeries
from gwmock_signal.waveform import WaveformFactory
from gwmock_signal.projection import project_polarizations_to_network
from gwmock_signal.injection import inject_strain

fs = 4096.0
duration = 8.0
t0 = 1_400_000_000.0
n = int(duration * fs)

target = TimeSeries(np.zeros(n), t0=t0, sample_rate=fs)

factory = WaveformFactory()
pol = factory.generate(
    "IMRPhenomD",
    {
        "tc": t0 + 2.0,
        "detector_frame_mass_1": 30.0,
        "detector_frame_mass_2": 25.0,
        "spin_1z": 0.0,
        "spin_2z": 0.0,
        "distance": 400.0,
        "inclination": 0.0,
        "coa_phase": 0.0,
    },
    sampling_frequency=fs,
    minimum_frequency=20.0,
)
strains = project_polarizations_to_network(
    pol,
    ["H1"],
    right_ascension=1.0,
    declination=0.5,
    polarization_angle=0.2,
    earth_rotation=False,
)
h1 = strains["H1"]

out = inject_strain(target, h1)
assert out is not target  # immutability: new object (recommended contract)

Example 2 — Multiple injections in time order

from gwmock_signal.injection import inject_strains_sequential

# target: long segment; inj1, inj2: non-overlapping or overlapping time series
out = inject_strains_sequential(target, [inj1, inj2], interpolate_if_offset=True)

Example 3 — Disable resampling

If you want an injection added only when it needs no resampling:

out = inject_strain(target, h1, interpolate_if_offset=False)

When interpolate_if_offset=False and the injection would be resampled (it is not aligned, in the sense below), the function returns target.copy() — i.e., the background unchanged, with a debug-level log message. The flag guarantees that nothing is resampled; an aligned injection is still added, rounded to whole samples.

An injection counts as aligned when rounding its start to the nearest target sample moves it by no more than 1e-4 samples, the threshold LALSimulation's XLALSimAddInjectionREAL8TimeSeries uses; anything further off is placed at the time it carries. A start a few float64 steps off the grid is therefore not aligned: at GPS 1e9 and 8192 Hz two steps are 1/512 sample, 0.24 µs. A series you built on the target's grid can round further off it than 1e-4 samples at a rate that is not a power of two (at GPS 1e9, t0 + k / 4000 misses its sample by up to half a float64 step, 2.4e-4 samples). Declare it, and it is added sample for sample:

out = inject_strain(target, built_on_grid, on_grid=True)

A declared series further off the grid than both its timestamps' float64 resolution and LAL's 1e-4-sample shift allow is refused with ValueError.

The high-level paths (CBCSimulator.simulate, inject_cbc_signal) place a projected waveform at its coalescence time and never declare it, so they always follow LAL's rule.

Example 4 — One-call CBC injection with inject_cbc_signal

The convenience function inject_cbc_signal orchestrates waveform generation, projection, and injection in one call:

import numpy as np
from gwmock_signal import TimeSeries
from gwmock_signal.pipeline import inject_cbc_signal

fs = 4096.0
duration = 8.0
n = int(duration * fs)
t0 = 1_400_000_000.0

params = {
    "detector_frame_mass_1": 36.0,
    "detector_frame_mass_2": 29.0,
    "coa_time": t0 + duration / 2,
    "distance": 410.0,
    "inclination": 0.0,
    "right_ascension": 1.375,
    "declination": -1.211,
    "polarization_angle": 0.0,
}

background = {name: TimeSeries(np.zeros(n), t0=t0, sample_rate=fs) for name in ["H1", "L1"]}

result = inject_cbc_signal(
    "IMRPhenomD",
    params,
    ["H1", "L1"],
    background,
    sampling_frequency=fs,
    minimum_frequency=20.0,
    earth_rotation=False,
)

for name in result.detector_names:
    rms = float(np.sqrt(np.mean(result[name].value ** 2)))
    print(f"{name}: rms={rms:.4e}")

Example 5 — Simulate and save to disk with CBCSimulator.write

The highest-level convenience is CBCSimulator.write, which runs the full pipeline and writes both the strain data and a JSON sidecar with the injection parameters:

from gwmock_signal import CBCSimulator, TimeSeries
import numpy as np

sim = CBCSimulator("IMRPhenomD")

# The waveform_model property exposes the approximant name:
print(sim.waveform_model)  # IMRPhenomD

params = {
    "detector_frame_mass_1": 36.0,
    "detector_frame_mass_2": 29.0,
    "coa_time": 1_126_259_462.4,
    "distance": 410.0,
    "inclination": 0.0,
    "right_ascension": 1.375,
    "declination": -1.211,
    "polarization_angle": 0.0,
}
fs = 4096.0
n = 8192
background = {name: TimeSeries(np.zeros(n), t0=params["coa_time"] - 1, sample_rate=fs) for name in ["H1", "L1"]}

result = sim.write(
    "my_injection.h5",
    params,
    ["H1", "L1"],
    background,
    sampling_frequency=fs,
    minimum_frequency=20.0,
    format="hdf5",
)
# Also creates my_injection_params.json with the injection parameters

Supported write formats: "hdf5" (default), "gwf", "npy", "txt".

Example 6 — Inject one CW into a segment with CBC

A continuous wave has no duration of its own, so the segment being generated is defined by the background's epoch and length. The background is the existing strain called out from example 1, so t0 and fs are that example's values: the injection lands on the CBC segment's own time grid, which sampling_frequency has to match. A real CW run would use a much longer segment than that 8 s example; the point here is only that the two signals add.

from gwmock_signal.continuous import ContinuousWaveSimulator

t0 = 1_400_000_000.0  # the CBC background's epoch, from example 1
fs = 4096.0  # and its sample rate, which sampling_frequency must match

cw_sim = ContinuousWaveSimulator(
    earth_ephemeris="earth00-40-DE405.dat.gz",
    sun_ephemeris="sun00-40-DE405.dat.gz",
    reference_time_ssb=t0,
    spindowns=(-1.0e-10,),
)

params = {
    "right_ascension": 1.1,
    "declination": 0.3,
    "frequency": 20.0,
    "initial_phase": 0.4,
    "amplitude_plus": 1.0e-24,
    "amplitude_cross": 7.0e-25,
    "polarization_angle": 0.2,  # optional; defaults to 0.0
}

# The CBC time series is dimensionless.
# Relabels CBC background to `strain` units
cbc_background = TimeSeries(out.data, t0=out.t0, sample_rate=out.sample_rate, unit="strain")

result = cw_sim.simulate(
    params=params,
    detector_names=["H1"],
    background={"H1": cbc_background},  # An existing strain with the CBC signal
    sampling_frequency=fs,
    minimum_frequency=0.0,  # required by the shared simulator signature; unused for CW
    earth_rotation=True,  # the only accepted value
)

Pitfalls

  • Units: Target and injection should use compatible strain units (typically dimensionless); mixed units should raise a clear error.
  • No overlap: If the injection lies entirely outside the target span, the API returns a copy of the target (same samples, new object).
  • Interpolation: An off-grid injection is resampled with a band-limited windowed-sinc kernel, which treats the injection as zero outside its span, so an injection that does not taper to zero rings at its edges; prefer aligned waveforms from the same sampling_frequency and GPS grid when possible.
  • Content near Nyquist: The kernel is accurate to 0.8 x Nyquist; above that it is off by up to about 0.4 of the signal at 0.98 x Nyquist. For a whole signal that falls smoothly to zero at both ends, such as a generated compact-binary waveform, pass tapered=True to inject_strain or inject_strains_sequential: the injection is then oversampled exactly first and placed accurately up to its Nyquist frequency. Leave it False for a piece cut from a longer signal: the exact interpolation is global, so the cut ends would ring across the whole piece. CBCSimulator.simulate sets it from the waveform backend and the waveform's options (polarizations_tapered): True for the LALSimulation backend's approximants measured in the waveform guide's table, which leave at most 7.3e-05 of peak outside their buffers, except IMRPhenomXPHM with its precession-angle multibanding on, which leaves up to 1.7e-02; set "PhenomXPHMThresholdMband": 0.0 in waveform_arguments to turn it off. IMRPhenomPv2_NRTidalv2 counts only at 8192 Hz or above, since at lower rates a binary neutron star's merger is cut at Nyquist. An approximant not measured there is not taken as tapered. A cut of amplitude ε rings by at most ε·|sin(πδ)|/(πd) at d samples from it under the global interpolation, below the content the buffer already lacks.
  • Performance: Large segments are memory-bound; avoid unnecessary copies if the API documents mutability (default recommendation: return a new TimeSeries).

Scientific notes

  • Conventions follow common LIGO injection practice: coherent addition of strain in the time domain on a fixed grid.
  • For colored noise backgrounds, build target as noise first, then inject; stationary Gaussian noise can be added in a separate noise-focused package.

See also