[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-dev] [APPROVE] Adding a RunningAverage UGen

> >         //update sum
> >         sum -=data[count];
> >         float next= ZXP(in);
> >         next *= next;
> >         data[count]= next;
> >         sum += next;

This is not stable for floating point variables. Rounding errors can
accumulate in sum.

This was discussed in a thread on music-dsp last November under the topic
"floating point running sum optimization".

The best solution I could come up with was recalculate the sum every N
samples using an amortized algorithm something like the following:

sum -=data[count];
float next= ZXP(in);
data[count]= next;
sum += next;
sum2 += next;

if( count == N ){
    count = 0;
    sum = sum2;
    sum2 = 0;

have fun hoisting the conditional out of the inner loop for arbitrary N :-)