2
votes

I need to compute a weighted moving average withous loops and withoud storing infromation. The weight could be linear, so that the old sample is weighted less than the new one.

For example, using a 20 samples window, my weights vector would be:

[1 2 3 4 5 ... 20]

I'm using the following formula to compute the moving mean:

   newMean = currMean + (newSample - currMean)/WindowSize

now I need to "inject" weight.

What I can know: 1. which sample I'm considering (14th....26th....), I can count. 2. of course, I can know currMean

What I can know but I don't want to do: 1. storing all the samples (in my case they are 1200 x 1980 x 3 matrix, I simply can't store them).

I'm currently using Matlab, but I really do not need the code, just the concept, if it exists.

Thank you.

1
I would just use convolution and construct your filter such that all of the weights sum to 1. i.e. (1:20) ./ sum(1:20) - Suever
Mmm I'm using very big matrices, I don't think convolution would be good for me. I mean..I'm just using 3 simple math operations right now, why I should introduce convolution only for weights? - Leo91
Convolution does precisely what you are trying to do and does so very efficiently - Suever
I've access to only a sample at time (I do not remember other samples). - Leo91
You might want to look at (en.wikipedia.org/wiki/Moving_average) That tells you how to compute an exponentially weighted moving average using no more storage that you do now. - dmuir

1 Answers

1
votes

Look into techniques in digital signal processing. You are describing a FIR filter, which can be implemented as a convolution, or as a memory efficient circuit. Basically you can rewrite it as a recursive equation that keeps only the filter-length past filtered intermediate state variables. MATLAB does this in filter function (you can chain the internal state to continue filtering). See documentation of filter and I also recommend reading a DSP textbook.