a
    d                     @   sn   d dl Z d dl mZ d dlmZ d dlmZ deeeedddZeeed	d
dZdeeeedddZ	dS )    N)Tensor'scale_invariant_signal_distortion_ratio)_check_same_shapeF)predstarget	zero_meanreturnc                 C   s   t | | t| jj}|rD|tj|ddd }| tj| ddd } ||  }tj|d dd| tj|d dd|  }dt| S )a  Calculate `Signal-to-noise ratio`_ (SNR_) meric for evaluating quality of audio.

    .. math::
        \text{SNR} = \frac{P_{signal}}{P_{noise}}

    where  :math:`P` denotes the power of each signal. The SNR metric compares the level of the desired signal to
    the level of background noise. Therefore, a high value of SNR means that the audio is clear.

    Args:
        preds: float tensor with shape ``(...,time)``
        target: float tensor with shape ``(...,time)``
        zero_mean: if to zero mean target and preds or not

    Returns:
        Float tensor with shape ``(...,)`` of SNR values per sample

    Raises:
        RuntimeError:
            If ``preds`` and ``target`` does not have the same shape

    Example:
        >>> from torchmetrics.functional.audio import signal_noise_ratio
        >>> target = torch.tensor([3.0, -0.5, 2.0, 7.0])
        >>> preds = torch.tensor([2.5, 0.0, 2.0, 8.0])
        >>> signal_noise_ratio(preds, target)
        tensor(16.1805)

    T)dimZkeepdim   )r   
   )r   torchZfinfoZdtypeepsmeansumlog10)r   r   r   r   ZnoiseZ	snr_value r   j/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/audio/snr.pysignal_noise_ratio   s    
,r   )r   r   r	   c                 C   s   t | |ddS )a  `Scale-invariant signal-to-noise ratio`_ (SI-SNR).

    Args:
        preds: float tensor with shape ``(...,time)``
        target: float tensor with shape ``(...,time)``

    Returns:
         Float tensor with shape ``(...,)`` of SI-SNR values per sample

    Raises:
        RuntimeError:
            If ``preds`` and ``target`` does not have the same shape

    Example:
        >>> import torch
        >>> from torchmetrics.functional.audio import scale_invariant_signal_noise_ratio
        >>> target = torch.tensor([3.0, -0.5, 2.0, 7.0])
        >>> preds = torch.tensor([2.5, 0.0, 2.0, 8.0])
        >>> scale_invariant_signal_noise_ratio(preds, target)
        tensor(15.0918)

    Tr   r   r   r   )r   r   r   r   r   "scale_invariant_signal_noise_ratio@   s    r   c                 C   s   |   rt| } |  r$t|}| jdk sT| jd dksT|jdk sT|jd dkr\td| jg | jdd dR  } |jg |jdd dR  }t| ||dS )a~  `Complex scale-invariant signal-to-noise ratio`_ (C-SI-SNR).

    Args:
        preds: real float tensor with shape ``(...,frequency,time,2)`` or complex float tensor with
            shape ``(..., frequency,time)``
        target: real float tensor with shape ``(...,frequency,time,2)`` or complex float tensor with
            shape ``(..., frequency,time)``
        zero_mean: When set to True, the mean of all signals is subtracted prior to computation of the metrics

    Returns:
         Float tensor with shape ``(...,)`` of C-SI-SNR values per sample

    Raises:
        RuntimeError:
            If ``preds`` is not the shape (...,frequency,time,2) (after being converted to real if it is complex).
            If ``preds`` and ``target`` does not have the same shape.

    Example:
        >>> import torch
        >>> from torchmetrics.functional.audio import complex_scale_invariant_signal_noise_ratio
        >>> g = torch.manual_seed(1)
        >>> preds = torch.randn((1,257,100,2))
        >>> target = torch.randn((1,257,100,2))
        >>> complex_scale_invariant_signal_noise_ratio(preds, target)
        tensor([-63.4849])

       r
   r   z{Predictions and targets are expected to have the shape (..., frequency, time, 2), but got {preds.shape} and {target.shape}.Nr   )Z
is_complexr   Zview_as_realndimshapeRuntimeErrorZreshaper   r   r   r   r   *complex_scale_invariant_signal_noise_ratioZ   s    

0r   )F)F)
r   r   Z!torchmetrics.functional.audio.sdrr   Ztorchmetrics.utilities.checksr   boolr   r   r   r   r   r   r   <module>   s   *