Envelope detector

References : Posted by Bram
Notes :
Basicaly a one-pole LP filter with different coefficients for attack and release fed by the abs() of the signal. If you don't need different attack and decay settings, just use in->abs()->LP
Code :
//attack and release in milliseconds
float ga = (float) exp(-1/(SampleRate*attack));
float gr = (float) exp(-1/(SampleRate*release));

float envelope=0;

for(...)
{
  //get your data into 'input'
  EnvIn = abs(input);

  if(envelope < EnvIn)
  {
     envelope *= ga;
     envelope += (1-ga)*EnvIn;
  }
  else
  {
     envelope *= gr;
     envelope += (1-gr)*EnvIn;
  }
  //envelope now contains.........the envelope ;)
}

Comments
from : arguru[AT]smartelectronix
comment : Nice , just a typo: //attack and release is entered in SECONDS actually in this code ;)

from : antiprosynthesis[AT]gmail[DOT]com
comment : // Slightly faster version of the envelope follower using one multiply form. // attTime and relTime is in seconds float ga = exp(-1.0f/(sampleRate*attTime)); float gr = exp(-1.0f/(sampleRate*relTime)); float envOut = 0.0f; for( ... ) { // get your data into 'input' envIn = fabs(input); if( envOut < envIn ) envOut = envIn + ga * (envOut - envIn); else envOut = envIn + gr * (envOut - envIn); // envOut now contains the envelope }

from : madgel79[AT]nate[DOT]com
comment : in my code , attack_coef and release_coef are always '0'. If I use only 'abs()' , it also work well. why can it be possible? Would you please give me some infomation about this problem. Thanks.

from : bob[AT]yahoob[DOT]com
comment : Should use "fabsf" really. :)

from : bouzidom[AT]yahoo[DOT]com
comment : i have a signal and i need to extract the envelope usig this code, so what are the initial attack and release values? thanks,