Curve Fitting Toolbox | ![]() ![]() |
Moving Average Filtering
A moving average filter smooths data by replacing each data point with the average of the neighboring data points defined within the span. This process is equivalent to lowpass filtering with the response of the smoothing given by the difference equation
where ys(i) is the smoothed value for the i
th data point, N is the number of neighboring data points on either side of ys(i), and 2N+1 is the span.
The moving average smoothing method used by the Curve Fitting Toolbox follows these rules:
Note that you can use MATLAB's filter
function to implement difference equations such as the one shown above. However, because of the way that the end points are treated, the toolbox moving average result will differ from the result returned by filter
. Refer to Difference Equations and Filtering in the MATLAB documentation for more information.
For example, suppose you smooth data using a moving average filter with a span of 5. Using the rules described above, the first four elements of y
s are given by
ys(1) = y(1) ys(2) = (y(1)+y(2)+y(3))/3 ys(3) = (y(1)+y(2)+y(3)+y(4)+y(5))/5 ys(4) = (y(2)+y(3)+y(4)+y(5)+y(6))/5
Note that y
s(1)
, y
s(2)
, ... ,y
s(end)
refer to the order of the data after sorting, and not necessarily the original order.
The smoothed values and spans for the first four data points of a generated data set are shown below.
Plot (a)
indicates that the first data point is not smoothed because a span cannot be constructed. Plot (b)
indicates that the second data point is smoothed using a span of three. Plots (c)
and (d)
indicate that a span of five is used to calculate the smoothed value.
![]() | Smoothing Data | Lowess and Loess: Local Regression Smoothing | ![]() |