B
    ~d'              
   @   s   d Z ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
 yddlmZ W n  ek
rl   ddlmZ Y nX dZeed	d
 Zdd Zdddddddddd	ZdddZdddZdS )z-Utilities for signal-processing with windows
    )wraps)ceilN)
get_window)expit)
_win_equivz(Duncan Macleod <duncan.macleod@ligo.org>c          	   O   s   yt t| |f||S  ttfk
r.   Y nX t| ttfrPt | |f||S t| } | j	dkslt
d| j|ks~t
d| S )z\Wrapper around :func:`scipy.signal.get_window` to allow
    pre-computed window arrays.
       z+multi-dimensional windows are not supportedzwindow array wrong size)_get_windowfloat	TypeError
ValueError
isinstancestrtuplenumpyZasarrayndimAssertionErrorsize)ZwindowZNxargskwargs r   _/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/signal/window.pyr   %   s    
r   c             C   sD   |   dkrdS yt|    jS  tk
r>   td|  Y nX dS )a)  Find the canonical name for the given window in scipy.signal

    Parameters
    ----------
    name : `str`
        the name of the window you want

    Returns
    -------
    realname : `str`
        the name of the window as implemented in `scipy.signal.window`

    Raises
    -------
    ValueError
        if ``name`` cannot be resolved to a window function in `scipy.signal`

    Examples
    --------
    >>> from gwpy.signal.window import canonical_name
    >>> canonical_name('hann')
    'hann'
    >>> canonical_name('ksr')
    'kaiser'
    planckz3no window function in scipy.signal equivalent to %rN)lowerWINDOWS__name__KeyErrorr   )namer   r   r   canonical_name9   s    r   g      ?gx&?g?gˡE?)	ZboxcarZbartlettZbarthannZblackmanharrisZflattopZhannZhammingZnuttallZtriangc          
   C   s   yt | } W n. tk
r: } ztt|W dd}~X Y nX yt|  }W n  tk
rh   td|  Y nX |r~tt|| S |S )a  Returns the recommended fractional overlap for the given window

    If ``nfft`` is given, the return is in samples

    Parameters
    ----------
    name : `str`
        the name of the window you are using

    nfft : `int`, optional
        the length of the window

    Returns
    -------
    rov : `float`, `int`
        the recommended overlap (ROV) for the given window, in samples if
        ``nfft` is given (`int`), otherwise fractional (`float`)

    Examples
    --------
    >>> from gwpy.signal.window import recommended_overlap
    >>> recommended_overlap('hann')
    0.5
    >>> recommended_overlap('blackmanharris', nfft=128)
    85
    Nz$no recommended overlap for %r window)r   r   r   r   ROVintr   )r   ZnfftexcZrovr   r   r   recommended_overlapm   s    r!   c                s   t | } rV|d  d9  < t  fddtd D }|d   t| 9  < r|| d   d9  < t fddtdD }||  | d   t| 9  < |S )a  Return a Planck taper window.

    Parameters
    ----------
    N : `int`
        Number of samples in the output window

    nleft : `int`, optional
        Number of samples to taper on the left, should be less than `N/2`

    nright : `int`, optional
        Number of samples to taper on the right, should be less than `N/2`

    Returns
    -------
    w : `ndarray`
        The window, with the maximum value normalized to 1 and at least one
        end tapered smoothly to 0.

    Examples
    --------
    To taper 0.1 seconds on both ends of one second of data sampled at 2048 Hz:

    >>> from gwpy.signal.window import planck
    >>> w = planck(2048, nleft=205, nright=205)

    References
    ----------
    .. [1] McKechan, D.J.A., Robinson, C., and Sathyaprakash, B.S. (April
           2010). "A tapering window for time-domain templates and simulated
           signals in the detection of gravitational waves from coalescing
           compact binaries". Classical and Quantum Gravity 27 (8).
           :doi:`10.1088/0264-9381/27/8/084020`

    .. [2] Wikipedia, "Window function",
           https://en.wikipedia.org/wiki/Window_function#Planck-taper_window
    r   c                s$   g | ]} d | d |     qS )g      ?r   ).0k)nleftr   r   
<listcomp>   s   zplanck.<locals>.<listcomp>r   c                s&   g | ]}  d |   d |   qS )g      ?r   )r"   r#   )nrightr   r   r%      s   )r   Zonesarrayranger   )Nr$   r&   wZzleftZzrightr   )r$   r&   r   r      s    '
"r   )N)r   r   )__doc__	functoolsr   mathr   r   Zscipy.signalr   r   Zscipy.specialr   Zscipy.signal.windows._windowsr   r   ImportErrorZscipy.signal.windows.windows
__author__r   r   r!   r   r   r   r   r   <module>   s.   (
,