convenc

codedout = convenc( msg , trellis ) encodes the input binary message by using a convolutional encoder represented by a trellis structure. For details about trellis structures in MATLAB ® , see Trellis Description of a Convolutional Code. The input message contains one or more symbols, each of which consists of log2( trellis . numInputSymbols ) bits. The coded output, codedout , contains one or more symbols, each of which consists of log2( trellis . numOutputSymbols ) bits.

codedout = convenc( msg , trellis , puncpat ) specifies a puncture pattern, puncpat , to enable higher rate encoding than unpunctured coding.

For some commonly used puncture patterns for specific rates and polynomials, see the last three references.

codedout = convenc( ___ , istate ) enables the encoder registers to start at a state specified by istate . Specify istate as the last input parameter preceded by any of the input argument combinations in the previous syntaxes.

[ codedout , fstate ] = convenc( ___ ) also returns the final state of the encoder. When calling convenc iteratively, fstate is typically used to set istate for subsequent calls to the convenc function.

Examples

Create Convolutional Codes

Create convolutional codes by using a trellis structure. You can define the trellis by using the poly2trellis function or by manually specifying the trellis structure. The example shows both methods.

Define trellis by using poly2trellis function

Define the trellis structure to be used to configure the encoder by using the poly2trellis function.

trellis_a = poly2trellis([5 4],[23 35 0; 0 5 13])
trellis_a = struct with fields: numInputSymbols: 4 numOutputSymbols: 8 numStates: 128 nextStates: [128x4 double] outputs: [128x4 double]

Use the trellis structure to configure the convenc function. Encode five two-bit symbols for a K / N rate 2/3 convolutional code by using the convenc function.

K = log2(trellis_a.numInputSymbols) % Number of input bit streams
K = 2
N = log2(trellis_a.numOutputSymbols) % Number of output bit streams
N = 3
numReg = log2(trellis_a.numStates) % Number of coder registers
numReg = 7
numSymPerFrame = 5; % Number of symbols per frame data = randi([0 1],K*numSymPerFrame,1); [code_a,fstate_a] = convenc(data,trellis_a);

Verify that the encoded output is 15 bits, which is 3/2 ( N / K ) times the length of the input sequence, data .

code_a'
ans = 1×15 1 1 1 0 0 1 1 1 1 1 0 1 0 1 0
length(data)
ans = 10
length(code_a)
ans = 15

Define trellis manually

Manually define a trellis structure for a K / N rate 1/2 convolutional code.

trellis_b = struct('numInputSymbols',2,'numOutputSymbols',4, . 'numStates',4,'nextStates',[0 2;0 2;1 3;1 3], . 'outputs',[0 3;1 2;3 0;2 1])
trellis_b = struct with fields: numInputSymbols: 2 numOutputSymbols: 4 numStates: 4 nextStates: [4x2 double] outputs: [4x2 double]

Use the trellis structure to configure the convenc function when encoding 10 one-bit symbols.

K = log2(trellis_b.numInputSymbols) % Number of input bit streams
K = 1
N = log2(trellis_b.numOutputSymbols) % Number of output bit streams
N = 2
numReg = log2(trellis_b.numStates) % Number of coder registers
numReg = 2
numSymPerFrame = 10; % Number of symbols per frame data = randi([0 1],K*numSymPerFrame,1); code_b = convenc(data,trellis_b);

Verify that the encoded output is 20 bits, which is 2/1 ( N / K ) times the length of the input sequence, data .

code_b'
ans = 1×20 0 0 1 1 0 0 1 0 1 0 1 1 0 1 1 1 0 0 0 1
length(data)
ans = 10
length(code_b)
ans = 20

Adjust Convolutional Encoding Code Rate by Using Puncturing

Use puncturing to adjust the K / N code rate of the convolutional encoder from 1/2 to 3/4.

Initialize parameters for the encoding operation.

trellis = poly2trellis(7,[171 133])
trellis = struct with fields: numInputSymbols: 2 numOutputSymbols: 4 numStates: 64 nextStates: [64x2 double] outputs: [64x2 double]
puncpat = [1;1;0];

Calculate the unpunctured and punctured code rates.

K = log2(trellis.numInputSymbols); % Number of input streams N = log2(trellis.numOutputSymbols); % Number of output streams unpunc_coderate = K/N; % Unpunctured code rate punc_coderate = (K/N)*length(puncpat)/sum(puncpat); % Punctured code rate fprintf(['K is %d and N is %d. ' . 'The unpunctured code rate is %3.2f ' . 'and the punctured code rate is %3.2f.\n'], . K,N,unpunc_coderate,punc_coderate)
K is 1 and N is 2. The unpunctured code rate is 0.50 and the punctured code rate is 0.75.

Convolutionally encode an all 1s three-bit message without puncturing applied to the coded output. Then, convolutionally encode the same message with puncturing.

msg = ones(length(puncpat),1); unpuncturedcode = convenc(msg,trellis); puncturedcode = convenc(msg,trellis,puncpat);

Show the message, the unpunctured code, the punctured code, and the puncture pattern.

ans = 1×3 1 1 1
unpuncturedcode'
ans = 1×6 1 1 0 1 1 0
puncpat'
ans = 1×3 1 1 0
puncturedcode'
ans = 1×4 1 1 1 1

Without puncturing, the configured convolutional encoding inputs three message bits and outputs six coded bits. Confirm the resulting code rate matches the expected code rate of 1/2.

length(msg)/length(unpuncturedcode)
ans = 0.5000

With puncturing, bits in positions 1 and 2 of the input message are transmitted, while the bit in position 3 is removed. For every three bits of input, the punctured code generates four bits of output. Confirm the resulting code rate matches the expected code rate of 3/4.

length(msg)/length(puncturedcode)
ans = 0.7500

Use Trellis Structure for Rate 1/2 Feedforward Convolutional Encoder

Use a trellis structure to configure the rate 1/2 feedforward convolutional code in this diagram.

Diagram of a rate 1/2 feedforward convolutional encoder with codegenerators [6 7] and constraint length 3

Create a trellis structure, setting the constraint length to 3 and specifying the code generator as a vector of octal values. The diagram indicates the binary values and polynomial form, indicating the left-most bit is the most-significant-bit (MSB). The binary vector [1 1 0] represents octal 6 and corresponds to the upper row of binary digits in the diagram. The binary vector [1 1 1] represents octal 7 and corresponds to the lower row of binary digits in the diagram. These binary digits indicate connections from the outputs of the registers to the two adders in the diagram.

trellis = poly2trellis(3,[6 7])
trellis = struct with fields: numInputSymbols: 2 numOutputSymbols: 4 numStates: 4 nextStates: [4x2 double] outputs: [4x2 double]

Generate random binary data. Convolutionally encode the data, by using the specified trellis structure. Decode the coded data by using the Viterbi algorithm with the specified trellis structure, 34 for its traceback depth, truncated operation mode, and hard decisions.

data = randi([0 1],70,1); codedData = convenc(data,trellis); tbdepth = 34; decodedData = vitdec(codedData,trellis,tbdepth,'trunc','hard');

Verify the decoded data has zero bit errors.

biterr(data,decodedData)
ans = 0

Use Trellis Structure for Rate 1/2 Feedback Convolutional Encoder

Create a trellis structure to represent the rate 1/2 systematic convolutional encoder with feedback shown in this diagram.

This encoder has 5 for its constraint length, [37 33] as its generator polynomial matrix, and 37 for its feedback connection polynomial.

The first generator polynomial is octal 37. The second generator polynomial is octal 33. The feedback polynomial is octal 37. The first generator polynomial matches the feedback connection polynomial because the first output corresponds to the systematic bits.

The binary vector [1 1 1 1 1] represents octal 37 and corresponds to the upper row of binary digits in the diagram. The binary vector [1 1 0 1 1] represents octal 33 and corresponds to the lower row of binary digits in the diagram. These binary digits indicate connections from the outputs of the registers to the two adders in the diagram. The initial 1 corresponds to the input bit.

Convert the polynomial to a trellis structure by using the poly2trellis function. When used with a feedback polynomial, poly2trellis makes a feedback connection to the input of the trellis.

trellis = poly2trellis(5,[37 33],37)
trellis = struct with fields: numInputSymbols: 2 numOutputSymbols: 4 numStates: 16 nextStates: [16x2 double] outputs: [16x2 double]

Generate random binary data. Convolutionally encode the data by using the specified trellis structure. Decode the coded data by using the Viterbi algorithm with the specified trellis structure, 34 for its traceback depth, truncated operation mode, and hard decisions.

data = randi([0 1],70,1); codedData = convenc(data,trellis); tbdepth = 34; % Traceback depth for Viterbi decoder decodedData = vitdec(codedData,trellis,tbdepth,'trunc','hard');

Verify the decoded data has zero bit errors.

biterr(data,decodedData)