smoothdata2
Syntax
Description
specifies additional parameters for smoothing using one or more namevalue arguments. For
example, S
= smoothdata2(___,Name=Value
)smoothdata2(A,SmoothingFactor=0.5)
adjusts the level of
smoothing by specifying a factor that scales the window size that
smoothdata2
determines from the entries in A.
Examples
Smooth Data Using Moving Average
Create a matrix containing noise, and visualize the matrix along the z
dimension using a surface plot.
A = peaks;
rng(0,"twister")
A = A + 0.5*randn(size(A));
surf(A)
Smooth the data using a moving average.
B = smoothdata(A);
Visualize the smoothed data.
surf(B)
Compare Smoothing Methods
In this example, explore and compare 2D smoothing methods. Each method has its own advantages and is suitable for different types of data and noise characteristics.
Create a matrix of data that is discretized and has sharp edges, and visualize the matrix using a surface plot.
[bins,edges] = discretize(peaks,10); A = edges(bins); surf(A)
Smooth the data using moving statistic smoothing methods.
B = smoothdata2(A);
B2 = smoothdata2(A,"movmedian");
Compare the results of the smoothing methods by visualizing the smoothed data. The moving mean method is preferable to the moving median method for discretized 2D data with sharp edges. While the moving median method is robust against outliers, it may not adequately address the sharp edges in the data due to its focus on the middle value alone.
tiledlayout(2,1) nexttile surf(B) title("Mean") nexttile surf(B2) title("Median")
Create a matrix with sharp variations occurring over an area smaller than the size of the smoothing window.
x = 1:59; y = sin(x/5*pi); A2 = y + y'; figure surf(A2)
Smooth the data using a moving statistic smoothing method and a local regression smoothing method.
B3 = smoothdata2(A2,"movmean",9); B4 = smoothdata2(A2,"gaussian",9);
Compare the results of the smoothing methods by visualizing the smoothed data. The Gaussian smoothing method is better suited than the moving mean method for smoothing data with sharp variations due to its ability to preserve the sharp features while reducing noise. Unlike the moving mean method, which applies a simple average over the window, Gaussian smoothing uses a weighted average that assigns higher weights to nearby points.
tiledlayout(2,1) nexttile surf(B3) title("Mean") nexttile surf(B4) title("GaussianWeighted Average")
Specify Moving Window Size
Create a matrix containing noise, and visualize the matrix along the z
dimension using a heatmap chart.
A = peaks(25);
rng(0,"twister")
A = A + 6*randn(size(A));
heatmap(A)
Smooth the data by averaging over each 2D window of A
. Define the moving window as a 2by3 block of entries neighboring the current element.
B = smoothdata2(A,"movmean",{2 3});
Smooth the data over a larger moving window that is a 4by4 block of entries.
B2 = smoothdata2(A,"movmean",4);
Compare the results of the window sizes by visualizing the smoothed data.
tiledlayout(2,1) nexttile heatmap(B,Title="2by3 Window") nexttile heatmap(B2,Title="4by4 Window")
Specify Window Size Factor
Create a matrix containing noise, and visualize the matrix using a filled 2D contour plot.
A = peaks;
rng(0,"twister")
A = A + 4*randn(size(A));
contourf(A);
colorbar
Smooth the data by averaging over each 2D window of A
. smoothdata2
determines the window size heuristically based on the input data and the default window size factor 0.25. Return the window size.
[B,winsize] = smoothdata2(A); winsize
winsize=1×2 cell array
{[3]} {[3]}
Smooth using a larger window size, resulting in more smoothing, by specifying a larger window size factor.
[B2,winsize2] = smoothdata2(A,SmoothingFactor=0.5); winsize2
winsize2=1×2 cell array
{[5]} {[5]}
Compare the results of the window size factors by visualizing the smoothed data.
tiledlayout(2,1) nexttile contourf(B) title("Window Size Factor 0.25") colorbar nexttile contourf(B2) title("Window Size Factor 0.5") colorbar
Smooth Data with Sample Points
Create a matrix of nonuniformly spaced data. Specify the sample points in vectors xs
and ys
.
t = linspace(0,10,30);
xs = cumsum(sin(pi/11*t));
ys = xs;
A = sin(xs'/5)*cos(ys/5);
rng(0,"twister")
A = A+randn(size(A))*0.5;
Visualize the matrix using a mesh surface plot.
m = mesh(xs',ys,A,FaceAlpha=0.5);
m.FaceColor = "flat";
view(0,90)
Smooth the data using linear regression over each 2D window of A
. Specify the window size, and specify the x
axis and y
axis locations of the data.
B = smoothdata2(A,"loess",{[2 3],[4 3]},SamplePoints={xs,ys});
Visualize the smoothed data.
m2 = mesh(xs',ys,B,FaceAlpha=0.5);
m2.FaceColor = "flat";
view(0,90)
Input Arguments
A
— Input data
numeric array
Input data, specified as a numeric array.
Complex Number Support: Yes
method
— Smoothing method
"movmean"
(default)  "movmedian"
 "gaussian"
 "lowess"
 "loess"
 "sgolay"
Smoothing method, specified as one of these values:
"movmean"
— Average over each 2D window ofA
. This method is useful for reducing periodic trends in data."movmedian"
— Median over each 2D window ofA
. This method is useful for reducing periodic trends in data when outliers are present."gaussian"
— Gaussianweighted average over each 2D window ofA
."lowess"
— Linear regression over each 2D window ofA
. This method can be computationally expensive but results in fewer discontinuities."loess"
— Quadratic regression over each 2D window ofA
. This method is slightly more computationally expensive than"lowess"
."sgolay"
— SavitzkyGolay filter, which smooths according to a quadratic polynomial that is fitted over each 2D window ofA
. This method can be more effective than other methods when the data varies rapidly.
window
— Window size
positive integer or duration
scalar  twoelement cell array of positive integer or duration
values  twoelement cell array of twoelement vectors of nonnegative integer or
duration
values
Window size, specified as a positive integer or duration
scalar,
twoelement cell array of positive integer or duration
values, or
twoelement cell array of twoelement vectors of nonnegative integer or
duration
values. smoothdata2
defines the
window relative to the sample points.
When
window
is a positive integer scalar, then the window is awindow
bywindow
block centered about the current element.If
window
is a twoelement cell array of positive integers{m n}
, then the window is anm
byn
block centered about the current element.If
window
is a twoelement cell array of twoelement vectors of nonnegative integers{[bRow fRow] [bCol fCol]}
, then the window contains the row and column of the current element, the precedingbRow
and succeedingfRow
rows, and the precedingbCol
and succeedingfCol
columns.
When SamplePoints
contains datetime
or
duration
values, window
must be of type
duration
.
For more information about the window position, see Moving Window Size.
Example: smoothdata2(A,"movmean",4)
Example: smoothdata2(A,"movmedian",{2 3})
Example: smoothdata2(A,"movmedian",{[0 2] [3 3]})
nanflag
— Missing value condition
"omitmissing"
(default)  "omitnan"
 "includemissing"
 "includenan"
Missing value condition, specified as one of these values:
"omitmissing"
or"omitnan"
— IgnoreNaN
values inA
when smoothing. If all elements in the window areNaN
, then the corresponding elements inS
areNaN
."omitmissing"
and"omitnan"
have the same behavior."includemissing"
or"includenan"
— IncludeNaN
values inA
when smoothing. If any element in the window isNaN
, then the corresponding elements inS
areNaN
."includemissing"
and"includenan"
have the same behavior.
NameValue Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Namevalue arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: S = smoothdata2(A,SmoothingFactor=0.5)
SamplePoints
— Sample points
twoelement cell array of vectors
Sample points, specified as a twoelement cell array of vectors of sample point
values. The sample points in the first vector represent the data locations along the
columns of A
, and the sample points in the second vector represent
the data locations along the rows of A
. Both vectors must be sorted
and must not contain duplicate elements. Sample points do not need to be uniformly
spaced. If A
is an
m
byn
matrix, then the
default value of SamplePoints
is {1:n
1:m}
.
Moving windows are defined relative to the sample points. When the sample point
vectors have data type datetime
or duration
, the
moving window description must have type duration
.
SmoothingFactor
— Window size factor
0.25 (default)  scalar ranging from 0 to 1
Window size factor, specified as a scalar ranging from 0 to 1. Generally, the
value of SmoothingFactor
adjusts the level of smoothing by scaling
the heuristic window size. Values near 0 produce smaller moving window sizes,
resulting in less smoothing. Values near 1 produce larger moving window sizes,
resulting in more smoothing. In some cases, depending on the input data from which the
heuristic window size is determined, the value of SmoothingFactor
may not have a significant impact on the window size used by
smoothdata2
.
You cannot specify SmoothingFactor
if you specify the
window
input argument.
Degree
— SavitzkyGolay degree
2 (default)  nonnegative integer
SavitzkyGolay degree, specified as a nonnegative integer. You can only specify
this namevalue argument when "sgolay"
is the specified smoothing
method. The value of Degree
corresponds to the degree of the
polynomial in the SavitzkyGolay filter that fits the data within each window.
Output Arguments
S
— Smoothed data
numeric array
Smoothed data, returned as a numeric array. S
is the same size as
A
.
winsize
— Window description
positive integer or duration
scalar  twoelement cell array of positive integer or duration
values  twoelement cell array of twoelement vectors of nonnegative integer or
duration
values
Window description, returned as a positive integer or duration
scalar, a twoelement cell array of positive integer or duration
values, or a twoelement cell array of twoelement vectors of nonnegative integer or
duration
values.
If you specify window
as an input argument, then
winsize
is the same as window
. If you do not
specify window
as an input argument, then winsize
is the scalar that smoothdata2
determines heuristically based on the
input data.
More About
Moving Window Size
This table illustrates the window position along each dimension for the
default uniformly spaced sample points vector [1 2 3 4 5 6
7]
.
Description  Window Size and Location  Sample Points in Window  Diagram 

For a scalar window size, the leading edge of the window is included and the trailing edge of the window is excluded. 
Current sample point = 4  3, 4, 5 

Current sample point = 4  2, 3, 4, 5 
 
For a vector window size, the leading edge and the trailing edge are included. 
Current sample point = 4  2, 3, 4, 5, 6 

For sample points near the endpoints of the input data, these moving statistic smoothing methods truncate the window so it begins at the first sample point or ends at the last sample point.

Current sample point = 2  1, 2, 3, 4 

For sample points near the endpoints of the input data, these local regression smoothing methods shift the window to include the first or last sample point.

Current sample point = 2  1, 2, 3, 4, 5 

Algorithms
When the window size for the smoothing method is not specified,
smoothdata2
computes a default window size based on a heuristic. For a
smoothing factor τ, the heuristic estimates a moving average window size that attenuates
approximately 100*τ percent of the energy of the input data.
Version History
Introduced in R2023b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)