---
title: Perceptual Evaluation of Speech Quality (PESQ)
description: Calculate Perceptual Evaluation of Speech Quality (PESQ).
canonical: >-
  https://lightning.ai/docs/torchmetrics/stable/audio/perceptual_evaluation_speech_quality
---
# Perceptual Evaluation of Speech Quality (PESQ) {#perceptual-evaluation-of-speech-quality-pesq}

## Module Interface {#module-interface}

### `class torchmetrics.audio.pesq.PerceptualEvaluationSpeechQuality(fs, mode, n_processes=1, **kwargs)` {#torchmetrics.audio.pesq.PerceptualEvaluationSpeechQuality}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/audio/pesq.py#L30-L175)

Calculate [Perceptual Evaluation of Speech Quality](https://en.wikipedia.org/wiki/Perceptual_Evaluation_of_Speech_Quality) (PESQ).

It’s a recognized industry standard for audio quality that takes into considerations characteristics such as: audio sharpness, call volume, background noise, clipping, audio interference etc. PESQ returns a score between -0.5 and 4.5 with the higher scores indicating a better quality.

This metric is a wrapper for the [pesq package](https://github.com/ludlows/python-pesq). Note that input will be moved to `cpu` to perform the metric calculation.

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

*   `pesq` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): float tensor of PESQ value reduced across the batch
    

Hint

Using this metrics requires you to have `pesq` install. Either install as `pip install torchmetrics[audio]` or `pip install pesq`. `pesq` will compile with your currently installed version of numpy, meaning that if you upgrade numpy at some point in the future you will most likely have to reinstall `pesq`.

Caution

The `forward` and `compute` methods in this class return a single (reduced) PESQ value for a batch. To obtain a PESQ value for each sample, you may use the functional counterpart in [`perceptual_evaluation_speech_quality()`](#torchmetrics.functional.audio.pesq.perceptual_evaluation_speech_quality "torchmetrics.functional.audio.pesq.perceptual_evaluation_speech_quality").

Parameters:

*   **fs** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – sampling frequency, should be 16000 or 8000 (Hz)
    
*   **mode** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")) – `'wb'` (wide-band) or `'nb'` (narrow-band)
    
*   **keep\_same\_device** – whether to move the pesq value to the device of preds
    
*   **n\_processes** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – integer specifying the number of processes to run in parallel for the metric calculation. Only applies to batches of data and if `multiprocessing` package is installed.
    
*   **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.
    

Raises:

*   [**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If `pesq` package is not installed
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `fs` is not either `8000` or `16000`
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `mode` is not either `"wb"` or `"nb"`
    

Example

```python
>>> from torch import randn
>>> from torchmetrics.audio import PerceptualEvaluationSpeechQuality
>>> preds = randn(8000)
>>> target = randn(8000)
>>> pesq = PerceptualEvaluationSpeechQuality(8000, 'nb')
>>> pesq(preds, target)
tensor(2.2885)
>>> wb_pesq = PerceptualEvaluationSpeechQuality(16000, 'wb')
>>> wb_pesq(preds, target)
tensor(1.6805)
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/audio/pesq.py#L137-L175)

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 PerceptualEvaluationSpeechQuality
>>> metric = PerceptualEvaluationSpeechQuality(8000, 'nb')
>>> metric.update(torch.rand(8000), torch.rand(8000))
>>> fig_, ax_ = metric.plot()
```

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

## Functional Interface {#functional-interface}

### `torchmetrics.functional.audio.pesq.perceptual_evaluation_speech_quality(preds, target, fs, mode, keep_same_device=False, n_processes=1)` {#torchmetrics.functional.audio.pesq.perceptual_evaluation_speech_quality}

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

Calculate [Perceptual Evaluation of Speech Quality](https://en.wikipedia.org/wiki/Perceptual_Evaluation_of_Speech_Quality) (PESQ).

It’s a recognized industry standard for audio quality that takes into considerations characteristics such as: audio sharpness, call volume, background noise, clipping, audio interference etc. PESQ returns a score between -0.5 and 4.5 with the higher scores indicating a better quality.

This metric is a wrapper for the [pesq package](https://github.com/ludlows/python-pesq). Note that input will be moved to cpu to perform the metric calculation.

Hint

Usingsing this metrics requires you to have `pesq` install. Either install as `pip install torchmetrics[audio]` or `pip install pesq`. Note that `pesq` will compile with your currently installed version of numpy, meaning that if you upgrade numpy at some point in the future you will most likely have to reinstall `pesq`.

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)`
    
*   **fs** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – sampling frequency, should be 16000 or 8000 (Hz)
    
*   **mode** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")) – `'wb'` (wide-band) or `'nb'` (narrow-band)
    
*   **keep\_same\_device** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – whether to move the pesq value to the device of preds
    
*   **n\_processes** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – integer specifying the number of processes to run in parallel for the metric calculation. Only applies to batches of data and if `multiprocessing` package is installed.
    

Return type:

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

Returns:

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

Raises:

*   [**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If `pesq` package is not installed
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `fs` is not either `8000` or `16000`
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `mode` is not either `"wb"` or `"nb"`
    
*   [**RuntimeError**](https://docs.python.org/3/builtins/exceptions.html#RuntimeError "(in Python v3.15)") – If `preds` and `target` do not have the same shape
    

Example

```python
>>> from torch import randn
>>> from torchmetrics.functional.audio.pesq import perceptual_evaluation_speech_quality
>>> preds = randn(8000)
>>> target = randn(8000)
>>> perceptual_evaluation_speech_quality(preds, target, 8000, 'nb')
tensor(2.2885)
>>> perceptual_evaluation_speech_quality(preds, target, 16000, 'wb')
tensor(1.6805)
```

---

**Navigation**

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