o
    .wi                  	   @   s~   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de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  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)dimkeepdim   )r   
   )r   torchfinfodtypeepsmeansumlog10)r   r   r   r   noise	snr_value r   ^/home/ubuntu/sommelier/.venv/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_ratio@   s   r   c                 C   s   |   r	t| } |  rt|}| jdk s*| jd dks*|jdk s*|jd dkr7td| j d|j d| jg | jdd dR  } |jg |jdd dR  }t| ||d	S )
aX  `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:
        >>> from torch import randn
        >>> from torchmetrics.functional.audio import complex_scale_invariant_signal_noise_ratio
        >>> preds = randn((1,257,100,2))
        >>> target = randn((1,257,100,2))
        >>> complex_scale_invariant_signal_noise_ratio(preds, target)
        tensor([-38.8832])

       r
   r   zZPredictions and targets are expected to have the shape (..., frequency, time, 2), but got z and .Nr   )
is_complexr   view_as_realndimshapeRuntimeErrorreshaper   r   r   r   r   *complex_scale_invariant_signal_noise_ratioZ   s   

0r&   )F)
r   r   !torchmetrics.functional.audio.sdrr   torchmetrics.utilities.checksr   boolr   r   r&   r   r   r   r   <module>   s   * 