Main Content

Decode and Recover Message From RS Codeword

This example shows how to use the RS Decoder block to decode and recover a message from a Reed-Solomon (RS) codeword. In this example, a set of random inputs are generated and provided as an input to the comm.RSEncoder function. The output of this function is provided as in input to the RS Decoder block. After that the output of the RS Decoder block is compared with the input of the comm.RSEncoder function to check whether any errors are encountered. The example model supports HDL code generation for the RS Decoder subsystem.

Set Up Input Data Variables

Specify the input variables for the RS encoding and decoding process.
n = 255;
k = 239;
primPoly = [1 0 0 0 1 1 1 0 1];
B = 1;
nMessages = 4;
data = zeros(k,nMessages);
inputMsg = (zeros(n,nMessages));
startSig = [];
endSig = [];

Generate Random Input Samples

Generate random samples based on n, k, and m values and provide them as input to the comm.RSEncoder function. Here, n is the codeword length, k is the message length, and m is the gap between the frames. numErrors specifies the number of errors to introduce in the encoded message and numErasures specifies the number of erasures to introduce in the encoded message.

hRSEnc = comm.RSEncoder;
hRSEnc.CodewordLength = n;
hRSEnc.MessageLength = k;
m=0;

for ii = 1:nMessages
data(:,ii) = randi([0 n],k,1);
[inputMsg(1:n,ii)] = hRSEnc(data(:,ii));
inputMsg1(1:n,ii) = inputMsg(1:n,ii);
[inputMsg(n+1:n+m,ii)] = zeros(m,1);
validIn(1:n,ii) = true;
validIn(n+1:n+m) = false;

% Introduce random erasures
numErrors = 4;
numErasures = 6;
errorIndices = randperm(n, numErrors);
erasureIndices = randperm(n, numErasures);
errorIndices = setdiff(errorIndices,erasureIndices); % Avoid duplication between errors and erasures
erasureSig(:,ii) = false(n,1);
erasureSig(erasureIndices, ii) = true;
inputMsg(erasureIndices, ii) = 0; % Set erasure positions to zero
inputMsg(errorIndices, ii) = 123; % Add constant error 123
endSig = [endSig [false(n-1,1); true;false(m,1);]];
startSig = [startSig [true;false(n+m-1,1)]];

end

refOutput = data(:);

Import Encoded Random Input Samples to Model

Provide the output of the comm.RSEncoder function as input to the Simulink model.

simDataIn = inputMsg(:);
simStartIn = startSig(:);
simEndIn = endSig(:);
simValidIn = validIn(:);
simErasuresIn = erasureSig(:); % Add erasure information

Run Model

Run the Simulink® model to export the decoded samples of the Simulink block to the MATLAB® workspace.

modelname = 'RSDecoder';
open_system(modelname);
out = sim(modelname);
simOutput = out.dataOut(out.validOut);

Compare Simulink Block Output with MATLAB Function Input

Compare the output of the RS Decoder block with the input of the comm.RSEncoder function.

fprintf('\nHDL RS Decoder\n');
difference = double(simOutput) - double(refOutput);
fprintf('\nTotal number of samples differed between Simulink block output and MATLAB function output is: %d \n',sum(difference));
HDL RS Decoder

Total number of samples differed between Simulink block output and MATLAB function output is: 0 

See Also

Blocks