
    WhN                     L    d dl Zd dlmZ d dlmZ d ZddZddZddZ	ddZ
dS )    N)gaussian_filter)gaussian_filter1dc                 D    ddt          j        | | |z
  z            z   z  S )Ng      ?)npexp)W	thresholdslopes      U/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/norbert/contrib.py_logitr      s'    rvufa	k233344       c                 "   t          j        | j                  j        }t          j        |t          j        |          |z            }t          j        | d          }	 t          j        ||z  dd          |t          j        |dz  dd          z   z  }| |d         z  }n!# t          $ r t          d           | }Y nw xY wt          j        | d          }t          j        d||z
            }t          j	        ||d         fd	          S )
a  Compute a model for the residual based on spectral subtraction.

    The method consists in two steps:

    * The provided spectrograms are summed up to obtain the *input* model for
      the mixture. This *input* model is scaled frequency-wise to best
      fit with the actual observed mixture spectrogram.

    * The residual model is obtained through spectral subtraction of the
      input model from the mixture spectrogram, with flooring to 0.

    Parameters
    ----------
    v: np.ndarray [shape=(nb_frames, nb_bins, {1, nb_channels}, nb_sources)]
        Estimated spectrograms for the sources

    x: np.ndarray [shape=(nb_frames, nb_bins, nb_channels)]
        complex mixture

    alpha: float [scalar]
        exponent for the spectrograms `v`. For instance, if `alpha==1`,
        then `v` must be homogoneous to magnitudes, and if `alpha==2`, `v`
        must homogeneous to squared magnitudes.

    Returns
    -------
    v: np.ndarray [shape=(nb_frames, nb_bins, nb_channels, nb_sources+1)]
        Spectrograms of the sources, with an appended one for the residual.

    Note
    ----
    It is not mandatory to input multichannel spectrograms. However, the
    output spectrograms *will* be multichannel.

    Warning
    -------
    You must be careful to set `alpha` as the exponent that corresponds to `v`.
    In other words, *you must have*: ``np.abs(x)**alpha`` homogeneous to `v`.
    )axisr   Tr   keepdims   ).NziAutomatic scaling for residual model failed. This is probably due to a very long file. Trying without it.   )
r   finfodtypeepsmaximumabssum	Exceptionprintconcatenate)	vxalphar   vxv_totalgainv_gvrs	            r   residual_modelr'   
   s-   R (17


C 
CE)	*	*B fQR   G	F2g:A555
T::::< 	 $y/!     	 	 	 	 fQR   G 
ArG|	$	$B>39.Q7777s    AB$ $CCFc                 N    |rt          | |d          S t          | ||          S )a  
    smoothes a ndarray with a Gaussian blur.

    Parameters
    ----------
    v: np.ndarray [shape=(nb_frames, ...)]
        input array

    sigma: int [scalar]
        lengthscale of the gaussian blur

    temporal: boolean
        if True, will smooth only along time through 1d blur. Will use a
        multidimensional Gaussian blur otherwise.

    Returns
    -------
    result: np.ndarray [shape=(nb_frames, ...)]
        filtered array

    r   )sigmar   )r)   truncate)r   r   )r   widthtemporals      r   smoothr-   Q   s6    ,  ? %a8888q>>>>r   333333?   c                     t          j        t           j                  j        }t	          | d          }|t          j        |dd          z   }t          ||z  dd          | z  } | S )u  
    Reduction of interferences between spectrograms.

    The objective of the method is to redistribute the energy of the input in
    order to "sparsify" spectrograms along the "source" dimension. This is
    motivated by the fact that sources are somewhat sparse and it is hence
    unlikely that they are all energetic at the same time-frequency bins.

    The method is inspired from [1]_ with ad-hoc modifications.

    References
    ----------

   .. [1] Thomas Prätzlich, Rachel Bittner, Antoine Liutkus, Meinard Müller.
           "Kernel additive modeling for interference reduction in multi-
           channel music recordings" Proc. of ICASSP 2015.

    Parameters
    ----------
    v: np.ndarray [shape=(..., nb_sources)]
        non-negative data on which to apply interference reduction

    thresh: float [scalar]
        threshold for the compression, should be between 0 and 1. The closer
        to 1, the more reduction of the interferences, at the price of more
        distortion.

    slope: float [scalar]
            the slope at which binarization is done. The higher, the more
            brutal

    Returns
    -------
    v: np.ndarray [same shape as input]
        `v` with reduced interferences

    
   r   Tr   g?r/   )r   r   float32r   r-   r   r   )r   threshr
   r   vsmoothtotal_energys         r   reduce_interferencesr6   m   sa    L (2:


"CQmmGb4@@@@Lw|#S"--1AHr   c                     t          j        |           j        }| j        d         }|dk    r8t          j        | dd          }| t          |||          ||z   z  d         z  } nt          | ||          } | S )af  Applies a logit compression to a filter. This enables to "binarize" a
    separation filter. This allows to reduce interferences at the price
    of distortion.

    In the case of multichannel filters, decomposes them as the cascade of a
    pure beamformer (selection of one direction in space), followed by a
    single-channel mask. Then, compression is applied on the mask only.

    Parameters
    ----------
    W: ndarray, shape=(..., nb_channels, nb_channels)
        filter on which to apply logit compression.

    thresh: float
        threshold for the compression, should be between 0 and 1. The closer
        to 1, the less interferences, but the more distortion.

    slope: float
        the slope at which binarization is done. The higher, the more brutal

    Returns
    -------
    W: np.ndarray [same shape as input]
        Compressed filter
    r   r   )axis1axis2).NN)r   r   r   shapetracer   )r   r3   r
   r   nb_channelsgainss         r   compress_filterr?      sy    6 (1++/C'"+KQ"B///	fUFE**cEk:OLL1fe$$Hr   )r   )r   F)r.   r/   )numpyr   scipy.ndimager   scipy.ndimage.filtersr   r   r'   r-   r6   r?    r   r   <module>rD      s        ) ) ) ) ) ) 3 3 3 3 3 35 5 5D8 D8 D8 D8N? ? ? ?8* * * *Z" " " " " "r   