%
%  Generation of Gaussian pdf with mean mu and standard deviation sigma
%
function f = gauss_pdf(Npoints,Nbins,mu,sigma);
%
%  generate the gaussian density
%
  data = mu+sigma*randn(Npoints,1);
  min_data = min(data);
  max_data = max(data);
  dx = linspace(min_data,max_data,Nbins+1);
  delta = dx(2)-dx(1);
%
% Now construct the pdf
%
  x = [dx(1)];
  f = [y(data,dx(1),dx(2))];
  for i=2:length(dx)-1
     f = [f y(data,dx(i),dx(i+1))];
     x = [x dx(i)];
  end;
  %
  %  get the true values
  %
    ftrue = exp(-(x-mu).*(x-mu)/(2*sigma*sigma))/(sqrt(2*pi*sigma*sigma));
    plot(x,f,'r-',x,ftrue,'k:','Linewidth',1.5); grid;
    legend('Estimated pdf','Exact pdf');
    ylabel('f_X(x)'); xlabel('x');
    title(['Gaussian pdf with \mu = ', num2str(mu,3) ', \sigma = ' ...
        num2str(sigma,4) ', Npoints = ' num2str(Npoints) ', Nbins = ' num2str(Nbins) ]);
    return;
%
%  the function y determines number of data values of X in (x,x+dx]
%    divided by dx
%
function f = y(X,x1,x2)
%
  f = sum( (X>x1)&(X<=x2))/((x2-x1)*length(X));
  return;
  
  
