o
    Me1                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlmZ G dd deZe Z	e	
 dee d	ed
efddZe	
 deeef d
efddZe	
 deded
efddZe	
 ded
efddZe	
 deded
efddZe	
 	dBdededed
efddZe	
 	dBdedededed
ef
dd Ze	
 	dBded!ededed
ef
d"d#Ze	
 	dCdededed
efd&d'Ze	
 dBdededed
efd(d)Ze	
 dBdededed
efd*d+Ze	
 	dDdededed
efd-d.Ze	
 	dBded/ededed
ef
d0d1Ze	
 	dEdededed
efd3d4Ze	
 dBdededed
efd5d6Ze	
 dBdededed
efd7d8Ze	
 dBdededed
efd9d:Ze	
 dBdededed
efd;d<Z 		dBd=eeeeef f d>ed?eded
ef
d@dAZ!dS )F    N)List)Tuple)Union)Tensorc                   @   s&   e Zd Zdd ZdddZdd ZdS )	WindowFunctionRegisterc                 C   s   t  | _d S N)dict_functions_dictself r   ND:\Projects\ConvertPro\env\Lib\site-packages\paddle/audio/functional/window.py__init__   s   zWindowFunctionRegister.__init__Nc                    s    fdd}|S )Nc                    s   | j }|  j|< | S r   )__name__r	   )funcnamer
   r   r   add_subfunction   s   
z8WindowFunctionRegister.register.<locals>.add_subfunctionr   )r   r   r   r   r
   r   register   s   zWindowFunctionRegister.registerc                 C   s
   | j | S r   )r	   )r   r   r   r   r   get"   s   
zWindowFunctionRegister.getr   )r   
__module____qualname__r   r   r   r   r   r   r   r      s    
r   x	data_typereturnc                    s    fdd| D }t |S )Nc                    s   g | ]}t | qS r   )paddleZ	to_tensor).0_r   r   r   
<listcomp>+   s    z_cat.<locals>.<listcomp>)r   concat)r   r   lr   r   r   _cat)   s   
r!   c                 C   sD   t | trt| t| d d  S t| tt| d  S )N      )
isinstancefloatmathlogsqrtr   Zsquare)r   r   r   r   _acosh/   s   
r)   Msymc                 C   s   |s| d dfS | dfS )z:Extend window by 1 sample if needed for DFT-even symmetry.r#   TFr   )r*   r+   r   r   r   _extend6   s   r,   c                 C   s$   t | | ks
| dk rtd| dkS )z)Handle small or incorrect window lengths.r   z.Window length M must be a non-negative integerr#   )int
ValueError)r*   r   r   r   _len_guards?   s   r/   wneededc                 C   s   |r| dd S | S )z<Truncate window by 1 sample if needed for DFT-even symmetry.Nr   )r0   r1   r   r   r   	_truncateH   s   r3   Tfloat64dtypec                 C   sl   t | rtj| f|dS t| |\} }tjd| |d| d d  }tdt|| d|   }t||S )zCompute a window with a generalized Gaussian shape.
    This function is consistent with scipy.signal.windows.general_gaussian().
    r5   r         ?       @g      r"   )r/   r   onesr,   arangeexpabsr3   )r*   psigr+   r5   needs_truncnr0   r   r   r   _general_gaussianQ   s    
rA   ac                 C   s   t | rtj| f|dS t| |\} }tjtj tj| |d}tj| f|d}tt	|D ]}||| t
||  7 }q-t||S )zCompute a generic weighted sum of cosine terms window.
    This function is consistent with scipy.signal.windows.general_cosine().
    r6   )r/   r   r9   r,   linspacer&   piZzerosrangelencosr3   )r*   rB   r+   r5   r?   facr0   kr   r   r   _general_cosineb   s   
rJ   alphac                 C   s   t | |d| g||dS )zvCompute a generalized Hamming window.
    This function is consistent with scipy.signal.windows.general_hamming()
    r7   r6   rJ   )r*   rK   r+   r5   r   r   r   _general_hammings   s   rM         c              	      s  t rtjf|dS t|\}d|d  }t|tj }|d |d |d d   }	tjd||dtj|d f|d t	}
d|
ddd< d|
ddd<  }t
tD ]u}|
| td|| |	 |d d d     }|d	krdtd|| ||d d    }n<|td krdtd|| |d|    }n"dtd|| |d|    td|| ||d d    }||  |< q] fd
d}|tjd	|d}|rd|d d  }||9 }| }t||S )zCompute a Taylor window.
    The Taylor window taper function approximates the Dolph-Chebyshev window's
    constant sidelobe level for a parameterized number of near-in sidelobes.
    r6   
      r"         ?r#   Nr2   r   c                    sD   ddt  dt dtj d | d  d     S )Nr#   r"   r   r8   rR   )r   matmulZ	unsqueezerG   r&   rD   )r@   ZFmr*   mar   r   W   s   ,z_taylor.<locals>.Wr7   )r/   r   r9   r,   r)   r&   rD   r:   emptyZ
empty_likerE   rF   prodZsqueezer3   )r*   ZnbarZsllZnormr+   r5   r?   BAs2Zsignsm2miZnumerdenomrV   r0   scaler   rT   r   _taylor}   sD   

"($ 
r`   c                 C      t | d||dS )zCompute a Hamming window.
    The Hamming window is a taper formed by using a raised cosine with
    non-zero endpoints, optimized to minimize the nearest side lobe.
    gHzG?r6   rM   r*   r+   r5   r   r   r   _hamming      rd   c                 C   ra   )zCompute a Hann window.
    The Hann window is a taper formed by using a raised cosine or sine-squared
    with ends that touch zero.
    rR   r6   rb   rc   r   r   r   _hann   re   rf   rR   c              	   C   s<  t | rtj| f|dS |dkrtj| f|dS |dkr"t| |dS t| |\} }tjd| |d}t|| d  d }|d|d  }||d | | d  }|| | d d }	ddttj	d	d| | | d      }
tj|j
|d}ddttj	d
| d d|	 | | d      }t|
||g}t||S )z[Compute a Tukey window.
    The Tukey window is also known as a tapered cosine window.
    r6   r   r7   )r+   r#   r8   NrR   r2   g       )r/   r   r9   Zhannr,   r:   r-   rG   r&   rD   shaper   r3   )r*   rK   r+   r5   r?   r@   widthZn1Zn2Zn3Zw1Zw2Zw3r0   r   r   r   _tukey   s*   ,*
ri   stdc                 C   sl   t | rtj| f|dS t| |\} }tjd| |d| d d  }d| | }t|d  | }t||S )ztCompute a Gaussian window.
    The Gaussian widows has a Gaussian shape defined by the standard deviation(std).
    r6   r   r7   r8   r"   )r/   r   r9   r,   r:   r;   r3   )r*   rj   r+   r5   r?   r@   Zsig2r0   r   r   r   	_gaussian   s   
rk   r7   c                 C   s   |r
|dur
t dt| rtj| f|dS t| |\} }|du r'| d d }tjd| |d}tt||  | }t||S )z+Compute an exponential (or Poisson) window.Nz"If sym==True, center must be None.r6   r#   r"   r   )	r.   r/   r   r9   r,   r:   r;   r<   r3   )r*   centertaur+   r5   r?   r@   r0   r   r   r   _exponential   s   
rn   c                 C   s   t | rtj| f|dS t| |\} }tjd| d d d |d}| d dkr<d| d |  }t||ddd g}nd| | d  }t||ddd g}t||S )	zCompute a triangular window.r6   r#   r"   r   r7   Nr2   )r/   r   r9   r,   r:   r   r3   )r*   r+   r5   r?   r@   r0   r   r   r   _triang  s   
rp   c                 C   s   t | rtj| f|dS t| |\} }ttjdd| |ddd }d| ttj|  dtj t	tj|   }t
d|dg|}t||S )z^Compute a Bohman window.
    The Bohman window is the autocorrelation of a cosine window.
    r6   r2   r#   r7   r   )r/   r   r9   r,   r<   rC   rG   r&   rD   sinr!   r3   )r*   r+   r5   r?   rH   r0   r   r   r   _bohman"  s    "
rr   c                 C   s   t | g d||dS )a  Compute a Blackman window.
    The Blackman window is a taper formed by using the first three terms of
    a summation of cosines. It was designed to have close to the minimal
    leakage possible.  It is close to optimal, only slightly worse than a
    Kaiser window.
    )gzG?rR   g{Gz?r6   rL   rc   r   r   r   	_blackman4  s   rs   c                 C   sT   t | rtj| f|dS t| |\} }ttj|  tjd| |dd  }t||S )z,Compute a window with a simple cosine shape.r6   r   rR   )	r/   r   r9   r,   rq   r&   rD   r:   r3   )r*   r+   r5   r?   r0   r   r   r   _cosine?  s
   $
rt   window
win_lengthfftbinsc              
   C   s   | }d}t | tr| d }t| dkr| dd }nt | tr/| dv r,td|  d | }n
tdtt|  z	td	| }W n tyS } ztd
|d}~ww |f| }	d|i}
||	d|i|
S )a  Return a window of a given length and type.

    Args:
        window (Union[str, Tuple[str, float]]): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'gaussian', 'general_gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor'.
        win_length (int): Number of samples.
        fftbins (bool, optional): If True, create a "periodic" window. Otherwise, create a "symmetric" window, for use in filter design. Defaults to True.
        dtype (str, optional): The data type of the return window. Defaults to 'float64'.

    Returns:
        Tensor: The window represented as a tensor.

    Examples:
        .. code-block:: python

            import paddle

            n_fft = 512
            cosine_window = paddle.audio.functional.get_window('cosine', n_fft)

            std = 7
            gaussian_window = paddle.audio.functional.get_window(('gaussian',std), n_fft)
    r   r   r#   N)ZgaussianZexponentialzThe 'z6' window needs one or more parameters -- pass a tuple.z#%s as window type is not supported.r   zUnknown window type.r+   r5   )	r$   tuplerF   strr.   typewindow_function_registerr   KeyError)ru   rv   rw   r5   r+   argsZwinstrZwinfunceparamskwargsr   r   r   
get_windowJ  s2   




r   )Tr4   )rN   rO   TTr4   )rR   Tr4   )Nr7   Tr4   )"r&   typingr   r   r   r   r   objectr   r{   r   ry   r!   r%   r)   r-   boolr,   r/   r3   rA   rJ   rM   r`   rd   rf   ri   rk   rn   rp   rr   rs   rt   r   r   r   r   r   <module>   s   	8!
