o
    yio                  	   @   sb   d dl Z d dl mZ d dlmZ d dlmZ ddededed	efd
dZdeded	ef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}|r"|tj|ddd }| tj| ddd } ||  }tj|d dd| tj|d dd|  }dt| }|S )a  Calculates `Signal-to-noise ratio`_ (SNR_) meric for evaluating quality of audio. It is defined as:

    .. 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)dimkeepdim   )r   
   )r   torchfinfodtypeepsmeansumlog10)r   r   r   r   noise	snr_value r   U/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/audio/snr.pysignal_noise_ratio   s   
,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_ratioA   s   r   )F)	r   r   !torchmetrics.functional.audio.sdrr   torchmetrics.utilities.checksr   boolr   r   r   r   r   r   <module>   s   +