markzzz
markzzz

Reputation: 47945

How would you calculate the X° step of an Exponential Moving Average?

I smooth some data using a basic Exponential Moving Average filter:

int main ()
{
    double a0 = 0.1;
    double input = 8.0;
    double z = 0.0;

    for(int i=0; i < 200; i++) {
        z += a0 * (input - z);
        std::cout << i  << "° : "<< z << std::endl;
    }
}

For some reasons, I'd like to do it every X (=8) steps. The fact is that as for now, I don't know how to calculate it every 8° input. I still process at every input and "store" only the 8°.

How would you "save CPU" avoid to calculate it on each step? Is there a series where I can just calculate 8° value ahead?

This is the actual code I have (which smooth at each step):

int main ()
{
    double a0 = 0.1;
    double input = 8.0;
    double z = 0.0;
    int step = 8;


    for(int i=0; i < 200; i+=8) {
        z += a0 * (input - z);
        std::cout << i  << "° : "<< z << std::endl;

        int j = 1;
        while (j++ < step) {
            z += a0 * (input - z);
        }
    }
}

I'd like to avoid the "7 steps of while" into a unique operation. Is it possible?

Upvotes: 2

Views: 128

Answers (1)

MSalters
MSalters

Reputation: 179799

It's called an exponential moving average function for a reason: the difference (input - z0) is an exponentially decreasing function of the number of steps. In fact, after N steps the decrease is pow(1-a0,N).

Now the relevant math is pow(x,N) == pow(pow(x,8), N/8).

Upvotes: 3

Related Questions