Main Content

LDPC Encode and Decode of Streaming Data

This example shows how to use LDPC Encoder and LDPC Decoder blocks and verify their functionality. The Simulink® model in this example contains a chain of communication system with encoding, symbol modulation, Additive white Gaussian noise (AWGN) channel, symbol demodulation, and decoding blocks. All the blocks in this example support HDL code generation, except the AWGN Channel block.

Generate Input Data

Generate random frames of input data based on the parity check matrix and block size. frameGap accommodates the latency of the complete communication chain shown in the model. You can select either Min-sum or Normalized min-sum algorithm.

numFrames = 4;
parityCheckMatrix = [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;];
blockSize = 56;

numIter = 8;
algo = 'Min-sum'; % 'Min-sum' or 'Normalized min-sum'
if strcmpi(algo,'Min-sum')
    alpha = 1; %#ok<*UNRCH>
else
    alpha = 0.75;
end

K = (size(parityCheckMatrix,2) - size(parityCheckMatrix,1))*blockSize;
N = size(parityCheckMatrix,2)*blockSize;
R = K/N;

EbNo = 4;
EsNo = EbNo + 10*log10(1);
snrdB = EsNo + 10*log10(R); % in dB
noiseVar = 1./(10.^(snrdB/10));

msg = {numFrames};
encSampleIn = [];encStartIn = [];encEndIn = [];encValidIn = [];
nonZeros = length(find(parityCheckMatrix+1));
frameGap = numIter*(nonZeros*2 + size(parityCheckMatrix,1)*10)+13 + N + K + 200;
for ii = 1:numFrames
    msg{ii} = randi([0 1],1,K);
    encIn = msg{ii};
    encSampleIn = logical([encSampleIn encIn zeros(size(encIn,1),frameGap)]); %#ok<*AGROW>
    encStartIn = logical([encStartIn  1  zeros(1,K-1)  zeros(1,frameGap)]);
    encEndIn = logical([encEndIn    zeros(1,K-1) 1   zeros(1,frameGap)]);
    encValidIn = logical([encValidIn    ones(1,K)      zeros(1,frameGap)]);
end

encSampleIn = timeseries(logical(encSampleIn'));

sampleTime = 1;
simTime = length(encValidIn);   %#ok<NASGU>

Run Model

Running the model imports the input signal variables encSampleIn, encStartIn, encEndIn, encValidIn, parityCheckMatrix, blockSize, sampleTime, and simTime and exports a stream of decoded output samples sampleOut along with the control signal ctrlOut to the MATLAB workspace. The model runs and generates output at the LDPC Decoder block.

open_system('HDLLDPCEncoderDecoder');

if alpha ~= 1
    set_param('HDLLDPCEncoderDecoder/LDPC Decoder','Algorithm','Normalized min-sum');
else
    set_param('HDLLDPCEncoderDecoder/LDPC Decoder','Algorithm','Min-sum');
end
decOut = sim('HDLLDPCEncoderDecoder');

Compare Decoder Output with Encoder Input

Compare the output of the LDPC Decoder block with the input of the LDPC Encoder block to verify the results.

startIdx = find(decOut.ctrlOut.start.Data);
endIdx = find(decOut.ctrlOut.end.Data);

for ii = 1:numFrames
    decHDL{ii} = squeeze(decOut.dataOut(startIdx(ii):endIdx(ii))); %#ok<*SAGROW>
    error = sum(abs(msg{ii} - decHDL{ii}'));
    fprintf(['Decoded Frame %d: LDPC Decoder output and ' ...
        'LDPC Encoder input differ by %d bits\n'],ii,error);
end
Decoded Frame 1: LDPC Decoder output and LDPC Encoder input differ by 0 bits
Decoded Frame 2: LDPC Decoder output and LDPC Encoder input differ by 0 bits
Decoded Frame 3: LDPC Decoder output and LDPC Encoder input differ by 0 bits
Decoded Frame 4: LDPC Decoder output and LDPC Encoder input differ by 0 bits

See Also

Blocks