| Communications Toolbox | ![]() |
This section further extends the example by addressing the following problem:
Problem Modify the previous example so that it includes convolutional coding and decoding, given the constraint lengths and generator polynomials of the convolutional code. |
The solution uses the convenc and vitdec functions to perform encoding and decoding, respectively. It also uses the poly2trellis function to define a trellis that represents a convolutional encoder. To learn more about these functions, see Convolutional Coding.
See also vitsimdemo for an example of convolutional coding and decoding.
This solution modifies the code from Pulse Shaping Using a Raised Cosine Filter. To view the original code in an editor window, enter the following command in the MATLAB Command Window.
edit commdoc_rrc
To view a completed M-file for this example, enter edit commdoc_code in the MATLAB Command Window.
1. Increase the Number of Symbols. Convolutional coding at this value of EbNo reduces the BER markedly. As a result, accumulating enough errors to compute a reliable BER requires you to process more symbols. In the Setup section, replace the definition of the number of bits, n, with the following.
n = 5e5; % Number of bits to process
Note The larger number of bits in this example causes it to take a noticeably longer time to run compared to the examples in previous sections. |
2. Encode the Binary Data. To encode the binary data before mapping it to integers for modulation, insert the following after the Signal Source section of the example and before the Bit-to-Symbol Mapping section.
%% Encoder % Define a convolutional coding trellis and use it % to encode the binary data. t = poly2trellis([5 4],[23 35 0; 0 5 13]); % Trellis code = convenc(x,t); % Encode. coderate = 2/3;
The poly2trellis command defines the trellis that represents the convolutional code that convenc uses for encoding the binary vector, x. The two input arguments in the poly2trellis command indicate the constraint length and generator polynomials, respectively, of the code. A diagram showing this encoder is in Example: A Rate-2/3 Feedforward Encoder.
3. Apply the Bit-to-Symbol Mapping to the Encoded Signal. The bit-to-symbol mapping must apply to the encoded signal, code, not the original uncoded data. Replace the first definition of xsym (within the Bit-to-Symbol Mapping section) with the following.
% B. Do ordinary binary-to-decimal mapping. xsym = bi2de(reshape(code,k,length(code)/k).','left-msb');
Recall that k is 4, the number of bits per symbol in 16-QAM.
4. Account for Code Rate When Defining SNR. Converting from Eb/N0 to the signal-to-noise ratio requires you to account for the number of information bits per symbol. Previously, each symbol corresponded to k bits. Now, each symbol corresponds to k*coderate information bits. More concretely, three symbols correspond to 12 coded bits in 16-QAM, which correspond to 8 uncoded (information) bits, so the ratio of symbols to information bits is 8/3 = 4*(2/3) = k*coderate.
Therefore, change the definition of snr (within the Channel section) to the following.
snr = EbNo + 10*log10(k*coderate)-10*log10(nsamp);
5. Decode the Convolutional Code. To decode the convolutional code before computing the error rate, insert the following after the entire Symbol-to-Bit Mapping section and just before the BER Computation section.
%% Decoder % Decode the convolutional code. tb = 16; % Traceback length for decoding z = vitdec(z,t,tb,'cont','hard'); % Decode.
The syntax for the vitdec function instructs it to use hard decisions. The 'cont' argument instructs it to use a mode designed for maintaining continuity when you invoke the function repeatedly (as in a loop). 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. The delay is discussed further in More About Delays.
6. Account for Delay When Computing BER. The continuous operation mode of the Viterbi decoder incurs a delay whose duration in bits equals the traceback length, tb, times the number of input streams to the encoder. For this rate 2/3 code, the encoder has two input streams, so the delay is 2*tb bits.
As a result, the first 2*tb bits in the decoded vector, z, are just zeros. When computing the bit error rate, you should ignore the first 2*tb bits in z and the last 2*tb bits in the original vector, x. If you do not compensate for the delay, then the BER computation is meaningless because it compares two vectors that do not truly correspond to each other.
Therefore, replace the BER Computation section with the following.
%% BER Computation % Compare x and z to obtain the number of errors and % the bit error rate. Take the decoding delay into account. decdelay = 2*tb; % Decoder delay, in bits [number_of_errors,bit_error_rate] = ... biterr(x(1:end-decdelay),z(decdelay+1:end))
The decoding operation in this example incurs a delay, which means that the output of the decoder lags the input. Timing information does not appear explicitly in the example, and the duration of the delay depends on the specific operations being performed. Delays occur in various communications-related operations, including convolutional decoding, convolutional interleaving/deinterleaving, equalization, and filtering. To find out the duration of the delay caused by specific functions or operations, refer to the specific documentation for those functions or operations. For example:
The vitdec reference page
The Effect of Delays on Recovery of Convolutionally Interleaved Data discussion also includes two typical ways to compensate for delays.
| Pulse Shaping Using a Raised Cosine Filter | Simulating a Communication System | ![]() |
© 1994-2005 The MathWorks, Inc.