主要内容

hampel

R2026b

使用汉佩尔标识符去除离群值

描述

y = hampel(x) 对输入向量 x 应用汉佩尔滤波器,以检测并去除离群值。对于 x 的每个采样,该函数计算由该采样及其六个周围采样(每侧三个)组成的窗的中位数。它还使用中位数绝对偏差估计每个采样相对于其窗中位数的标准差。如果某个采样与中位数的差超出三倍标准差,则将其替换为中位数。如果 x 是矩阵,则该函数将 x 的每列视为独立通道。

示例

y = hampel(x,k) 指定测量窗中 x 的每个采样任一侧的相邻采样数 k。k 默认为 3。

y = hampel(x,k,nsigma) 指定 x 的采样必须与局部中位数相差的标准差数 nsigma,达到此值才会替换为中位数。nsigma 默认为 3。

示例

[y,j] = hampel(___) 还返回逻辑矩阵,其中在识别为离群值的所有点的位置上为 true。此语法接受上述语法中的任何输入参量。

示例

[y,j,xmedian,xsigma] = hampel(___) 还返回 x 的每个元素的局部中位数和估计标准差。

示例

不带输出参量的 hampel(___) 绘制滤波后的信号并注解去除的离群值。

示例

全部折叠

生成正弦信号的 100 个采样。将第六个和第二十个采样替换为尖峰。

x = sin(2*pi*(0:99)/100);
x(6) = 2;
x(20) = -2;

使用 hampel 定位与局部中位数相差超出三倍标准差的每个采样。测量窗由该采样及其六个周围采样(每侧三个)组成。

[y,i,xmedian,xsigma] = hampel(x);

绘制滤波后的信号并注解离群值。

n = 1:length(x);
plot(n,x)
hold on
plot(n,xmedian-3*xsigma,n,xmedian+3*xsigma)
plot(find(i),x(i),'sk')
hold off
legend('Original signal','Lower limit','Upper limit','Outliers')

Figure contains an axes object. The axes object contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Original signal, Lower limit, Upper limit, Outliers.

重复计算,但现在,在计算中位数时每侧只取一个相邻采样。该函数将极值视为离群值。

hampel(x,1)

Figure contains an axes object. The axes object contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent original signal, filtered signal, outliers.

生成一个由不同频率的正弦波组成的双通道信号。将尖峰放在随机的位置。使用 NaN 随机添加缺失采样。重置随机数生成器以获得可重现的结果。绘制信号。

rng('default')

n = 59;
x = sin(pi./[15 10]'*(1:n)+pi/3)';

spk = randi(2*n,9,1);
x(spk) = x(spk)*2;
x(randi(2*n,6,1)) = NaN;

plot(x)

Figure contains an axes object. The axes object contains 2 objects of type line.

使用 hampel 和默认设置对信号进行滤波。

y = hampel(x);
plot(y)

Figure contains an axes object. The axes object contains 2 objects of type line.

增大移动窗的长度并降低将采样视为离群值的阈值。

y = hampel(x,4,2);
plot(y)

Figure contains an axes object. The axes object contains 2 objects of type line.

输出每个通道的滚动中位数。将中位数在信号图上叠加。

[y,j,xmd,xsd] = hampel(x,4,2);
plot(x)
hold on
plot(xmd,'--')

Figure contains an axes object. The axes object contains 4 objects of type line.

生成一个多通道信号,该信号由嵌入单位方差的高斯白噪声中的两个不同频率正弦波组成。

t = 0:60;
x = sin(pi./[10;2]*t)' + randn(numel(t),2);

对信号应用汉佩尔滤波器。将与周围包含九个采样的窗中位数相差超出两倍标准差的点视为离群值。输出逻辑矩阵,其中在离群值的位置处为 true。

k = 4;
nsig = 2;

[y,h] = hampel(x,k,nsig);

在各自的坐标区中绘制信号的每个通道。绘制原始信号、滤波后的信号和离群值。注解离群值位置。

tiledlayout("flow")
for k = 1:2
    hk = h(:,k);
    ax = nexttile;
    plot(t,x(:,k))
    hold on
    plot(t,y(:,k))
    plot(t(hk),x(hk,k),"*")
    hold off
    ax.XTick = t(hk);
end

Figure contains 2 axes objects. Axes object 1 contains 3 objects of type line. One or more of the lines displays its values using only markers Axes object 2 contains 3 objects of type line. One or more of the lines displays its values using only markers

生成正弦信号的 100 个采样。将第六个和第二十个采样替换为尖峰。

n = 1:100;
x = sin(2*pi*n/100);
x(6) = 2;
x(20) = -2;

使用 hampel 计算每个采样的局部中位数和估计的标准差。使用输入参数的默认值:

  • 窗大小为 2×3+1=7。

  • 与其窗中位数相差超出三倍标准差的点被视为离群值。

绘制结果。

[y,i,xmedian,xsigma] = hampel(x);

plot(n,x)
hold on
plot(n,[1;1]*xmedian+3*[-1;1]*xsigma)
plot(find(i),x(i),'sk')
hold off
legend('Signal','Lower','Upper','Outliers')

Figure contains an axes object. The axes object contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Signal, Lower, Upper, Outliers.

使用窗大小 2×10+1=21 和两倍标准差作为识别离群值的判别条件重复计算。

sds = 2;
adj = 10;
[y,i,xmedian,xsigma] = hampel(x,adj,sds);

plot(n,x)
hold on
plot(n,[1;1]*xmedian+sds*[-1;1]*xsigma)
plot(find(i),x(i),'sk')
hold off
legend('Signal','Lower','Upper','Outliers')

Figure contains an axes object. The axes object contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Signal, Lower, Upper, Outliers.

输入参数

全部折叠

输入信号,指定为向量或矩阵。如果 x 是矩阵,则 hampel 将 x 的每列视为独立通道。

示例: cos(pi/4*(0:159))+randn(1,160) 是单通道行向量信号。

示例: cos(pi./[4;2]*(0:159))'+randn(160,2) 是双通道信号。

数据类型: single | double

采样 xs 任一侧的相邻采样数,指定为整数标量。靠近信号边缘且一侧采样数少于 k 的采样,会与较小窗的中位数进行比较。

数据类型: single | double

x 的采样必须与其局部中位数相差的标准差数,达到此值才会被视为离群值。将 nsigma 指定为实数标量。该函数将局部中位数绝对偏差 (MAD) 乘以 κ=12erf−1(1/2)≈1.4826 的因子来估计标准差。

数据类型: single | double

输出参量

全部折叠

滤波后的信号,以与 x 大小相同的向量或矩阵形式返回。

数据类型: single | double

离群值索引,以与 x 大小相同的向量或矩阵形式返回。

数据类型: logical

局部中位数,以与 x 大小相同的向量或矩阵形式返回。

数据类型: single | double

估计的标准差,以与 x 大小相同的向量或矩阵形式返回。

数据类型: single | double

详细信息

全部折叠

参考

[1] Liu, Hancong, Sirish Shah, and Wei Jiang. "On-line outlier detection and data cleaning." Computers and Chemical Engineering. Vol. 28, March 2004, pp. 1635–1647.

[2] Suomela, Jukka. "Median Filtering Is Equivalent to Sorting." https://arxiv.org/pdf/1406.1717, 2014.

扩展功能

全部展开

版本历史记录

在 R2015b 中推出

全部展开

另请参阅

| | | | | (Statistics and Machine Learning Toolbox) | | |