Skip to content

Injection

gwmock_signal.injection

Time-domain strain injection into segments.

See docs/user_guide/strain-injection.md (examples) and docs/api/injection/index.md (API).

inject_strain(target, injection, *, interpolate_if_offset=True, tapered=False, on_grid=False)

Return a new series equal to target plus injection on overlapping samples.

The two must share a sample rate and a unit. Only the samples of injection that fall within target are added. An injection off the target's sample lattice is resampled onto it with the band-limited windowed-sinc kernel the projection uses (see interpolate_if_offset), reading the whole injection, so a target sample near either end of target takes its kernel taps from injection samples beyond that end where there are any.

The injection is placed at the time it carries. It is added sample for sample only when rounding its offset to whole samples moves it by no more than LAL's no-op shift of 1e-4 samples (gwmock_data.injection.places_whole_samples), as LALSimulation's XLALSimAddInjectionREAL8TimeSeries places an injection, or when the caller declares it built on the target's lattice (on_grid). A few float64 steps off the lattice is a genuine offset for a signal placed at its coalescence time: two steps are 0.24 µs at GPS 1e9.

The target samples filled are those whose position in the injection's own sample index lies in [0, injection.n_samples - 1]; a single off-lattice injection sample therefore fills none. If nothing is added (no overlap, or offset skipped), returns a copy of target so the result is never the same object as target. Both series must be one-dimensional. The samples are combined on the host, so a JAX-backed input is copied there.

Parameters:

Name Type Description Default
target TimeSeries | TimeSeries

Background segment (e.g. zeros or noise): a :class:~gwmock_signal.timeseries.TimeSeries or a GWpy TimeSeries.

required
injection TimeSeries | TimeSeries

Strain to add (e.g. a projected waveform), of either kind.

required
interpolate_if_offset bool

If False, nothing is resampled: an injection that would be resampled (one not placed by whole samples, above) is skipped and a copy of target returned. One declared on the lattice but off it is refused, not skipped.

True
tapered bool

Whether injection is a whole signal that falls smoothly to zero at both ends. If so, an off-lattice injection is first oversampled exactly, and placed accurately up to its Nyquist frequency rather than to 0.8 of it. Leave it False for a piece cut from a longer signal: the exact interpolation is global, and the cut ends would ring across the whole piece.

False
on_grid bool

Whether injection was built on the target's lattice, so that any fraction of a sample in its offset is the float64 rounding of its timestamps (:meth:~gwmock_signal.sampling_grid.SamplingGrid.lattice_tolerance_samples). It is then added sample for sample.

False

Returns:

Name Type Description
New TimeSeries

class:~gwmock_signal.timeseries.TimeSeries with injected strain.

Raises:

Type Description
ValueError

If either series is not one-dimensional, the sample rates or units differ, or injection is declared on the lattice but starts further off it than both its timestamps' float64 resolution and LAL's no-op shift allow.

Source code in src/gwmock_signal/injection/core.py
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def inject_strain(
    target: TimeSeries | gwpy.timeseries.TimeSeries,
    injection: TimeSeries | gwpy.timeseries.TimeSeries,
    *,
    interpolate_if_offset: bool = True,
    tapered: bool = False,
    on_grid: bool = False,
) -> TimeSeries:
    """Return a new series equal to ``target`` plus ``injection`` on overlapping samples.

    The two must share a sample rate and a unit. Only the samples of ``injection`` that fall
    within ``target`` are added. An injection off the target's sample lattice is resampled onto
    it with the band-limited windowed-sinc kernel the projection uses (see
    ``interpolate_if_offset``), reading the whole injection, so a target sample near either end
    of ``target`` takes its kernel taps from injection samples beyond that end where there are
    any.

    The injection is placed at the time it carries. It is added sample for sample only when
    rounding its offset to whole samples moves it by no more than LAL's no-op shift of 1e-4
    samples (``gwmock_data.injection.places_whole_samples``), as LALSimulation's
    ``XLALSimAddInjectionREAL8TimeSeries`` places an injection, or when the caller declares it
    built on the target's lattice (``on_grid``). A few float64 steps off the lattice is a genuine
    offset for a signal placed at its coalescence time: two steps are 0.24 µs at GPS 1e9.

    The target samples filled are those whose position in the injection's own sample index lies in
    ``[0, injection.n_samples - 1]``; a single off-lattice injection sample therefore fills none. If
    nothing is added (no overlap, or offset skipped), returns a **copy** of ``target`` so the result
    is never the same object as ``target``. Both series must be one-dimensional. The samples are
    combined on the host, so a JAX-backed input is copied there.

    Args:
        target: Background segment (e.g. zeros or noise): a
            :class:`~gwmock_signal.timeseries.TimeSeries` or a GWpy ``TimeSeries``.
        injection: Strain to add (e.g. a projected waveform), of either kind.
        interpolate_if_offset: If ``False``, nothing is resampled: an injection that would be
            resampled (one not placed by whole samples, above) is skipped and a copy of ``target``
            returned. One declared on the lattice but off it is refused, not skipped.
        tapered: Whether ``injection`` is a whole signal that falls smoothly to zero at both
            ends. If so, an off-lattice injection is first oversampled exactly, and placed
            accurately up to its Nyquist frequency rather than to 0.8 of it. Leave it ``False``
            for a piece cut from a longer signal: the exact interpolation is global, and the
            cut ends would ring across the whole piece.
        on_grid: Whether ``injection`` was built on the target's lattice, so that any fraction of
            a sample in its offset is the float64 rounding of its timestamps
            (:meth:`~gwmock_signal.sampling_grid.SamplingGrid.lattice_tolerance_samples`). It is
            then added sample for sample.

    Returns:
        New :class:`~gwmock_signal.timeseries.TimeSeries` with injected strain.

    Raises:
        ValueError: If either series is not one-dimensional, the sample rates or units differ, or
            ``injection`` is declared on the lattice but starts further off it than both its
            timestamps' float64 resolution and LAL's no-op shift allow.
    """
    return _inject(
        as_timeseries(target),
        as_timeseries(injection),
        interpolate_if_offset=interpolate_if_offset,
        tapered=tapered,
        on_grid=on_grid,
    )

inject_strains_sequential(target, injections, *, interpolate_if_offset=True, tapered=False, on_grid=False)

Apply inject_strain to each series in order.

Parameters:

Name Type Description Default
target TimeSeries | TimeSeries

Initial segment, a :class:~gwmock_signal.timeseries.TimeSeries or a GWpy TimeSeries.

required
injections Sequence[TimeSeries | TimeSeries]

Strain series applied in list order, of either kind.

required
interpolate_if_offset bool

Forwarded to each inject_strain call.

True
tapered bool

Forwarded to each inject_strain call: whether every series in injections is a whole signal tapered to zero at both ends.

False
on_grid bool

Forwarded to each inject_strain call: whether every series in injections was built on the target's lattice.

False

Returns:

Name Type Description
Final TimeSeries

class:~gwmock_signal.timeseries.TimeSeries.

TimeSeries

If injections is empty, returns a copy of target.

Source code in src/gwmock_signal/injection/core.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def inject_strains_sequential(
    target: TimeSeries | gwpy.timeseries.TimeSeries,
    injections: Sequence[TimeSeries | gwpy.timeseries.TimeSeries],
    *,
    interpolate_if_offset: bool = True,
    tapered: bool = False,
    on_grid: bool = False,
) -> TimeSeries:
    """Apply ``inject_strain`` to each series in order.

    Args:
        target: Initial segment, a :class:`~gwmock_signal.timeseries.TimeSeries` or a GWpy
            ``TimeSeries``.
        injections: Strain series applied in list order, of either kind.
        interpolate_if_offset: Forwarded to each ``inject_strain`` call.
        tapered: Forwarded to each ``inject_strain`` call: whether every series in ``injections``
            is a whole signal tapered to zero at both ends.
        on_grid: Forwarded to each ``inject_strain`` call: whether every series in ``injections``
            was built on the target's lattice.

    Returns:
        Final :class:`~gwmock_signal.timeseries.TimeSeries`.
        If ``injections`` is empty, returns a copy of ``target``.
    """
    result = as_timeseries(target).copy()
    for injection in injections:
        result = _inject(
            result,
            as_timeseries(injection),
            interpolate_if_offset=interpolate_if_offset,
            tapered=tapered,
            on_grid=on_grid,
        )
    return result

For usage examples, see the User guide — Strain injection examples.