It is sometimes useful to generate noise directly in the frequency domain. PyCBC provides functionality to generate noise that has the same spectrum as a PSD you provide. Note that due to how this is made, the equivelant time series will have circular boundary conditions.
import sys
!{sys.executable} -m pip install pycbc ligo-common --no-cache-dir
Requirement already satisfied: pycbc in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (1.13.5) Requirement already satisfied: lalsuite in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (6.53) Requirement already satisfied: numpy<1.15.3,>=1.13.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (1.15.2) Requirement already satisfied: Mako>=1.0.1 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (1.0.8) Requirement already satisfied: cython in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (0.28.5) Requirement already satisfied: decorator>=3.4.2 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (4.3.0) Requirement already satisfied: scipy>=0.16.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (1.1.0) Requirement already satisfied: matplotlib>=1.5.1 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (2.2.3) Requirement already satisfied: pillow in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (5.3.0) Requirement already satisfied: h5py>=2.5 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (2.8.0) Requirement already satisfied: jinja2 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (2.10) Requirement already satisfied: astropy<3.0.0,>=2.0.3 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (2.0.8) Requirement already satisfied: mpld3>=0.3 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (0.3) Requirement already satisfied: lscsoft-glue>=1.59.3 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (2.0.0) Requirement already satisfied: kombine>=0.8.2 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (0.8.3) Requirement already satisfied: emcee==2.2.1 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (2.2.1) Requirement already satisfied: requests>=1.2.1 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (2.20.1) Requirement already satisfied: beautifulsoup4>=4.6.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (4.6.3) Requirement already satisfied: six>=1.10.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (1.11.0) Requirement already satisfied: ligo-segments in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (1.2.0) Requirement already satisfied: weave>=0.16.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pycbc) (0.17.0) Requirement already satisfied: python-dateutil in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from lalsuite) (2.7.5) Requirement already satisfied: MarkupSafe>=0.9.2 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from Mako>=1.0.1->pycbc) (1.1.0) Requirement already satisfied: cycler>=0.10 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from matplotlib>=1.5.1->pycbc) (0.10.0) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from matplotlib>=1.5.1->pycbc) (2.3.0) Requirement already satisfied: pytz in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from matplotlib>=1.5.1->pycbc) (2018.7) Requirement already satisfied: kiwisolver>=1.0.1 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from matplotlib>=1.5.1->pycbc) (1.0.1) Requirement already satisfied: backports.functools_lru_cache in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from matplotlib>=1.5.1->pycbc) (1.5) Requirement already satisfied: subprocess32 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from matplotlib>=1.5.1->pycbc) (3.5.3) Requirement already satisfied: pytest>=2.8 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from astropy<3.0.0,>=2.0.3->pycbc) (3.8.0) Requirement already satisfied: pyOpenSSL in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from lscsoft-glue>=1.59.3->pycbc) (18.0.0) Requirement already satisfied: idna<2.8,>=2.5 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from requests>=1.2.1->pycbc) (2.7) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from requests>=1.2.1->pycbc) (3.0.4) Requirement already satisfied: urllib3<1.25,>=1.21.1 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from requests>=1.2.1->pycbc) (1.23) Requirement already satisfied: certifi>=2017.4.17 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from requests>=1.2.1->pycbc) (2018.10.15) Requirement already satisfied: setuptools in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from kiwisolver>=1.0.1->matplotlib>=1.5.1->pycbc) (40.8.0) Requirement already satisfied: py>=1.5.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pytest>=2.8->astropy<3.0.0,>=2.0.3->pycbc) (1.6.0) Requirement already satisfied: attrs>=17.4.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pytest>=2.8->astropy<3.0.0,>=2.0.3->pycbc) (18.2.0) Requirement already satisfied: more-itertools>=4.0.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pytest>=2.8->astropy<3.0.0,>=2.0.3->pycbc) (4.3.0) Requirement already satisfied: atomicwrites>=1.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pytest>=2.8->astropy<3.0.0,>=2.0.3->pycbc) (1.2.1) Requirement already satisfied: pluggy>=0.7 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pytest>=2.8->astropy<3.0.0,>=2.0.3->pycbc) (0.7.1) Requirement already satisfied: funcsigs in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pytest>=2.8->astropy<3.0.0,>=2.0.3->pycbc) (1.0.2) Requirement already satisfied: pathlib2>=2.2.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pytest>=2.8->astropy<3.0.0,>=2.0.3->pycbc) (2.3.2) Requirement already satisfied: cryptography>=2.2.1 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pyOpenSSL->lscsoft-glue>=1.59.3->pycbc) (2.3.1) Requirement already satisfied: scandir in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from pathlib2>=2.2.0->pytest>=2.8->astropy<3.0.0,>=2.0.3->pycbc) (1.9.0) Requirement already satisfied: asn1crypto>=0.21.0 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from cryptography>=2.2.1->pyOpenSSL->lscsoft-glue>=1.59.3->pycbc) (0.24.0) Requirement already satisfied: cffi!=1.11.3,>=1.7 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from cryptography>=2.2.1->pyOpenSSL->lscsoft-glue>=1.59.3->pycbc) (1.11.5) Requirement already satisfied: enum34 in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from cryptography>=2.2.1->pyOpenSSL->lscsoft-glue>=1.59.3->pycbc) (1.1.6) Requirement already satisfied: ipaddress in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from cryptography>=2.2.1->pyOpenSSL->lscsoft-glue>=1.59.3->pycbc) (1.0.22) Requirement already satisfied: pycparser in /home/nbuser/anaconda2_501/lib/python2.7/site-packages (from cffi!=1.11.3,>=1.7->cryptography>=2.2.1->pyOpenSSL->lscsoft-glue>=1.59.3->pycbc) (2.19)
%matplotlib inline
import pycbc.noise
import pycbc.psd
import pylab
# Generate a PSD using an analytic expression for
# the full design Advanced LIGO noise curve
f_lower = 10
duration = 128
sample_rate = 4096
tsamples = sample_rate * duration
fsamples = tsamples // 2 + 1
df = 1.0 / duration
psd = pycbc.psd.from_string('aLIGOZeroDetHighPower', fsamples, df, f_lower)
# Let's take a look at the spectrum
pylab.loglog(psd.sample_frequencies, psd)
pylab.xlim(20, 1024)
pylab.ylim(1e-48, 1e-45)
pylab.xlabel('Frequency (Hz)')
pylab.ylabel('Strain^2 / Hz')
pylab.grid()
# Now, let's generate noise that has the same spectrum
htilde = pycbc.noise.frequency_noise_from_psd(psd, seed=857)
# Equivelantly in the time domain
hoft = htilde.to_timeseries()
# Well zoom in around a short time
hoft_zoom = hoft.time_slice(43.5, 44)
pylab.plot(hoft_zoom.sample_times, hoft_zoom)
# Notice the extreme difference in amplitudes of noise at different frequencies.
# You can see clearly the loud low frequency noise.
[<matplotlib.lines.Line2D at 0x7f3a29d5d490>]
# Now we can verify that the PSD of this noise is as expected.
# Use Welch's method with 4s segments
psd_estimated = hoft.psd(4)
pylab.loglog(psd_estimated.sample_frequencies, psd_estimated, label='Estimated')
pylab.loglog(psd.sample_frequencies, psd, label='Original', linewidth=3)
pylab.legend()
pylab.xlim(20, 1024)
pylab.ylim(1e-48, 1e-45)
(1e-48, 1e-45)