function E = envelope(S, f=.9999, d=0)

  v = 0;
  fn = 1;
  
  for i = 1 : length(S)
    if ( v < S(i) )
      E(i) = S(i);
      v = S(i);
      fn = 1;
    else
      if ( fn > d )
        v = v * f;
      endif
      fn = fn + 1;
      E(i) = v;
    endif
  endfor

endfunction
