Main Content

Encode Data with Quasi-Cyclic Low Density Parity Check Code

This example shows how to use the LDPC Encoder block to encode data bits with quasi-cyclic low-density parity-check (QC LDPC) codes. Specify the parity-check matrix and the block size and then generate input data bits corresponding to the specified parity-check matrix and the block size. Provide these inputs to the ldpcEncode function in Communications Toolbox™ and the LDPC Encoder block and then compare their outputs. The Simulink® model in this example supports HDL code generation for the HDL LDPC Encoder subsystem.

Set Up Input Variables

Set up workspace variables for the Simulink model and the MATLAB function. You can modify these variable values according to your requirements.

blockSize = 56;
PCM = [5 14 12 1 2 37 45 26 24 0 3 -1 34 7 46 10 -1 -1 -1 -1;
    0 35 1 26 0 10 16 16 34 4 2 23 0 51 -1 49 20 -1 -1 -1;
    12 28 22 46 3 16 51 2 25 29 19 18 52 -1 37 -1 34 39 -1 -1;
    0 51 16 31 13 39 27 33 8 27 53 13 -1 52 33 -1 -1 38  7 -1;
    36 6 3 51 4 19 4 45 48 9 -1 11 22 23 43 -1 -1 -1 14  1;];

Generate Input Data

Generate input data bits corresponding to the parity-check matrix and the block size.

numFrames = 4;
msg = {numFrames};
refOut = cell(1,numFrames);
encSampleIn = [];
encStartIn = [];
encEndIn = [];
encValidIn = [];

% Calculate the input data length.
[M,N] = size(PCM);
K = (N-M) * blockSize;
for idx = 1:numFrames
    msg{idx} = randi([0 1],K,1,'int8');

    encFrameGap = 2000 + K;
    encIn =  msg{idx}';
    encSampleIn = [encSampleIn encIn zeros(size(encIn,1),encFrameGap)]; %#ok<*AGROW>
    encStartIn = logical([encStartIn 1 zeros(1,K-1) zeros(1,encFrameGap)]);
    encEndIn = logical([encEndIn zeros(1,K-1) 1 zeros(1,encFrameGap)]);
    encValidIn = logical([encValidIn ones(1,K) zeros(1,encFrameGap)]);
end

dataIn = timeseries(encSampleIn'>0);
startIn = timeseries(encStartIn);
endIn = timeseries(encEndIn);
validIn = timeseries(encValidIn);
simTime = length(encValidIn);

Encode Data Using MATLAB Function

Create an LDPC Encoder configuration object to pass as input to the ldpcEncode function.

H = ldpcQuasiCyclicMatrix(blockSize,PCM);
encoderCfg = ldpcEncoderConfig(H);
for i = 1:numFrames
    refOut{i} = ldpcEncode(msg{i}, encoderCfg);
end

Encode Data Using Simulink Block

Encode data using the LDPC Encoder block. Running the model imports the input signal variables from the MATLAB workspace to the LDPC Encoder block in the model.

modelName = 'HDLLDPCEncoder';
open_system(modelName);
sim(modelName);
startIdx = find(squeeze(startOut));
endIdx = find(squeeze(endOut));
enc = squeeze(dataOut);

Compare Simulink Block Output with MATLAB Function Output

Compare the Simulink block output with the MATLAB function output.

for i = 1:numFrames
    idx = startIdx(i):endIdx(i);
    HDLOutput = enc(idx);
    error = sum(abs(double(refOut{i})-HDLOutput(:)));
   fprintf('Encoded frame %d: Output data differs by %d bits\n',i,error);
end
Encoded frame 1: Output data differs by 0 bits
Encoded frame 2: Output data differs by 0 bits
Encoded frame 3: Output data differs by 0 bits
Encoded frame 4: Output data differs by 0 bits

See Also

Blocks

Functions