function Sig = envelope(Sig, NEnv)
%
% function SIG = ENVELOPE (SIG, NENV)
%
% .. Drs. P ...
%
%  default NEnv = 250 (which is 5 ms at 50 kHz sampling rate)
%
if nargin<2
   NEnv=250; % 5 ms at 50000 Hz
   end

SigLen = size(Sig,1);

if (SigLen < 2*NEnv)

  disp ('-- ERROR: Envelope length greater than signal');

else

  Env1 = ( sin(0.5*pi*[0:NEnv]/NEnv) ).^2;
  Env2 = Env1((NEnv+1):-1:1);
  head = 1:(NEnv+1);
  tail = (SigLen-NEnv):SigLen;

  for i=1:size(Sig,2)
    Sig(head,i) = Env1' .* Sig(head,i);
    Sig(tail,i) = Env2' .* Sig(tail,i);
  end;
end;
