%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .m script of web demo Normal Distribution
% Written for GNU Octave
%
% INSTITUTE OF TELECOMMUNICATIONS
% University of Stuttgart
% www.inue-uni-stuttgart.de
% author: Marvin Geiselhart
% date: 17.04.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 [] = demo_pdf_1(paramtype, varvalue, logy, showsigma, max_xi, 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", 8);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 8);
  set(0, "defaulttextfontname", "Helvetica");
  
  % This to test and demonstrate the debug engine:
  % fprintf("This is a debug message. Checking format: %s", output_format);
  
  p = varvalue
  switch (paramtype)
    case "std"
    sigma = abs(p)
    case "var"
    sigma = abs(sqrt(p))
    case "snr_r"
    sigma = sqrt(10^(-p/10))
    case "snr_c"
    sigma = sqrt(10^(-p/10)/2)
  endswitch
  
  if sigma < eps
    sigma = eps
  endif
  
  if abs(max_xi) < eps
    max_xi = eps
  endif
  
  
  x = (-1:0.001:1)*abs(max_xi)
  y = pdf(x, sigma)
  if logy
    semilogy(x,y)
  else
    plot(x,y)
  endif
  
  xlim([-max_xi, max_xi])
  
  if showsigma
    line([-sigma,-sigma],[min(ylim), pdf(sigma, sigma)], "linestyle", "--", "color", "m" )
    line([-sigma,sigma],[pdf(sigma, sigma), pdf(sigma, sigma)], "linestyle", "--", "color", "m" )
    line([sigma,sigma],[min(ylim), pdf(sigma, sigma)], "linestyle", "--", "color", "m" )
  endif
  
  snr_r = 10*log10(1/(sigma^2));
  snr_c = 10*log10(1/(2*sigma^2));
  
  title(['\sigma^2 = ' num2str(sigma^2,'%4.2f') ', SNR (real-val.) = ' num2str(snr_r, '%4.2f') ' dB, SNR (complex-val.) = ' num2str(snr_c, '%4.2f') ' dB'])
  xlabel('\xi')
  ylabel('p_n(\xi)')
  grid on
  
  % Create an image file. This png image is displayed on the webpage. Its size
  % must be 640x480.
  print(filename, "-dpng", output_format);
  
  
end

function y = pdf(x, sigma)
  y=  1/(sqrt(2*pi)*sigma)*exp(-1/(2*sigma^2)*x.^2)
endfunction


