Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a sound recognition algorithm where an "exponential moving average" is used for "adapting" to sound levels. It turns out that taking an average of logs works better than simple sums ("smooths" erratic data better), so I used this algorithm:

newAverageX = exp((((dInterval - 1.0) * log(dOldAverage)) + log(dValue)) / dInterval)

This works well, but now I've extended into an area where I have some values that may be zero or negative, and that obviously causes problems with log. So I tried the following:

min = minimumOf(dOldAverage, dValue);
fudge = 1.0 - min;
newAverage = exp((((dInterval - 1.0) * log(dOldAverage + fudge)) + log(dValue + fudge)) / dInterval) - fudge;

This avoids errors with log of a negative, but the results for positive values are different from the original.

Is there an algorithm that would produce the "logically correct" results (whatever that means) for all cases. Or is there another averaging technique that would work similarly to using log above but be tolerant of negatives?

share|cite|improve this question
How negative can the values be? Is it just that the values would ideally all be positive but are corrupted by some additive noise, or are you looking at something like a sine wave that oscillates significantly above and below zero? – Rahul Mar 12 '12 at 21:16
@RahulNarain -- I've finally gotten the numbers to where they mostly stay positive, so mainly we're handling noise situations and "transitions". But it would be nice to have a general algorithm so I don't have to worry about discontinuities. – Daniel R Hicks Mar 12 '12 at 22:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.