% This version is an integrated annotation version of the ALAN version
function imf = emd(x)
% Empiricial Mode Decomposition (Hilbert-Huang Transform)
% imf = emd(x)
% Func: findpeaks
x = transpose(x(:));% transpose to row matrix
imf = [];
while ~ismonotonic(x) %When x is not a monotonic function, the decomposition termination condition
x1 = x;
sd = Inf;% mean
% until x1 satisfies the IMF condition, get c1
while (sd> 0.1) | ~isimf(x1)% When the deviation coefficient sd is greater than 0.1 or x1 is not an intrinsic modal function, the component termination condition
s1 = getspline(x1);% upper envelope
s2 = -getspline(-x1);% lower envelope
x2 = x1-(s1+ s2)/2;% where x2 is h in the article
sd = sum((x1-x2).^2)/sum(x1.^2);
x1 = x2;
end
imf(end+1) = x1 ;
x = x-x1;
end
imf{end+1} = x;