o
    yi2                     @   s\   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	 G dd de	Z
G dd de	Zd	S )
    )Any)Tensortensor)"scale_invariant_signal_noise_ratiosignal_noise_ratio)Metricc                       s   e Zd ZU dZdZeed< dZeed< dZeed< e	ed< e	ed< 	dd	ed
e
ddf fddZde	de	ddfddZde	fddZ  ZS )SignalNoiseRatioa?  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.

    As input to `forward` and `update` the metric accepts the following input

    - ``preds`` (:class:`~torch.Tensor`): float tensor with shape ``(...,time)``
    - ``target`` (:class:`~torch.Tensor`): float tensor with shape ``(...,time)``

    As output of `forward` and `compute` the metric returns the following output

    - ``snr`` (:class:`~torch.Tensor`): float scalar tensor with average SNR value over samples

    Args:
        zero_mean: if to zero mean target and preds or not
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        TypeError:
            if target and preds have a different shape

    Example:
        >>> import torch
        >>> from torchmetrics import SignalNoiseRatio
        >>> target = torch.tensor([3.0, -0.5, 2.0, 7.0])
        >>> preds = torch.tensor([2.5, 0.0, 2.0, 8.0])
        >>> snr = SignalNoiseRatio()
        >>> snr(preds, target)
        tensor(16.1805)
    Ffull_state_updateTis_differentiablehigher_is_bettersum_snrtotal	zero_meankwargsreturnNc                    sD   t  jdi | || _| jdtddd | jdtddd d S )Nr           sumdefaultdist_reduce_fxr   r    )super__init__r   	add_stater   )selfr   r   	__class__r   J/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/audio/snr.pyr   ?   s   zSignalNoiseRatio.__init__predstargetc                 C   s8   t ||| jd}|  j| 7  _|  j| 7  _dS )*Update state with predictions and targets.)r   r   r   N)r   r   r   r   r   numel)r   r   r   	snr_batchr   r   r   updateJ   s   zSignalNoiseRatio.updatec                 C      | j | j S zComputes metric.)r   r   r   r   r   r   computeQ      zSignalNoiseRatio.compute)F)__name__
__module____qualname____doc__r	   bool__annotations__r
   r   r   r   r   r#   r'   __classcell__r   r   r   r   r      s"   
 "r   c                       sh   e Zd ZU dZdZeed< eed< dZdeddf fdd	Z	d
ededdfddZ
defddZ  ZS )ScaleInvariantSignalNoiseRatioa  Calculates `Scale-invariant signal-to-noise ratio`_ (SI-SNR) metric for evaluating quality of audio.

    As input to `forward` and `update` the metric accepts the following input

    - ``preds`` (:class:`~torch.Tensor`): float tensor with shape ``(...,time)``
    - ``target`` (: :class:`~torch.Tensor`): float tensor with shape ``(...,time)``

    As output of `forward` and `compute` the metric returns the following output

    - ``si_snr`` (: :class:`~torch.Tensor`): float scalar tensor with average SI-SNR value over samples

    Args:
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        TypeError:
            if target and preds have a different shape

    Example:
        >>> import torch
        >>> from torchmetrics import ScaleInvariantSignalNoiseRatio
        >>> target = torch.tensor([3.0, -0.5, 2.0, 7.0])
        >>> preds = torch.tensor([2.5, 0.0, 2.0, 8.0])
        >>> si_snr = ScaleInvariantSignalNoiseRatio()
        >>> si_snr(preds, target)
        tensor(15.0918)
    T
sum_si_snrr   r   r   Nc                    s>   t  jdi | | jdtddd | jdtddd d S )Nr1   r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   x   s   z'ScaleInvariantSignalNoiseRatio.__init__r   r   c                 C   s4   t ||d}|  j| 7  _|  j| 7  _dS )r    )r   r   N)r   r1   r   r   r!   )r   r   r   si_snr_batchr   r   r   r#      s   z%ScaleInvariantSignalNoiseRatio.updatec                 C   r$   r%   )r1   r   r&   r   r   r   r'      r(   z&ScaleInvariantSignalNoiseRatio.compute)r)   r*   r+   r,   r
   r   r.   r   r   r   r#   r'   r/   r   r   r   r   r0   V   s   
 	r0   N)typingr   torchr   r   !torchmetrics.functional.audio.snrr   r   torchmetrics.metricr   r   r0   r   r   r   r   <module>   s   @