B
    d|                  @   sZ   d dl Z d dlZd dlmZmZ d dlmZ dZdd Z	ddd	Z
dddZdddZdS )    N)
TimeSeriescomplex_same_precision_as)RandomStatei   c             C   s*   t }t| d }|d }|j||d dS )a#   Return block of normal random numbers

    Parameters
    ----------
    seed : {None, int}
        The seed to generate the noise.sd
    sample_rate: float
        Sets the variance of the white noise

    Returns
    --------
    noise : numpy.ndarray
        Array of random numbers
    l           g      ?)sizescale)BLOCK_SAMPLESr   normal)seedsample_ratenumrngZvariance r   f/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/noise/reproduceable.pyblock   s    r    @  c       	         s   t   }tt| | }tt|| }|| dkr@|d8 }t|ddt fddt||d dD }t|d  || d}|	| |S )	a#   Generate data with a white Gaussian (normal) distribution

    Parameters
    ----------
    start_time : int
        Start time in GPS seconds to generate noise
    end_time : int
        End time in GPS seconds to generate noise
    sample-rate: float
        Sample rate to generate the data at. Keep constant if you want to
        ensure continuity between disjoint time spans.
    seed : {None, int}
        The seed to generate the noise.

    Returns
    --------
    noise : TimeSeries
        A TimeSeries containing gaussian noise
    r      l        l           c                s   g | ]}t |  qS r   )r   ).0i)r   svr   r   
<listcomp>Q   s   znormal.<locals>.<listcomp>g      ?)delta_tepoch)
r   intnumpyfloorr   randintZconcatenateZaranger   
time_slice)	startendr   r
   Z	block_dursedatatsr   )r   r   r   r	   3   s    r	         ?   c             C   s  |   } t|| j d d }t| }	| | |  }
xHtt| D ]8}||	d krh| |	d  | j|< | | dkrF|
| j|< qFW t|| }t|| d|  }| jdd|  kr0tj	
| dd|  } dtj	jd|  ||dd } | t| } |  } | | | t||  | |  |  } n.tj	
| d| } dtj	jd|  ||dd } t|| j }| d|   |  d	 }~ t|| || ||d
}| }||| 9 }~|jd| d}~|||S )a   Create noise from a PSD

    Return noise from the chosen PSD. Note that if unique noise is desired
    a unique seed should be provided.

    Parameters
    ----------
    psd : pycbc.types.FrequencySeries
        PSD to color the noise
    start_time : int
        Start time in GPS seconds to generate noise
    end_time : int
        End time in GPS seconds to generate nosie
    seed : {None, int}
        The seed to generate the noise.
    sample_rate: {16384, float}
        The sample rate of the output data. Keep constant if you want to
        ensure continuity between disjoint time spans.
    low_frequency_cutof : {1.0, float}
        The low frequency cutoff to pass to the PSD generation.
    filter_duration : {128, float}
        The duration in seconds of the coloring filter

    Returns
    --------
    noise : TimeSeries
        A TimeSeries containing gaussian noise colored by the given psd.
    r   r   r   g      ?g       @Zhann)low_frequency_cutoffZtrunc_methodNg      ?)r
   r   )r   )copyr   delta_flenresizemaxranger"   pycbcpsdZinterpolateZinverse_spectrum_truncationZastyper   Zto_timeseriesZrollZto_frequencyseriesclearZsquared_normr	   r   )r.   
start_timeend_timer
   r   r&   filter_durationr   flenZoldlenZmax_valr   Zfil_lenZwn_durZkminZasdZwhite_noiseZcoloredr   r   r   colored_noiseV   sZ    !




r4   c          
   C   sF   d| }t || d d }	tj| |	||}
t|
|||||||dS )a   Create noise from an analytic PSD

    Return noise from the chosen PSD. Note that if unique noise is desired
    a unique seed should be provided.

    Parameters
    ----------
    psd_name : str
        Name of the analytic PSD to use.
    start_time : int
        Start time in GPS seconds to generate noise
    end_time : int
        End time in GPS seconds to generate nosie
    seed : {None, int}
        The seed to generate the noise.
    sample_rate: {16384, float}
        The sample rate of the output data. Keep constant if you want to
        ensure continuity between disjoint time spans.
    low_frequency_cutof : {10.0, float}
        The low frequency cutoff to pass to the PSD generation.
    filter_duration : {128, float}
        The duration in seconds of the coloring filter

    Returns
    --------
    noise : TimeSeries
        A TimeSeries containing gaussian noise colored by the given psd.
    g      ?r   r   )r
   r   r&   r2   r   )r   r-   r.   Zfrom_stringr4   )Zpsd_namer0   r1   r
   r   r&   r2   r   r(   r3   r.   r   r   r   noise_from_string   s    "r5   )r   r   )r   r   r$   r%   r$   )r   r   r$   r%   r$   )r   Z	pycbc.psdr-   Zpycbc.typesr   r   Znumpy.randomr   r   r   r	   r4   r5   r   r   r   r   <module>   s   
$   
]    