Envelope follower not smooth enough With the following code, I get the "envelope" of a sound signal.
However in the "envelope" there are small oscillations (high frequency) : it is not smooth enough...



I imagine that it's possible to apply a low-pass to the "envelope signal" in order to  remove the high frequencies oscillations, but I also imagine it's probably better to do it directly by modifying this code :

double attack_coef = Math.Exp(Math.Log(0.01)/( 10 * 44100 * 0.001));   // 10 ms
double release_coef = Math.Exp(Math.Log(0.01)/( 100 * 44100 * 0.001));  // 100 ms
double envelope = 0.0;

for (int n = 0; n &lt; file.Length; ++n) 
{
 double tmp = Math.Abs(x[n]);
 if (tmp &gt; envelope)
  envelope = attack_coef * (envelope - tmp) + tmp;
 else
  envelope = release_coef * (envelope - tmp) + tmp;

 env[n] = envelope;
}


Do you have an idea for that ?
You are already just low-pass filtering the magnitude of the input signal. Right now, your envelope detection "circuit" is just a single-pole filter. Let $f[n]$ be the input to your system and, for simplicity sake, assume that attack_coef $=$ release_coef $ = a_1$. Then we can express the output $y$ of your system as follows:

$$y[n] = a_1 \left(y[n-1] - |f[n]|\ \right) + |f[n]|$$

Which we can rearrange as a transfer function $h$ as follows:
$$y = a_1 \left(y\,z^{-1} - |f|\right) + f$$
$$h = \frac{y}{|f|} = \frac{1 - a_1}{1 - a_1 z^{-1}}$$

You can use this information to either adjust your coefficients to get the desired cutoff frequency, or you could add another pole if you want a steeper roll-off.