0001 function [y] = randg_yp(N,av,fwhm,display_mode)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 if nargin == 0,
0024 N = 100000;
0025 av = 0;
0026 fwhm = 0.5;
0027 display_mode = 100;
0028 end
0029
0030 if nargin == 1,
0031 av = 0;
0032 fwhm = 0.5;
0033 display_mode = 0;
0034 end
0035
0036 if nargin == 2,
0037 fwhm = 0.5;
0038 display_mode = 0;
0039 end
0040
0041 if nargin == 3,
0042 display_mode = 0;
0043 end
0044
0045 fwhm = fwhm(:);
0046 av = av(:);
0047 if size(av) ~= size(fwhm), error('Inconsistent size of av and hwhm in randg_yp.m');end
0048
0049 sigma = fwhm/sqrt(2*log(2))/2;
0050 Xsigma = repmat(sigma,[1,N]);
0051 Xav = repmat(av,[1,N]);
0052
0053 y = sqrt(-2*log(rand(size(Xav,1),N))).*cos(2*pi*rand(size(Xav,1),N)).*Xsigma + Xav;
0054
0055 if nargin == 4,
0056 if display_mode,
0057 if display_mode < round(N/100),
0058 disp(['WARNING: display_mode parameter must exceed be of the order of ',int2str(round(N/100)),' for a correct histogramm.']);
0059 display_mode = round(N/100);
0060 else
0061 disp(['WARNING: not enough trials for drawing a correct histogramm -> display_mode parameter set to zero.']);
0062 display_mode = 0;
0063 end
0064 end
0065 end
0066
0067 if display_mode,
0068 [z,x] = hist(y,display_mode);
0069 normz = sum(z)*(x(2)-x(1));
0070 figure(1),plot(x,z/normz,'b',x,exp(-(x - av).^2/2/sigma^2)/sqrt(2*pi)/sigma,'r');
0071 norm_th = sum(exp(-(x - av).^2/2/sigma^2))/sqrt(2*pi)/sigma*(x(2)-x(1));
0072 title('Gaussian white noise distribution function (normalized to 1)')
0073 xlabel('y');ylabel('f(y)')
0074 legend('Numerical','Theoretical')
0075 hold off
0076 end