encode, add noise and decode a bit vector using ldpc codes in matlab

Assuming you have generated an LDPC parity check matrix H and a message m that you want to encode, add noise and decode, here are the steps to encode, add noise and decode a bit vector using LDPC codes in MATLAB:

  1. Import the LDPC encoder method from the Communications Toolbox library by typing ldpcenc = comm.LDPCEncoder(H);. This will create an object that will be used to encode the message with the parity check matrix H.

  2. Encode the message using the ldpcenc object by typing cword = ldpcenc(m);. This will create a codeword cword from the message m that has been encoded using the LDPC parity check matrix H.

  3. Add noise to the cword using a noise generator, e.g., nword = awgn(cword,SNRdB,'measured');, where SNRdB is the signal-to-noise ratio in decibels that you want to simulate the channel to have.

  4. Import the LDPC decoder method from the Communications Toolbox library by typing ldpcdec = comm.LDPCDecoder(H);. This will create an object that will be used to decode the noisy codeword back to the original message.

  5. Decode the noisy codeword using the ldpcdec object by typing rxmsg = ldpcdec(nword);. This will decode the noisy codeword nword back to the original message rxmsg using the LDPC parity check matrix H.

Here is the complete example code:

main.m
% Generate the parity check matrix H
H = dvbs2ldpc(1/2);

% Generate a random message
m = randi([0 1],1,length(H));

% Create an LDPC encoder object
ldpcenc = comm.LDPCEncoder(H);

% Encode the message to create a codeword 
cword = ldpcenc(m);

% Add noise to the codeword using AWGN channel
SNRdB = 5;
nword = awgn(cword,SNRdB,'measured');

% Create an LDPC decoder object
ldpcdec = comm.LDPCDecoder(H);

% Decode the received noisy codeword
rxmsg = ldpcdec(nword);

% Check the error rate
[number_of_errors,bit_error_rate] = biterr(m,rxmsg);
fprintf('\nNumber of errors = %d\n', number_of_errors);
fprintf('Bit Error Rate = %d\n', bit_error_rate);
650 chars
27 lines

Note that you will need the Communications Toolbox library to use ldpcenc and ldpcdec methods.

gistlibby LogSnag