function imf = emd(x_sig)
x=x_sig;
c=x';
N = length(x); %原始数据的个数
% SDstop = 0.3; %计算IMF停止的标准
SDstop = 0.25;
imf = []; % IMF矩阵初始化
% 获得IMF的循环
while (1)
h = c; % 获得初始数据
SD = 1; % 初始化停止的标准
while (SD > SDstop)
maxVec = [];
minVec = [];
% 寻找上下包络线上的点
for i = 2: N - 1
if h (i - 1) < h (i) & h (i) > h (i + 1)
maxVec = [maxVec i]; % 获得极大值点
end
if h (i - 1) > h (i) & h (i) < h (i + 1)
minVec = [minVec i]; %获得极小值点
end
end
% 验证是否只剩下残留
if (length (maxVec) + length (minVec)) < 2
break;
end
%处理边界点
lenmax=length(maxVec);
lenmin=length(minVec);
%左边界点
if h(1)>0
if(maxVec(1)<minVec(1))
yleft_max=h(maxVec(1));
yleft_min=-h(1);
else
yleft_max=h(1);
yleft_min=h(minVec(1));
end
else
if (maxVec(1)<minVec(1))
yleft_max=h(maxVec(1));
yleft_min=h(1);
else
yleft_max=-h(1);
yleft_min=h(minVec(1));
end
end
%右边界点
if h(N)>0
if(maxVec(lenmax)<minVec(lenmin))
yright_max=h(N);
yright_min=h(minVec(lenmin));
else
yright_max=h(maxVec(lenmax));
yright_min=-h(N);
end
else
if(maxVec(lenmax)<minVec(lenmin))
yright_max=-h(N);
yright_min=h(minVec(lenmin));
else
yright_max=h(maxVec(lenmax));
yright_min=h(N);
end
end
%获得上下包络线函数
%三次样条插值
maxEnv=spline([1 maxVec N],[yleft_max h(maxVec) yright_max],1:N);
minEnv=spline([1 minVec N],[yleft_min h(minVec) yright_min],1:N);
m = (maxEnv + minEnv) /2;
preh = h; % 保存原始数据防止被改变
h = h - m;
% 计算标准
ee = 0.0000001;
% SD = sum ((preh - h) ^ 2) / (preh ^ 2 + ee);
SD = ((preh - h) * (preh - h)' ) / (preh * preh' + ee);
end % 停止的条件
imf = [imf; h]; % 保存IMF
% 判断是否只剩下残留
if (length (maxVec) + length (minVec)) < 2
break;
end
c = c - h; % 分离出IMF;
end % 结束循环
line=size(imf,1);
subplot(line+1,1,1),plot(x);
for i=1:line
subplot(line+1,1,i+1),plot(imf(i,:));
end
return;
评论0