Accelerating the pace of engineering and science

# Documentation

## Configure Eb/No for AWGN Channels with Coding

This example shows how to set the bit energy to noise density ratio (Eb/No) for communication links employing channel coding.

Construct a (15,9) Reed-Solomon encoder and a 16-PSK modulator. Set the System objects™ so that they accept bit inputs.

```N = 15;
K = 9;
hEnc = comm.RSEncoder('CodewordLength', N, 'MessageLength', K, ...
'BitInput',true);
hMod = comm.PSKModulator('ModulationOrder', 16, 'BitInput', true);
```

Create the corresponding Reed-Solomon decoder and 16-PSK demodulator objects.

```hDec = comm.RSDecoder('CodewordLength', N,'MessageLength', K, ...
'BitInput',true);
hDemod = comm.PSKDemodulator('ModulationOrder', 16, 'BitOutput', true);
```

Calculate the Reed-Solomon code rate based on the ratio of message symbols to the codeword length.

```codeRate = hEnc.MessageLength/hEnc.CodewordLength;
```

Determine the bits per symbol for the PSK modulator.

```bitsPerSymbol = log2(hMod.ModulationOrder);
```

Set the uncoded Eb/No in dB.

```UncodedEbNo = 6;
```

Convert the uncoded Eb/No to the corresponding coded Eb/No using the code rate.

```CodedEbNo = UncodedEbNo + 10*log10(codeRate);
```

Construct an AWGN channel taking into account the number of bits per symbol. Set the EbNo property of hCh to the coded Eb/No.

```hCh = comm.AWGNChannel('BitsPerSymbol',bitsPerSymbol);
hCh.EbNo = CodedEbNo;
```

Set the random number generator seed so that results are repeatable.

```prevState = rng;
rng(577)
```

Set the total number of errors and bits for the simulation. For accuracy, the simulation should run until a sufficient number of bit errors are encountered. The number of total bits is used to ensure that the simulation does not run too long.

```totalErrors = 100;
totalBits = 1e6;
```

Construct an error rate calculator System object™ and initialize the error rate vector.

```hErr = comm.ErrorRate;
errorVec = zeros(1,3);
```

Run the simulation to determine the BER.

```while errorVec(2) < totalErrors && errorVec(3) < totalBits
% Generate random bits
dataIn = randi([0,1],360,1);
% Use the RS (15,9) encoder to add error correction capability
dataEnc = step(hEnc, dataIn);
% Apply 16-PSK modulation
txSig = step(hMod, dataIn);
% Pass the modulated data through the AWGN channel
rxSig = step(hCh, txSig);
demodData = step(hDemod, rxSig);
% Decode the demodulated data with the RS (15,9) decoder
dataOut = step(hDec, demodData);
% Collect error statistics
errorVec = step(hErr, dataIn, demodData);
end
```

Display the resultant bit error rate.

```ber = errorVec(1)
```
```ber =

0.1065

```

Return the random number generator to its initial state.

```rng(prevState)
```