資料預處理——噪聲值平滑處理
資料預處理中對於資料清洗的時候對於離群值和噪聲值的處理是關鍵的一環。
靈魂歌手水雲天:資料預處理小結
上文對於資料預處理做了一個總結,本文關於噪聲值的處理提供下面4個函式來學習。
1、smooth(曲線擬合工具箱)
1、smoothts(金融工具箱)(smoothts will be removed in a future release。 Use smoothdata instead。)
由於官方將會將smoothts函式用smoothdata函式替換,因此這裡詳細介紹smoothdata函式的使用。
3、medfilt1(訊號處理工具箱)
一、smooth
yy
=
smooth
(
y
)
yy
=
smooth
(
y
,
span
)
yy
=
smooth
(
y
,
method
)
yy
=
smooth
(
y
,
span
,
method
)
注意:
1、此處span為窗寬,簡單理解為用於計算平滑值的資料點數,預設為5
2、method為平滑方法,具體方法如下:
smooth函式具體呼叫格式請參考官網:
Smooth response data - MATLAB smooth - MathWorks 中國
下面使用正弦波和隨機生成的噪聲波來顯示smooth的平滑處理結果
%% 產生加噪正弦波訊號,繪製加噪波形圖
t
=
linspace
(
0
,
2
*
pi
,
500
)
‘
;
% 產生一個從0到2*pi的向量,長度為500
y
=
100
*
sin
(
t
);
% 產生正弦波訊號
%% 產生500行1列的服從N(0,152)分佈的隨機數,作為噪聲訊號
noise
=
normrnd
(
0
,
15
,
500
,
1
);
y
=
y
+
noise
;
% 將正弦波訊號加入噪聲訊號
figure
;
plot
(
t
,
y
);
xlabel
(
’t‘
);
ylabel
(
’y = sin(t) + 噪聲‘
);
首先使用移動平滑法:
%% 移動平滑法
yy1 = smooth(y,30); % 利用移動平均法對y進行平滑處理
figure;
plot(t,y,’k:‘);
hold on;
plot(t,yy1,’b‘,’linewidth‘,3);
xlabel(’t‘);
ylabel(’moving‘);
legend(’加噪波形‘,’平滑後波形‘);
%% 移動平滑法
yy1
=
smooth
(
y
,
30
);
% 利用移動平均法對y進行平滑處理
figure
;
plot
(
t
,
y
,
’k:‘
);
hold
on
;
plot
(
t
,
yy1
,
’b‘
,
’linewidth‘
,
3
);
xlabel
(
’t‘
);
ylabel
(
’moving‘
);
legend
(
’加噪波形‘
,
’平滑後波形‘
);
%% 利用lowess方法對加噪訊號進行平滑處理
yy2
=
smooth
(
y
,
30
,
’lowess‘
);
% 利用lowess方法對y進行平滑處理
subplot
(
221
);
plot
(
t
,
y
,
’k:‘
);
hold
on
;
plot
(
t
,
yy2
,
’b‘
,
’linewidth‘
,
3
);
xlabel
(
’t‘
);
ylabel
(
’lowess‘
);
legend
(
’加噪波形‘
,
’平滑後波形‘
);
%% 利用rlowess方法對加噪訊號進行平滑處理
yy3
=
smooth
(
y
,
30
,
’rlowess‘
);
% 利用rlowess方法對y進行平滑處理
subplot
(
222
);
plot
(
t
,
y
,
’k:‘
);
hold
on
;
plot
(
t
,
yy3
,
’b‘
,
’linewidth‘
,
3
);
xlabel
(
’t‘
);
ylabel
(
’rlowess‘
);
legend
(
’加噪波形‘
,
’平滑後波形‘
);
%% 利用loess方法對加噪訊號進行平滑處理
yy4
=
smooth
(
y
,
30
,
’loess‘
);
% 利用loess方法對y進行平滑處理
subplot
(
223
);
plot
(
t
,
y
,
’k:‘
);
hold
on
;
plot
(
t
,
yy4
,
’b‘
,
’linewidth‘
,
3
);
% 繪製平滑後波形圖
xlabel
(
’t‘
);
% 為X軸加標籤
ylabel
(
’loess‘
);
% 為Y軸加標籤
legend
(
’加噪波形‘
,
’平滑後波形‘
);
%% 利用sgolay方法對加噪訊號進行平滑處理
yy5
=
smooth
(
y
,
30
,
’sgolay‘
,
3
);
% 利用sgolay方法對y進行平滑處理
subplot
(
224
);
plot
(
t
,
y
,
’k:‘
);
hold
on
;
plot
(
t
,
yy5
,
’b‘
,
’linewidth‘
,
3
);
xlabel
(
’t‘
);
ylabel
(
’sgolay‘
);
legend
(
’加噪波形‘
,
’平滑後波形‘
);
二、smoothdata
呼叫格式:
B
=
smoothdata
(
A
)
%使用以啟發方式確定的固定視窗長度返回向量元素的移動平均值。
%視窗向下滑動向量的長度,計算每個視窗中的元素的平均值。
%如果 A 為矩陣,smoothdata 計算每列的移動平均值。
%如果 A 是多維陣列,則 smoothdata 沿大小不等於 1 的第一個維度進行運算。
%如果 A 是包含數值變數的表或時間表,則 smoothdata 針對每個變數單獨執行運算。
B
=
smoothdata
(
A
,
dim
)
%沿 A 的維度 dim 執行運算。例如,如果 A 是一個矩陣,
%則 smoothdata(A,2) 對 A 中的每行資料進行平滑處理。
B
=
smoothdata
(
___
,
method
)
%為上述任一語法指定平滑處理方法。例如,B = smoothdata(A,’sgolay‘)
%使用 Savitzky-golay 濾波器對 A 中的資料進行平滑處理。
B
=
smoothdata
(
___
,
method
,
window
)
%指定平滑處理方法使用的視窗長度。例如,smoothdata(A,’movmedian‘,5)
%透過求五元素移動視窗的中位數,來對 A 中的資料進行平滑處理。
注意:
smoothdata函式在舊版本中為smooths函式,在後續新版本中將會移除smoothts函式,使用smoothdata替代。
首先將資料檔案從當前資料夾匯入到MATLAB中,可以使用xlsread等函式匯入xls檔案
可以參考下面這篇文章
靈魂歌手水雲天:MATLAB中檔案的操作
此處以網上一組資料——某日某城市股市日收盤價來學習smoothdata函式,匯入之後的資料為一個向量price,繪製該資料的散點圖如下:
函式smoothdata共支援八種平滑方法,類似上面的smooth函式,分別是:‘movmean’、‘movmedian’、‘gaussian’、‘lowess’、‘loess’、‘rlowess’、‘rloess’ 或 ‘sgolay’。下面使用
'
movmean‘、’movmedian‘、’gaussian‘和’sgolay‘平滑。
%% 使用movmean法對資料平滑
output1
=
smoothdata
(
price
,
’movmean‘
,
30
);
% 用movmean法平滑資料,窗寬為30
output2
=
smoothdata
(
price
,
’movmean‘
,
100
);
% 用movmean法平滑資料,窗寬為100
subplot
(
221
);
plot
(
price
,
’。‘
);
hold
on
plot
(
output1
,
’k‘
,
’LineWidth‘
,
1。5
);
plot
(
output2
,
’k-。‘
,
’LineWidth‘
,
1。5
);
xlabel
(
’觀測序號‘
);
ylabel
(
’movmean法‘
);
legend
(
’原始散點‘
,
’平滑曲線(窗寬30)‘
,
’平滑曲線(窗寬100)‘
,
’location‘
,
’northwest‘
);
%————————————————————————————————
%————————————————————————————————
%% 使用movmedian法對資料平滑
output3
=
smoothdata
(
price
,
’movmedian‘
,
30
);
% 窗寬為30,標準差為預設值0。65
output4
=
smoothdata
(
price
,
’movmedian‘
,
100
);
% 窗寬為100,標準差為100
subplot
(
222
);
plot
(
price
,
’。‘
);
hold
on
plot
(
output3
,
’k‘
,
’LineWidth‘
,
1。5
);
plot
(
output4
,
’k-。‘
,
’LineWidth‘
,
1。5
);
xlabel
(
’觀測序號‘
);
ylabel
(
’movmedian法‘
);
legend
(
’原始散點‘
,
’平滑曲線(窗寬30,標準差0。65)‘
,
。。。
’平滑曲線(窗寬100,標準差100)‘
,
’location‘
,
’northwest‘
);
%————————————————————————————————
%————————————————————————————————
%% 利用指數法對資料進行平滑處理,繪製平滑波形圖*
output5
=
smoothdata
(
price
,
’gaussian‘
,
30
);
% 用gaussian法平滑資料,窗寬為30
output6
=
smoothdata
(
price
,
’gaussian‘
,
100
);
% 用gaussian法平滑資料,窗寬為100
subplot
(
223
);
plot
(
price
,
’。‘
);
hold
on
plot
(
output5
,
’k‘
,
’LineWidth‘
,
1。5
);
plot
(
output6
,
’k-。‘
,
’LineWidth‘
,
1。5
);
xlabel
(
’觀測序號‘
);
ylabel
(
’gaussian法‘
);
legend
(
’原始散點‘
,
’平滑曲線(窗寬30)‘
,
’平滑曲線(窗寬100)‘
,
’location‘
,
’northwest‘
);
%————————————————————————————————
%————————————————————————————————
%% 使用sgolay法對資料進行平滑
output7
=
smoothdata
(
price
,
’sgolay‘
,
30
);
% 用sgolay法平滑資料,窗寬為30
output8
=
smoothdata
(
price
,
’sgolay‘
,
100
);
% 用sgolay法平滑資料,窗寬為100
subplot
(
224
);
plot
(
price
,
’。‘
);
hold
on
plot
(
output5
,
’k‘
,
’LineWidth‘
,
1。5
);
plot
(
output6
,
’k-。‘
,
’LineWidth‘
,
1。5
);
xlabel
(
’觀測序號‘
);
ylabel
(
’sgolay法‘
);
legend
(
’原始散點‘
,
’平滑曲線(窗寬30)‘
,
’平滑曲線(窗寬100)‘
,
’location‘
,
’northwest‘
);
執行程式碼得到影象:
為了節省進行對比將上述四個方法繪製在一張圖中,由於縮小了圖幅,窗寬變化顯示的對比不太明顯,可以分別將四幅圖匯出比較。比較之後可以得出窗框越大,曲線越平滑。增加窗寬的同時可能會過度平滑導致資料失真,因此需要選擇適度的窗寬。
函式smoothts的呼叫格式可以參考官網介紹:
https://
ww2。mathworks。cn/help/f
inance/smoothts。html?s_tid=doc_ta
smoothts函式的方法有’b‘, ’g‘, or ’e‘ ,官網解釋如下:
Smoothing method (essentially the type of filter used)。 Can be Exponential (e), Gaussian (g), or Box (b)。 Default = b。
實際上為指數法,高斯法和盒子法。
三、medfilt1
呼叫格式:
y
=
medfilt1
(
x
)
%將三階一維中值濾波器應用於輸入向量 x。該函式認為訊號超出端點為0。輸出y的長度與相同x。
y
=
medfilt1
(
x
,
n
)
%將n階一維一維中值濾波器應用於 x。
y
=
medfilt1
(
x
,
n
,
blksz
,
dim
)
%或指定過濾器沿其執行的尺寸。是向後相容所必需的,將被忽略。y = medfilt1(x,n,[],dim)dimblksz
y
=
medfilt1
(
___
,
nanflag
,
padding
)
%指定NaN使用先前語法中的任何輸入引數,如何在每個段上對待值。
%此語法還指定padding在訊號邊緣執行的過濾型別。
%nanflag並且padding可以出現x在函式呼叫之後的任何位置。
使用正弦訊號,並隨機生成噪聲波進行平滑
t
=
linspace
(
0
,
2
*
pi
,
500
)
’
;
% 產生一個從0到2*pi的向量,長度為500
y
=
100
*
sin
(
t
);
% 產生正弦波訊號
noise
=
normrnd
(
0
,
15
,
500
,
1
);
y
=
y
+
noise
;
% 將正弦波訊號加入噪聲訊號
plot
(
t
,
y
);
xlabel
(
‘t’
);
ylabel
(
‘y = sin(t) + 噪聲’
);
%% 呼叫medfilt1對加噪正弦波訊號y進行中值濾波,並繪製波形圖
yy
=
medfilt1
(
y
,
30
);
% 指定窗寬為30,對y進行中值濾波
plot
(
t
,
y
,
‘k:’
);
hold
on
plot
(
t
,
yy
,
‘k’
,
‘LineWidth’
,
3
);
xlabel
(
‘t’
);
ylabel
(
‘中值濾波’
);
legend
(
‘加噪波形’
,
‘平滑後波形’
);
下面是MathWorks對資料平滑和離群值檢測的介紹:
本文部分資料、程式碼和方法介紹來源於網路。
本文僅供大家參考學習,歡迎指正!
更多MATLAB學習資源請入QQ群:953314432。
上一篇:高中考三百分用什麼教輔?