# biterr

Compute number of bit errors and bit error rate (BER)

## Syntax

## Description

`[`

compares the unsigned binary representation of elements in `number`

,`ratio`

] = biterr(`x,y`

)`x`

to those
in `y`

. The function returns `number`

, the number of
bits that differ in the comparison, and `ratio`

, the ratio of
`number`

to the total number of bits. The function determines the order
in which it compares `x`

and `y`

based on their sizes.
For more information, see the Algorithms section.

`[`

returns the binary comparison result of `number`

,`ratio`

,`individual`

] = biterr(___)`x`

and `y`

as
matrix `individual`

. You can specify any of the input argument
combination from the previous syntaxes.

## Examples

### Bit Error Rate Computation

Create two binary matrices.

x = [0 0; 0 0; 0 0; 0 0]

`x = `*4×2*
0 0
0 0
0 0
0 0

y = [0 0; 0 0; 0 0; 1 1]

`y = `*4×2*
0 0
0 0
0 0
1 1

Determine the number of bit errors.

numerrs = biterr(x,y)

numerrs = 2

Compute the number of column-wise errors.

`numerrs = biterr(x,y,[],'column-wise')`

`numerrs = `*1×2*
1 1

Compute the number of row-wise errors.

`numerrs = biterr(x,y,[],'row-wise')`

`numerrs = `*4×1*
0
0
0
2

Compute the number of overall errors. Behavior is the same as the default behavior.

`numerrs = biterr(x,y,[],'overall')`

numerrs = 2

### Estimate Bit Error Rate for 64-QAM in AWGN

Demodulate a noisy 64-QAM signal and estimate the bit error rate (BER) for a range of Eb/No values. Compare the BER estimate to theoretical values.

Set the simulation parameters.

M = 64; % Modulation order k = log2(M); % Bits per symbol EbNoVec = (5:15); % Eb/No values (dB) numSymPerFrame = 100; % Number of QAM symbols per frame

Convert the EbN0 values to SNR.

snrdB =convertSNR(EbNoVec,"ebno","snr",BitsPerSymbol=k);

Initialize the results vector.

berEst = zeros(size(EbNoVec));

The main processing loop executes these steps.

Generate binary data and convert to 64-ary symbols.

QAM-modulate the data symbols.

Pass the modulated signal through an AWGN channel.

Demodulate the received signal.

Convert the demodulated symbols into binary data.

Calculate the number of bit errors.

The `while`

loop continues to process data until either 200 errors are encountered or 1e7 bits are transmitted.

for n = 1:length(snrdB) % Reset the error and bit counters numErrs = 0; numBits = 0; while numErrs < 200 && numBits < 1e7 % Generate binary data and convert to symbols dataIn = randi([0 1],numSymPerFrame*k,1); dataSym = bit2int(dataIn,k); % QAM modulate using 'Gray' symbol mapping txSig = qammod(dataSym,M); % Pass through AWGN channel rxSig = awgn(txSig,snrdB(n),'measured'); % Demodulate the noisy signal rxSym = qamdemod(rxSig,M); % Convert received symbols to bits dataOut = int2bit(rxSym,k); % Calculate the number of bit errors nErrors = biterr(dataIn,dataOut); % Increment the error and bit counters numErrs = numErrs + nErrors; numBits = numBits + numSymPerFrame*k; end % Estimate the BER berEst(n) = numErrs/numBits; end

Determine the theoretical BER curve by using the `berawgn`

function.

`berTheory = berawgn(EbNoVec,'qam',M);`

Plot the estimated and theoretical BER data. The estimated BER data points are well aligned with the theoretical curve.

semilogy(EbNoVec,berEst,'*') hold on semilogy(EbNoVec,berTheory) grid legend('Estimated BER','Theoretical BER') xlabel('Eb/No (dB)') ylabel('Bit Error Rate')

## Input Arguments

`x,y`

— Inputs to be compared (as separate arguments)

vector | matrix

Inputs to be compared, specified as separate arguments, as a vector or matrix of
nonnegative integer elements. The function converts each element of
`x`

and `y`

to its unsigned binary
representation for comparison.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `logical`

`k`

— Maximum number of bits for input elements

positive integer

Maximum number of bits for input elements of `x`

and
`y`

, specified as a positive integer. If the number of bits
required for binary representation of any element in `x`

or
`y`

is greater than `k`

, the function
errors.

If you do not set `k`

, the function sets it as the number of bits
in the binary representation of the largest element in `x`

and
`y`

.

**Data Types: **`single`

| `double`

`flag`

— Flag to override default settings

`'overall'`

| `'row-wise'`

| `'column-wise'`

Flag to override default settings of the function, specified as
`'overall'`

, `'row-wise'`

, or
`'column-wise'`

. Flag specifies how the function compares elements in
inputs `x,y`

and computes the output. For more information, see the
Algorithms
section.

**Data Types: **`string`

| `char`

## Output Arguments

`number`

— Number of bit errors

nonnegative integer | integer vector

Number of bit errors, returned as a nonnegative integer or integer vector.

**Data Types: **`single`

| `double`

`individual`

— Results of each individual binary comparison

matrix

Results of each individual binary comparison, returned as a matrix whose dimensions
are those of the larger of inputs `x`

and `y`

.
Each element specifies the number of bits by which the elements in the pair differ. For
more information, see the Algorithms
section.

**Data Types: **`single`

| `double`

## Algorithms

### Comparing Inputs Based on Sizes

The function uses the sizes of `x`

and `y`

to
determine the order in which it compares their elements.

If inputs are matrices of the same dimensions, then the function compares the inputs element by element.

`number`

is a nonnegative integer in this case. For example, see case (a) in the figure.If one input is a matrix and the other input is a column vector, then the function compares each column of the matrix element by element with the column vector. The number of rows in the matrix must be equal to the length of the column vector. In other words, if the matrix has dimensions

*m*-by-*n*, then the column vector must have dimensions*m*-by-1. For example, see case (b) in the figure.If one input is a matrix and the other input is a row vector, then the function compares each row of the matrix element by element with the row vector. The number of columns in the matrix must be equal to the length of the row vector. In other words, if the matrix has dimensions

*m*-by-*n*, then the row vector must have dimensions 1-by-*n*. For example, see case (c) in the figure.

### Comparing Inputs Based on Flag

This table describes how the output is computed based on the different values of
`flag`

. `x`

is considered as a matrix in this table
and the size of `y`

is varied.

Size of `y` | `flag` Value | Type of Comparison | `number` Value | Total Number of Bits |
---|---|---|---|---|

Matrix | `'overall'` (default) | Element by element | Total number of bit errors | `k` times the number of elements in
`y` |

`'row-wise'` | mth row of `x` to
mth row of `y` | Column vector whose elements represent the bit errors in each row | `k` times the number of elements in
`y` | |

`'column-wise'` | mth column of `x` to
mth column of `y`
| Row vector whose elements represent the bit errors in each column | `k` times the number of elements in
`y` | |

Row vector | `'overall'` | `y` to each row of `x` | Total number of bit errors | `k` times the number of elements of
`x` |

`'row-wise'` (default) | `y` to each row of `x` | Column vector whose elements represent the bit errors in each row of
`x` | `k` times the size of `y` | |

Column vector | `'overall'` | `y` to each column of `x` | Total number of bit errors | `k` times the number of elements of
`x` |

`'column-wise'` (default) | `y` to each column of `x` | Row vector whose elements represent bit errors in each column of
`x` | `k` times the size of `y` |

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function supports GPU array inputs. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

## Version History

**Introduced before R2006a**

### R2024a: Add GPU array support

The `biterr`

function adds support for `gpuArray`

(Parallel Computing Toolbox) object processing to run code on a graphics processing unit
(GPU).

## See Also

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)