{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import bilby\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# We first establish a dictionary of parameters that includes \n",
    "# all of the different waveform parameters, including masses \n",
    "# of the two black holes (mass_1, mass_2), spins of both black \n",
    "# holes (a, tilt, phi), etc. \n",
    "\n",
    "# Here we set the value of each parameter as the median of its prior's \n",
    "# range, which is given to train VItamin_B\n",
    "parameters = dict(\n",
    "        mass_1=57.5, mass_2=57.5, a_1=0., a_2=0., tilt_1=0., tilt_2=0.,\n",
    "        phi_12=0., phi_jl=0., luminosity_distance=2000, theta_jn=3.141592653589793/2, psi=3.141592653589793/2,\n",
    "        phase=6.283185307179586/2, geocent_time=0.25, ra=6.283185307179586/2, dec=0.)\n",
    "\n",
    "\n",
    "\n",
    "# Set up interferometers. In this case we'll use three interferometer \n",
    "# L1 H1 and V1. Duration and sampling frequency follow VItamin_B's\n",
    "# requirement.\n",
    "\n",
    "ifos_list = [ 'L1','H1','V1']\n",
    "duration = 1.\n",
    "sampling_frequency = 256\n",
    "\n",
    "\n",
    "# Set up a random seed for result reproducibility. \n",
    "np.random.seed(8870)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Use Bilby to generate BBH waveform\n",
    "def generate_bbh_waveform(parameters,ifos_list,sampling_frequency,duration):\n",
    "\n",
    "    \n",
    "    # Fixed arguments passed into the source model\n",
    "    waveform_arguments = dict(\n",
    "            waveform_approximant='IMRPhenomPv2',reference_frequency=50., minimum_frequency=20.)\n",
    "    \n",
    "    # Create the waveform_generator using a LAL BinaryBlackHole source function\n",
    "    # the generator will convert all the parameters\n",
    "    waveform_generator = bilby.gw.WaveformGenerator(\n",
    "            duration=duration, sampling_frequency=sampling_frequency,\n",
    "            frequency_domain_source_model=bilby.gw.source.lal_binary_black_hole,\n",
    "            parameter_conversion=bilby.gw.conversion.convert_to_lal_binary_black_hole_parameters,\n",
    "            waveform_arguments=waveform_arguments)\n",
    "    \n",
    "    ifos = bilby.gw.detector.InterferometerList(ifos_list)\n",
    "    \n",
    "    ifos.set_strain_data_from_power_spectral_densities(\n",
    "            sampling_frequency=sampling_frequency, duration=duration,\n",
    "            start_time=parameters['geocent_time'] - 0.1)\n",
    "    \n",
    "    waveform = {}\n",
    "    waveform['times'] = {}\n",
    "    waveform['h_plus'] = {}\n",
    "    waveform['h_cross'] = {}\n",
    "    \n",
    "    for ifo in ifos_list:\n",
    "        k = 0\n",
    "        waveform['times'][ifo] = np.linspace(parameters['geocent_time'], \n",
    "                parameters['geocent_time']+duration,int(sampling_frequency) )\n",
    "        waveform['h_plus'][ifo] = ifos.inject_signal(waveform_generator=waveform_generator,\n",
    "                parameters=parameters)[k]['plus']\n",
    "        waveform['h_cross'][ifo] = ifos.inject_signal(waveform_generator=waveform_generator,\n",
    "                parameters=parameters)[k]['cross']\n",
    "        k += 1\n",
    "    \n",
    "    \n",
    "    return waveform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "06:59 bilby INFO    : Waveform generator initiated with\n",
      "  frequency_domain_source_model: bilby.gw.source.lal_binary_black_hole\n",
      "  time_domain_source_model: None\n",
      "  parameter_conversion: bilby.gw.conversion.convert_to_lal_binary_black_hole_parameters\n",
      "/home/changfeng/.local/lib/python3.8/site-packages/bilby/gw/detector/psd.py:356: RuntimeWarning: invalid value encountered in multiply\n",
      "  frequency_domain_strain = self.__power_spectral_density_interpolated(frequencies) ** 0.5 * white_noise\n",
      "06:59 bilby INFO    : Injected signal in L1:\n",
      "06:59 bilby INFO    :   optimal SNR = 8.25\n",
      "06:59 bilby INFO    :   matched filter SNR = 8.55+0.66j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in H1:\n",
      "06:59 bilby INFO    :   optimal SNR = 5.16\n",
      "06:59 bilby INFO    :   matched filter SNR = 4.79+0.13j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in V1:\n",
      "06:59 bilby INFO    :   optimal SNR = 0.45\n",
      "06:59 bilby INFO    :   matched filter SNR = 1.23-0.61j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in L1:\n",
      "06:59 bilby INFO    :   optimal SNR = 8.25\n",
      "06:59 bilby INFO    :   matched filter SNR = 16.80+0.66j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in H1:\n",
      "06:59 bilby INFO    :   optimal SNR = 5.16\n",
      "06:59 bilby INFO    :   matched filter SNR = 9.94+0.13j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in V1:\n",
      "06:59 bilby INFO    :   optimal SNR = 0.45\n",
      "06:59 bilby INFO    :   matched filter SNR = 1.68-0.61j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in L1:\n",
      "06:59 bilby INFO    :   optimal SNR = 8.25\n",
      "06:59 bilby INFO    :   matched filter SNR = 25.05+0.66j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in H1:\n",
      "06:59 bilby INFO    :   optimal SNR = 5.16\n",
      "06:59 bilby INFO    :   matched filter SNR = 15.10+0.13j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in V1:\n",
      "06:59 bilby INFO    :   optimal SNR = 0.45\n",
      "06:59 bilby INFO    :   matched filter SNR = 2.14-0.61j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in L1:\n",
      "06:59 bilby INFO    :   optimal SNR = 8.25\n",
      "06:59 bilby INFO    :   matched filter SNR = 33.31+0.66j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in H1:\n",
      "06:59 bilby INFO    :   optimal SNR = 5.16\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "06:59 bilby INFO    :   matched filter SNR = 20.26+0.13j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in V1:\n",
      "06:59 bilby INFO    :   optimal SNR = 0.45\n",
      "06:59 bilby INFO    :   matched filter SNR = 2.59-0.61j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in L1:\n",
      "06:59 bilby INFO    :   optimal SNR = 8.25\n",
      "06:59 bilby INFO    :   matched filter SNR = 41.56+0.66j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in H1:\n",
      "06:59 bilby INFO    :   optimal SNR = 5.16\n",
      "06:59 bilby INFO    :   matched filter SNR = 25.41+0.13j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in V1:\n",
      "06:59 bilby INFO    :   optimal SNR = 0.45\n",
      "06:59 bilby INFO    :   matched filter SNR = 3.04-0.61j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in L1:\n",
      "06:59 bilby INFO    :   optimal SNR = 8.25\n",
      "06:59 bilby INFO    :   matched filter SNR = 49.82+0.66j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in H1:\n",
      "06:59 bilby INFO    :   optimal SNR = 5.16\n",
      "06:59 bilby INFO    :   matched filter SNR = 30.57+0.13j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n",
      "06:59 bilby INFO    : Injected signal in V1:\n",
      "06:59 bilby INFO    :   optimal SNR = 0.45\n",
      "06:59 bilby INFO    :   matched filter SNR = 3.49-0.61j\n",
      "06:59 bilby INFO    :   mass_1 = 57.5\n",
      "06:59 bilby INFO    :   mass_2 = 57.5\n",
      "06:59 bilby INFO    :   a_1 = 0.0\n",
      "06:59 bilby INFO    :   a_2 = 0.0\n",
      "06:59 bilby INFO    :   tilt_1 = 0.0\n",
      "06:59 bilby INFO    :   tilt_2 = 0.0\n",
      "06:59 bilby INFO    :   phi_12 = 0.0\n",
      "06:59 bilby INFO    :   phi_jl = 0.0\n",
      "06:59 bilby INFO    :   luminosity_distance = 2000\n",
      "06:59 bilby INFO    :   theta_jn = 1.5707963267948966\n",
      "06:59 bilby INFO    :   psi = 1.5707963267948966\n",
      "06:59 bilby INFO    :   phase = 3.141592653589793\n",
      "06:59 bilby INFO    :   geocent_time = 0.25\n",
      "06:59 bilby INFO    :   ra = 3.141592653589793\n",
      "06:59 bilby INFO    :   dec = 0.0\n"
     ]
    }
   ],
   "source": [
    "bbh_waveform = generate_bbh_waveform(parameters,ifos_list,sampling_frequency,duration)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "times = bbh_waveform['times']['L1']\n",
    "h_plus_L1 = bbh_waveform['h_plus']['L1']\n",
    "h_cross_L1 = bbh_waveform['h_cross']['L1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "x and y must have same first dimension, but have shapes (256,) and (129,)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-0ebffccb07eb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimes\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mh_plus_L1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimes\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mh_cross_L1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/matplotlib/pyplot.py\u001b[0m in \u001b[0;36mplot\u001b[0;34m(scalex, scaley, data, *args, **kwargs)\u001b[0m\n\u001b[1;32m   2822\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0m_copy_docstring_and_deprecators\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mAxes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2823\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscalex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscaley\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2824\u001b[0;31m     return gca().plot(\n\u001b[0m\u001b[1;32m   2825\u001b[0m         \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscalex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mscalex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscaley\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mscaley\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2826\u001b[0m         **({\"data\": data} if data is not None else {}), **kwargs)\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py\u001b[0m in \u001b[0;36mplot\u001b[0;34m(self, scalex, scaley, data, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1741\u001b[0m         \"\"\"\n\u001b[1;32m   1742\u001b[0m         \u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcbook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnormalize_kwargs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmlines\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLine2D\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1743\u001b[0;31m         \u001b[0mlines\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_lines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1744\u001b[0m         \u001b[0;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mlines\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1745\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_line\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, data, *args, **kwargs)\u001b[0m\n\u001b[1;32m    271\u001b[0m                 \u001b[0mthis\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    272\u001b[0m                 \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 273\u001b[0;31m             \u001b[0;32myield\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_plot_args\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mthis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    274\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    275\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mget_next_color\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py\u001b[0m in \u001b[0;36m_plot_args\u001b[0;34m(self, tup, kwargs)\u001b[0m\n\u001b[1;32m    397\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    398\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 399\u001b[0;31m             raise ValueError(f\"x and y must have same first dimension, but \"\n\u001b[0m\u001b[1;32m    400\u001b[0m                              f\"have shapes {x.shape} and {y.shape}\")\n\u001b[1;32m    401\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: x and y must have same first dimension, but have shapes (256,) and (129,)"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 4740 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(times,h_plus_L1,label='h_plus_L1')\n",
    "plt.plot(times,h_cross_L1,label='h_cross_L1')\n",
    "plt.legend(loc='upper center')\n",
    "plt.ylabel('Waveform')\n",
    "plt.xlabel('Times(s)')\n",
    "plt.title('BBH waveform')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "duration = 1. sampling_frequency = 256 . So times has 256 points.  \n",
    "Generally, the length of h_plus/cross should equal duration$\\times$sampling_frequency   \n",
    "However, h_plus and h_cross given by bilby both have 129 points.  \n",
    "I ran bilby's tutorial script to validate this and the situation remains the same: duration = 4. sampling_frequency = 2048. The length of h_plus/cross is 4097, which means: h_plus/cross points generated by bilby -1 = (duration*sampling_frequency)/2  \n",
    "\n",
    "Then I try to interpolate h_plus and h_cross to plot the waveform."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "times_short = np.linspace(0.25, 1.25, 128 )\n",
    "\n",
    "h_plus_L1_interp = np.interp(times, times_short, h_plus_L1[0:128]) \n",
    "h_cross_L1_interp = np.interp(times, times_short, h_cross_L1[0:128]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/changfeng/.local/lib/python3.8/site-packages/numpy/core/_asarray.py:83: ComplexWarning: Casting complex values to real discards the imaginary part\n",
      "  return array(a, dtype, copy=False, order=order)\n",
      "/home/changfeng/.local/lib/python3.8/site-packages/numpy/core/_asarray.py:83: ComplexWarning: Casting complex values to real discards the imaginary part\n",
      "  return array(a, dtype, copy=False, order=order)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'BBH waveform')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22064 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(times,h_plus_L1_interp,label='h_plus_L1')\n",
    "plt.plot(times,h_cross_L1_interp,label='h_cross_L1')\n",
    "plt.legend(loc='upper center')\n",
    "plt.ylabel('Waveform')\n",
    "plt.xlabel('Times(s)')\n",
    "plt.title('BBH waveform')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "h_cross looks like a straight line, but it s correct.  \n",
    "Because it has a very small amplitude(~10^-39) due to those angles's value, "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
