%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .m script of web demo "Insert title here" 
% Written for GNU Octave
% 
% INSTITUTE OF TELECOMMUNICATIONS  
% University of Stuttgart 
% www.inue-uni-stuttgart.de 
% author: Sherif Abdulatif  
% date: 01.09.2014   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function matrixH(s,v,c,d,hr,hd,uf,filename)
% matrixH takes degree profiles, density in percentage from user and filename to save on
fh = figure(1);             % Create figure
t=[];
t1=[];
if hr == 1                  % Given H-matrix
	try
		H = load(uf);       % Load the parity check matrix (H) from a given path
   catch		
      matrixH(s,v,c,d,0,uf,filename);
   end
   if all(all((H==1|H==0)))
		dvi = sum(H,1);             % <-- Following 6 lines to compute the average dv and dc as it might be an irregular code
		dci = sum(H,2)';
		[adv,bdv] = hist(dvi,unique(dvi));
		[adc,bdc] = hist(dci,unique(dci));
		dv = sum((adv./sum(adv)).*bdv);
		dc = sum((adc./sum(adc)).*bdc);
		t = ['H uploaded with Rate rd = ',num2str(1-(dv/dc)),' and density = ',num2str(dv/size(H,1)),'%'];
	   t1 = ['Degree profiles uploaded with Rate rd = ',num2str(1-(dv/dc)),' and density = ',num2str(dv/size(H,1)),'%'];
   else
      matrixH(s,v,c,d,0,uf,filename);
   end
else
if s==1
rs = 100/d;                 % rs is number of rows per set which is m/v and so opposite of density v/m and 100/d since d is given as percentage
H = generateH(v,c,rs);      % Generate H using Gallager's method
t=['H of LDPC with Rate rd = ',num2str(1-(v/c)),' and density = ',num2str(d),'%'];
t1=['Degree profiles of LDPC with Rate rd = ',num2str(1-(v/c)),' and density = ',num2str(d),'%'];
elseif s==2
H = load('HWifi.mat');
H = H.H;
t=['H of Wifi IEEE 802.11n with Rate rd = 0.5 and density = 1.13%'];
t1=['Degree profiles of Wifi IEEE 802.11n with Rate rd = 0.5 and density = 1.13%'];
elseif s==3 
H = load('HDVB_910.mat');
H = H.H;
t=['H of DVB-S2 with Rate rd = 0.9 and density = 0.04%'];
t1=['Degree profiles of DVB-S2 with Rate rd = 0.9 and density = 0.04%'];
elseif s==4
H = load('HWimax.mat');
H = H.H;
t=['H of WiMax IEEE 802.16e with Rate rd = 0.5 and density = 0.14%'];
t1=['Degree profiles of WiMax IEEE 802.16e with Rate rd = 0.5 and density = 0.14%'];
end
end
if hd==0
spy(H,'+',0.5);               % Plots matrix H
xlabel('n');                 % Remove x-label
ylabel('n-k');
set(gca,'XTick',[]);        % Remove numbers on x-axis
set(gca,'YTick',[]);        % Remove numbers on y-axis
title(t);   % Add title
else
dvi = sum(H,1);           % <-- Following 6 lines to compute the average dv and dc as it might be an irregular code
dci = sum(H,2)';
[adv,bdv] = hist(dvi,unique(dvi));
[adc,bdc] = hist(dci,unique(dci));
adv(adv == 0) = NaN;
stem(bdv,adv,'linewidth',2);
hold on;
adc(adc == 0) = NaN;
stem (bdc,adc, 'linewidth',2,"r");
xlim([min(min(bdv),min(bdc))-1 max(max(bdv),max(bdc))+1]);
title(t1);
xlabel('Degree of nodes');
ylabel('Number of nodes');
grid on;
axis on;
legend('dv','dc','location','NorthEast');
end
% Create an image file. This png image is displayed on the webpage. Its size 
% must be 640x480.
print(filename, "-dgif","-S640,480", "-r0");  % Prints the figure
end
function [H] = generateH(v,c,rs)
H1 = zeros(rs*v,rs*c);     % Initiate matrix H (mxn)
cs = ones(1,c);            % Generate c consecutive 1's
for i=1:rs                 % Initiate first set as consecutive 1's in each row
    H1(i,((i-1)*c)+1:c+((i-1)*c))=cs;
end
for i=2:v                  % Remaining sets are permutations of the first set
    a = randperm(rs*c);
    H1(((i-1)*rs)+1:((i-1)*rs)+rs,:)=H1(1:rs,a);
end
H1=H1(:,randperm(rs*c));   % Permute remaining rows and columns for better structure
H1=H1(randperm(rs*v),:);
H = H1;
end
