
    i              	       x    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	Zdededefd
ZddedededefdZ	y)    N)Tensor'scale_invariant_signal_distortion_ratio)_check_same_shapepredstarget	zero_meanreturnc                    t        | |       t        j                  | j                        j                  }|r6|t        j
                  |dd      z
  }| t        j
                  | dd      z
  } || z
  }t        j                  |dz  d      |z   t        j                  |dz  d      |z   z  }dt        j                  |      z  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_values         v/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/audio/snr.pysignal_noise_ratior      s    : eV$
++ekk
"
&
&C%**VTBB

5b$??UNE619"-3		%(PR8SVY8YZII&&&    c                     t        | |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   s     r   "scale_invariant_signal_noise_ratior   @   s    . 3vY]^^r   c                    | j                         rt        j                  |       } |j                         rt        j                  |      }| j                  dk  s3| j                  d   dk7  s!|j                  dk  s|j                  d   dk7  r&t        d| j                   d|j                   d       | j                  g | j                  dd d }  |j                  g |j                  dd d }t        | ||	      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   s      r   *complex_scale_invariant_signal_noise_ratior*   Z   s    6 ""5)##F+

Q%++b/Q.FKK!Ov||TVGW[\G\}E&,,q:
 	

 EMM05;;s+0R0EV^^3V\\#2.33F2vYbccr   )F)
r   r   !torchmetrics.functional.audio.sdrr   torchmetrics.utilities.checksr   boolr   r   r*    r   r   <module>r/      s|      U ;''f ''f '' ''RX ''T_f _f _ _4)df )df )dY] )djp )dr   