B
    dH                 @   sl   d Z ddlmZ ddlmZmZ ddlmZmZ ddlZddl	Z
edZdddZdd	d
ZdddZdS )zYThis module contains functions to generate gaussian noise colored with a
noise spectrum.
    )libutils)
TimeSerieszeros)complex_same_precision_asFrequencySeriesNlalsimulationc       
      C   s   d| | j  d  }|dk	r&tj| | }t| }|dk}|| }tjd|}tjd|}|d|  }tjt||d}	||	|< t|	| j |dS )al   Create noise with a given psd.

    Return noise coloured with the given psd. The returned noise
    FrequencySeries has the same length and frequency step as the given psd.
    Note that if unique noise is desired a unique seed should be provided.

    Parameters
    ----------
    psd : FrequencySeries
        The noise weighting to color the noise.
    seed : {0, int} or None
        The seed to generate the noise. If None specified,
        the seed will not be reset.

    Returns
    --------
    noise : FrequencySeriesSeries
        A FrequencySeries containing gaussian noise colored by the given psd.
    g      ?Nr   y              ?)dtype)delta_fr   )	r	   numpyrandomseedr   normalr   lenr   )
psdr   sigmar   Znot_zeroZ	sigma_redZnoise_reZnoise_coZ	noise_rednoise r   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/noise/gaussian.pyfrequency_noise_from_psd&   s    r   c             C   s4  t t| |d}|dkr$tjd}td|}td| |j }|d d }|d }|t	|krjt
d|d	|  }d	|jj|d < d	|jjd	< t t||d }	d	}
t|	d	|| xp|
| k r.|
| | k r|	jjd	| |j|
|
| < n|	jjd	| |
  |j|
| < |
|7 }
t|	||| qW |S )
a/   Create noise with a given psd.

    Return noise with a given psd. Note that if unique noise is desired
    a unique seed should be provided.

    Parameters
    ----------
    length : int
        The length of noise to generate in samples.
    delta_t : float
        The time step of the noise.
    psd : FrequencySeries
        The noise weighting to color the noise.
    seed : {0, int}
        The seed to generate the noise.

    Returns
    --------
    noise : TimeSeries
        A TimeSeries containing gaussian noise colored by the given psd.
    )delta_tNl        Zranluxg      ?      z)PSD not compatible with requested delta_tr   )r   r   r
   r   randintlalZgsl_rngintr	   r   
ValueErrordatar   ZSimNoise)lengthr   r   r   Znoise_tsZ
randomnessNnZstridesegmentZlength_generatedr   r   r   noise_from_psdN   s,     r!         $@c       	      C   sF   ddl }d}td| | d }|j| |||}t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.
    low_fr
    length : int
        The length of noise to generate in samples.
    delta_t : float
        The time step of the noise.
    seed : {None, int}
        The seed to generate the noise.
    low_frequency_cutof : {10.0, float}
        The low frequency cutoff to pass to the PSD generation.

    Returns
    --------
    noise : TimeSeries
        A TimeSeries containing gaussian noise colored by the given psd.
    r   Ng      ?g      ?r   )r   )Z	pycbc.psdr   r   Zfrom_stringr!   )	Zpsd_namer   r   r   Zlow_frequency_cutoffpycbcr	   Zflenr   r   r   r   noise_from_string   s
    r$   )N)N)Nr"   )__doc__r#   r   Zpycbc.typesr   r   r   r   r   Znumpy.randomr
   Zimport_optionalr   r   r!   r$   r   r   r   r   <module>   s   

(
7