The moving average is the most common filter in DSP, mainly because it is the easiest digital filter to understand and use. In spite of its simplicity, the moving average filter is optimal for a common task: reducing random noise while retaining a sharp
277CHAPTER15EQUATION 15-1Equation of the moving average filter. Inthis equation, is the input signal, isx[ ] y[ ]the output signal, and M is the number ofpoints used in the moving average. Thisequation only uses points on one side of theoutput sample being calculated.y[i] '1MjM &1j '0x [i %j ]y [80] 'x [80] % x [81] % x [82] % x [83] % x [84]5Moving Average FiltersThe moving average is the most common filter in DSP, mainly because it is the easiest digitalfilter to understand and use. In spite of its simplicity, the moving average filter is optimal fora common task: reducing random noise while retaining a sharp step response. This makes it thepremier filter for time domain encoded signals. However, the moving average is the worst filterfor frequency domain encoded signals, with little ability to separate one band of frequencies fromanother. Relatives of the moving average filter include the Gaussian, Blackman, and multiple-pass moving average. These have slightly better performance in the frequency domain, at theexpense of increased computation time. Implementation by ConvolutionAs the name implies, the moving average filter operates by averaging a numberof points from the input signal to produce each point in the output signal. Inequation form, this is written:Where is the input signal, is the output signal, and M is the numberx[ ] y[ ]of points in the average. For example, in a 5 point moving average filter, point80 in the output signal is given by: The Scientist and Engineer's Guide to Digital Signal Processing278y[80] 'x[78] % x[79] % x[80] % x[81] % x[82]5100 'MOVING AVERAGE FILTER110 'This program filters 5000 samples with a 101 point moving120 'average filter, resulting in 4900 samples of filtered data.130 '140 DIM X[4999] 'X[ ] holds the input signal150 DIM Y[4999] 'Y[ ] holds the output signal160 '170 GOSUB XXXX 'Mythical subroutine to load X[ ]180 '190 FOR I% = 50 TO 4949 'Loop for each point in the output signal200 Y[I%] = 0 'Zero, so it can be used as an accumulator 210 FOR J% = -50 TO 50 'Calculate the summation220 Y[I%] = Y[I%] + X(I%+J%]230 NEXT J%240 Y[I%] = Y[I%]/101 'Complete the average by dividing250 NEXT I%260 '270 END TABLE 15-1As an alternative, the group of points from the input signal can be chosensymmetrically around the output point:This corresponds to changing the summation in Eq. 15-1 from: ,j ' 0 to M&1to: . For instance, in a 10 point moving averagej ' &(M&1)/2 to (M&1)/2filter, the index, j, can run from 0 to 11 (one side averaging) or -5 to 5(symmetrical averaging). Symmetrical averaging requires that M be an oddnumber. Programming is slightly easier with the points on only one side;however, this produces a relative shift between the input and output signals.You should recognize that the moving average filter is a convolution using avery simple filter kernel. For example, a 5 point filter has the filter kernel: . That is, the moving average filter is aþ 0, 0, 1/5, 1/5, 1/5, 1/5, 1/5, 0, 0 þconvolution of the input signal with a rectangular pulse having an area of one.Table 15-1 shows a program to implement the moving average filter.Noise Reduction vs. Step ResponseMany scientists and engineers feel guilty about using the moving average filter.Because it is so very simple, the moving average filter is often the first thingtried when faced with a problem. Even if the problem is completely solved,there is still the feeling that something more should be done. This situation istruly ironic. Not only is the moving average filter very good for manyapplications, it is optimal for a common problem, reducing random white noisewhile keeping the sharpest step response. Chapter 15- Moving Average Filters 279Sample number0 100 200 300 400 500-1012a. Original signalSample number0 100 200 300 400 500-1012b. 11 point moving averageFIGURE 15-1Example of a moving average filter. In (a), arectangular pulse is buried in random noise. In(b) and (c), this signal is filtered with 11 and 51point moving average filters, respectively. Asthe number of points in the filter increases, thenoise becomes lower; however, the edgesbecoming less sharp. The moving average filteris the optimal solution for this problem,providing the lowest noise possible for a givenedge sharpness. Sample number0 100 200 300 400 500-1012c. 51 point moving averageAmplitude AmplitudeAmplitudeFigure 15-1 shows an example of how this works. The signal in (a) is a pulseburied in random noise. In (b) and (c), the smoothing action of the movingaverage filter decreases the amplitude of the random noise (good), but alsoreduces the sharpness of the edges (bad). Of all the possible linear filters thatcould be used, the moving average produces the lowest noise for a given edgesharpness. The amount of noise reduction is equal to the square-root of thenumber of points in the average. For example, a 100 point moving averagefilter reduces the noise by a factor of 10.To understand why the moving average if the best solution, imagine we wantto design a filter with a fixed edge sharpness. For example, let's assume we fixthe edge sharpness by specifying that there are eleven points in the rise of thestep response. This requires that the filter kernel have eleven points. Theoptimization question is: how do we choose the eleven values in the filterkernel to minimize the noise on the output signal? Since the noise we aretrying to reduce is random, none of the input points is special; each is just asnoisy as its neighbor. Therefore, it is useless to give preferential treatment toany one of the input points by assigning it a larger coefficient in the filterkernel. The lowest noise is obtained when all the input samples are treatedequally, i.e., the moving average filter. (Later in this chapter we show thatother filters are essentially as good. The point is, no filter is better than thesimple moving average). The Scientist and Engineer's Guide to Digital Signal Processing280EQUATION 15-2Frequency response of an M point movingaverage filter. The frequency, f, runs between0 and 0.5. For , use: f ' 0 H[ f ] ' 1H [f ] 'sin(Bf M )M sin(Bf )Frequency0 0.1 0.2 0.3 0.4 0.50.00.20.40.60.81.01.23 point11 point31 pointFIGURE 15-2Frequency response of the moving averagefilter. The moving average is a very poorlow-pass filter, due to its slow roll-off andpoor stopband attenuation. These curves aregenerated by Eq. 15-2. AmplitudeFrequency ResponseFigure 15-2 shows the frequency response of the moving average filter. It ismathematically described by the Fourier transform of the rectangular pulse, asdiscussed in Chapter 11: The roll-off is very slow and the stopband attenuation is ghastly. Clearly, themoving average filter cannot separate one band of frequencies from another.Remember, good performance in the time domain results in poor performancein the frequency domain, and vice versa. In short, the moving average is anexceptionally good smoothing filter (the action in the time domain), but anexceptionally bad low-pass filter (the action in the frequency domain).Relatives of the Moving Average FilterIn a perfect world, filter designers would only have to deal with timedomain or frequency domain encoded information, but never a mixture ofthe two in the same signal. Unfortunately, there are some applicationswhere both domains are simultaneously important. For instance, televisionsignals fall into this nasty category. Video information is encoded in thetime domain, that is, the shape of the waveform corresponds to the patternsof brightness in the image. However, during transmission the video signalis treated according to its frequency composition, such as its totalbandwidth, how the carrier waves for sound & color are added, elimination& restoration of the DC component, etc. As another example, electro-magnetic interference is best understood in the frequency domain, even if Chapter 15- Moving Average Filters 281Sample number0 6 12 18 240.00.10.22 pass4 pass1 passa. Filter kernelSample number0 6 12 18 240.00.20.40.60.81.01.21 pass4 pass2 passb. Step responseFrequency0 0.1 0.2 0.3 0.4 0.5-120-100-80-60-40-20020401 pass2 pass4 passd. Frequency response (dB)FIGURE 15-3 Characteristics of multiple-pass moving average filters. Figure (a) shows the filter kernels resulting frompassing a seven point moving average filter over the data once, twice and four times. Figure (b) shows thecorresponding step responses, while (c) and (d) show the corresponding frequency responses. FFTIntegrate 20 Log( )AmplitudeAmplitudeFrequency0 0.1 0.2 0.3 0.4 0.50.000.250.500.751.001.251 pass2 pass4 passc. Frequency responseAmplitude (dB)Amplitudethe signal's information is encoded in the time domain. For instance, thetemperature monitor in a scientific experiment might be contaminated with 60hertz from the power lines, 30 kHz from a switching power supply, or 1320kHz from a local AM radio station. Relatives of the moving average filterhave better frequency domain performance, and can be useful in these mixeddomain applications. Multiple-pass moving average filters involve passing the input signalthrough a moving average filter two or more times. Figure 15-3a shows theoverall filter kernel resulting from one, two and four passes. Two passes areequivalent to using a triangular filter kernel (a rectangular filter kernelconvolved with itself). After four or more passes, the equivalent filter kernellooks like a Gaussian (recall the Central Limit Theorem). As shown in (b),multiple passes produce an "s" shaped step response, as compared to thestraight line of the single pass. The frequency responses in (c) and (d) aregiven by Eq. 15-2 multiplied by itself for each pass. That is, each time domainconvolution results in a multiplication of the frequency spectra. The Scientist and Engineer's Guide to Digital Signal Processing282Figure 15-4 shows the frequency response of two other relatives of the movingaverage filter. When a pure Gaussian is used as a filter kernel, the frequencyresponse is also a Gaussian, as discussed in Chapter 11. The Gaussian isimportant because it is the impulse response of many natural and manmadesystems. For example, a brief pulse of light entering a long fiber optictransmission line will exit as a Gaussian pulse, due to the different paths takenby the photons within the fiber. The Gaussian filter kernel is also usedextensively in image processing because it has unique properties that allowfast two-dimensional convolutions (see Chapter 24). The second frequencyresponse in Fig. 15-4 corresponds to using a Blackman window as a filterkernel. (The term window has no meaning here; it is simply part of theaccepted name of this curve). The exact shape of the Blackman window isgiven in Chapter 16 (Eq. 16-2, Fig. 16-2); however, it looks much like aGaussian.How are these relatives of the moving average filter better than the movingaverage filter itself? Three ways: First, and most important, these filters havebetter stopband attenuation than the moving average filter. Second, the filterkernels taper to a smaller amplitude near the ends. Recall that each point inthe output signal is a weighted sum of a group of samples from the input. If thefilter kernel tapers, samples in the input signal that are farther away are givenless weight than those close by. Third, the step responses are smooth curves,rather than the abrupt straight line of the moving average. These last two areusually of limited benefit, although you might find applications where they aregenuine advantages. The moving average filter and its relatives are all about the same at reducingrandom noise while maintaining a sharp step response. The ambiguity lies inhow the risetime of the step response is measured. If the risetime is measuredfrom 0% to 100% of the step, the moving average filter is the best you can do,as previously shown. In comparison, measuring the risetime from 10% to 90%makes the Blackman window better than the moving average filter. The pointis, this is just theoretical squabbling; consider these filters equal in thisparameter.The biggest difference in these filters is execution speed. Using a recursivealgorithm (described next), the moving average filter will run like lightning inyour computer. In fact, it is the fastest digital filter available. Multiple passesof the moving average will be correspondingly slower, but still very quick. Incomparison, the Gaussian and Blackman filters are excruciatingly slow,because they must use convolution. Think a factor of ten times the number ofpoints in the filter kernel (based on multiplication being about 10 times slowerthan addition). For example, expect a 100 point Gaussian to be 1000 timesslower than a moving average using recursion.Recursive ImplementationA tremendous advantage of the moving average filter is that it can beimplemented with an algorithm that is very fast. To understand this Chapter 15- Moving Average Filters 283FIGURE 15-4Frequency response of the Blackman windowand Gaussian filter kernels. Both these filtersprovide better stopband attenuation than themoving average filter. This has no advantage inremoving random noise from time domainencoded signals, but it can be useful in mixeddomain problems. The disadvantage of thesefilters is that they must use convolution, aterribly slow algorithm. Frequency0 0.1 0.2 0.3 0.4 0.5-140-120-100-80-60-40-20020GaussianBlackmanAmplitude (dB) y[50] ' x[47] % x[48] % x[49] % x[50] % x[51] % x[52] % x[53]y[51] ' x[48] % x[49] % x[50] % x[51] % x[52] % x[53] % x[54]y[51] ' y[50] % x[54] & x[47]EQUATION 15-3Recursive implementation of the movingaverage filter. In this equation, is thex[ ]input signal, is the output signal, M is they[ ]number of points in the moving average (anodd number). Before this equation can beused, the first point in the signal must becalculated using a standard summation.y[i] ' y[i &1] % x[i %p] & x[i &q]q ' p % 1where:p ' (M&1)/2algorithm, imagine passing an input signal, , through a seven point movingx[ ]average filter to form an output signal, . Now look at how two adjacenty[ ]output points, and , are calculated:y[50] y[51]These are nearly the same calculation; points through must bex[48] x[53]added for , and again for . If has already been calculated, they[50] y[51] y[50]most efficient way to calculate is:y[51]Once has been found using , then can be calculated fromy[51] y[50] y[52]sample , and so on. After the first point is calculated in , all of they[51] y[ ]other points can be found with only a single addition and subtraction per point.This can be expressed in the equation:Notice that this equation use two sources of data to calculate each point in theoutput: points from the input and previously calculated points from the output.This is called a recursive equation, meaning that the result of one calculation The Scientist and Engineer's Guide to Digital Signal Processing284100 'MOVING AVERAGE FILTER IMPLEMENTED BY RECURSION110 'This program filters 5000 samples with a 101 point moving120 'average filter, resulting in 4900 samples of filtered data.130 'A double precision accumulator is used to prevent round-off drift.140 '150 DIM X[4999] 'X[ ] holds the input signal160 DIM Y[4999] 'Y[ ] holds the output signal170 DEFDBL ACC 'Define the variable ACC to be double precision180 '190 GOSUB XXXX 'Mythical subroutine to load X[ ]200 '210 ACC = 0 'Find Y[50] by averaging points X[0] to X[100]220 FOR I% = 0 TO 100 230 ACC = ACC + X[I%]240 NEXT I%250 Y[50] = ACC/101260 ' 'Recursive moving average filter (Eq. 15-3)270 FOR I% = 51 TO 4949280 ACC = ACC + X[I%+50] - X[I%-51]290 Y[I%] = ACC/101300 NEXT I% 310 '320 END TABLE 15-2is used in future calculations. (The term "recursive" also has other meanings,especially in computer science). Chapter 19 discusses a variety of recursivefilters in more detail. Be aware that the moving average recursive filter is verydifferent from typical recursive filters. In particular, most recursive filters havean infinitely long impulse response (IIR), composed of sinusoids andexponentials. The impulse response of the moving average is a rectangularpulse (finite impulse response, or FIR). This algorithm is faster than other digital filters for several reasons. First,there are only two computations per point, regardless of the length of the filterkernel. Second, addition and subtraction are the only math operations needed,while most digital filters require time-consuming multiplication. Third, theindexing scheme is very simple. Each index in Eq. 15-3 is found by adding orsubtracting integer constants that can be calculated before the filtering starts(i.e., p and q). Fourth, the entire algorithm can be carried out with integerrepresentation. Depending on the hardware used, integers can be more than anorder of magnitude faster than floating point. Surprisingly, integer representation works better than floating point with thisalgorithm, in addition to being faster. The round-off error from floating pointarithmetic can produce unexpected results if you are not careful. For example,imagine a 10,000 sample signal being filtered with this method. The lastsample in the filtered signal contains the accumulated error of 10,000 additionsand 10,000 subtractions. This appears in the output signal as a drifting offset.Integers don't have this problem because there is no round-off error in thearithmetic. If you must use floating point with this algorithm, the program inTable 15-2 shows how to use a double precision accumulator to eliminate thisdrift. . %j ]y [80] 'x [80] % x [81] % x [82] % x [83] % x [84] 5Moving Average FiltersThe moving average is the most common filter in DSP, mainly because it. % x[81] % x[82]5100 &apos ;MOVING AVERAGE FILTER110 'This program filters 5000 samples with a 101 point moving1 20 &apos ;average filter, resulting in