**Use Forward Error Correction on 16-QAM Signal**

This example extends the Use Pulse Shaping on 16-QAM Signal example to show bit error rate (BER) performance improvement when using forward error correction (FEC) coding.

This example shows how to process a binary data stream by using a communications link that consists of a baseband modulator, channel, demodulator, pulse shaping, raised cosine filtering, and error correction.

**Establish Simulation Framework**

In this example, to achieve a more accurate BER estimate, the number of bits to process is increased from the value used in the Use Pulse Shaping on 16-QAM Signal example. Other simulation variables match the settings in that example.

Define simulation parameters for a 16-QAM modulation scheme with raised cosine filtering and an AWGN channel.

M = 16; % Modulation order k = log2(M); % Bits per symbol numBits = k*2.5e5; % Total bits to process sps = 4; % Samples per symbol (oversampling factor) filtlen = 10; % Filter length in symbols rolloff = 0.25; % Filter rolloff factor

**Generate Random Data**

Set the `rng`

function to its default state, or any static seed value, so that the example produces repeatable results. Then, use the `randi`

function to generate random binary data.

rng default; % Default random number generator dataIn = randi([0 1],numBits,1); % Generate vector of binary data

**Apply Convolutional Encoding**

To correct errors arising from the noisy channel, apply convolutional coding to the data before transmission and Viterbi decoding to the received data. The decoder uses a hard decision algorithm, which means each received data bit is interpreted as either `0`

or `1`

.

Define a convolutional coding trellis for a rate 2/3 code by using the `poly2trellis`

function. The defined trellis represents the convolutional code that the `convenc`

function uses for encoding the binary vector, `dataIn`

.

constrlen = [5 4]; % Code constraint length genpoly = [23 35 0; 0 5 13] % Generator polynomials

`genpoly = `*2×3*
23 35 0
0 5 13

tPoly = poly2trellis(constrlen,genpoly); codeRate = 2/3;

Encode the input data by using the `tPoly`

trellis.

dataEnc = convenc(dataIn,tPoly);

**Modulate Data**

Use the `bit2int`

function to convert the `k`

-tuple encoded binary data to an integer values.

dataSymbolsIn = bit2int(dataEnc,k);

Use the `qammod`

function to apply 16-QAM modulation.

dataMod = qammod(dataSymbolsIn,M);

**Apply Raised Cosine Filtering**

Use the `rcosdesign`

function to create an RRC filter.

rrcFilter = rcosdesign(rolloff,filtlen,sps);

Use the `upfirdn`

function to upsample the signal by the oversampling factor and apply the RRC filter. The `upfirdn`

function pads the upsampled signal with zeros at the end to flush the filter. Then, the function applies the filter.

txSignal = upfirdn(dataMod,rrcFilter,sps,1);

**Apply AWGN Channel**

Use the bits per symbol, samples per symbol, code rate, and the `convertSNR`

function to convert the ratio of energy per bit to noise power spectral density (`EbNo`

) to an SNR value for use by the `awgn`

function. When converting the ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ to SNR, you must account for the number of information bits per symbol. With no FEC applied, each symbol corresponded to `k`

bits. With FEC applied, each symbol corresponds to (`k`

$\times $ `codeRate`

) information bits. For the 2/3 code rate and 16-QAM transmissions used in this example, three symbols correspond to 12 coded bits and 8 uncoded (information) bits.

EbNo = 10; snr = convertSNR(EbNo,'ebno', ... samplespersymbol=sps, ... bitspersymbol=k,CodingRate=codeRate);

Pass the filtered signal through an AWGN channel.

`rxSignal = awgn(txSignal,snr,'measured');`

**Receive and Demodulate Signal**

Filter the received signal by using the RRC filter. Remove a portion of the signal to account for the filter delay.

rxFiltSignal = ... upfirdn(rxSignal,rrcFilter,1,sps); % Downsample and filter rxFiltSignal = ... rxFiltSignal(filtlen + 1:end - filtlen); % Account for delay

Use the `qamdemod`

function to demodulate the received filtered signal.

dataSymbOut = qamdemod(rxFiltSignal,M);

**Apply Viterbi Decoding**

Use the `int2bit`

function to convert the recovered integer symbols into binary data.

`codedDataOut = int2bit(dataSymbOut,k); % Return data in column vector`

Use the `vitdec`

function, configured for hard decisions and continuous operation mode, to decode the convolutionally encoded data. The continuous operation mode maintains the internal state when the decoder is repeatedly invoked, such as when receiving frames of data operating in a loop. The continuous operation mode also adds delay to the system. Although this example does not use a loop, the '`cont`

' mode is used for the purpose of illustrating how to compensate for the delay in this decoding operation.

traceBack = 16; % Decoding traceback length numCodeWords = ... floor(length(codedDataOut)*2/3); % Number of complete codewords dataOut = ... vitdec(codedDataOut(1:numCodeWords*3/2), ... tPoly,traceBack,'cont','hard'); % Decode data

### Compute System BER

The delay introduced by the transmit and receive RRC filters is already accounted for in the recovered data, but the decoder delay is not accounted for yet. The continuous operation mode of the Viterbi decoder incurs a delay with a duration in bits equal to the traceback length, `traceBack`

, times the number of input streams at the encoder. For the 2/3 code rate used in this example, the encoder has two input streams, so the delay is 2×`traceBack`

bits. As a result, the first 2×`traceBack`

bits in the decoded vector, `dataOut`

, are zeros. When computing the BER, discard the first 2×`traceBack`

bits in `dataOut`

and the last 2×`traceBack`

bits in the original vector, `dataIn`

.

Use the `biterr`

function to compute the number of errors and the BER by comparing `dataIn`

and `dataOut`

. For the same ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ of 10 dB, less errors occur when FEC is included in the processing chain.

decDelay = 2*traceBack; % Decoder delay, in bits [numErrors,ber] = ... biterr(dataIn(1:end - decDelay),dataOut(decDelay + 1:end)); fprintf('\nThe bit error rate is %5.2e, based on %d errors.\n', ... ber,numErrors)

The bit error rate is 6.40e-05, based on 64 errors.

**More About Delays**

The decoding operation in this example incurs a delay that causes the output of the decoder to lag the input. Timing information does not appear explicitly in the example, and the length of the delay depends on the specific operations being performed. Delays occur in various communications system operations, including convolutional decoding, convolutional interleaving and deinterleaving, equalization, and filtering. To find out the duration of the delay caused by specific functions or operations, see the specific documentation for those functions or operations. For more information on delays, see Delays of Convolutional Interleavers and Fading Channels.