function y = browniannoise(T, Fs)
   // Brownian noise generator
   //
   // Usage:
   //       y = browniannoise(T, Fs)
   //
   // where T:  duration in s
   //       Fs: sampling rate in Hz
   //       y:  brownian noise output row vector 
   //
   // The function proceeds by FFT filtering white noise 
   // with 1/f frequency response to get an 1/f^2 spectrum. 
   //
   // Se also: whitenoise, pinknoise, bandnoise
   //
   // ------------------------------------
   // Author: Federico Miyara
   // Date:   2010-06-08
   //         2011-01-29
   //         2012-12-24
   //         2016-01-03
   //         2019-05-24
   //         2020-08-29
   //         2023-03-07  

   // Provide default values for missing arguments
   nargin = argn(2);
   if nargin < 2
      Fs = 44100;
   end

   N = 16384;

   // White noise generation 
   y = grand(1, 1 + floor(Fs*T), "nor", 0, 1);

   // Filtering window design
   w = 1 ./ [1:N/2];

   // Load function
   flag = exists("fftfilter")
   if ~flag
      exec fftfilter.sci;
   end

   // Signal filtering
   y = fftfilter(y, w, N);

   // Unload function if previously not loaded
   if ~flag
      clear fftfilter;
   end

   // DC component removal
   y = y - mean(y);

   // Normalization to get RMS value equal to 1.
   y = y / sqrt(mean(y.^2));

endfunction


