# Signal-to-Noise Ratio (SNR)¶

## Module Interface¶

class torchmetrics.audio.SignalNoiseRatio(zero_mean=False, **kwargs)[source]

Calculate Signal-to-noise ratio (SNR) meric for evaluating quality of audio.

$\text{SNR} = \frac{P_{signal}}{P_{noise}}$

where $$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

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

Parameters:
Raises:

TypeError – if target and preds have a different shape

Example

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

plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters:
Return type:
Returns:

Figure and Axes object

Raises:

ModuleNotFoundError – If matplotlib is not installed

>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.audio import SignalNoiseRatio
>>> metric = SignalNoiseRatio()
>>> metric.update(torch.rand(4), torch.rand(4))
>>> fig_, ax_ = metric.plot()

>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.audio import SignalNoiseRatio
>>> metric = SignalNoiseRatio()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(torch.rand(4), torch.rand(4)))
>>> fig_, ax_ = metric.plot(values)


## Functional Interface¶

torchmetrics.functional.audio.signal_noise_ratio(preds, target, zero_mean=False)[source]

Calculate Signal-to-noise ratio (SNR) meric for evaluating quality of audio.

$\text{SNR} = \frac{P_{signal}}{P_{noise}}$

where $$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.

Parameters:
Return type:

Tensor

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)