# psnr

Peak signal-to-noise ratio (PSNR)

## Syntax

``peaksnr = psnr(A,ref)``
``peaksnr = psnr(A,ref,peakval)``
``````[peaksnr,snr] = psnr(___)``````

## Description

example

````peaksnr = psnr(A,ref)` calculates the peak signal-to-noise ratio (PSNR) for the image `A`, with the image `ref` as the reference.```
````peaksnr = psnr(A,ref,peakval)` calculates the PSNR of image `A` using the peak signal value `peakval`.```
``````[peaksnr,snr] = psnr(___)``` also returns the simple signal-to-noise ratio, `snr`.```

## Examples

collapse all

Read image and create a copy with added noise. The original image is the reference image.

```ref = imread('pout.tif'); A = imnoise(ref,'salt & pepper', 0.02);```

Calculate the PSNR.

```[peaksnr, snr] = psnr(A, ref); fprintf('\n The Peak-SNR value is %0.4f', peaksnr);```
``` The Peak-SNR value is 22.6437 ```
`fprintf('\n The SNR value is %0.4f \n', snr);`
``` The SNR value is 15.5524 ```

## Input Arguments

collapse all

Image to be analyzed, specified as a numeric array of any dimension.

Data Types: `single` | `double` | `int16` | `uint8` | `uint16`

Reference image, specified as a numeric array of the same size and data type as image `A`.

Data Types: `single` | `double` | `int16` | `uint8` | `uint16`

Peak signal level, specified as a nonnegative number. If not specified, the default value for `peakval` depends on the class of `A` and `ref`.

• If the images are of data type `double` or `single`, then `psnr` assumes that image data is in the range [0, 1]. The default value of `peakval` is `1`.

• If the images are of integer data types, then the default value of `peakval` is the largest value allowed by the range of the class. For `uint8` data, the default value of `peakval` is `255`. For `uint16` or `int16`, the default is `65535`.

## Output Arguments

collapse all

PSNR in decibels, returned as a scalar of type `double`, except if `A` and `ref` are of class `single`, in which case `peaksnr` is of class `single`.

Data Types: `single` | `double`

Signal-to-noise ratio in decibels, returned as a numeric scalar of type `double`, except if `A` and `ref` are of class `single`, in which case `peakval` is of type `single`.

Data Types: `single` | `double`

## Algorithms

The `psnr` function implements this equation to calculate PSNR:

`$PSNR=10{\mathrm{log}}_{10}\left(peakva{l}^{2}/MSE\right)$`

`peakval` is either specified by the user or taken from the range of the image data type. For example, for image of `uint8` data type, then `peakval` is `255`. MSE is the mean square error between `A` and `ref`.