%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .m script of web demo "Insert title here" 
% Written for GNU Octave
% 
% INSTITUTE OF TELECOMMUNICATIONS  
% University of Stuttgart 
% www.inue-uni-stuttgart.de 
% author: Marc Gauger / Alexander Knaub   
% date: 11.04.2014   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [h_x,r] = transinformation_fun_1d(T, svg_en, webdemo_en, filename)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN, set up graphical output

% Create an invisible figure.
if (webdemo_en)
  fig = figure(1); set(fig, "visible", "off");
else
  fig = figure(1); set(fig, "visible", "on");
endif

landscape = "-S930,350";
portrait  = "-S640,480";

output_format = landscape; %portrait;     % default


set(0, "defaultlinelinewidth", 1);

if (webdemo_en)
  set(0, "defaultaxesfontsize", 8);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 8);
  set(0, "defaulttextfontname", "Helvetica");
  const_lw = 1;
elseif (svg_en)
  set(0, "defaultaxesfontsize", 6);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 6);
  set(0, "defaulttextfontname", "Helvetica");
  const_lw = 1;
else
  set(0, "defaultaxesfontsize", 10);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 8);
  set(0, "defaulttextfontname", "Helvetica");
  const_lw = 2;
endif

% END, set up graphical output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=1001
x1 = linspace (0, 1 , N);

for n = 1:N
    x2 = 1- x1(n);
    x = [x1(n) x2];
    y=x*T;
    Y_inv = diag(1./y);
    I=log(T*Y_inv)/log(2);
    I(T==0)=0;
    I_x_y(n)=sum(x*(T.*I));
end 



plot(x1,I_x_y,'linewidth',5);
xlabel({'P[x_1], P[x_2]=1-P[x_1]'});
ylabel({'I(X;Y)'});
if T(1,1)==T(2,2) && T(1,2)==T(2,1)
title({'I(X;Y) in bit/symbol (BSC-model)'});
else
title({'I(X;Y) in bit/symbol'});
end
peak=max(I_x_y);
imax = find(max(I_x_y) == I_x_y)

text(0.1,peak*0.2,['C = max(I(X;Y)}) = ' num2str(peak) ' bit/symbol';...
'P[x_1] = ' num2str(x1(imax)) ;...
'P[x_2] = ' num2str(1-x1(imax))])

print(filename, "-dpng","-S640,480");

if (svg_en)
  print(["fig_",filename], "-dsvg",output_format);
endif

end

