%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .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_phi_q_animation(numberOfPictures, filename)
  
  from = -4;
  to = 4;
  
  q_color = [.2,.7,.2];
  phi_color = [.8,.3,.3];
  
  % define formats
  landscape = "-S930,350";
  portrait  = "-S1280,960";
  
  % 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", 12);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 12);
  set(0, "defaulttextfontname", "Helvetica");
  
  % This to test and demonstrate the debug engine:
  % fprintf("This is a debug message. Checking format: %s", output_format);
  
  x = linspace(from, to, 101)
  
  index = 0;
  for xprime = linspace(from,to,numberOfPictures)
    newFilename = [filename '_' num2str(index)];
    index = index + 1;
    
    clf;
    subplot(2,1,1);
    hold on;
    y = pdf(x, sigma=1);
    plot(x, y, "color", 'b')
    
    
    [val,cut_idx]=min(abs(x-xprime));
    
    if cut_idx > 1
      H=area(x(1:cut_idx),y(1:cut_idx));
      set(H(1),'FaceColor',phi_color);
    endif
    if cut_idx < 101
      H=area(x(cut_idx:end),y(cut_idx:end));
      set(H(1),'FaceColor',q_color);
    endif
    
    
    ylabel('p_n(\xi)');
    xlabel('\xi');
    grid on;
    
    subplot(2,1,2);
    hold on;
    plot(x,PHI(x), "color", phi_color)
    plot(x,Q(x), "color", q_color)
    
    plot(xprime,PHI(xprime),'o', "color", phi_color)
    plot(xprime,Q(xprime),'o', "color", q_color)
    
    grid on;
    xlabel("x'");
    legend({['\Phi(x' "')"],"Q(x')"})
    print(newFilename, "-dgif",output_format);
    
  endfor
  
endfunction

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


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

function y = PHI(x)
  y = Q(-x);
end
