www.gusucode.com > signal 案例源码程序 matlab代码 > signal/PlotStatisticsReturnedByHampelIdentifierExample.m

    %% Signal Statistics Returned by Hampel Filter
% Generate 100 samples of a sinusoidal signal. Replace the sixth and twentieth 
% samples with spikes.

% Copyright 2015 The MathWorks, Inc.


%%

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

%% 
% Use |hampel| to compute the local median and estimated standard deviation 
% for every sample. Use the default values of the input parameters:
% 
% * The window size is $2\times3+1=7$.
% * The points that differ from their window median by more than three standard 
% deviations are considered outliers.
% 
% Plot the result.

[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')

%% 
% Repeat the calculation using a window size of $2\times10+1=21$ and two
% standard deviations as the criteria for identifying outliers.

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')