---
title: Signal to Distortion Ratio (SDR)
description: Calculate Signal to Distortion Ratio (SDR) metric.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/audio/signal_distortion_ratio'
---
# Signal to Distortion Ratio (SDR) {#signal-to-distortion-ratio-sdr}

## Module Interface {#module-interface}

### `class torchmetrics.audio.SignalDistortionRatio(use_cg_iter=None, filter_length=512, zero_mean=False, load_diag=None, **kwargs)` {#torchmetrics.audio.SignalDistortionRatio}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/audio/sdr.py#L38-L170)

Calculate Signal to Distortion Ratio (SDR) metric.

See [SDR ref1](https://ieeexplore.ieee.org/abstract/document/1643671) and [SDR ref2](https://arxiv.org/abs/2110.06440) for details on the metric.

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

*   `preds` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): float tensor with shape `(...,time)`
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): float tensor with shape `(...,time)`
    

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

*   `sdr` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): float scalar tensor with average SDR value over samples
    

Parameters:

*   **use\_cg\_iter** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")\]) – If provided, conjugate gradient descent is used to solve for the distortion filter coefficients instead of direct Gaussian elimination, which requires that `fast-bss-eval` is installed and pytorch version >= 1.8. This can speed up the computation of the metrics in case the filters are long. Using a value of 10 here has been shown to provide good accuracy in most cases and is sufficient when using this loss to train neural separation networks.
    
*   **filter\_length** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – The length of the distortion filter allowed
    
*   **zero\_mean** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – When set to True, the mean of all signals is subtracted prior to computation of the metrics
    
*   **load\_diag** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]) – If provided, this small value is added to the diagonal coefficients of the system metrics when solving for the filter coefficients. This can help stabilize the metric in the case where some reference signals may sometimes be zero
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Example

```python
>>> from torch import randn
>>> from torchmetrics.audio import SignalDistortionRatio
>>> preds = randn(8000)
>>> target = randn(8000)
>>> sdr = SignalDistortionRatio()
>>> sdr(preds, target)
tensor(-11.9930)
>>> # use with pit
>>> from torchmetrics.audio import PermutationInvariantTraining
>>> from torchmetrics.functional.audio import signal_distortion_ratio
>>> preds = randn(4, 2, 8000)  # [batch, spk, time]
>>> target = randn(4, 2, 8000)
>>> pit = PermutationInvariantTraining(signal_distortion_ratio,
...     mode="speaker-wise", eval_func="max")
>>> pit(preds, target)
tensor(-11.7277)
```

#### `plot(val=None, ax=None)` {#torchmetrics.audio.SignalDistortionRatio.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/audio/sdr.py#L132-L170)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.audio import SignalDistortionRatio
>>> metric = SignalDistortionRatio()
>>> metric.update(torch.rand(8000), torch.rand(8000))
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/signal\_distortion\_ratio-1.png](/docs/torchmetrics/1.9.0/static/_images/signal_distortion_ratio-1.png)](/docs/torchmetrics/1.9.0/static/_images/signal_distortion_ratio-1.png)

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

[![../../\_images/signal\_distortion\_ratio-2.png](/docs/torchmetrics/1.9.0/static/_images/signal_distortion_ratio-2.png)](/docs/torchmetrics/1.9.0/static/_images/signal_distortion_ratio-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.audio.signal_distortion_ratio(preds, target, use_cg_iter=None, filter_length=512, zero_mean=False, load_diag=None)` {#torchmetrics.functional.audio.signal_distortion_ratio}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/audio/sdr.py#L88-L197)

Calculate Signal to Distortion Ratio (SDR) metric. See [SDR ref1](https://ieeexplore.ieee.org/abstract/document/1643671) and [SDR ref2](https://arxiv.org/abs/2110.06440) for details on the metric.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – float tensor with shape `(...,time)`
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – float tensor with shape `(...,time)`
    
*   **use\_cg\_iter** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")\]) – If provided, conjugate gradient descent is used to solve for the distortion filter coefficients instead of direct Gaussian elimination, which requires that `fast-bss-eval` is installed and pytorch version >= 1.8. This can speed up the computation of the metrics in case the filters are long. Using a value of 10 here has been shown to provide good accuracy in most cases and is sufficient when using this loss to train neural separation networks.
    
*   **filter\_length** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – The length of the distortion filter allowed
    
*   **zero\_mean** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – When set to True, the mean of all signals is subtracted prior to computation of the metrics
    
*   **load\_diag** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")\]) – If provided, this small value is added to the diagonal coefficients of the system metrics when solving for the filter coefficients. This can help stabilize the metric in the case where some reference signals may sometimes be zero
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

Returns:

Float tensor with shape `(...,)` of SDR values per sample

Raises:

[**RuntimeError**](https://docs.python.org/3/builtins/exceptions.html#RuntimeError "(in Python v3.15)") – If `preds` and `target` does not have the same shape

Example

```python
>>> from torch import randn
>>> from torchmetrics.functional.audio import signal_distortion_ratio
>>> preds = randn(8000)
>>> target = randn(8000)
>>> signal_distortion_ratio(preds, target)
tensor(-11.9930)
>>> # use with permutation_invariant_training
>>> from torchmetrics.functional.audio import permutation_invariant_training
>>> preds = randn(4, 2, 8000)  # [batch, spk, time]
>>> target = randn(4, 2, 8000)
>>> best_metric, best_perm = permutation_invariant_training(preds, target, signal_distortion_ratio)
>>> best_metric
tensor([-11.7748, -11.7948, -11.7160, -11.6254])
>>> best_perm
tensor([[1, 0],
        [1, 0],
        [1, 0],
        [0, 1]])
```

---

**Navigation**

- Parent: [Audio](/docs/torchmetrics/1.9.0/audio.md)
