Smooth response data
yy = smooth(y)
yy = smooth(y,span)
yy = smooth(y,
yy = smooth(y,span,
yy = smooth(y,'sgolay',degree)
yy = smooth(y,span,'sgolay',degree)
yy = smooth(x,y,...)
gpuarrayYY = smooth(gpuarrayY)
yy = smooth(y) smooths the
data in the column vector
y using a moving average
filter. Results are returned in the column vector
The default span for the moving average is
The first few elements of
yy are given by
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 endpoints are handled, the result differs
from the result returned by the
yy = smooth(y,span) sets
the span of the moving average to
yy = smooth(y, smooths
the data in
y using the method
the default span. Supported values for
listed in the table below.
Moving average (default). A lowpass filter with filter coefficients equal to the reciprocal of the span.
Local regression using weighted linear least squares and a 1st degree polynomial model
Local regression using weighted linear least squares and a 2nd degree polynomial model
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.
A robust version of
A robust version of
yy = smooth(y,span, sets
the span of
a percentage of the total number of data points, less than or equal
to 1. For the moving average and Savitzky-Golay methods,
be odd (an even
span is automatically reduced by
yy = smooth(y,'sgolay',degree) uses
the Savitzky-Golay method with polynomial degree specified by
yy = smooth(y,span,'sgolay',degree) uses
the number of data points specified by
the Savitzky-Golay calculation.
span must be odd
degree must be less than
yy = smooth(x,y,...) additionally
x data. If
x is not
provided, methods that require
x data assume
= 1:length(y). You should specify
when it is not uniformly spaced or sorted. If
not uniform and you do not specify
used. If the smoothing method requires
x to be
sorted, the sorting occurs automatically.
gpuarrayYY = smooth(gpuarrayY) performs
the operation on a GPU. The input
a gpuArray column vector. The output
a gpuArray column vector. This syntax requires the Parallel Computing Toolbox™.
You can use gpuArray x and y inputs with the smooth function,
but this is only recommended with the default
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.
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);
Use the same 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')
Plot and compare the results of data smoothed using the
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
rloess methods using 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.
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')
Another way to generate smoothed data is to fit it with a smoothing
spline. Refer to the
for more information.