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

% This example script just prints the values of the given parameters.
% Make sure that the function prototype matches the prototype defined
% in the XML config file. (Parameter names are arbitrary but should be the same
% for better readability)

function [tex_link] = code_properties(code, what, filename)


  % define formats
  landscape = "-S930,350";
  portrait  = "-S640,480";
  
  % select format
  output_format = portrait;
  
  % Create an invisible figure.
  fh = figure(1);
  set(fh, "visible", "off");
  
  % define style
  set(0, "defaultlinelinewidth", 2);
  set(0, "defaultaxesfontsize", 16);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 16);
  set(0, "defaulttextfontname", "Helvetica");
  
  % This to test and demonstrate the debug engine:
  %fprintf("This is a debug message. Checking format: %s", output_format);
  
  clf;
  hold on;
  nonlinear = false;
  switch (code)
  case "rep"
    G = [1 1];
    code_name = "Repetition Code"
    short_name = 'Rep';
    systematic = true;
case "four_two"
    G = [1 0 1 1; 0 1 1 0];
    code_name = "(4,2) Code"
    short_name = '4,2';
    systematic = true;
case "six_three"
    G = [1 0 0 1 1 0; 0 1 0 1 0 1; 0 0 1 0 1 1];
    code_name = "(6,3) Code"
    short_name = '6,3';
    systematic = true;
case "ext_hamming"
    code_name = "(8,4) Ext. Hamming Code"
    G = [1 0 0 0 0 1 1 1; 0 1 0 0 1 0 1 1; 0 0 1 0 1 1 0 1; 0 0 0 1 1 1 1 0];
    systematic = true;
    short_name = 'eHam(8,4)';
case "hamming_seven"
    code_name = "(7,4) Hamming Code"
    G = [1 0 0 0 0 1 1;
       0 1 0 0 1 0 1;
       0 0 1 0 1 1 0;
       0 0 0 1 1 1 1];
    systematic = true;
    short_name = 'Ham(7,4)';
case "hamming_fifteen"
    code_name = "(15,11) Hamming Code"
    G = [1 0 0 0 0 0 0 0 0 0 0 0 0 1 1;
       0 1 0 0 0 0 0 0 0 0 0 0 1 0 1;
       0 0 1 0 0 0 0 0 0 0 0 0 1 1 0;
       0 0 0 1 0 0 0 0 0 0 1 0 0 0 1;
       0 0 0 0 1 0 0 0 0 0 1 0 0 1 0;
       0 0 0 0 0 1 0 0 0 0 1 0 1 0 0;
       0 0 0 0 0 0 1 0 0 0 1 0 1 1 1;
       0 0 0 0 0 0 0 1 0 0 1 0 1 1 0;
       0 0 0 0 0 0 0 0 1 0 1 0 1 0 1;
       0 0 0 0 0 0 0 0 0 1 1 0 0 1 1;
       0 0 0 0 0 0 0 0 0 0 0 1 1 1 1];
    systematic = false;
    short_name = 'Ham(15,11)';
case "rm_one_four"
    code_name = "(16,5) Reed-Muller Code"
    G = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;      0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1;      0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1;      0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1;      0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1];
    short_name = 'RM(1,4)';
    systematic = false;
case "polar"
    code_name = "(16,8) Polar Code"
    G = [1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
    1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0;
    1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0;
    1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0;
    1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0;
    1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0;
    1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0;
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
    systematic = false;
    short_name = 'P(16,8)';
case "ldpc"
      code_name = "(16,8) LDPC Code"
      G = [1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1;
        0 1 0 0 0 0 0 0 0 0 1 0 1 1 1 0;
        0 0 1 0 0 0 0 0 1 1 1 1 0 1 1 0;
        0 0 0 1 0 0 0 0 1 1 1 1 1 0 0 1;
        0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 1;
        0 0 0 0 0 1 0 0 1 1 0 1 0 0 1 0;
        0 0 0 0 0 0 1 0 1 0 0 1 1 1 1 1;
        0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1];
        systematic=true;
      short_name = 'LDPC(16,8)';
case "egolay"
    code_name = "(24,12) ext. Golay Code"
    G = [1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 1 1;
    0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 1 0;
    0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 1 1;
    0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 0 1 1 0;
    0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 1 0 1 1 0 0 1;
    0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 1 0 1 1 0 1;
    0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 1 0 1 1 1;
    0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 1 0 1 1 1 1 0 0 0;
    0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 1 0 1 1 1 1 0 0;
    0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 1 0 1 1 1 1 0;
    0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 1 1 0 1;
    0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 1 1 1];
    systematic = true;
    short_name = 'G24';
case "bch"
    code_name = "(31,16) BCH Code"
    G = [
    1 0 0 0 0 0 0 0 0 0 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"sixteen_eight_five"
    code_name = "(16,8) Distance-5 Code"
    G = [1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0;
    0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1;
    0 0 1 0 0 0 0 0 0 1 1 0 1 1 0 0;
    0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 1;
    0 0 0 0 1 0 0 0 1 1 0 0 1 0 1 0;
    0 0 0 0 0 1 0 0 0 0 1 1 0 1 1 0;
    0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 1;
    0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1];
    systematic = true;
    short_name = '16,8,5';
case "bluetooth"
    code_name = "(15,10) Bluetooth Code"
    G = [1 0 0 0 0 0 0 0 0 0 0 1 0 1 1;
    0 1 0 0 0 0 0 0 0 0 1 0 1 1 0;
    0 0 1 0 0 0 0 0 0 0 0 0 1 1 1;
    0 0 0 1 0 0 0 0 0 0 0 1 1 1 0;
    0 0 0 0 1 0 0 0 0 0 1 1 1 0 0;
    0 0 0 0 0 1 0 0 0 0 1 0 0 1 1;
    0 0 0 0 0 0 1 0 0 0 0 1 1 0 1;
    0 0 0 0 0 0 0 1 0 0 1 1 0 1 0;
    0 0 0 0 0 0 0 0 1 0 1 1 1 1 1;
    0 0 0 0 0 0 0 0 0 1 1 0 1 0 1];
    systematic = true;
    short_name = '15,10,4';
case "NR"
    code_name = "(16,8,6) nonlinear NR-code"
    cwt = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    0 0 0 0 0 0 0 1 1 1 1 0 0 1 0 1;
    0 0 0 0 0 0 1 1 0 0 1 1 1 1 1 1;
    0 0 0 0 0 0 1 0 1 1 0 1 1 0 1 0;
    0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 1;
    0 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0;
    0 0 0 0 0 1 1 1 1 0 0 1 0 1 0 0;
    0 0 0 0 0 1 1 0 0 1 0 0 1 1 0 1;
    0 0 0 0 1 1 0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 0 0 1 1 1 1 1 0 1 0;
    1 0 1 1 0 1 0 1 0 0 0 1 0 0 0 0;
    1 0 1 1 0 1 1 1 1 1 0 0 0 1 0 1;
    1 0 1 1 1 1 1 0 1 0 1 1 0 1 0 0;
    1 0 1 1 1 1 0 0 0 1 0 1 1 1 0 1;
    1 0 1 1 1 1 0 1 1 0 0 0 1 0 1 1;
    1 0 1 1 1 1 1 1 0 1 1 0 0 0 1 0];
    systematic = false;
    nonlinear = true;
    K = 8;
    N = 16;
    short_name = 'NR';
case "CCSDS"
    code_name = "(32,16) LDPC Code"
    G = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 1 1 1 1 1 0 0;
        0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 1 1 0 1 1 0;
        0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 1 1 1 0 1 0 0 1 1;
        0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 0 1 0 0 1;
        0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 1 1 0 0 1 0 1 1;
        0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 1 0 1 1 0 1 1 0 1;
        0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 0;
        0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0 1 0 1 1 1;
        0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 1 0 1 1 0 1 0;
        0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 1 0 1 0 1 1 0 1 0 0 1 0 1;
        0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0;
        0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1;
        0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1 0 0 1 0 1;
        0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 0 1 0;
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 0 1 0 0 1 0 0 1 0 1 0 1;
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 0 1 1 0 0 1 0 1 0];
    systematic = true;
    short_name = 'LDPC';
otherwise
    continue
endswitch


if !nonlinear
  	% generate codeword table
   [K,N] = size(G);
   A = dec2bin(0:2^K - 1) - '0';
   cwt = mod(A*G, 2);
endif
Rc = K/N;


switch (what)
case "ws"

    weights = sum(cwt, 2);
    numbers=unique(weights);
    count=hist(weights,numbers);
    all_count = zeros(N+1,1);
    all_count(numbers+1) = count;
    all_weights = 0:1:N;
    
    sfname = strcat('spectra/', code, '_spectrum.mat')
    
    if (!exist(sfname))
      A_j = all_count;
      save( sfname, "A_j");
  endif


  bar(all_weights, all_count, "w", 1, "facecolor", "r", "edgecolor", "k");

  if N < 20
      fs = 16;
  else
      fs = 12;
  endif

  set(gca,'XTick',all_weights, "fontsize", fs);
  axis("tight");

    %axisHandle = fh.Children;
    %histHandle = axisHandle.Children;
    %histHandle.BinEdges = histHandle.BinEdges + histHandle.BinWidth/2;
    
    xlabel('Hamming Weight',"fontsize", 16);
    ylabel('Number of Codewords',"fontsize", 16);
    title(code_name,"fontsize", 16);
    grid on;
    tex_link = "";
    print(filename, "-dpng", output_format);
    
    
case "gen"

    tikz_template = fopen('tex_template.tex');
    nlines = fskipl (tikz_template, Inf)
    frewind(tikz_template)
    tikz_code = ''
    for line=0:nlines
      cur_line = fgets(tikz_template);
      tikz_code = sprintf('%s%s', tikz_code, cur_line);
  end;
  fclose(tikz_template);
  if nonlinear
   array_code = '\text{Nonlinear Code; no generator matrix!}';
else
   array_code = ['\mathbf{G_{\mathrm{', short_name, '}}} = \left[\begin{array}{'];
   for i=1:N
     array_code = [array_code, 'c'];
 endfor
 array_code = [array_code, '}'];
 for j=1:K
     for i=1:N
       array_code = [array_code, num2str(G(j,i))];
       if i != N
         array_code = [array_code, '&'];
     else
         array_code = [array_code, '\\'];
     endif
 endfor
endfor
array_code = [array_code, '\end{array}\right]'];
endif

tikz_code = strrep(tikz_code, 'INSERTHERE', array_code);

basename = strrep(filename, '.png', '');
tikz_file = fopen(sprintf('%s.tex', basename), 'w');
fwrite(tikz_file, tikz_code);
fclose(tikz_file)

tex_link = ['<a href=./',sprintf('%s.tex', basename),'> LaTeX code</a>'];


    %      cmd = sprintf('pdflatex -output-directory=./tmp %s.tex; pdf2ps -eps %s.pdf; convert -density 200 %s.eps %s.tikz.png', filename, filename, filename, filename);
    cmd = 'pdflatex -output-directory=./tmp %s.tex; pdf2ps %s.pdf %s.ps; ps2eps -q %s.ps; convert -density 600 -resize 640x480 %s.eps %s.tikz.png ';
    cmd = strrep(cmd, '%s', basename);
    %                  filename,... % TEX
    %                  filename,... % PDF
    %                  filename,... % PS from PDF
    %                  filename,... % PS
    %                  filename,... % EPS
    %                  filename... % PNG
    %                  );
    fprintf('about to execute the command\n%s', cmd);
    [status, output] = system(cmd);
    copyfile(sprintf('%s.tikz.png', basename), filename);
case "gen_p"
    if nonlinear
    	text(0.1,0.4,'Nonlinear Code; no generator matrix!')
    else
       imshow(1-G);
   endif
   title(code_name);
   tex_link = "";
   print(filename, "-dpng", output_format);

case "cwt"
    tikz_template = fopen('tex_template.tex');
    nlines = fskipl (tikz_template, Inf)
    frewind(tikz_template)
    tikz_code = ''
    for line=0:nlines
      cur_line = fgets(tikz_template);
      tikz_code = sprintf('%s%s', tikz_code, cur_line);
  end;
  fclose(tikz_template);
  array_code = ['CWT_{\mathrm{', short_name, '}} = \left(\begin{array}{'];
  for i=1:N
      array_code = [array_code, 'c'];
      if (i==K) && systematic
        array_code = [array_code, '|'];
    endif
endfor
array_code = [array_code, '}'];
if (length(cwt) < 32)
  for j=1:length(cwt)
    for i=1:N
      array_code = [array_code, num2str(cwt(j,i))];
      if (i != N)
        array_code = [array_code, '&'];
    else
        array_code = [array_code, '\\'];
    endif
endfor
endfor
else
  for j=1:30
    for i=1:N
      array_code = [array_code, num2str(cwt(j,i))];
      if (i != N)
        array_code = [array_code, '&'];
    else
        array_code = [array_code, '\\'];
    endif
endfor
endfor
for i=1:N
    array_code = [array_code, '\vdots'];
    if (i != N)
      array_code = [array_code, '&'];
  else
      array_code = [array_code, '\\'];
  endif
endfor
for i=1:N
    array_code = [array_code, num2str(cwt(end,i))];
    if (i != N)
      array_code = [array_code, '&'];
  else
      array_code = [array_code, '\\'];
  endif
endfor
endif

array_code = [array_code, '\end{array}\right)'];

tikz_code = strrep(tikz_code, 'INSERTHERE', array_code);

basename = strrep(filename, '.png', '');
tikz_file = fopen(sprintf('%s.tex', basename), 'w');
fwrite(tikz_file, tikz_code);
fclose(tikz_file)

tex_link = ['<a href=./',sprintf('%s.tex', basename),'> LaTeX code</a>'];


    %      cmd = sprintf('pdflatex -output-directory=./tmp %s.tex; pdf2ps -eps %s.pdf; convert -density 200 %s.eps %s.tikz.png', filename, filename, filename, filename);
    cmd = 'pdflatex -output-directory=./tmp %s.tex; pdf2ps %s.pdf %s.ps; ps2eps -q %s.ps; convert -density 600 -resize 640x480 %s.eps %s.tikz.png ';
    cmd = strrep(cmd, '%s', basename);
    %                  filename,... % TEX
    %                  filename,... % PDF
    %                  filename,... % PS from PDF
    %                  filename,... % PS
    %                  filename,... % EPS
    %                  filename... % PNG
    %                  );
    fprintf('about to execute the command\n%s', cmd);
    [status, output] = system(cmd);
    copyfile(sprintf('%s.tikz.png', basename), filename);
    
endswitch



end


function y=Perr(EsN0dB)
  EsN0=10.^(EsN0dB/10.0);
  y=Q(sqrt(EsN0));       % Perr
end


function y=dB(x)
  y=10*log10(x);
end

function y=nodB(x)
  y=10.^(x./10);
end

function y = Q(x)
  y = 0.5*(1-erf(x/sqrt(2)));
end
