6.2: Simple Averages
The simplest numerical filter is the simple averaging filter. This filter is defined by the equation
\[x=\frac{1}{N} \sum_{n=1}^{N} u_{n} \nonumber \]
The filter output \(x\) is the average of the \(N\) filter inputs \(u_1,u_2,...u_N\). These inputs may be real or complex numbers, and \(x\) may be real or complex. This simple averaging filter is illustrated in Figure 1.
If the averaging filter is excited by the constant sequence \(u_1=u_2=⋯=u_N=u\), then the output is
\[x=\frac{1}{N} \sum_{n=1}^{N} u=u \nonumber \]
The output is, truly, the average of the inputs. Now suppose the filter is excited by the linearly increasing sequence
\(u_{n}=n, \quad n=1,2, \ldots, N\)
This sequence is plotted in Figure 2. How do we sum such a sequence in order to produce the average \(x\)? For \(N\) even, the average may be written as
\[x=\frac{1}{N}\left(x_{1}+x_{N}\right)+\frac{1}{N}\left(x_{2}+x_{N-1}\right)+\cdots+\frac{1}{N}\left(x_{N / 2}+x_{(N / 2)+1}\right) . \nonumber \]
Each pair-sum in parentheses equals \(N+1\), and there are \(\frac{N}{2}\) such pair-sums, so the average is
\[x=\frac{1}{N} \frac{N}{2}(N+1)=\frac{N+1}{2} \nonumber \]
This is certainly a reasonable answer for the average of a linearly increasing sequence. See Figure 2.
Write \(x=\frac{1}{N} \sum_{n=1}^{N} n\) as a sum of pair-sums for \(N\) odd. What does \(x\) equal?
General Sum Formula
Suppose the input to the simple averaging filter is the polynomial sequence
\(u_{n}=n^{k}, n=1,2, \ldots, N\)
where \(k\) is a non-negative integer such as \(k\)=0,1,2,....The output of the filter is
\[x_{N}^{(k)}=\frac{1}{N} \sum_{n=1}^{N} n^{k} \nonumber \]
We rewrite \(x\) as \(x_{N}^{(k)}\) to remind ourselves that we are averaging \(N\) numbers, each of which is \(n^k\). For example, when \(N=8\) and \(k=2\),
\[x_{8}^{(2)}=\frac{1}{8} \sum_{n=1}^{8} n^{2}=\frac{1}{8}(1+4+9+\cdots+64) \nonumber \]
Rather than study the average \(x_{N}^{(k)}\), we will study the sum \(Nx_{N}^{(k)}\) and divide by \(N\) at the very end:
\[S_{N}^{(k)}=N x_{N}^{(k)}=\sum_{n=1}^{N} n^{k} \nonumber \]
The sum \(S_{N}^{(k)}\) may be rewritten as the sum
\[\begin{align}
S_{N}^{(k)} &=\sum_{n=1}^{N-1} n^{k}+N^{k} \nonumber \\
&=S_{N-1}^{(k)}+N^{k}
\end{align} \nonumber \]
This result is very important because it tells us that the sum \(S_{N}^{(k)}\), viewed as a function of \(N\), obeys a recursion in which \(S_{N}^{(k)}\) is just the sum using one less input, namely, \(S_{N-1}^{(k)}\), plus \(N^k\). Now, since polynomials are the most general functions that obey such recursions, we know that \(s_{N}^{(k)}\) must be a polynomial of order \(k+1\) in the variable \(N\):
\[s_{N}^{(k)}=a_{0}+a_{1} N+a_{2} N^{2}+\cdots+a_{k+1} N^{k+1} \nonumber \]
Let's check to see that this polynomial really can obey the required recursion. First note that \(S_{N-1}^{(k)}\) is the following polynomial:
\[s_{N-1}^{(k)}=a_{0}+a_{1}(N-1)+\cdots+a_{k+1}(N-1)^{k+1} \nonumber \]
The term \((N-1)^{k+1}\) produces \(\left(\begin{array}{c}
k+1 \\
0
\end{array}\right) N^{k+1}(-1)^{0}+\left(\begin{array}{c}
k+1 \\
1
\end{array}\right) N^{k}(-1)^{1}+\cdots\). (Remember the binomial expansion?) Therefore the difference between \(s_{N}^{(k)}\) and \(s_{N-1}^{(k)}\)
\[S_{N}^{(k)}-S_{N-1}^{(k)}=c_{0}+c_{1} N+\cdots+c_{k} N^{k} \nonumber \]
This recursion is general enough to produce the difference \(N^{k}\) provided we can solve for \(a_{0}, a_{1}, \ldots, a_{k+1}\) to make \(c_{0}=c_{1}=\cdots=c_{k-1}=0\) and \(c_{k}=1\). We know that \(S_{N}^{(k)}=0\) for \(N = 0\), so we know that \(a_0 = 0\) meaning that the polynomial for \(S_{N}^{(k)}\) can really be written as
\[S_{N}^{(k)}=a_{1} N+a_{2} N^{2}+\cdots+a_{k+1} N^{k+1} \nonumber \]
In order to solve for the coefficients of this polynomial, we propose to write out our equation for \(s_{N}^{(k)}\) as follows:
\[\begin{array}{ccc}
(N=1) & S_{1}^{(k)}= & a_{1}+\cdots+a_{k+1} \\
(N=2) & S_{2}^{(k)}= & 2 a_{1}+\cdots+2^{k+1} a_{k+1} \\
(N=3) & s_{3}^{(k)}= & 3 a_{1}+\cdots+3^{k+1} a_{k+1} \\
\vdots & & \vdots \\
(N=k) & S_{k}^{(k)}= & k a_{1}+\cdots+k^{k+1} a_{k+1} \\
(N=k+1) & S_{k+1}^{(k)}= & (k+1) a_{1}+\cdots+(k+1)^{k+1} a_{k+1} .
\end{array} \nonumber \]
Using the linear algebra we learned earlier, we may write these equations as the matrix equation
\[\left[\begin{array}{cccl}
1 & 1 & \ldots & 1 \\
2 & 4 & \ldots & 2^{k+1} \\
\vdots & \vdots & & \vdots \\
k & k^{2} & \ldots & k^{k+1} \\
(k+1) & (k+1)^{2} & \ldots & (k+1)^{k+1}
\end{array}\right]\left[\begin{array}{l}
a_{1} \\
a_{2} \\
\vdots \\
\, \\
a_{k+1}
\end{array}\right]=\left[\begin{array}{l}
s_{1}^{(k)} \\
S_{2}^{(k)} \\
\vdots \\
S_{k+1}^{(k)}
\end{array}\right] \nonumber \]
The terms on the right-hand side of the equal sign are “initial conditions” that tell us how the sum \(S_{N}^{(k)}\) begins for \(N=1,2,...,k+1\). These initial conditions must be computed directly. (For example, \(S_{2}^{(k)}=1^{k}+2^{k}\).) Then the linear system of \((k+1)\) equations in \((k+1)\) unknowns may be solved for \(a_{1}, a_{2}, \ldots, a_{k+1}\). The solution for \(S_{N}^{k}\) is then complete, and we may use it to solve for \(S_{N}^{k}\) for arbitrary \(N\).
When \(k=2\), we have the following equation for the coefficients \(a_1,a_2\), and \(a_3\) in the polynomial \(S_{N}^{(2)}=a_{1} N+a_{2} N^{2}+a_{3} N^{3}\):
\[\left[\begin{array}{lll}
1 & 1 & 1 \\
2 & 4 & 8 \\
3 & 9 & 27
\end{array}\right]\left[\begin{array}{l}
a_{1} \\
a_{2} \\
a_{3}
\end{array}\right]=\left[\begin{array}{l}
1^{2} \\
1^{2}+2^{2} \\
1^{2} +2^{2}+3^{2}
\end{array}\right]=\left[\begin{array}{l}
1 \\
5 \\
14
\end{array}\right] \nonumber \]
Solve for \(a_1,a_2,a_3\) in the linear equation of Example 2 . Show that \(S_{N}^{(2)}=a_{1} N+a_{2} N^{2}+a_{3} N^{3}\) obeys the recursion \(S_{N}^{(2)}-S_{N-1}^{(2)}=N^{2}\).
(MATLAB) Write a MATLAB program to determine the coefficients \(a_{1}, a_{2}, \ldots, a_{k+1}\) for the polynomial \(S_{N}^{(k)}\). Generate a table of formulas for the averages (x_{N}^{(k)}\) for \(k\)=1,2,...,5. Evaluate these formulas for \(N\)=2, 4, 8, and 16.
Exponential Sums
When the input to an averaging filter is the sequence
\[u_{n}=a^{n}, n=0,1,2, \ldots, N-1, \nonumber \]
we say that the input is exponential (or geometric). Typical sequences are illustrated in Figure 6.5 for \(a\)=0.9, \(a\)=1, and \(a\)=1.1. Don't let it throw you that we have changed the index to run from 0 to \(N-1\) rather than from 1 to \(N\). This change is not fundamentally important, but it simplifies our study. The sum of the inputs is
\[S_{N}=\sum_{n=0}^{N-1} a^{n} . \nonumber \]
How do we evaluate this sum? Well, we note that the sum \(a S_{N}\) is
\[\begin{align}
a S_{N} &=\sum_{n=0}^{N-1} a^{n+1}=\sum_{k=1}^{N} a^{k} \nonumber \\
&=\sum_{k=0}^{N-1} a_{k}+a^{N}-1 \nonumber \\
&=\quad S_{N}+a^{N}-1
\end{align} \nonumber \]
Therefore, provided \(a \neq 1\), the sum \(S_N\) is
\[S_{N}=\frac{1-a^{N}}{1-a}, \quad a \neq 1 \nonumber \]
This formula, discovered already in the chapter covering the functions \(e^x\) and \(e^{jθ}\) , works for \(a \neq 1\). When \(a=1\), then \(S_N=N\):
\[S_{N}= \begin{cases}\frac{1-a^{N}}{1-a}, & a \neq 1 \\ N, & a=1\end{cases} \nonumber \]
When \(|a| < 1\), then \(a^N \rightarrow 0\) for \(N \rightarrow \infty\), and we have the asymptotic formula
\[\lim _{N \rightarrow \infty} S_{N}=\frac{1}{1-a}, \quad|a|<1 \nonumber \]
Evaluate \(S_{N}=\sum_{n=0}^{N-1} a^{n}\) and \(X_{N}=\frac{1}{N} S_{N}\) for \(a=0.9,1\), and 1.1 and for \(N\)=1,2,4,8,16.
Prove that \(S_{N}=\sum_{n=0}^{N-1} a^{n}\) obeys the recursion
\(S_{N}=S_{N-1}+a^{N-1}\)
Prove that \(S_N=N\) obeys this recursion for \(a=1\) and that \(S_{N}=\frac{1-a^{N}}{1-a}\) obeys it for \(a \neq 1\).
Recursive Computation
Every sum of the form
\[S_{N}=\sum_{n=0}^{N-1} u_{n} \nonumber \]
obeys the recursion
\[S_{N}=S_{N-1}+u_{N-1} \nonumber \]
This means that when summing numbers you may “use them and discard them.” That is, you do not need to read them, store them, and sum them.
You may read \(u_0\) to form \(S_1\) and discard \(u_0\); add \(u_1\) to \(S_1\) and discard \(u_1\) ; add \(u_2\) to \(S_2\) ; and continue.
This is very important for hardware and software implementations of running sums. You need only store the current sum, not the measurements that produced it. Two illustrations of the recursion \(S_{n+1}=S_{n}+u_{n}\) are provided in Figure 4. The diagram on the left is self-explanatory. The diagram on the right says that the sum \(S_n\) is stored in a memory location, to be added to \(u_n\) to produce \(S_{n+1}\), which is then stored back in the memory location to be added to \(u_{n+1}\), and so on.