clear all
clf
CodeRate=2/5;
ParityCheckM=dvbs2ldpc(CodeRate);
enc = fec.ldpcenc(ParityCheckM);  % Construct a default LDPC encoder object

% Construct a companion LDPC decoder object
dec = fec.ldpcdec(ParityCheckM);
dec.DecisionType = 'Hard decision';
dec.OutputFormat = 'Information part';
dec.NumIterations = 50;
% Stop if all parity-checks are satisfied
dec.DoParityChecks = 'Yes';

% Generate and encode a random binary message
msg = floor(2*rand(enc.NumInfoBits,1));
msg=msg.';
%msg = randint(1,enc.NumInfoBits,2);
codeword = encode(enc,msg);

% Construct a BPSK modulator object
modObj = modem.pskmod('M',2,'InputType','Bit');

% Modulate the signal (map bit 0 to 1 + 0i, bit 1 to -1 + 0i)
modulatedsig = modulate(modObj, codeword);

% Noise parameters
SNRdB = 1;
sigma = sqrt(10^(-SNRdB/10));

% Transmit signal through AWGN channel
receivedsig = awgn(modulatedsig, SNRdB, 0); 
    % Signal power = 0 dBW

% Visualize received signal
scatterplot(receivedsig)

% Construct a BPSK demodulator object to compute
% log-likelihood ratios
demodObj = modem.pskdemod(modObj,'DecisionType','LLR', ...
     'NoiseVariance',sigma^2);

% Compute log-likelihood ratios (AWGN channel)
llr = demodulate(demodObj, receivedsig);

% Decode received signal
decodedmsg = decode(dec, llr);

% Actual number of iterations executed
disp(['Number of iterations executed = ' ...
     num2str(dec.ActualNumIterations)]);
% Number of parity-checks violated
disp(['Number of parity-checks violated = ' ...
     num2str(sum(dec.FinalParityChecks))]);
% Compare with original message
disp(['Number of bits incorrectly decoded = ' ...
     num2str(nnz(decodedmsg-msg))]);