%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% function [H_no_dep H] = get_h_matrix(p,tr,tc,first_1);
%% Generate LDPC H Matrix Uses Generic-LDPC Method As Per Cioffi's Class Notes
%% Example: to Generate (529,462) code, p=23, rw=23, cw=3, first_1=2
%%          H = get_h_matrix(23,23,3,2),
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Definition of input variables
%% p      :  Prime number of the size of base matrix of size p-by-p 
%% tr     :  Row weight = # of base matrices (or 1's) /row, equivalent to K
%% tc     :  Col weight = # of base matrices (or 1's) per column,eq to J
%% first_1:  Set to 2 in generic LDPC code, so right shift by first_1-1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Definition of output variables
%% H_no_dep  : the parity check matrix with no dependent rows
%% H         : without removing the dependent rows
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EE379, Chien-Hsin Lee, first version 06/2006, edits by J. Cioffi since
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [H_no_dep , H] = get_h_matrix(p,tr,tc,first_1_start);
% generate base matrix
a = eye(p,p);   
a = [a(first_1_start:p,:);a(1:first_1_start-1,:)];

% generate H matrix 
H = [];
for row = 1:tc
    current_row = [];
    for cl = 1:tr
        current_row = [current_row,a^((row-1)*(cl-1))];
    end
    H = [H;current_row];
end

% remove dependent rows
% this need to be update with mod2 operation
[Q R] = qr(H);
H_no_dep = [];
for i = 1:p*tc
    if( abs(R(i,i)) > 1E-9)
        H_no_dep = [H_no_dep;H(i,:)];
    end
end