
    Vh                     >   d Z ddlZ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 ddlmZ  G d	 d
ej        j                  Z G d dej        j                  Z	 	 ddej        dee         dedededee         fdZ	 	 ddej        dededededee         fdZdS )z$
FIR windowed sinc lowpass filters.
    N)SequenceOptional)
functional   )sinc)
fft_conv1dsimple_reprc                   b     e Zd ZdZ	 	 ddee         deded	ed
ee         f
 fdZ	d Z
d Z xZS )LowPassFiltersa
  
    Bank of low pass filters. Note that a high pass or band pass filter can easily
    be implemented by substracting a same signal processed with low pass filters with different
    frequencies (see `julius.bands.SplitBands` for instance).
    This uses a windowed sinc filter, very similar to the one used in
    `julius.resample`. However, because we do not change the sample rate here,
    this filter can be much more efficiently implemented using the FFT convolution from
    `julius.fftconv`.

    Args:
        cutoffs (list[float]): list of cutoff frequencies, in [0, 0.5] expressed as `f/f_s` where
            f_s is the samplerate and `f` is the cutoff frequency.
            The upper limit is 0.5, because a signal sampled at `f_s` contains only
            frequencies under `f_s / 2`.
        stride (int): how much to decimate the output. Keep in mind that decimation
            of the output is only acceptable if the cutoff frequency is under `1/ (2 * stride)`
            of the original sampling rate.
        pad (bool): if True, appropriately pad the input with zero over the edge. If `stride=1`,
            the output will have the same length as the input.
        zeros (float): Number of zero crossings to keep.
            Controls the receptive field of the Finite Impulse Response filter.
            For lowpass filters with low cutoff frequency, e.g. 40Hz at 44.1kHz,
            it is a bad idea to set this to a high value.
            This is likely appropriate for most use. Lower values
            will result in a faster filter, but with a slower attenuation around the
            cutoff frequency.
        fft (bool or None): if True, uses `julius.fftconv` rather than PyTorch convolutions.
            If False, uses PyTorch convolutions. If None, either one will be chosen automatically
            depending on the effective filter size.


    ..warning::
        All the filters will use the same filter size, aligned on the lowest
        frequency provided. If you combine a lot of filters with very diverse frequencies, it might
        be more efficient to split them over multiple modules with similar frequencies.

    ..note::
        A lowpass with a cutoff frequency of 0 is defined as the null function
        by convention here. This allows for a highpass with a cutoff of 0 to
        be equal to identity, as defined in `julius.filters.HighPassFilters`.

    Shape:

        - Input: `[*, T]`
        - Output: `[F, *, T']`, with `T'=T` if `pad` is True and `stride` is 1, and
            `F` is the numer of cutoff frequencies.

    >>> lowpass = LowPassFilters([1/4])
    >>> x = torch.randn(4, 12, 21, 1024)
    >>> list(lowpass(x).shape)
    [1, 4, 12, 21, 1024]
    r   T   Ncutoffsstridepadzerosfftc                    t                                                       t          |          | _        t	          | j                  dk     rt          d          t          | j                  dk    rt          d          || _        || _        || _	        t          |t	          d | j        D                       z  dz            | _        || j        dk    }|| _        t          j        d| j        z  dz   d	
          }t          j        | j         | j        dz             }g }|D ]t}	|	dk    rt          j        |          }
nBd|	z  |z  t#          d|	z  t$          j        z  |z            z  }
|
|
                                z  }
|                    |
           u|                     dt          j        |          d d d f                    d S )Nr   z(Minimum cutoff must be larger than zero.g      ?z'A cutoff above 0.5 does not make sense.c                     g | ]
}|d k    |S )r    ).0cs     T/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/julius/lowpass.py
<listcomp>z+LowPassFilters.__init__.<locals>.<listcomp>S   s    )K)K)KQUU!UUU           r   F)periodicfilters)super__init__listr   min
ValueErrormaxr   r   r   int	half_sizer   torchhann_windowarange
zeros_liker   mathpisumappendregister_bufferstack)selfr   r   r   r   r   windowtimer   cutofffilter_	__class__s              r   r    zLowPassFilters.__init__H   s   G}}t|q  GHHHt|s""FGGG
US)K)KT\)K)K)K%L%LLqPQQ;.2%C"1t~#5#9EJJJ|T^OT^a-?@@ 	$ 	$F{{*400f*v-QZ$'5ID5P0Q0QQ 7;;==(NN7####YG(<(<QQQW(EFFFFFr   c                    t          |j                  }|                    dd|d                   }| j        r#t	          j        || j        | j        fd          }| j        rt          || j        | j	                  }n!t	          j
        || j        | j	                  }|                    dt          | j                             |j        d         |d<   |                    ddd                              |          S )Nr   	replicate)mode)r   r   r   )r!   shapeviewr   Fr&   r   r   r   r   conv1dinsertlenr   permutereshape)r1   inputr;   outs       r   forwardzLowPassFilters.forwarde   s    U[!!

2q%),,8 	UE%$.$.!ATTTE8 	DUDLEEECC(5$,t{CCCCQDL))***IbMb	{{1a##++E222r   c                      t          |           S Nr	   r1   s    r   __repr__zLowPassFilters.__repr__r       4   r   r   Tr   N)__name__
__module____qualname____doc__r   floatr%   boolr   r    rE   rI   __classcell__r6   s   @r   r   r      s        3 3j OS9=G G G Gt GG(0G G G G G G:3 3 3! ! ! ! ! ! !r   r   c                        e Zd ZdZ	 	 ddededed	ed
ee         f
 fdZe	d             Z
e	d             Ze	d             Ze	d             Ze	d             Zd Zd Z xZS )LowPassFiltera7  
    Same as `LowPassFilters` but applies a single low pass filter.

    Shape:

        - Input: `[*, T]`
        - Output: `[*, T']`, with `T'=T` if `pad` is True and `stride` is 1.

    >>> lowpass = LowPassFilter(1/4, stride=2)
    >>> x = torch.randn(4, 124)
    >>> list(lowpass(x).shape)
    [4, 62]
    r   Tr   Nr4   r   r   r   r   c                 z    t                                                       t          |g||||          | _        d S rG   )r   r    r   
_lowpasses)r1   r4   r   r   r   r   r6   s         r   r    zLowPassFilter.__init__   s6    (&63sKKr   c                 &    | j         j        d         S Nr   )rW   r   rH   s    r   r4   zLowPassFilter.cutoff   s    &q))r   c                     | j         j        S rG   )rW   r   rH   s    r   r   zLowPassFilter.stride   s    %%r   c                     | j         j        S rG   )rW   r   rH   s    r   r   zLowPassFilter.pad       ""r   c                     | j         j        S rG   )rW   r   rH   s    r   r   zLowPassFilter.zeros   s    $$r   c                     | j         j        S rG   )rW   r   rH   s    r   r   zLowPassFilter.fft   r\   r   c                 8    |                      |          d         S rY   )rW   )r1   rC   s     r   rE   zLowPassFilter.forward   s    u%%a((r   c                      t          |           S rG   r	   rH   s    r   rI   zLowPassFilter.__repr__   rJ   r   rK   )rL   rM   rN   rO   rP   r%   rQ   r   r    propertyr4   r   r   r   r   rE   rI   rR   rS   s   @r   rU   rU   v   s0         DH9=L Lu Lc LD LL(0L L L L L L
 * * X* & & X& # # X# % % X% # # X#) ) )! ! ! ! ! ! !r   rU   Tr   rC   r   r   r   r   r   c                 `     t          |||||                              |           |           S )z[
    Functional version of `LowPassFilters`, refer to this class for more information.
    )r   to)rC   r   r   r   r   r   s         r   lowpass_filtersrd      s3     F>'63s;;>>uEEeLLLr   r4   c                 8    t          | |g||||          d         S )z
    Same as `lowpass_filters` but with a single cutoff frequency.
    Output will not have a dimension inserted in the front.
    r   )rd   )rC   r4   r   r   r   r   s         r   lowpass_filterrf      s#     56(FCDDQGGr   rK   )rO   r+   typingr   r   r'   torch.nnr   r=   corer   fftconvr   utilsr
   nnModuler   rU   TensorrP   r%   rQ   rd   rf   r   r   r   <module>ro      s     % % % % % % % %  $ $ $ $ $ $                  a! a! a! a! a!UX_ a! a! a!H,! ,! ,! ,! ,!EHO ,! ,! ,!` 26<@M M5< M8E? MM*.M M+3D>M M M M 15;?H H%, H HH)-HH*24.H H H H H Hr   