U
    •Ê¹j½Y  ã                   @   sÜ  d dl mZmZ d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 dddd	d
ddddddgZe
e	dƒeddiƒZdd„ Zeeejejddœdd„Zeddjf eŽƒddddejdddœeee eeeej ejeej eedœ	d d	„ƒZed!d"jf eŽƒddejddd#œeeeej ejeej eed$œd%d„ƒZed&d'jf eŽƒdddejddd(œeeeeej ejeej eed)œd*d
„ƒZed+d,jf eŽƒd-ddejddd.œeeeeej ejeej eed/œd0d„ƒZed1d2jf eŽƒddejddd#œeeeej ejeej eed$œd3d„ƒZed4d5jf eŽƒddejddd#œeeeej ejeej eed$œd6d„ƒZed7d8jf eŽƒddejddd#œeeeej ejeej eed$œd9d„ƒZed:d;jf eŽƒddejddd#œeeeej ejeej eed$œd<d„ƒZed=d>jf eŽƒddejddd#œeeeej ejeej eed?œd@d„ƒZ edAdBjf eŽƒdCddejdddDœeeeej ejeej eedEœdFd„ƒZ!edGdHjf eŽƒddejddd#œeeeej ejeej eed$œdId„ƒZ"dS )Jé    )ÚOptionalÚIterableN)Úsqrt)ÚTensor)Úfactory_common_argsÚparse_kwargsÚmerge_dictsÚbartlettÚblackmanÚcosineÚexponentialÚgaussianÚgeneral_cosineÚgeneral_hammingÚhammingÚhannÚkaiserÚnuttalla6  
    M (int): the length of the window.
        In other words, the number of points of the returned window.
    sym (bool, optional): If `False`, returns a periodic window suitable for use in spectral analysis.
        If `True`, returns a symmetric window suitable for use in filter design. Default: `True`.
Znormalizationz�The window is normalized to 1 (maximum value is 1). However, the 1 doesn't appear if :attr:`M` is even and :attr:`sym` is `True`.c                     s   ‡ fdd„}|S )a8  Adds docstrings to a given decorated function.

    Specially useful when then docstrings needs string interpolation, e.g., with
    str.format().
    REMARK: Do not use this function if the docstring doesn't need string
    interpolation, just write a conventional docstring.

    Args:
        args (str):
    c                    s   d  ˆ ¡| _| S )NÚ )ÚjoinÚ__doc__)Úo©Úargs© ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/signal/windows/windows.pyÚ	decorator4   s    z_add_docstr.<locals>.decoratorr   )r   r   r   r   r   Ú_add_docstr(   s    r   )Úfunction_nameÚMÚdtypeÚlayoutÚreturnc                 C   s\   |dk rt | › d|› �ƒ‚|tjk	r6t | › d|› �ƒ‚|tjtjfkrXt | › d|› �ƒ‚dS )aŠ  Performs common checks for all the defined windows.
     This function should be called before computing any window.

     Args:
         function_name (str): name of the window function.
         M (int): length of the window.
         dtype (:class:`torch.dtype`): the desired data type of returned tensor.
         layout (:class:`torch.layout`): the desired layout of returned tensor.
     r   z, requires non-negative window length, got M=z/ is implemented for strided tensors only, got: z) expects float32 or float64 dtypes, got: N)Ú
ValueErrorÚtorchÚstridedZfloat32Zfloat64)r   r   r    r!   r   r   r   Ú_window_function_checks;   s    

r&   zì
Computes a window with an exponential waveform.
Also known as Poisson window.

The exponential window is defined as follows:

.. math::
    w_n = \exp{\left(-\frac{|n - c|}{\tau}\right)}

where `c` is the ``center`` of the window.
    aF  

{normalization}

Args:
    {M}

Keyword args:
    center (float, optional): where the center of the window will be located.
        Default: `M / 2` if `sym` is `False`, else `(M - 1) / 2`.
    tau (float, optional): the decay value.
        Tau is generally associated with a percentage, that means, that the value should
        vary within the interval (0, 100]. If tau is 100, it is considered the uniform window.
        Default: 1.0.
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric exponential window of size 10 and with a decay value of 1.0.
    >>> # The center will be at (M - 1) / 2, where M is 10.
    >>> torch.signal.windows.exponential(10)
    tensor([0.0111, 0.0302, 0.0821, 0.2231, 0.6065, 0.6065, 0.2231, 0.0821, 0.0302, 0.0111])

    >>> # Generates a periodic exponential window and decay factor equal to .5
    >>> torch.signal.windows.exponential(10, sym=False,tau=.5)
    tensor([4.5400e-05, 3.3546e-04, 2.4788e-03, 1.8316e-02, 1.3534e-01, 1.0000e+00, 1.3534e-01, 1.8316e-02, 2.4788e-03, 3.3546e-04])
    ç      ð?TF)ÚcenterÚtauÚsymr    r!   ÚdeviceÚrequires_grad)	r   r(   r)   r*   r    r!   r+   r,   r"   c          
   	   C   sÐ   |d krt  ¡ }td| ||ƒ |dkr6td|› d�ƒ‚|rJ|d k	rJtdƒ‚| dkrft jd||||d�S |d krŠ|s~| dkr~| n| d d	 }d| }t j| | | | d  | | ||||d
�}	t  t  |	¡ ¡S )Nr   r   zTau must be positive, got: ú	 instead.z)Center must be None for symmetric windows©r   ©r    r!   r+   r,   é   ç       @©ÚstartÚendZstepsr    r!   r+   r,   )r$   Úget_default_dtyper&   r#   ÚemptyÚlinspaceÚexpÚabs)
r   r(   r)   r*   r    r!   r+   r,   ÚconstantÚkr   r   r   r   M   s*    9úzì
Computes a window with a simple cosine waveform.
Also known as the sine window.

The cosine window is defined as follows:

.. math::
    w_n = \cos{\left(\frac{\pi n}{M} - \frac{\pi}{2}\right)} = \sin{\left(\frac{\pi n}{M}\right)}
    aô  

{normalization}

Args:
    {M}

Keyword args:
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric cosine window.
    >>> torch.signal.windows.cosine(10)
    tensor([0.1564, 0.4540, 0.7071, 0.8910, 0.9877, 0.9877, 0.8910, 0.7071, 0.4540, 0.1564])

    >>> # Generates a periodic cosine window.
    >>> torch.signal.windows.cosine(10, sym=False)
    tensor([0.1423, 0.4154, 0.6549, 0.8413, 0.9595, 1.0000, 0.9595, 0.8413, 0.6549, 0.4154])
©r*   r    r!   r+   r,   )r   r*   r    r!   r+   r,   r"   c          	   	   C   sŽ   |d krt  ¡ }td| ||ƒ | dkr:t jd||||d�S d}t j|sV| dkrV| d n|  }t j|| || d  | | ||||d�}t  |¡S )Nr   r   r.   r/   ç      à?r0   r2   )r$   r5   r&   r6   Úpir7   Úsin©	r   r*   r    r!   r+   r,   r3   r:   r;   r   r   r   r   ¤   s     .
úz§
Computes a window with a gaussian waveform.

The gaussian window is defined as follows:

.. math::
    w_n = \exp{\left(-\left(\frac{n}{2\sigma}\right)^2\right)}
    a   

{normalization}

Args:
    {M}

Keyword args:
    std (float, optional): the standard deviation of the gaussian. It controls how narrow or wide the window is.
        Default: 1.0.
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric gaussian window with a standard deviation of 1.0.
    >>> torch.signal.windows.gaussian(10)
    tensor([4.0065e-05, 2.1875e-03, 4.3937e-02, 3.2465e-01, 8.8250e-01, 8.8250e-01, 3.2465e-01, 4.3937e-02, 2.1875e-03, 4.0065e-05])

    >>> # Generates a periodic gaussian window and standard deviation equal to 0.9.
    >>> torch.signal.windows.gaussian(10, sym=False,std=0.9)
    tensor([1.9858e-07, 5.1365e-05, 3.8659e-03, 8.4658e-02, 5.3941e-01, 1.0000e+00, 5.3941e-01, 8.4658e-02, 3.8659e-03, 5.1365e-05])
)Ústdr*   r    r!   r+   r,   )r   rA   r*   r    r!   r+   r,   r"   c          
   	   C   s¸   |d krt  ¡ }td| ||ƒ |dkr6td|› d�ƒ‚| dkrRt jd||||d�S |sb| dkrb| n| d  d }d|td	ƒ  }t j|| || d  | | ||||d
�}	t  |	d	  ¡S )Nr   r   z*Standard deviation must be positive, got: r-   r.   r/   r0   r1   é   r2   )r$   r5   r&   r#   r6   r   r7   r8   )
r   rA   r*   r    r!   r+   r,   r3   r:   r;   r   r   r   r   è   s$    0
úaK  
Computes the Kaiser window.

The Kaiser window is defined as follows:

.. math::
    w_n = I_0 \left( \beta \sqrt{1 - \left( {\frac{n - N/2}{N/2}} \right) ^2 } \right) / I_0( \beta )

where ``I_0`` is the zeroth order modified Bessel function of the first kind (see :func:`torch.special.i0`), and
``N = M - 1 if sym else M``.
    aò  

{normalization}

Args:
    {M}

Keyword args:
    beta (float, optional): shape parameter for the window. Must be non-negative. Default: 12.0
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric gaussian window with a standard deviation of 1.0.
    >>> torch.signal.windows.kaiser(5)
    tensor([4.0065e-05, 2.1875e-03, 4.3937e-02, 3.2465e-01, 8.8250e-01, 8.8250e-01, 3.2465e-01, 4.3937e-02, 2.1875e-03, 4.0065e-05])
    >>> # Generates a periodic gaussian window and standard deviation equal to 0.9.
    >>> torch.signal.windows.kaiser(5, sym=False,std=0.9)
    tensor([1.9858e-07, 5.1365e-05, 3.8659e-03, 8.4658e-02, 5.3941e-01, 1.0000e+00, 5.3941e-01, 8.4658e-02, 3.8659e-03, 5.1365e-05])
g      (@)Úbetar*   r    r!   r+   r,   )r   rC   r*   r    r!   r+   r,   r"   c          
   	   C   sä   |d krt  ¡ }td| ||ƒ |dk r6td|› d�ƒ‚| dkrRt jd||||d�S | dkrnt jd||||d�S | }d	| |s‚| n| d  }t j||| d |  | ||||d
�}	t  t  || t  	|	d¡ ¡¡t  t j
||d�¡ S )Nr   r   z beta must be non-negative, got: r-   r.   r/   r0   ©r0   r1   r2   rB   )r+   )r$   r5   r&   r#   r6   Úonesr7   Zi0r   ÚpowÚtensor)
r   rC   r*   r    r!   r+   r,   r3   r:   r;   r   r   r   r   2  s(    1úzœ
Computes the Hamming window.

The Hamming window is defined as follows:

.. math::
    w_n = \alpha - \beta\ \cos \left( \frac{2 \pi n}{M - 1} \right)
    a¡  

{normalization}

Arguments:
    {M}

Keyword args:
    {sym}
    alpha (float, optional): The coefficient :math:`\alpha` in the equation above.
    beta (float, optional): The coefficient :math:`\beta` in the equation above.
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric Hamming window.
    >>> torch.signal.windows.hamming(10)
    tensor([0.0800, 0.1876, 0.4601, 0.7700, 0.9723, 0.9723, 0.7700, 0.4601, 0.1876, 0.0800])

    >>> # Generates a periodic Hamming window.
    >>> torch.signal.windows.hamming(10, sym=False)
    tensor([0.0800, 0.1679, 0.3979, 0.6821, 0.9121, 1.0000, 0.9121, 0.6821, 0.3979, 0.1679])
c                C   s   t | |||||d�S )Nr<   ©r   ©r   r*   r    r!   r+   r,   r   r   r   r     s    -zÔ
Computes the Hann window.

The Hann window is defined as follows:

.. math::
    w_n = \frac{1}{2}\ \left[1 - \cos \left( \frac{2 \pi n}{M - 1} \right)\right] =
    \sin^2 \left( \frac{\pi n}{M - 1} \right)
    añ  

{normalization}

Arguments:
    {M}

Keyword args:
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric Hann window.
    >>> torch.signal.windows.hann(10)
    tensor([0.0000, 0.1170, 0.4132, 0.7500, 0.9698, 0.9698, 0.7500, 0.4132, 0.1170, 0.0000])

    >>> # Generates a periodic Hann window.
    >>> torch.signal.windows.hann(10, sym=False)
    tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955])
c             	   C   s   t | d|||||d�S )Nr=   ©Úalphar*   r    r!   r+   r,   rH   rI   r   r   r   r   ¯  s    ,úzÊ
Computes the Blackman window.

The Blackman window is defined as follows:

.. math::
    w_n = 0.42 - 0.5 \cos \left( \frac{2 \pi n}{M - 1} \right) + 0.08 \cos \left( \frac{4 \pi n}{M - 1} \right)
    aá  

{normalization}

Arguments:
    {M}

Keyword args:
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric Blackman window.
    >>> torch.signal.windows.blackman(5)
    tensor([-1.4901e-08,  3.4000e-01,  1.0000e+00,  3.4000e-01, -1.4901e-08])

    >>> # Generates a periodic Blackman window.
    >>> torch.signal.windows.blackman(5, sym=False)
    tensor([-1.4901e-08,  2.0077e-01,  8.4923e-01,  8.4923e-01,  2.0077e-01])
c             	   C   s:   |d krt  ¡ }td| ||ƒ t| dddg|||||d�S )Nr
   gáz®GáÚ?r=   g{®Gáz´?©Úar*   r    r!   r+   r,   )r$   r5   r&   r   rI   r   r   r   r
   ä  s    +ÿa4  
Computes the Bartlett window.

The Bartlett window is defined as follows:

.. math::
    w_n = 1 - \left| \frac{2n}{M - 1} - 1 \right| = \begin{cases}
        \frac{2n}{M - 1} & \text{if } 0 \leq n \leq \frac{M - 1}{2} \\
        2 - \frac{2n}{M - 1} & \text{if } \frac{M - 1}{2} < n < M \\ \end{cases}
    a  

{normalization}

Arguments:
    {M}

Keyword args:
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric Bartlett window.
    >>> torch.signal.windows.bartlett(10)
    tensor([0.0000, 0.2222, 0.4444, 0.6667, 0.8889, 0.8889, 0.6667, 0.4444, 0.2222, 0.0000])

    >>> # Generates a periodic Bartlett window.
    >>> torch.signal.windows.bartlett(10, sym=False)
    tensor([0.0000, 0.2000, 0.4000, 0.6000, 0.8000, 1.0000, 0.8000, 0.6000, 0.4000, 0.2000])
c          	   	   C   s    |d krt  ¡ }td| ||ƒ | dkr:t jd||||d�S | dkrVt jd||||d�S d}d|sd| n| d  }t j||| d |  | ||||d	�}dt  |¡ S )
Nr	   r   r.   r/   r0   rD   éÿÿÿÿrB   r2   )r$   r5   r&   r6   rE   r7   r9   r@   r   r   r   r	     s$    -úz¹
Computes the general cosine window.

The general cosine window is defined as follows:

.. math::
    w_n = \sum^{M-1}_{i=0} (-1)^i a_i \cos{ \left( \frac{2 \pi i n}{M - 1}\right)}
    aÁ  

{normalization}

Arguments:
    {M}

Keyword args:
    a (Iterable): the coefficients associated to each of the cosine functions.
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric general cosine window with 3 coefficients.
    >>> torch.signal.windows.general_cosine(10, a=[0.46, 0.23, 0.31], sym=True)
    tensor([0.5400, 0.3376, 0.1288, 0.4200, 0.9136, 0.9136, 0.4200, 0.1288, 0.3376, 0.5400])

    >>> # Generates a periodic general cosine window wit 2 coefficients.
    >>> torch.signal.windows.general_cosine(10, a=[0.5, 1 - 0.5], sym=False)
    tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955])
)rM   r*   r    r!   r+   r,   r"   c             	   C   s  |d krt  ¡ }td| ||ƒ | dkr:t jd||||d�S | dkrVt jd||||d�S t|tƒshtdƒ‚|sttdƒ‚d	t j	 |s„| n| d  }t j
d| d | | ||||d
�}t jdd„ t|ƒD ƒ|||d�}	t j|	jd |	j|	j|	jd�}
|	 d¡t  |
 d¡| ¡  d¡S )Nr   r   r.   r/   r0   rD   z!Coefficients must be a list/tuplezCoefficients cannot be emptyrB   r2   c                 S   s   g | ]\}}d | | ‘qS )rN   r   )Ú.0ÚiÚwr   r   r   Ú
<listcomp>¥  s     z"general_cosine.<locals>.<listcomp>)r+   r    r,   )r    r+   r,   rN   )r$   r5   r&   r6   rE   Ú
isinstancer   Ú	TypeErrorr#   r>   r7   rG   Ú	enumerateZarangeÚshaper    r+   r,   Z	unsqueezeÚcosÚsum)r   rM   r*   r    r!   r+   r,   r:   r;   Za_irP   r   r   r   r   ^  s.    ,

ú z²
Computes the general Hamming window.

The general Hamming window is defined as follows:

.. math::
    w_n = \alpha - (1 - \alpha) \cos{ \left( \frac{2 \pi n}{M-1} \right)}
    a£  

{normalization}

Arguments:
    {M}

Keyword args:
    alpha (float, optional): the window coefficient. Default: 0.54.
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

Examples::

    >>> # Generates a symmetric Hamming window with the general Hamming window.
    >>> torch.signal.windows.general_hamming(10, sym=True)
    tensor([0.0800, 0.1876, 0.4601, 0.7700, 0.9723, 0.9723, 0.7700, 0.4601, 0.1876, 0.0800])

    >>> # Generates a periodic Hann window with the general Hamming window.
    >>> torch.signal.windows.general_hamming(10, alpha=0.5, sym=False)
    tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955])
gHáz®Gá?rJ   )rK   r*   r    r!   r+   r,   r"   c             	   C   s   t | |d| g|||||d�S )Nr'   rL   ©r   )r   rK   r*   r    r!   r+   r,   r   r   r   r   ª  s    -
úuÜ   
Computes the minimum 4-term Blackman-Harris window according to Nuttall.

.. math::
    w_n = 1 - 0.36358 \cos{(z_n)} + 0.48917 \cos{(2z_n)} - 0.13659 \cos{(3z_n)} + 0.01064 \cos{(4z_n)}

where ``z_n = 2 Ï€ n/ M``.
    uå  

{normalization}

Arguments:
    {M}

Keyword args:
    {sym}
    {dtype}
    {layout}
    {device}
    {requires_grad}

References::

    - A. Nuttall, â€œSome windows with very good sidelobe behavior,â€�
      IEEE Transactions on Acoustics, Speech, and Signal Processing, vol. 29, no. 1, pp. 84-91,
      Feb 1981. https://doi.org/10.1109/TASSP.1981.1163506

    - Heinzel G. et al., â€œSpectrum and spectral density estimation by the Discrete Fourier transform (DFT),
      including a comprehensive list of window functions and some new flat-top windowsâ€�,
      February 15, 2002 https://holometer.fnal.gov/GH_FFT.pdf

Examples::

    >>> # Generates a symmetric Nutall window.
    >>> torch.signal.windows.general_hamming(5, sym=True)
    tensor([3.6280e-04, 2.2698e-01, 1.0000e+00, 2.2698e-01, 3.6280e-04])

    >>> # Generates a periodic Nuttall window.
    >>> torch.signal.windows.general_hamming(5, sym=False)
    tensor([3.6280e-04, 1.1052e-01, 7.9826e-01, 7.9826e-01, 1.1052e-01])
c             	   C   s   t | ddddg|||||d�S )Ng¹zíD×?g;%¯Nß?g¡1“¨|Á?gžòC Ë…?rL   rY   rI   r   r   r   r   à  s    7
ú)#Útypingr   r   r$   Úmathr   r   Ztorch._torch_docsr   r   r   Ú__all__Zwindow_common_argsr   ÚstrÚintr    r!   r&   Úformatr%   ÚfloatÚboolr+   r   r   r   r   r   r   r
   r	   r   r   r   r   r   r   r   Ú<module>   sø  õÿ ÿöáô1÷ö)	èö(ùøæ÷)ø÷$èô*ø÷&æ÷(ú ú
	èö'ú úè÷&ú ú
èõ(ú ú ç÷'ú ú'ç÷'ù ù"Þ÷1ùø