3
$\begingroup$

I'm using a 5-point Triangle Moving Average:

$$S_j = (Y_{j-2} + 2Y_{j-1} + 3Y_j + 2Y_{j+1} + Y_{j+2}) / 9$$

The problem is that I often need to smooth my data more than once, and when I do this too much, it becomes noticeability very slow (and I'm using C++).

Is there a way to optimize this formula?

Like maybe skipping every other data point and then using interpolation in the end, or something clever?

Thanks

$\endgroup$

1 Answer 1

0
$\begingroup$

the "clever" way to improve the efficiency of the moving average filter is to implement it recursively; you will then need only two computations per data point, regardless of the length of the filter.

see page 281 and following of The Scientist and Engineer's Guide to Digital Signal Processing.

(the algorithm given there is for a rectangular filter kernel, but the triangular kernel is just the rectangular filter applied twice)

$\endgroup$
4
  • $\begingroup$ My formula is weighted though (i.e. 1Y, 2Y, 3Y). So before I get too excited, can it even be recursive? If so, do you have any pdf references for creating and using "kernel" math. The pdf you gave is a wonderful tease, I really hope I can go down that path. $\endgroup$ Commented May 15, 2014 at 21:31
  • $\begingroup$ your weighted "triangular" average is equivalent to applying the uniform "rectangular" average twice. $\endgroup$ Commented May 15, 2014 at 22:10
  • $\begingroup$ I toyed with it and discovered that the real secret is using the rectangular formula with "more points". For instance, a 5-point rect is similar enough that running it twice is not needed for me. And a 7-point rect is similar to running my 5-point triangular twice. So, thank-you very much for the help. I'll toy with higher points and get it the way I want. $\endgroup$ Commented May 16, 2014 at 23:24
  • $\begingroup$ I found a problem with using more points: I do not know how to correctly fill-in the "missing data" into the beginning and end of the output array. Looking at the computer code on the last page 284 of your pdf, Y[0] to Y[49] and Y[4950] to Y[4999] are not calculated. PS...Line 290 should be: Y[I%] = ACC/101 per link $\endgroup$ Commented May 19, 2014 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .