Matlab中,可以使用movstd函数获取数组的移动标准差,可以理解为局部 k
个数据点的标准差值组成的数组。本文将从以下几方面介绍movstd函数:movstd函数常见用法、movstd函数语法说明、movstd函数实例。其中,movstd函数实例包括:向量的中心移动标准差、向量的尾部移动标准差、指定移动标准差的归一化、矩阵的移动标准差、包含 NaN
元素的向量的移动标准差、基于样本点计算移动标准差、仅返回满窗口标准差。
movstd函数帮助文档如下:
>> help movstd
movstd Moving standard deviation value.
Y = movstd(X,K) for a vector X and positive integer scalar K computes a
centered moving standard deviation by sliding a window of length K
along X. Each element of Y is the local standard deviation of the
corresponding values of X inside the window, with Y the same size as X.
When K is even, the window is centered about the current and previous
elements of X. The sliding window is truncated at the endpoints where
there are fewer than K elements from X to fill the window.
For N-D arrays, movstd operates along the first array dimension whose
size does not equal 1.
By default, movstd normalizes by K-1 if K>1. If X consists of
independent, identically distributed samples, then movstd is the square
root of an unbiased estimator of the variance of the population of each
window.
Y = movstd(X,[NB NF]) for a vector X and nonnegative integers NB and NF
computes a moving standard deviation along the length of X, returning
the local standard deviation of the previous NB elements, the current
element, and the next NF elements of X.
movstd(X,K,NRM) specifies the normalization factor for the variance and
can be one of the following:
0 - (default) normalizes by K-1 for K>1 and by K when K=1.
1 - normalizes by K and produces the square root of the second
moment of the window about its mean.
Y = movstd(X,K,NRM,DIM) or Y = movstd(X,[NB NF],NRM,DIM) operates along
dimension DIM of X. When specifying DIM, you must specify NRM.
movstd(...,MISSING) specifies how NaN (Not-a-Number) values are treated
and can be one of the following:
'includenan' - (default) the standard deviation of any window
containing NaN values is also NaN.
'omitnan' - the standard deviation of any window containing
NaN values is the standard deviation of all its
non-NaN elements. If all elements are NaN, the
result is NaN.
movstd(...,'Endpoints',ENDPT) controls how the standard deviation is
calculated at the endpoints of X, where there are not enough elements
to fill the window. ENDPT can be either a scalar numeric or logical
value or one of the following:
'shrink' - (default) compute the standard deviation over the
number of elements of X that are inside the window,
effectively reducing the window size to fit X at the
endpoints.
'fill' - compute the standard deviation over the full window
size, filling missing values from X with NaN. This is
equivalent to padding X with NaN at the endpoints.
'discard' - compute the standard deviation only when the window
is filled with elements of X, discarding partial
endpoint calculations and their corresponding
elements in Y. This truncates the output; for a
vector X and window length K, Y has length
LENGTH(X)-K+1.
When ENDPT is a scalar numeric or logical value, the missing elements
of X inside the window are replaced with that value and Y remains the
same size as X.
Example: Compute a 5-point centered moving standard deviation.
t = 1:10;
x = [4 8 6 -1 -2 -3 -1 3 4 5];
yc = movstd(x,5);
plot(t,x,t,yc);
Example: Compute a 5-point trailing moving standard deviation.
t = 1:10;
x = [4 8 6 -1 -2 -3 -1 3 4 5];
yt = movstd(x,[4 0]);
plot(t,x,t,yt);
Example: Compute a 5-point centered moving standard deviation, padding
the ends of the input with NaN.
t = 1:10;
x = [4 8 6 -1 -2 -3 -1 3 4 5];
yp = movstd(x,5,'Endpoints','fill');
plot(t,x,t,yp);
Example: Compute a 5-point trailing moving standard deviation, ignoring
the first 4 window shifts that do not contain 5 input elements.
x = [4 8 6 -1 -2 -3 -1 3 4 5];
yd = movstd(x,[4 0],'Endpoints','discard');
movstd函数常见用法
M = movstd(A,k)
M = movstd(A,[kb kf])
M = movstd(___,w)
M = movstd(___,w,dim)
M = movstd(___,nanflag)
M = movstd(___,Name,Value)
movstd函数语法说明
M = movstd(A,k) 返回由局部 k 个数据点的标准差值组成的数组。每个标准差基于 A 的相邻元素的长度为 k 的滑动窗计算得出。当 k 为奇数时,窗口以当前位置的元素为中心。当 k 为偶数时,窗口以当前元素及其前一个元素为中心。当没有足够的元素填满窗口时,窗口将自动在端点处截断。当窗口被截断时,只根据窗口内的元素计算标准差。M 与 A 的大小相同。
- 如果 A 是向量,movstd 将沿向量 A 的长度运算。
- 如果 A 是多维数组,则 movstd 沿 A 的大小不等于 1 的第一个维度进行运算。
M = movstd(A,[kb kf]) 计算长度为 kb+kf+1 的窗口的标准差。该计算包括当前位置的元素、后面的 kb 个元素和前面的 kf 个元素。
M = movstd(_,w) 为上述任意语法指定归一化因子。当 w = 0 时(默认值),M 按 k-1 对窗长度 k 进行归一化。当 w = 1 时,M 按 k 进行归一化。
M = movstd(_,w,dim) 为上述任一语法指定 A 的运算维度。指定 dim 时,始终在上述语法中指定权重 w。例如,movstd(A,k,0,2) 沿矩阵 A 的列进行运算,计算每一行的 k 个元素的移动标准差。归一化因子是默认值 k-1。
M = movstd(_,nanflag) 指定在上述任意语法的计算中包括还是忽略 NaN 值。movstd(A,k,’includenan’) 会在计算中包括所有 NaN 值,而 movstd(A,k,’omitnan’) 则忽略这些值并基于较少的点计算标准差。
M = movstd(_,Name,Value) 使用一个或多个名称-值对组参数指定移动标准差的其他参数。例如,如果 x 是时间向量,则 movstd(A,k,’SamplePoints’,x) 相对于 x 中的时间计算移动标准差。
movstd函数实例
向量的中心移动标准差
计算行向量的三点中心移动标准差。当端点处的窗口中少于三个元素时,将根据可用元素计算标准差。
>> A = [4 8 6 -1 -2 -3 -1 3 4 5];
>> M = movstd(A,3)
M =
2.8284 2.0000 4.7258 4.3589 1.0000 1.0000 3.0551 2.6458 1.0000 0.7071
向量的尾部移动标准差
计算行向量的三点尾部移动标准差。当端点处的窗口中少于三个元素时,将根据可用元素计算标准差。
>> A = [4 8 6 -1 -2 -3 -1 3 4 5];
>> M = movstd(A,[2 0])
M =
0 2.8284 2.0000 4.7258 4.3589 1.0000 1.0000 3.0551 2.6458 1.0000
指定移动标准差的归一化
计算行向量的三点中心移动标准差,并按照窗口中的元素数对每个标准差进行归一化。
>> A = [4 8 6 -1 -2 -3 -1 3 4 5];
>> = movstd(A,3,1)
M =
2.0000 1.6330 3.8586 3.5590 0.8165 0.8165 2.4944 2.1602 0.8165 0.5000
矩阵的移动标准差
计算矩阵中每行的三点中心移动标准差。窗口从第一行开始,沿水平方向移动到该行的末尾,然后移到第二行,依此类推。维度参数为 2,即跨 A
的列移动窗口。指定维度时,始终指定归一化因子。
>> A = [4 8 6; -1 -2 -3; -1 3 4];
>> M = movstd(A,3,0,2)
M =
2.8284 2.0000 1.4142
0.7071 1.0000 0.7071
2.8284 2.6458 0.7071
包含 NaN 元素的向量的移动标准差
计算包含两个 NaN
元素的行向量的三点中心移动标准差。
>> A = [4 8 NaN -1 -2 -3 NaN 3 4 5];
>> M = movstd(A,3)
M =
2.8284 NaN NaN NaN 1.0000 NaN NaN NaN 1.0000 0.7071
重新计算标准差,但忽略 NaN
值。当 movstd
舍弃 NaN
元素时,它将根据窗口中的剩余元素计算标准差。
>> M = movstd(A,3,'omitnan')
M =
2.8284 2.8284 6.3640 0.7071 1.0000 0.7071 4.2426 0.7071 1.0000 0.7071
基于样本点计算移动标准差
>> A = [4 8 6 -1 -2 -3];
>> k = hours(3);
>> t = datetime(2016,1,1,0,0,0) + hours(0:5)
t =
1 至 5 列
2016-01-01 00:00:00 2016-01-01 01:00:00 2016-01-01 02:00:00 2016-01-01 03:00:00 2016-01-01 04:00:00
6 列
2016-01-01 05:00:00
>> M = movstd(A,k,'SamplePoints',t)
M = 1×6
2.8284 2.0000 4.7258 4.3589 1.0000 0.7071
根据时间向量 t
,计算 A
中数据的 3 小时中心移动标准差。
仅返回满窗口标准差
计算行向量的三点中心移动标准差,但在输出中舍弃使用的点数少于三个的计算。也就是说,只返回从满的三元素窗口计算的标准差,而舍弃端点计算。
>> A = [4 8 6 -1 -2 -3 -1 3 4 5];
>> M = movstd(A,3,'Endpoints','discard')
M =
2.0000 4.7258 4.3589 1.0000 1.0000 3.0551 2.6458 1.0000
原创文章,作者:古哥,转载需经过作者授权同意,并附上原文链接:https://iymark.com/articles/4035.html