经验模态分解matlab代码
时间: 2023-07-09 18:30:20 浏览: 74
以下是经验模态分解的Matlab代码示例:
```matlab
function [IMFs,Residual]=emd(x)
% Empirical Mode Decomposition
% Input: x is the 1-D signal, whose EMD needs to be computed
% Output: IMFs are the intrinsic mode functions, Residual is the residue
% EMD is computed as follows:
% 1. Compute the local mean (average of the maxima and minima) of x
% 2. Extract the local mean from x to obtain the first IMF, h1=x-m1
% 3. Compute the local mean of h1
% 4. Extract the second IMF, h2=h1-m2
% 5. Repeat steps 3 and 4 until the nth IMF is obtained
% 6. Compute the residual as the difference between the original signal and the sum of all IMFs
x=x(:)';
N=length(x);
IMFs=[];Residual=x;
% EMD processing
while(1)
% Calculate the number of extrema and their indices
n_ext=zeros(1,N);
n_ext(2:N-1)=(diff(sign(diff(x)))==2)+1;
[maxpos,minpos]=peakdet(x,0);
if length(maxpos)<2 || length(minpos)<2
break
end
if maxpos(1)<minpos(1)
maxpos(1)=[];
end
if length(maxpos)>length(minpos)
maxpos(end)=[];
end
if abs(maxpos(1)-minpos(1))>1
lbound=1;
ubound=min([maxpos(1),minpos(1)])-1;
else
lbound=[];
ubound=[];
end
if abs(maxpos(end)-minpos(end))>1
lbound=[lbound maxpos(end)];
ubound=[ubound N];
end
for k=1:length(maxpos)-1
if abs(maxpos(k)-minpos(k))>1
lbound=[lbound maxpos(k)];
ubound=[ubound minpos(k)];
end
if abs(maxpos(k+1)-minpos(k))>1
lbound=[lbound maxpos(k+1)];
ubound=[ubound minpos(k)];
end
end
if isempty(lbound) || isempty(ubound)
break
end
% Calculate the mean of the extrema envelopes
m=(maxpos+minpos)/2;
h=x-(interp1(m,x,maxpos)+interp1(m,x,minpos))/2;
m_old=m;
while(1)
% Calculate the number of extrema and their indices
n_ext=zeros(1,N);
n_ext(2:N-1)=(diff(sign(diff(h)))==2)+1;
[maxpos,minpos]=peakdet(h,0);
if length(maxpos)<2 || length(minpos)<2
break
end
if maxpos(1)<minpos(1)
maxpos(1)=[];
end
if length(maxpos)>length(minpos)
maxpos(end)=[];
end
if abs(maxpos(1)-minpos(1))>1
lbound=1;
ubound=min([maxpos(1),minpos(1)])-1;
else
lbound=[];
ubound=[];
end
if abs(maxpos(end)-minpos(end))>1
lbound=[lbound maxpos(end)];
ubound=[ubound N];
end
for k=1:length(maxpos)-1
if abs(maxpos(k)-minpos(k))>1
lbound=[lbound maxpos(k)];
ubound=[ubound minpos(k)];
end
if abs(maxpos(k+1)-minpos(k))>1
lbound=[lbound maxpos(k+1)];
ubound=[ubound minpos(k)];
end
end
if isempty(lbound) || isempty(ubound)
break
end
% Calculate the mean of the envelopes
m=(maxpos+minpos)/2;
hm=h-(interp1(m,h,maxpos)+interp1(m,h,minpos))/2;
% Check for convergence
if sum(abs(m-m_old))<0.1
break;
else
m_old=m;
h=hm;
end
end
% Extract the IMF
IMFs=[IMFs;h];
x=x-h;
% Check for the residue
if sum(abs(h))<0.01*sum(abs(Residual))
break;
else
Residual=Residual-h;
end
end
IMFs=[IMFs;Residual];
end
function [maxtab, mintab]=peakdet(v, delta, x)
% PEAKDET Detect peaks in a vector
% [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local maxima and minima ("peaks")
% in the vector V. MAXTAB and MINTAB consists of two columns. Column 1
% contains indices in V, column 2 the found values.
% With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices
% in MAXTAB and MINTAB are replaced with the corresponding X-values.
% Author: Jonas Almeida, modified by Jan Hasenauer, 2008
% Modified by Xiaoning Yang, 2019
% $Id: peakdet.m 143 2008-08-12 15:39:14Z hasenauer $
maxtab = [];
mintab = [];
v = v(:); % Just in case this wasn't a proper vector
% Find the difference between each element and the next
dv = diff(v);
% Calculate the index of all peaks
ind = find([dv(1); dv] < 0 & dv > -delta)+1;
for i = 1:length(ind)
if ind(i) <= 1 || ind(i) >= length(v)
continue
end
if v(ind(i)-1) < v(ind(i)) && v(ind(i)+1) < v(ind(i))
maxtab = [maxtab; ind(i) v(ind(i))];
end
if v(ind(i)-1) > v(ind(i)) && v(ind(i)+1) > v(ind(i))
mintab = [mintab; ind(i) v(ind(i))];
end
end
% Replace indices with X-values if X is given
if nargin == 3 && nargout == 2
maxtab(:,1) = x(maxtab(:,1));
mintab(:,1) = x(mintab(:,1));
end
end
```
该代码实现了经验模态分解的主要步骤,并计算出了每个内禀模态函数和残差。需要注意的是,该代码还包括一个辅助函数 `peakdet`,用于检测信号中的极大值和极小值。
阅读全文