function imf = emd(x) % Empiricial Mode Decomposition (Hilbert-Huang Transform) x = transpose(x(:)); imf = []; while ~ismonotonic(x) x1 = x; sd = Inf; kk = 1; while ((sd<0.2)&(sd > 0.3)) | ~isimf(x1) kk = kk + 1; s1 = getspline(x1); s2 = -getspline(-x1); x2 = x1-(s1+s2)/2; sd = sum((x1-x2).^2)/sum(x1.^2); x1 = x2; end imf{end+1} = x1; x = x-x1; end imf{end+1} = x; % FUNCTIONS % extra equationss function u = ismonotonic(x) u1 = length(findpeaks(x))*length(findpeaks(-x)); if u1 > 0 u = 0; else u = 1; end function u = isimf(x) N = length(x); u1 = sum(x(1:N-1).*x(2:N) < 0); u2 = length(findpeaks(x))+length(findpeaks(-x)); if abs(u1-u2) > 1 u = 0; else u = 1; end function s = getspline(x) N = length(x); p = findpeaks(x); s = spline([0 p N+1],[0 x(p) 0],1:N); function HHT(datafile,monthName); data = load(datafile); data = cell2mat(struct2cell(data)); % load datafile for one month % CHANGE NUMBER AND NAME RESPECTIVELY k = 1; noData = []; for i = 1 : 96 : size(data,1)-95 if isempty(find(isnan(data(i:i+95)))) imf{k} = emd(data(i:i+95)); else noData = [noData k]; imf{k} = []; end k = k + 1; end isData = 1:size(data,1)/96; isData(isData(noData)) = []; k = 1; for i = 1 : size(imf,2) if ~isempty(imf{i}) ii{k} = imf{i}; k = k+1; end end imf = ii; % if isempty(find(isnan(d(end-95:end)))) % imf{k} = emd(d(end-95,end)); % else % noData = [noData size(d,1)/96]; % end clear k i ; imfHilb = cell(size(imf)); for i = 1 : size(imf,2) for j = 1 : size(imf{i},2) imfHilb{i}{j} = hilbert(imf{i}{j}); end end clear i j; % amplitude spectrum k = 1; for i = 1 : size(imfHilb,2) figure; x = 0:95; day = zeros(1,96); for j = 1 : size(imfHilb{i},2) subplot(size(imfHilb{i},2),1,j); plot(x,abs(imfHilb{i}{j})); ylabel('amplitude'); xlabel('time'); % title(['day ',int2str(i),' IMF ',int2str(j)]); title([monthName,' day ',int2str(isData(k)),' IMF ',int2str(j)]); day = day + abs(imfHilb{i}{j}); end figure; plot(x,day);ylabel('amplitude'); xlabel('time'); title([monthName,' day ',int2str(isData(k))]); k = k+1; end clear i j k; % CHANGE NUMBER AND NAME RESPECTIVELY SaveAllFigures([monthName]); % don't forget to delete figures after processing % for k = 1 : size(data,2) % for i = 1 : 96 : size(data{k},2) % imf{k} = emd(data{k}); % for m = 1 : size(imf{k),2} % hilberImf{k}{m} = hilbert(imf{k}{m}); % end % end % end clear isData noData ii k isData function n = findpeaks(x) % Find peaks. % n = findpeaks(x) n = find(diff(diff(x) > 0) < 0); u = find(x(n+1) > x(n)); n(u) = n(u)+1; function SaveAllFigures(opt,filetype) % filetype by default .fig % opt is string array ; the name of the figures if nargin == 0 opt='Unknown'; end if nargin < 2 filetype = 'fig'; end ChildList = sort(get(0,'Children')); for cnum = 1:length(ChildList) if strncmp(get(ChildList(cnum),'Type'),'figure',6) saveas(ChildList(cnum), [opt, '_', num2str(ChildList(cnum)), '.' filetype]); end end