This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Moving Average of last N samples in F28069M launchpad

I am using F28069M launchpad .I has multiple control loops. In one of them, I need to get the get the average of a signal with 100Hz ripples. I will average the last N samples of the signal  in each cycle. I need the code that is efficient and N is as high as posible. How do Igo about it? Do I use FIR filter code or is there some more efficient method?

  • Arpan,

    There is an FIR filter implementation in the floating-point DSP library in control suite.  You can use this by converting your data to floating-point, however it will compute the entire N-tap filter each time.  If N is very large and you just want the arithmetic mean, it would be better to just subtract the oldest data and add the newest to a running total, then divide by N.  You'll have to store all the data in a circular buffer, but if you work in fixed-point and N is a power of 2, the divide can just be a right-shift.   Even in C this will be much more efficient.  Regret I don't know of any example code, but it's straightforward in C.

    Regards,

    Richard

  • Arpan,

    I'd like to make an addendum to the reply.

    A colleague pointed out that when working in floating-point there might be an issue with the technique.  This is because addition in floating-point is not necessarily associative:  "(sum + x1 ) - x2: does not necessarily equal "sum + (x1 - x2)".  This is caused by different operand exponent sizes and could lead to the result drifting over time.  Fixed-point operations should be fine, so if you're working with ADC data you may be better off staying in fixed-point and using the right-shifting technique I mentioned for the divide.

    I hope this doesn't cause any inconvenience.

    Thanks to David Alter for the correction.

    Regards,

    Richard