B
    }d7                 @   sb   d Z ddlZddlZddlmZ ddlmZ ddlmZ	 ddl
mZ dZd	gZG d
d	 d	eZdS )z%Representation of a frequency series
    N)fft)units)registry   )Seriesz'Duncan Macleod <duncan.macleod@ligo.orgFrequencySeriesc                   s  e Zd ZdZedZdddddgZd) fd	d
	Ze	e
jje
jje
jjdZe	e
jje
jje
jjdZe	e
jje
jje
jjddZedd Zdd Zd* fdd	Zdd Zd+ddZdd Zdd Zdd  Zed,d!d"Zd#d$ Zed-d%d&Zd.d'd(Z   Z!S )/r   a  A data array holding some metadata to represent a frequency series

    Parameters
    ----------
    value : array-like
        input data array

    unit : `~astropy.units.Unit`, optional
        physical unit of these data

    f0 : `float`, `~astropy.units.Quantity`, optional, default: `0`
        starting frequency for these data

    df : `float`, `~astropy.units.Quantity`, optional, default: `1`
        frequency resolution for these data

    frequencies : `array-like`
        the complete array of frequencies indexing the data.
        This argument takes precedence over `f0` and `df` so should
        be given in place of these if relevant, not alongside

    epoch : `~gwpy.time.LIGOTimeGPS`, `float`, `str`, optional
        GPS epoch associated with these data,
        any input parsable by `~gwpy.time.to_gps` is fine

    name : `str`, optional
        descriptive title for this array

    channel : `~gwpy.detector.Channel`, `str`, optional
        source data stream for these data

    dtype : `~numpy.dtype`, optional
        input data type

    copy : `bool`, optional, default: `False`
        choose to copy the input data to new memory

    subok : `bool`, optional, default: `True`
        allow passing of sub-classes by the array generator

    Notes
    -----
    Key methods:

    .. autosummary::

       ~FrequencySeries.read
       ~FrequencySeries.write
       ~FrequencySeries.plot
       ~FrequencySeries.zpk
    Hzf0dfepochnamechannelNc	       
         sP   |dk	r||	d< |dk	r ||	d< |dk	r0||	d< t  j| |f||||d|	S )z(Generate a new FrequencySeries.
        Nx0dxxindex)unitr   r   r   )super__new__)
clsdatar   r	   r
   frequenciesr   r   r   kwargs)	__class__ q/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/frequencyseries/frequencyseries.pyr   \   s    zFrequencySeries.__new__z{Starting frequency for this `FrequencySeries`

                  :type: `~astropy.units.Quantity` scalar
                  zyFrequency spacing of this `FrequencySeries`

                  :type: `~astropy.units.Quantity` scalar
                  z%Series of frequencies for each sample)fgetfsetfdeldocc             O   s   t j| |f||S )a  Read data into a `FrequencySeries`

        Arguments and keywords depend on the output format, see the
        online documentation for full details for each format, the
        parameters below are common to most formats.

        Parameters
        ----------
        source : `str`, `list`
            Source of data, any of the following:

            - `str` path of single data file,
            - `str` path of LAL-format cache file,
            - `list` of paths.

        *args
            Other arguments are (in general) specific to the given
            ``format``.

        format : `str`, optional
            Source format identifier. If not given, the format will be
            detected if possible. See below for list of acceptable
            formats.

        **kwargs
            Other keywords are (in general) specific to the given ``format``.

        Raises
        ------
        IndexError
            if ``source`` is an empty list

        Notes
        -----)io_registryread)r   sourceargsr   r   r   r   r       s    $zFrequencySeries.readc             O   s   t j| |f||S )a  Write this `FrequencySeries` to a file

        Arguments and keywords depend on the output format, see the
        online documentation for full details for each format, the
        parameters below are common to most formats.

        Parameters
        ----------
        target : `str`
            output filename

        format : `str`, optional
            output format identifier. If not given, the format will be
            detected if possible. See below for list of acceptable
            formats.

        Notes
        -----)r   write)selftargetr"   r   r   r   r   r#      s    zFrequencySeries.writelogc                sb   | j }y|j|jtj }W n tk
r2   Y nX |dk rH|dd |j|d t	 j
f |S )Nr   Zyscaler&   )xscale)r   Zpowersbasesindexr   r   
ValueError
setdefaultupdater   plot)r$   r'   r   uZhzpow)r   r   r   r-      s    zFrequencySeries.plotc             C   sT   ddl m} | jd d }t| j| d }||| j| j| jd| j	 | d}|S )aL  Compute the one-dimensional discrete inverse Fourier
        transform of this `FrequencySeries`.

        Returns
        -------
        out : :class:`~gwpy.timeseries.TimeSeries`
            the normalised, real-valued `TimeSeries`.

        See also
        --------
        numpy.fft.irfft : The inverse (real) FFT function

        Notes
        -----
        This method applies the necessary normalisation such that the
        condition holds:

        >>> timeseries = TimeSeries([1.0, 0.0, -1.0, 0.0], sample_rate=1.0)
        >>> timeseries.fft().ifft() == timeseries
        r   )
TimeSeries   )r   r   r   r   )
Z
timeseriesr/   sizenpfftZirfftvaluer   r   r   r   )r$   r/   ZnoutZdiftnewr   r   r   ifft   s    zFrequencySeries.ifftTc             C   s   | j ||||dS )a  Filter this `FrequencySeries` by applying a zero-pole-gain filter

        Parameters
        ----------
        zeros : `array-like`
            list of zero frequencies (in Hertz)

        poles : `array-like`
            list of pole frequencies (in Hertz)

        gain : `float`
            DC gain of filter

        analog : `bool`, optional
            type of ZPK being applied, if `analog=True` all parameters
            will be converted in the Z-domain for digital filtering

        Returns
        -------
        spectrum : `FrequencySeries`
            the frequency-domain filtered version of the input data

        See also
        --------
        FrequencySeries.filter
            for details on how a digital ZPK-format filter is applied

        Examples
        --------
        To apply a zpk filter with file poles at 100 Hz, and five zeros at
        1 Hz (giving an overall DC gain of 1e-10)::

            >>> data2 = data.zpk([100]*5, [1]*5, 1e-10)
        )analog)filter)r$   ZzerosZpolesZgainr6   r   r   r   zpk   s    #zFrequencySeries.zpkc             C   s   | j  j}| jd | j j|  d }tjdt|| jj	d| | }t
| t|| jj| j}||  ||_ ||_|S )a  Interpolate this `FrequencySeries` to a new resolution.

        Parameters
        ----------
        df : `float`
            desired frequency resolution of the interpolated `FrequencySeries`,
            in Hz

        Returns
        -------
        out : `FrequencySeries`
            the interpolated version of the input `FrequencySeries`

        See also
        --------
        numpy.interp
            for the underlying 1-D linear interpolation scheme
        r0   r   )dtype)r	   Z	decomposer3   r1   r
   numpyZarangeZrintrealr9   typeZinterpr   Z__array_finalize__)r$   r
   r	   NZfsamplesoutr   r   r   interpolate  s    

zFrequencySeries.interpolatec             O   s   ddl m} || f||S )a  Apply a filter to this `FrequencySeries`.

        Parameters
        ----------
        *filt : filter arguments
            1, 2, 3, or 4 arguments defining the filter to be applied,

                - an ``Nx1`` `~numpy.ndarray` of FIR coefficients
                - an ``Nx6`` `~numpy.ndarray` of SOS coefficients
                - ``(numerator, denominator)`` polynomials
                - ``(zeros, poles, gain)``
                - ``(A, B, C, D)`` 'state-space' representation

        analog : `bool`, optional
            if `True`, filter definition will be converted from Hertz
            to Z-domain digital representation, default: `False`

        inplace : `bool`, optional
            if `True`, this array will be overwritten with the filtered
            version, default: `False`

        Returns
        -------
        result : `FrequencySeries`
            the filtered version of the input `FrequencySeries`,
            if ``inplace=True`` was given, this is just a reference to
            the modified input array

        Raises
        ------
        ValueError
            if ``filt`` arguments cannot be interpreted properly
        r0   )fdfilter)Z	_fdcommonr@   )r$   Zfiltr   r@   r   r   r   r7   .  s    "zFrequencySeries.filterc             O   s   t dt | j||S )Nzafilterba will be removed soon, please use FrequencySeries.filter instead, with the same arguments)warningswarnDeprecationWarningr7   )r$   r"   r   r   r   r   filterbaS  s    zFrequencySeries.filterbac          
   C   s\   ddl m} y||j}W n tk
r2   d}Y nX | |jj|jpDd||j|j|jd|dS )z[Generate a new `FrequencySeries` from a LAL `FrequencySeries`
        of any type.
        r   )from_lal_unitN)r   r   r	   r
   r   r   copy)		utils.lalrE   ZsampleUnits	TypeErrorr   r   r	   ZdeltaFr   )r   lalfsrF   rE   r   r   r   r   from_lalY  s    
zFrequencySeries.from_lalc       
   
   C   s   ddl }ddlm}m} y|| j\}}W n< tk
rf } zt| d |j}d}W dd}~X Y nX |	| j
dkrzdn| j
j}|| jdd}|| j|| jj| jj|| jd }	| j| |	j_|	S )	a^  Convert this `FrequencySeries` into a LAL FrequencySeries.

        Returns
        -------
        lalspec : `FrequencySeries`
            an XLAL-format FrequencySeries of a given type, e.g.
            `REAL8FrequencySeries`

        Notes
        -----
        Currently, this function is unable to handle unit string
        conversion.
        r   Nr   )find_typed_functionto_lal_unitz%, defaulting to lal.DimensionlessUnitr0   ZCreater   )lalrG   rK   rL   r   r*   rA   rB   ZDimensionlessUnitZLIGOTimeGPSr   gpsr9   r   r	   r3   r
   shaper   )
r$   rM   rK   rL   r   scaleexcr   createrI   r   r   r   to_lalq  s    zFrequencySeries.to_lalc             C   s   | |j d|j|j|dS )a  Convert a `pycbc.types.frequencyseries.FrequencySeries` into
        a `FrequencySeries`

        Parameters
        ----------
        fs : `pycbc.types.frequencyseries.FrequencySeries`
            the input PyCBC `~pycbc.types.frequencyseries.FrequencySeries`
            array

        copy : `bool`, optional, default: `True`
            if `True`, copy these data to a new array

        Returns
        -------
        spectrum : `FrequencySeries`
            a GWpy version of the input frequency series
        r   )r	   r
   r   rF   )r   delta_fr   )r   fsrF   r   r   r   
from_pycbc  s    zFrequencySeries.from_pycbcc             C   sb   ddl m} | jdkrd}n| jj}| jdjrDtd| j d|j| j| j	dj||dS )a  Convert this `FrequencySeries` into a
        `~pycbc.types.frequencyseries.FrequencySeries`

        Parameters
        ----------
        copy : `bool`, optional, default: `True`
            if `True`, copy these data to a new array

        Returns
        -------
        frequencyseries : `pycbc.types.frequencyseries.FrequencySeries`
            a PyCBC representation of this `FrequencySeries`
        r   )typesNr   z2Cannot convert FrequencySeries to PyCBC with f0 = z+. Starting frequency must be equal to 0 Hz.)rT   r   rF   )
ZpycbcrW   r   rN   r	   tor3   r*   r   r
   )r$   rF   rW   r   r   r   r   to_pycbc  s    
zFrequencySeries.to_pycbc)NNNNNNN)r&   )T)T)T)T)"__name__
__module____qualname____doc__r   ZUnitZ_default_xunitZ_print_slotsr   propertyr   r   __get____set__
__delete__r	   r   r
   r   r   classmethodr    r#   r-   r5   r8   r?   r7   rD   rJ   rS   rV   rY   __classcell__r   r   )r   r   r   %   s6   3
 &
%%$)r]   rA   r:   r   r2   Zastropyr   Z
astropy.ior   r   rW   r   
__author____all__r   r   r   r   r   <module>   s   