function stm = gengwn (N, NEnvelope, order, RMS)
%
% FUNCTION STM = GENGWN (N, NEnvelope, order, RMS)
%              = GENGWN (N, NEnvelope)
%              = GENGWN (N)
%
% Generate Gaussian White Noise of N points (= N/50 msec), 
%    LP filtered at 22.5 kHz (sampling freq. = 50 kHz),
%    with on-offset ramps of NEnvelope samples (default 250 = 5 ms), 
%    and given LP filter order (default = 100). 
%    Nchan = 1 (mono) or 2 (stereo, default)
%    The default RMS-value (in [0, 1]) of the noise is taken at 0.5
%
%  P. Hofman
%

fnyquist  = 25000;
fcutoff   = 22500;
fs        = 2.0*fnyquist;   % sampling frequency

if nargin<4; RMS       = 0.5; end
if nargin<3; order     = 100; end
if nargin<2; NEnvelope = 250; end

sig = randn (N,1);
sig = lowpass (sig, fcutoff/fnyquist, order);
sig = lowpass (sig, fcutoff/fnyquist, order);
stm = RMS * envelope (sig, NEnvelope);

dur=N/50;  % duration in ms

if dur>50 pdur=fs*50/1000;
else pdur=fs*dur/1000;
end

p50=fs*50/1000;
figure(2)
clf
plot(stm(1:pdur,1));   % plot up to the first 50 ms of the noise
axis([0 p50 -1 1]);
set(gca, 'XTick',[0 p50/5 2*p50/5 3*p50/5 4*p50/5 p50]);
set(gca, 'XTickLabel',[' 0';'10';'20';'30';'40';'50']);
xlabel('Time [ms]', 'FontSize', 12);
ylabel('Amplitude [au]','FontSize',12);


return
