Curve Fitting Toolbox    
smooth

Smooth the response data

Syntax

Arguments

ydata
A column vector of response data.
span
The number of data points to include for each smooth calculation.
'method'
The smoothing method.
'sgolay'
Use Savitzky-Golay smoothing.
degree
The polynomial degree for the Savitzky-Golay method.
xdata
A column vector of predictor data.
yy
A vector of smoothed response data.

Description

yy = smooth(ydata) smooths the response data specified by ydata using the moving average method. The default number of data points in the average (the span) is five. yy is the smoothed response data. Note that you need not specify the predictor data if it is sorted and uniform.

yy = smooth(ydata,span) uses the number of data points specified by span in the moving average calculation. span must be odd.

yy = smooth(ydata,'method') smooths the response data using the method specified by method and the default span. The supported smoothing methods are given below. For the Savitzky-Golay method, the default polynomial degree is 2.

Method
Description
moving
Moving average filter.
lowess
Locally weighted scatter plot smooth using least squares linear polynomial fitting.
loess
Locally weighted scatter plot smooth using least squares quadratic polynomial fitting.
sgolay
Savitzky-Golay filter. Note that the algorithm used by the toolbox can accept nonuniform predictor data.
rlowess
Lowess smoothing that is resistant to outliers.
rloess
Loess smoothing that is resistant to outliers.

yy = smooth(ydata,span,'method') smooths data using the specified span and method. For the loess and lowess methods, you can specify span as a percentage of the total number of data points. In this case, span must be less than or equal to 1. For the moving average and Savitzky-Golay methods, span must be odd. If an even span is specified, it is reduced by 1.

yy = smooth(ydata,'sgolay',degree) uses the Savitzky-Golay method with polynomial degree specified by degree.

yy = smooth(ydata,span,'sgolay',degree) uses the number of data points specified by span in the Savitzky-Golay calculation. span must be odd and degree must be less than span.

yy = smooth(xdata,ydata,...) smooths the data specified by ydata and the associated predictor data, xdata. You should specify the predictor data when it is not uniformly spaced or it is not sorted. If xdata is not uniform and you do not specify method, lowess is used. If the smoothing method requires xdata to be sorted, the sorting occurs automatically.

Remarks

For the moving average and Savitzky-Golay methods, span must be odd. If an even span is specified, it is reduced by 1. If span is greater than the length of ydata, it is reduced to the length of ydata.

Use robust smoothing when you want to assign lower weight to outliers. The robust smoothing algorithm uses the 6MAD method, which assigns zero weight to data outside six mean absolute deviations.

Another way to generate a vector of smoothed response values is to fit your data using a smoothing spline. Refer to the fit function for more information.

Example

Suppose you want to smooth traffic count data with a moving average filter to see the average traffic flow over a 5-hour window (span is 5).

Plot the original data and the smoothed data.

The first four elements of yy are given by

Because of the way that the end points are treated, the result shown above differs from the result returned by the filter function described in Difference Equations and Filtering in the MATLAB documentation.

In this example, generate random data between 0 and 15, create a sine wave with noise, and add two outliers with the value 3.

Smooth the data using the loess and rloess methods with the span specified as 10% of the data.

Plot original data and the smoothed data.

Note how the outliers have less effect with the robust method.

See Also

fit, sort


 set