# comm.ConvolutionalEncoder

Convolutionally encode binary data

## Description

The ConvolutionalEncoder object encodes a sequence of binary input vectors to produce a sequence of binary output vectors.

To convolutionally encode a binary signal:

1. Define and set up your convolutional encoder object. See Construction.

2. Call step to encode a sequence of binary input vectors to produce a sequence of binary output vectors according to the properties of comm.ConvolutionalEncoder. The behavior of step is specific to each object in the toolbox.

Note

Starting in R2016b, instead of using the step method to perform the operation defined by the System object™, you can call the object with arguments, as if it were a function. For example, y = step(obj,x) and y = obj(x) perform equivalent operations.

## Construction

H = comm.ConvolutionalEncoder creates a System object, H, that convolutionally encodes binary data.

H = comm.ConvolutionalEncoder(Name,Value) creates a convolutional encoder object, H, with each specified property set to the specified value. You can specify additional name-value pair arguments in any order as (Name1,Value1,...,NameN,ValueN).

H = comm.ConvolutionalEncoder(TRELLIS,Name,Value) creates a convolutional encoder object, H This object has the TrellisStructure property set to TRELLIS, and the other specified properties set to the specified values.

## Properties

 TrellisStructure Trellis structure of convolutional code Specify the trellis as a MATLAB structure that contains the trellis description of the convolutional code. Use the istrellis function to check if a structure is a valid trellis structure. The default is the result of poly2trellis(7, [171 133]). TerminationMethod Termination method of encoded frame Specify how the encoded frame is terminated as one of Continuous | Truncated | Terminated. The default is Continuous. When you set this property to Continuous, the object retains the encoder states at the end of each input vector for use with the next input vector. When you set this property to Truncated, the object treats each input vector independently. The encoder states are reset at the start of each input vector. If you set the InitialStateInputPort property to false, the object resets its states to the all-zeros state. If you set the InitialStateInputPort property to true, the object resets the states to the values you specify in the initial states step method input. When you set this property to Terminated, the object treats each input vector independently. For each input vector, the object uses extra bits to set the encoder states to all-zeros states at the end of the vector. For a rate K/N code, the step method outputs a vector with length $N×\left(L+S\right)}{K}$, where S = constraintLength–1 (or, in the case of multiple constraint lengths, S = sum(constraintLength(i)–1)). L is the length of the input to the step method. ResetInputPort Enable encoder reset input Set this property to true to enable an additional input to the step method. The default is false. When this additional reset input is a nonzero value, the internal states of the encoder reset to their initial conditions. This property applies when you set the TerminationMethod property to Continuous. DelayedResetAction Delay output reset Set this property to true to delay resetting the object output. The default is false. When you set this property to true, the reset of the internal states of the encoder occurs after the object computes the encoded data. When you set this property to false, the reset of the internal states of the encoder occurs before the object computes the encoded data. This property applies when you set the ResetInputPort property to true. InitialStateInputPort Enable initial state input Set this property to true to enable a step method input that allows the specification of the initial state of the encoder for each input vector. The default is false. This property applies when you set the TerminationMethod property to Truncated. FinalStateOutputPort Enable final state output Set this property to true to obtain the final state of the encoder via a step method output. The default is false. This property applies when you set the TerminationMethod property to Continuous or Truncated. PuncturePatternSource Source of puncture pattern Specify the source of the puncture pattern as one of None | Property. The default is None. When you set this property to None the object does not apply puncturing. When you set this property to Property, the object punctures the code. This puncturing is based on the puncture pattern vector that you specify in the PuncturePattern property. This property applies when you set the TerminationMethod property to Continuous or Truncated. PuncturePattern Puncture pattern vector Specify the puncture pattern used to puncture the encoded data as a column vector. The default is [1; 1; 0; 1; 0; 1]. The vector contains 1s and 0s, where the 0 indicates the punctured, or excluded, bits. This property applies when you set the TerminationMethod property to Continuous or Truncated and the PuncturePatternSource property to Property.

## Methods

 step Convolutionally encode binary data
Common to All System Objects
release

Allow System object property value changes

reset

Reset internal states of System object

## Examples

collapse all

Transmit a convolutionally encoded 8-DPSK modulated bit stream through an AWGN channel. Then, demodulate and decode using a Viterbi decoder.

Create the necessary System objects.

hConEnc = comm.ConvolutionalEncoder;
hMod = comm.DPSKModulator('BitInput',true);
hChan = comm.AWGNChannel('NoiseMethod', ...
'Signal to noise ratio (SNR)',...
'SNR',10);
hDemod = comm.DPSKDemodulator('BitOutput',true);
hDec = comm.ViterbiDecoder('InputFormat','Hard');

Process the data using the following steps:

1. Generate random bits

2. Convolutionally encode the data

3. Apply DPSK modulation

4. Pass the modulated signal through AWGN

5. Demodulate the noisy signal

6. Decode the data using a Viterbi algorithm

7. Collect error statistics

for counter = 1:20
data = randi([0 1],30,1);
encodedData = step(hConEnc, data);
modSignal = step(hMod, encodedData);
end

Display the number of errors.

errors(2)
ans = 3

Encode and decode a sequence of bits using a convolutional encoder and a Viterbi decoder with a defined puncture pattern. Verify that the input and output bits are identical

Define a puncture pattern matrix and reshape it into vector form for use with the Encoder and Decoder objects.

pPatternMat = [1 0 1;1 1 0];
pPatternVec = reshape(pPatternMat,6,1);

Create convolutional encoder and a Viterbi decoder in which the puncture pattern is defined by pPatternVec.

ENC = comm.ConvolutionalEncoder(...
'PuncturePatternSource','Property', ...
'PuncturePattern',pPatternVec);

DEC = comm.ViterbiDecoder('InputFormat','Hard', ...
'PuncturePatternSource','Property',...
'PuncturePattern',pPatternVec);

Create an error rate counter with the appropriate receive delay.

Encode and decode a sequence of random bits.

dataIn = randi([0 1],600,1);

dataEncoded = step(ENC,dataIn);

dataOut = step(DEC,dataEncoded);

Verify that there are no errors in the output data.

errStats = step(ERR,dataIn,dataOut);
errStats(2)
ans = 0

Concatenated convolutional codes offer high reliability and have gained in prominence and usage as turbo codes. The comm.TurboEncoder and comm.TurboDecoder System objects support rate 1/n convolutional codes only. This example shows the parallel concatenation of two rate 2/3 convolutional codes to achieve an effective rate 1/3 turbo code by using comm.ConvolutionalEncoder and comm.APPDecoder System objects.

System parameters

blkLength = 1024;          % Block length
EbNo = 0:5;                % Eb/No values to loop over
numIter = 3;               % Number of decoding iterations
maxNumBlks = 1e2;          % maximum number of blocks per Eb/No value

Convolutional Encoder/Decoder Parameters

trellis = poly2trellis([5 4],[23 35 0; 0 5 13]);
k = log2(trellis.numInputSymbols);      % number of input bits
n = log2(trellis.numOutputSymbols);     % number of output bits
intrIndices = randperm(blkLength/k)';   % Random interleaving
decAlg = 'True App';                    % Decoding algorithm
modOrder = 2;                           % PSK-modulation order

Initialize System Objects

Initialize Systems object for convolutional encoding, APP Decoding, BPSK modulation and demodulation, AGWN channel, and error rate computation. The demodulation output soft bits using a log-likelihood ratio method.

cEnc1 = comm.ConvolutionalEncoder('TrellisStructure',...
trellis,'TerminationMethod','Truncated');
cEnc2 = comm.ConvolutionalEncoder('TrellisStructure',...
trellis,'TerminationMethod','Truncated');
cAPPDec1 = comm.APPDecoder('TrellisStructure',trellis,...
'TerminationMethod','Truncated','Algorithm',decAlg);
cAPPDec2 = comm.APPDecoder('TrellisStructure',trellis,...
'TerminationMethod','Truncated','Algorithm',decAlg);

bpskMod = comm.BPSKModulator;
bpskDemod = comm.BPSKDemodulator('DecisionMethod','Log-likelihood ratio', ...
'VarianceSource','Input port');

awgnChan = comm.AWGNChannel('NoiseMethod','Variance', ...
'VarianceSource','Input port');

bitError = comm.ErrorRate; % BER measurement

Frame Processing Loop

Loop through a range of ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values to generate results for BER performance. The helperTurboEnc and helperTurboDec helper functions perform the turbo encoding and decoding.

ber = zeros(length(EbNo),1);
bitsPerSymbol = log2(modOrder);
turboEncRate = k/(2*n);

for ebNoIdx = 1:length(EbNo)
% Calculate the noise variance from EbNo
EsNo = EbNo(ebNoIdx) + 10*log10(bitsPerSymbol);
SNRdB = EsNo + 10*log10(turboEncRate); % Account for code rate
noiseVar = 10^(-SNRdB/10);

for  numBlks = 1:maxNumBlks
% Generate binary data
data = randi([0 1],blkLength,1);

% Turbo encode the data
[encodedData,outIndices] = helperTurboEnc(data,cEnc1,cEnc2, ...
trellis,blkLength,intrIndices);

% Modulate the encoded data
modSignal = bpskMod(encodedData);

% Pass the modulated signal through an AWGN channel

% Demodulate the noisy signal using LLR to output soft bits

% Turbo decode the demodulated data
trellis,blkLength,intrIndices,outIndices,numIter);

% Calculate the error statistics
end

ber(ebNoIdx) = errorStats(1);
reset(bitError);
end

Display Results

While the practical wireless systems, such as LTE and CCSDS, specify base rate-1/n convolutional codes for turbo codes, the results show use of higher rate convolutional codes as turbo codes is viable.

figure;
semilogy(EbNo, ber, '*-');
grid on;
xlabel('E_b/N_0 (dB)');
ylabel('BER');
title('High Rate Convolutional Codes for Turbo Coding');
legend(['N = ' num2str(blkLength) ', ' num2str(numIter) ' iterations']);

Helper Functions

function [yEnc,outIndices] = helperTurboEnc(data,hCEnc1,hCEnc2,trellis,blkLength,intrIndices)
% Turbo encoding using two parallel convolutional encoders.
% No tail bits handling and assumes no output stream puncturing.

% Trellis parameters
k = log2(trellis.numInputSymbols);
n = log2(trellis.numOutputSymbols);
cLen = blkLength*n/k;

punctrVec = [0;0;0;0;0;0];      % assumes all streams are output
N = length(find(punctrVec==0));

% Encode random data bits
y1 = step(hCEnc1, data);
y2 = step(hCEnc2, reshape(intrlv(reshape(data, k, [])',intrIndices)', [], 1));
y1D = reshape(y1(1:cLen), n, []);
y2D = reshape(y2(1:cLen), n, []);
yDTemp = [y1D; y2D];
y = yDTemp(:);

% Generate output indices vector using puncturing vector
idx = 0 : 2*n : (blkLength - 1)*2*(n/k);
punctrVecIdx = find(punctrVec==0);
dIdx = repmat(idx, N, 1) + punctrVecIdx;
outIndices = dIdx(:);
yEnc = y(outIndices);
end

function yDec = helperTurboDec(yEnc,cAPPDec1,cAPPDec2,trellis,blkLength,intrIndices,inIndices,numIter)
% Turbo decoding using two a-posteriori probability (APP) decoders

% Trellis parameters
k = log2(trellis.numInputSymbols);
n = log2(trellis.numOutputSymbols);
rCodLen = 2*(n/k)*blkLength;
typeyEnc = class(yEnc);

% Re-order encoded bits according to outIndices
x = zeros(rCodLen, 1);
x(inIndices) = yEnc;

% Generate output of first encoder
yD = reshape(x(1:rCodLen), 2*n, []);
lc1D = yD(1:n, :);
Lc1_in = lc1D(:);

% Generate output of second encoder
lc2D   = yD(n+1:2*n, :);
Lc2_in = lc2D(:);

% Initialize unencoded data input
Lu1_in = zeros(blkLength, 1, typeyEnc);

% Turbo Decode
out1 = zeros(blkLength/k, k, typeyEnc);
for iterIdx = 1 : numIter
[Lu1_out, ~] = step(cAPPDec1, Lu1_in, Lc1_in);
tmp = Lu1_out(1:blkLength);
Lu2_in = reshape(tmp, k, [])';
[Lu2_out, ~] = step(cAPPDec2, ...
reshape(Lu2_in(intrIndices, :)', [], 1), Lc2_in);
out1(intrIndices, :) = reshape(Lu2_out(1:blkLength), k, [])';
Lu1_in = reshape(out1', [], 1);
end
% Calculate llr and decoded bits for the final iteration
llr = reshape(out1', [], 1) + Lu1_out(1:blkLength);
yDec = cast((llr>=0), typeyEnc);
end

## Algorithms

This object implements the algorithm, inputs, and outputs described on the Convolutional Encoder block reference page. The object properties correspond to the block parameters, except:

 The operation mode Reset on nonzero input via port block parameter corresponds to the ResetInputPort property.