.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/frequencyseries/inject-fs.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_frequencyseries_inject-fs.py: .. currentmodule:: gwpy.timeseries .. sectionauthor:: Alex Urban Inject a signal into a `FrequencySeries` ######################################## It can often be useful to add some known signal to inherently random or noisy data. For example, one might want to investigate what would happen if a binary black hole merger signal occured at or near the time of a glitch. In LIGO data analysis, this procedure is referred to as an *injection*. In the example below we will create a stream of random, white Gaussian noise, then inject a loud, steady sinuosoid. We will do this in the frequency domain because it is much easier to model a sinusoid there. .. GENERATED FROM PYTHON SOURCE LINES 38-42 Generate random data -------------------- First, we prepare one second of Gaussian noise using `numpy.random.Generator.normal`: .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. code-block:: Python from numpy.random import default_rng from gwpy.timeseries import TimeSeries rng = default_rng() noise = TimeSeries(rng.normal(scale=.1, size=1024), sample_rate=1024) .. GENERATED FROM PYTHON SOURCE LINES 49-50 To inject a signal in the frequency domain, we need to take an FFT: .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python noisefd = noise.fft() .. GENERATED FROM PYTHON SOURCE LINES 54-59 Inject noise ------------ We can now easily inject a loud sinusoid of unit amplitude at, say, 30 Hz. To do this, we use :meth:`~gwpy.frequencyseries.FrequencySeries.inject`: .. GENERATED FROM PYTHON SOURCE LINES 59-65 .. code-block:: Python import numpy from gwpy.frequencyseries import FrequencySeries signal = FrequencySeries(numpy.array([1.]), f0=30, df=noisefd.df) injfd = noisefd.inject(signal) .. GENERATED FROM PYTHON SOURCE LINES 66-70 Visualisation ------------- We can then visualize the data before and after injection in the frequency domain: .. GENERATED FROM PYTHON SOURCE LINES 70-80 .. code-block:: Python from gwpy.plot import Plot plot = Plot( numpy.abs(noisefd), numpy.abs(injfd), xscale="log", yscale="log", ) plot.show() .. image-sg:: /examples/frequencyseries/images/sphx_glr_inject-fs_001.png :alt: inject fs :srcset: /examples/frequencyseries/images/sphx_glr_inject-fs_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-83 Finally, for completeness we can visualize the effect before and after injection back in the time domain: .. GENERATED FROM PYTHON SOURCE LINES 83-94 .. code-block:: Python inj = injfd.ifft() plot = Plot( noise, inj, separate=True, sharex=True, sharey=True, figsize=(12, 6), ) plot.show() .. image-sg:: /examples/frequencyseries/images/sphx_glr_inject-fs_002.png :alt: inject fs :srcset: /examples/frequencyseries/images/sphx_glr_inject-fs_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.631 seconds) .. _sphx_glr_download_examples_frequencyseries_inject-fs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: inject-fs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: inject-fs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: inject-fs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_