o
    i                  	   @   sj   d Z ddlZddlZddlm  mZ ddlm	Z	 	dde
ded	ed
ejfddZG dd dejjZdS )zaPseudo QMF modules.

This code is modified from https://github.com/kan-bayashi/ParallelWaveGAN.

    N)kaiser>   V-?      "@tapscutoff_ratiobetareturnc                 C   s   | d dks
J dd|  k rdk sJ d J dt j| }t jdd& t |t | d	 d
|    t jt | d	 d
|     }W d   n1 sPw   Y  t d| || d < t| d	 |}|| }|S )aK  Design prototype filter for PQMF.

    This method is based on `A Kaiser window approach for the design of prototype
    filters of cosine modulated filterbanks`_.

    Args:
        taps (int): The number of filter taps.
        cutoff_ratio (float): Cut-off frequency ratio.
        beta (float): Beta coefficient for kaiser window.

    Returns:
        ndarray: Impluse response of prototype filter (taps + 1,).

    .. _`A Kaiser window approach for the design of prototype filters of cosine
        modulated filterbanks`: https://ieeexplore.ieee.org/abstract/document/681427

       r   z'The number of taps mush be even number.              ?z%Cutoff ratio must be > 0.0 and < 1.0.ignore)invalid   g      ?N)nppierrstatesinarangecosr   )r   r   r   omega_ch_iwh r   O/home/ubuntu/.local/lib/python3.10/site-packages/espnet2/gan_tts/melgan/pqmf.pydesign_prototype_filter   s   $
r   c                	       sh   e Zd ZdZ				ddededed	ef fd
dZdejdejfddZ	dejdejfddZ
  ZS )PQMFzPQMF module.

    This module is based on `Near-perfect-reconstruction pseudo-QMF banks`_.

    .. _`Near-perfect-reconstruction pseudo-QMF banks`:
        https://ieeexplore.ieee.org/document/258122

       r   r   r   subbandsr   r   r   c              	      s  t    t|||}t|t|f}t|t|f}t|D ]V}d| td| d tjd|   t	|d |d   d| tj d   ||< d| td| d tjd|   t	|d |d   d| tj d   ||< q!t
| d}	t
| d}
| d|	 | d|
 t
|||f }t|D ]	}d|||df< q| d	| || _t
j|d d
| _dS )a  Initilize PQMF module.

        The cutoff_ratio and beta parameters are optimized for #subbands = 4.
        See dicussion in https://github.com/kan-bayashi/ParallelWaveGAN/issues/195.

        Args:
            subbands (int): The number of subbands.
            taps (int): The number of filter taps.
            cutoff_ratio (float): Cut-off frequency ratio.
            beta (float): Beta coefficient for kaiser window.

        r
   r   r   r   analysis_filtersynthesis_filterr   updown_filterr   N)super__init__r   r   zeroslenranger   r   r   torch
from_numpyfloat	unsqueezeregister_bufferr   nnConstantPad1dpad_fn)selfr   r   r   r   h_proto
h_analysish_synthesiskr!   r"   r#   	__class__r   r   r%   A   sV   


zPQMF.__init__xr	   c                 C   s(   t | || j}t j|| j| jdS )zAnalysis with PQMF.

        Args:
            x (Tensor): Input tensor (B, 1, T).

        Returns:
            Tensor: Output tensor (B, subbands, T // subbands).

        stride)Fconv1dr0   r!   r#   r   r1   r8   r   r   r   analysis   s   
zPQMF.analysisc                 C   s.   t j|| j| j | jd}t | || jS )zSynthesis with PQMF.

        Args:
            x (Tensor): Input tensor (B, subbands, T // subbands).

        Returns:
            Tensor: Output tensor (B, 1, T).

        r9   )r;   conv_transpose1dr#   r   r<   r0   r"   r=   r   r   r   	synthesis   s   zPQMF.synthesis)r   r   r   r   )__name__
__module____qualname____doc__intr+   r%   r)   Tensorr>   r@   __classcell__r   r   r6   r   r   7   s"    Ar   )r   r   r   )rD   numpyr   r)   torch.nn.functionalr.   
functionalr;   scipy.signalr   rE   r+   ndarrayr   Moduler   r   r   r   r   <module>   s    
'