MATLAB Fundamentals>Common Data Analysis Techniques>Smoothing Data> (2/5) Smoothing Data with Moving Average
例1:
Smoothing method:Moving mean
Moving window:Centered 2
代码2:
% Smooth input data
ySm = smoothdata(y,"movmean",2,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")
例2:
Smoothing method:Moving mean
Smoothing factor:0.25
% Smooth input data
ySm = smoothdata(y,"movmean","SmoothingFactor",0.25,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")
例3:
Smoothing method:Moving median
Moving window:Centered 2
代码3:
% Smooth input data
ySm = smoothdata(y,"movmedian",2,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")
例4:
Smoothing method:Gaussian filter
Moving window:Centered 2
代码4:
% Smooth input data
ySm = smoothdata(y,"gaussian",2,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")
例5:
Smoothing method:Local linear regression(lowess)
Moving window:Centered 2
代码5:
% Smooth input data
ySm = smoothdata(y,"lowess",2,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")
例6:
Smoothing method:Local quadratic regression(loess)
Moving window:Centered 2
代码6:
% Smooth input data
ySm = smoothdata(y,"loess",2,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")
例7:
Smoothing method:Robust Lowess
Moving window:Centered 2
代码7:
% Smooth input data
ySm = smoothdata(y,"rlowess",2,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")
例8:
Smoothing method:Roubst Loess
Moving window:Centered 2
代码8:
% Smooth input data
ySm = smoothdata(y,"rloess",2,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")
例9:
Smoothing method:Savitzky-Golay polynomial filter
polynomial degree:2
Moving window:Centered 2
代码9:
% Smooth input data
ySm = smoothdata(y,"sgolay",2,"SamplePoints",x);
% Display results
figure
plot(x,y,"SeriesIndex",6,"DisplayName","Input data")
hold on
plot(x,ySm,"SeriesIndex",1,"LineWidth",1.5, ...
"DisplayName","Smoothed data")
hold off
legend
xlabel("x")