Smooth response data
The first few elements of
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 ...
smoothhandles endpoints, the result differs from the result returned by the
y inputs with the
function is only recommended if you use the default method,
Using GPU data with other methods does not offer any performance advantage.
Smooth Data Using Moving Average Filter
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.
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')
Smooth Data Using
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. 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
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')
y — Data to smooth
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.
span — Number of data points for calculating the smoothed value
5 (default) | integer | scalar value in the range (0,1)
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.
method — Smoothing method
'moving' (default) |
Smoothing method to smooth the response data, specified as one of the following methods.
Moving average. 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
degree — Polynomial degree for Savitzky-Golay method
Polynomial degree for the model used in the Savitzky-Golay method, specified as a
degree must be less than
x — Independent variable for response data
Independent variable for the response data
y, specified as a
column vector. If you do not provide
x, methods that require
x = 1:length(y). Specify
x data when
y is not sorted or uniformly spaced.
x is not uniform and you do not specify
lowess is used. If you specify a smoothing method that requires
x to be sorted, the function automatically sorts the
gpuarrayY — Data to smooth
Data to smooth, specified as a
gpuArray column vector.
gpuarrayX — Input data for response data
Input data for the response data
gpuarrayY, specified as a
gpuArray column vector. If you do not provide
gpuarrayX, methods that require
gpuarrayX = 1:length(y). Specify
when the data are not uniformly spaced or sorted. If the
data is not uniform and you do not specify the smoothing method,
'lowess' is used. If you specify a smoothing method that requires
gpuarrayX to be sorted, the function automatically sorts the
yy — Smoothed data
Smoothed data, returned as a column vector.
gpuarrayYY — Smoothed data
Smoothed data, returned as a
gpuArray column vector.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
yinputs with the smooth function is only recommended if you use the default method,
'moving'. Using GPU data with other methods does not offer any performance advantage.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Introduced before R2006a