Main Content

smooth

Smooth response data

Description

yy = smooth(y) smooths the response data in column vector y using a moving average filter.

The first few elements of yy follow.

yy(1) = y(1)
yy(2) = (y(1) + y(2) + y(3))/3
yy(3) = (y(1) + y(2) + y(3) + y(4) + y(5))/5
yy(4) = (y(2) + y(3) + y(4) + y(5) + y(6))/5
...
Because of the way smooth handles endpoints, the result differs from the result returned by the filter function.

example

yy = smooth(y,span) sets the span of the moving average to span.

yy = smooth(y,method) smooths the data in y using the method specified by method and the default span.

yy = smooth(y,span,method) sets the span of method to span.

example

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

yy = smooth(y,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(x,y,___) specifies values for the independent variable x. You can use this syntax with any of the arguments in the previous syntaxes.

example

Examples

collapse all

Smooth data by linear index and by each column separately, using a moving average filter. Plot and compare the results.

Load the data in count.dat. The 24-by-3 array count contains traffic counts at three intersections for each hour of the day.

load count.dat

Suppose that the data are from a single intersection over three consecutive days. Smoothing all the data together would then indicate the overall cycle of traffic flow through the intersection. Use a moving average filter with a 5-hour span to smooth all the data simultaneously (by linear index).

c = smooth(count(:));
C1 = reshape(c,24,3);

However, the data are in fact from three different intersections. Thus, smoothing columnwise gives a more meaningful picture of the traffic through each intersection in a day. Use the same moving average filter to smooth each column of the data separately.

C2 = zeros(24,3);
for I = 1:3
    C2(:,I) = smooth(count(:,I));
end

Plot the original data and the data smoothed by linear index and by each column separately. Then, plot the difference between the two smoothed data sets. The two methods give different results near the endpoints.

subplot(3,1,1)
plot(count,':');
hold on
plot(C1,'-');
title('Smooth C1 (All Data)')

subplot(3,1,2)
plot(count,':');
hold on
plot(C2,'-');
title('Smooth C2 (Each Column)')

subplot(3,1,3)
plot(C2 - C1,'o-')
title('Difference C2 - C1')

Figure contains 3 axes objects. Axes object 1 with title Smooth C1 (All Data) contains 6 objects of type line. Axes object 2 with title Smooth C2 (Each Column) contains 6 objects of type line. Axes object 3 with title Difference C2 - C1 contains 3 objects of type line.

Plot and compare the results of data smoothed using the loess and rloess methods. Then determine which method is less sensitive to outliers.

Create noisy data with two outliers.

x = (0:0.1:15)';
y = sin(x) + 0.5*(rand(size(x))-0.5);
y([90,110]) = 3;

Smooth the data with the loess and rloess methods. Use a span of 10% of the total number of data points.

yy1 = smooth(x,y,0.1,'loess');
yy2 = smooth(x,y,0.1,'rloess');

Plot the original and smoothed data. The outliers have less influence with the robust method rloess.

subplot(2,1,1)
plot(x,y,'b.',x,yy1,'r-')
set(gca,'YLim',[-1.5 3.5])
legend('Original data','Smoothed data using ''loess''',...
       'Location','NW')
   
subplot(2,1,2)
plot(x,y,'b.',x,yy2,'r-')
set(gca,'YLim',[-1.5 3.5])
legend('Original data','Smoothed data using ''rloess''',...
       'Location','NW')

Figure contains 2 axes objects. Axes object 1 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Original data, Smoothed data using 'loess'. Axes object 2 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Original data, Smoothed data using 'rloess'.

Input Arguments

collapse all

Data to smooth, specified as a column vector.

If your data contains NaNs and you do not specify x, your data is treated as nonuniform and the smoothing method 'lowess' is used.

Data Types: single | double

Number of data points for calculating the smoothed value, specified as an integer or as a scalar value in the range (0,1) denoting a fraction of the total number of data points.

If you use the moving average or Savitzky-Golay methods, the number of data points for calculating the smoothed value must be an odd integer. If you specify span as an even number or as a fraction that results in an even number of data points, span is automatically reduced by 1.

Example: 7; 0.6

Smoothing method to smooth the response data, specified as one of the following methods.

Method

Description

'moving' (default)

Moving average. A lowpass filter with filter coefficients equal to the reciprocal of the span.

'lowess'

Local regression using weighted linear least squares and a 1st degree polynomial model.

'loess'

Local regression using weighted linear least squares and a 2nd degree polynomial model.

'sgolay'

Savitzky-Golay filter. A generalized moving average with filter coefficients determined by an unweighted linear least-squares regression and a polynomial model of specified degree (default is 2). The method can accept nonuniform predictor data.

'rlowess'

A robust version of 'lowess' that assigns lower weight to outliers in the regression. The method assigns zero weight to data outside six mean absolute deviations.

'rloess'

A robust version of 'loess' that assigns lower weight to outliers in the regression. The method assigns zero weight to data outside six mean absolute deviations.

Data Types: char | string

Polynomial degree for the model used in the Savitzky-Golay method, specified as a scalar value. degree must be less than span.

Example: 3

Independent variable for the response data y, specified as a column vector. If you do not provide x, methods that require x assume x = 1:length(y). Specify x data when y is not sorted or uniformly spaced. If x is not uniform and you do not specify method, lowess is used. If you specify a smoothing method that requires x to be sorted, the function automatically sorts the x data.

Data Types: single | double

Output Arguments

collapse all

Smoothed data, returned as a column vector.

Tips

  • You can generate a smooth fit to your data using a smoothing spline. For more information, see fit.

Alternative Functionality

You can also smooth data by using the MATLAB® smoothdata function. With the exception of GPU array support, smoothdata includes all the functionality of the smooth function and has some advantages. Unlike smooth, the smoothdata function supports:

  • Matrices, tables, and timetables

  • Moving median and Gaussian methods

  • Option to specify how the NaN values are treated

  • Option to substitute smoothed data for the original matrix or append smoothed data to the original matrix

  • Tall arrays, C/C++ code generation, and thread-based environments

Extended Capabilities

Version History

Introduced before R2006a