0
$\begingroup$

Let's say I have an array of $N$ numbers:

$n_1, n_2, .... n_N$

I then calculate a running average of those numbers, by averaging $A$ numbers at a time, which will result in the following array:

$a_1, a_2, .... a_{N-A+1}$ , where

$a_1 = \frac {n_1 + n_2 + ... + n_A}{A}$

$a_2 = \frac {n_2 + n_3 + ... + n_{A+1}}{A}$

...

I then calculate a running average of the running average I obtained above, by averaging B numbers at a time, which will result in the following array:

$b_1, b_2, .... b_{N-A-B+2}$ , where

$b_1 = \frac {a_1 + a_2 + ... + a_B}{B}$

$b_2 = \frac {a_2 + a_3 + ... + a_{B+1}}{B}$

...

My question is, is there some general formula that can get you the second running average (b numbers) directly from original set (n numbers), with arbitrary A and B coefficients?

I'm asking, because I did a few of these by hand, and the results look very much like digital filters (weighted averages). Thanks.

$\endgroup$
  • 1
    $\begingroup$ Your examples are unclear. You say "averaging $A$ numbers at a time", but you show averages involving $3$ numbers (e.g., $a_1=\frac{n_1+n_2+n_3}{A}$). $\endgroup$ – Matthew Conroy Mar 7 '16 at 17:42
  • $\begingroup$ @MatthewConroy You're right, I'll fix it. $\endgroup$ – Eternal21 Mar 7 '16 at 17:47
  • $\begingroup$ If you look at the moving average as a digital filter, this is just passing a signal into two FIRs in cascade. So you can convolve the two impulse responses of the FIRs and get a combined system. Remember convolving two box car gives a triangle, that's why you get the increasing and then decreasing and also symmetric coefficients down there in one of the answers. $\endgroup$ – Andrew Au Mar 7 '16 at 17:57
1
$\begingroup$

For $A=3,B=4$, if we just substitute in we get $b_i=\frac {a_i+a_{i+1}+a_{i+2}+a_{i+3}}4=\frac{n_i+2n_{i+1}+3n_{i+2}+3n_{i+3}+2n_{i+4}+n_{i+5}}{12}$ The pattern should be suggestive. You ramp up the coefficients, stay constant at the peak, then ramp them down. You could write it with summation signs if you want. $$b_i=\frac 1B\sum_{j=i}^{i+B-1}a_j=\frac 1{AB}\sum_{j=i}^{i+B-1}\sum_{k=j}^{j+A-1}n_k$$ and you would like to write this as $$b_i=\frac 1{AB}\sum_{j=i}^{i+A+B-2}c_jn_n$$ The $c_j$ are the coefficients $1,2,3,3,2,1$ above. You can write $c_j=\min(A,B)-$end effects, where the end effects cause the ramp down at the ends. If $j-i \lt \min(A,B), c_j=j-i+1$ and if $i+A+B-2-j \lt \min (A,B), c_j=i+A+B-1-j$

$\endgroup$
0
$\begingroup$

For the sake of illustration, let us average on $3$ numbers, then $4$.

Ignoring the constant denominators for convenience,

$$b_1=a_1+a_2+a_3,b_2=a_2+a_3+a_4,b_3=a_3+a_4+a_5,\\ b_4=a_4+a_5+a_6,b_5=a_5+a_6+a_7,b_6=a_6+a_7+a_8\cdots$$

and

$$c_1=b_1+b_2+b_3+b_4=a_1+2a_2+3a_3+3a_4+2a_5+a_6,\\ c_2=a_2+2a_3+3a_4+3a_5+2a_6+a_7,\\ c_3=a_3+2a_4+3a_5+3a_6+2a_7+a_8\cdots$$

The pattern is a sum of $6$ consecutive elements weighted by $1,2,3,3,2,1$ (to be divided by $3\cdot4$). Such a sliding linear combination is called a convolution operation. (You can express this easily using the Discrete Fourier Transform.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.