## Trellis Coded Modulation Examples

These examples demonstrate trellis-coded modulation (TCM) techniques.

### Modulate and Demodulate Data Using QAM TCM

Modulate and demodulate noisy data using QAM TCM modulation with an arbitrary four point constellation. Estimate the resultant BER.

Define a trellis structure with two input symbols and four output symbols using a [171 133] generator polynomial. Define an arbitrary four-point constellation.

```qamTrellis = poly2trellis(7,[171 133]); refConst = exp(pi*1i*[1 2 3 6]/4);```

Create a QAM TCM modulator and demodulator System object™ pair using `qamTrellis` and `refConst`.

```qamtcmod = comm.GeneralQAMTCMModulator( ... qamTrellis, ... Constellation=refConst); qamtcdemod = comm.GeneralQAMTCMDemodulator( ... qamTrellis, ... Constellation=refConst);```

Create an error rate calculator with delay (in bits) equal to the product of `TracebackDepth` and the number of bits per symbol.

```errorrate = comm.ErrorRate( ... ReceiveDelay=qamtcdemod.TracebackDepth * ... log2(qamTrellis.numInputSymbols));```

Generate random binary data and apply QAM TCM modulation. Pass the signal through an AWGN channel and demodulate the signal. Collect the error statistics.

```for counter = 1:10 % Generate binary data data = randi([0 1],500,1); % Modulate modSignal = qamtcmod(data); % Pass through an AWGN channel noisySignal = awgn(modSignal,4); % Demodulate receivedData = qamtcdemod(noisySignal); % Calculate the error statistics errorStats = errorrate(data,receivedData); end```

Display the BER and the number of bit errors.

```fprintf("Error rate = %5.2e\nNumber of errors = %d\n", ... errorStats(1), errorStats(2))```
```Error rate = 9.84e-03 Number of errors = 49 ```

### Demodulate Noisy PSK TCM Data

Modulate and demodulate data using 8-PSK TCM modulation in an AWGN channel. Estimate the resulting error rate.

Define a trellis structure with four input symbols and eight output symbols.

`trellis = poly2trellis([5 4],[23 35 0; 0 5 13]);`

Create 8-PSK TCM modulator and demodulator System objects using trellis, `t`.

```M = 8; psktcmod = comm.PSKTCMModulator(trellis,ModulationOrder=M); psktcdemod = comm.PSKTCMDemodulator(trellis, ... ModulationOrder=M, ... TracebackDepth=16);```

Create an error rate calculator with delay in bits equal to `TracebackDepth` times the number of bits per symbol.

```errRate = comm.ErrorRate( ... ReceiveDelay=psktcdemod.TracebackDepth*log2(trellis.numInputSymbols));```

Generate random binary data and modulate it with 8-PSK TCM. Pass the modulated signal through the AWGN channel and demodulate the signal. Calculate the error statistics.

```for counter = 1:10 % Transmit frames of 250 2-bit symbols data = randi([0 1],500,1); % Modulate modSignal = psktcmod(data); % Pass through AWGN channel noisySignal = awgn(modSignal,7); % Demodulate receivedData = psktcdemod(noisySignal); % Calculate error statistics errorStats = errRate(data,receivedData); end```

Display the BER and the number of bit errors.

```fprintf("Error rate = %5.2e\nNumber of errors = %d\n", ... errorStats(1),errorStats(2))```
```Error rate = 2.44e-02 Number of errors = 121 ```

### Modulate and Demodulate Using Rectangular 16-QAM TCM

Modulate and demodulate data using rectangular 16-QAM TCM in an AWGN channel. Estimate the bit error rate (BER).

Create QAM TCM modulator and demodulator System objects.

```rqamtcmod = comm.RectangularQAMTCMModulator; rqamtcdemod = comm.RectangularQAMTCMDemodulator(TracebackDepth=16);```

Determine the delay through the QAM TCM demodulator. The demodulator uses the Viterbi algorithm to decode the TCM signal that was modulated using rectangular QAM. The error rate calculation must align the received samples with the transmitted sample to accurately calculate the bit error rate. Calculate the delay in the system with the number of bits per symbol and the decoder traceback depth in the TCM demodulator.

```bitsPerSymbol = log2(rqamtcdemod.TrellisStructure.numInputSymbols); delay = rqamtcdemod.TracebackDepth*bitsPerSymbol;```

Create an error rate calculator object with the `ReceiveDelay` property set to `delay`.

`errRate = comm.ErrorRate(ReceiveDelay=delay);`

Generate binary data and modulate it with 16-QAM TCM. Pass the signal through an AWGN channel and demodulate the signal. Calculate the error statistics. The loop runs until either 100 bit errors are encountered or 1e7 total bits are transmitted.

```% Initialize the error results vector. errStats = [0 0 0]; while errStats(2) < 100 && errStats(3) < 1e7 % Transmit frames of 200 3-bit symbols txData = randi([0 1],600,1); % Modulate txSig = rqamtcmod(txData); % Pass through AWGN channel rxSig = awgn(txSig,5); % Demodulate rxData = rqamtcdemod(rxSig); % Collect error statistics errStats = errRate(txData,rxData); end```

Display the error data.

```fprintf("Error rate = %4.2e\nNumber of errors = %d\n", ... errStats(1),errStats(2))```
```Error rate = 2.03e-03 Number of errors = 100 ```