中值滤波是一种可以有效的消除脉冲噪声的滤波算法,其滤波方法是每次取N(一般为奇数)个采样点,然后按照数据的大小进行排序,最后取中间值作为本次滤波的结果。这里分别取N=5和N=11对实测加速度信号进行处理,滤波效果图3-4所示。
从图可以看出,当取11个点时,滤波效果更好。此外在滤波过程中,根据算法原理,需要在每一个采样点进行了一次N点的排序运算(冒泡法排序),因此当信号的数据量较大以及N取值较大时,计算量稍大。
中值滤波算法的相关Matlab程序如下:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
qhdata_43_8_201301GT
t=0:0.049*1.024:(0.049*1.024*1953);
t(end)=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear acceleration_Filter cc cs displace maxPress min middleIndex
clear PeakDetector press_buff startIndex v press
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a0=accele_Raw(99,:);
a0=a0(80:688);
a1=accele_Raw(18,:);
a1=a1(74:684);
a2=accele_Raw(52,:);
a2=a2(24:end);
a=[a0 a1 a2];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%中值滤波
N = 5;
ind = round(N / 2) - 1;
len = length( a );
a1 = zeros(1, len);
for iter = ( ind + 1) : len - ind
input = a(1, iter-ind : iter+ind);
%冒泡法排序。
for i = 1 : N - 1
for j = 1 : N - 1
if(input(1, j) > input(1, j + 1))
temp = input(1, j);
input( 1, j) = input(1, j + 1);
input( 1, j+1) = temp;
end
end
end
a1(1, iter) = input(1, ind + 1 );
end
t1=t;
a1((len - ind):end)=[];
t1((len - ind):end)=[];
a1(1:ind)=[];
t1(1:ind)=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% clear i ind input iter j len N temp
% clearvars -except t2 a2 a t
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%中值滤波
N = 11;
ind = round(N / 2) - 1;
len = length( a );
a2 = zeros(1, len);
for iter = ( ind + 1) : len - ind
input = a(1, iter-ind : iter+ind);
%冒泡法排序。
for i = 1 : N - 1
for j = 1 : N - 1
if(input(1, j) > input(1, j + 1))
temp = input(1, j);
input( 1, j) = input(1, j + 1);
input( 1, j+1) = temp;
end
end
end
a2(1, iter) = input(1, ind + 1 );
end
t2=t;
a2((len - ind):end)=[];
t2((len - ind):end)=[];
a2(1:ind)=[];
t2(1:ind)=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% clear i ind input iter j len N temp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(2,1,1)
plot(t1,a1)
xlabel('时间/s','fontsize',10.5)
ylabel('加速度/m·s^{-2}','fontsize',10.5)
legend('处理后的加速度信号','Location','SouthWest');
l1=legend('N=5时滤波后的加速度信号');
set(l1,'fontsize',10.5)
axis([0 100 0.5 3.5])
subplot(2,1,2)
plot(t2,a2)
xlabel('时间/s','fontsize',10.5)
ylabel('加速度/m·s^{-2}','fontsize',10.5)
legend('处理后的加速度信号','Location','SouthWest');
l1=legend('N=11时滤波后的加速度信号');
set(l1,'fontsize',10.5)
axis([0 100 0.5 3.5])
%%%%%%%%%%%%%%%%%