├── EMDOS.tar ├── startup.m ├── faux │ ├── stop_emd.m │ ├── io.m │ ├── madiff.m │ ├── matrice_pass.m │ ├── build_lambda.m │ ├── matrice_der.m │ ├── extr.m │ ├── build_m02.m │ ├── init.m │ ├── boundary_conditions.m │ ├── build_m0.m │ └── build_m0.m~ ├── plot_imf.m ├── paperfig.m ├── README.txt ├── figfun │ ├── gen_tests.m │ ├── sensitiv_io.m │ ├── fig_diff.m │ ├── sensitiv_spo.m │ └── separTest.m └── emdos.m ├── HuangEMD ├── findEE.m ├── findEEfsp.m ├── endprocess1.p ├── medianfilter.m ├── ratio1.m ├── ratioa.m ├── emax.m ├── emin.m ├── test.m ├── skiphilbt_m.m ├── FAhilbert.m ├── FAimphilbert.m ├── hilbertnormalize.m ├── dist_value.m ├── hilbert.m ├── significanceIMF.m ├── local_max.m ├── signiplotIMF.m ├── FAcosfor.m ├── findcriticalpoints.m ├── FAacos.m ├── FSPHSP.m ├── ifndq.m ├── linearnormalize.m ├── pchipnormalize.m ├── splinenormalize.m ├── splinenormalizeep.m ├── blocknormalize.m ├── extrema.m ├── ex02d.m ├── eemd.m ├── confidenceLine.m ├── FAzc.m ├── NCU2009V1.txt └── nspplote.m ├── emd_samps_part2.mat ├── 2sample_CA1_DG_LFP_10s.mat ├── miniEMD ├── memd_doNothingPost.m ├── memd_doNothingPre.m ├── memd_stop_emd.m ├── memd_io.m ├── memd_sumdumw.m ├── memd_flandrin_weights.m ├── memd_nonegfreq.m ├── memd_extr.m ├── memd_boundary_conditions.m └── memd_smoothBinSeries.m ├── README.md ├── minimalEMDOS ├── stop_emd.m ├── io.m ├── extr.m ├── init.m ├── boundary_conditions.m └── emdos.m ├── git_test_text_master.m ├── ViewDataEMD(2).m ├── dg_AMFMdecomp.m ├── eemdRF.m ├── ceemdan.m ├── ViewDataEMD.m └── dg_mkwave.m /EMDOS.tar/startup.m: -------------------------------------------------------------------------------- 1 | addpath('../'); 2 | addpath('faux/'); 3 | addpath('figfun/'); -------------------------------------------------------------------------------- /HuangEMD/findEE.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benpolletta/HHT-Tutorial/HEAD/HuangEMD/findEE.m -------------------------------------------------------------------------------- /emd_samps_part2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benpolletta/HHT-Tutorial/HEAD/emd_samps_part2.mat -------------------------------------------------------------------------------- /HuangEMD/findEEfsp.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benpolletta/HHT-Tutorial/HEAD/HuangEMD/findEEfsp.m -------------------------------------------------------------------------------- /HuangEMD/endprocess1.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benpolletta/HHT-Tutorial/HEAD/HuangEMD/endprocess1.p -------------------------------------------------------------------------------- /2sample_CA1_DG_LFP_10s.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benpolletta/HHT-Tutorial/HEAD/2sample_CA1_DG_LFP_10s.mat -------------------------------------------------------------------------------- /miniEMD/memd_doNothingPost.m: -------------------------------------------------------------------------------- 1 | function r = memd_doNothingPost(r, aux) 2 | disp(aux); 3 | fprintf('That also did nothing...\n'); 4 | 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HHT-Tutorial 2 | ============ 3 | 4 | Code used to parametrize the HHT and some of its variations for neurophysiological data. 5 | -------------------------------------------------------------------------------- /miniEMD/memd_doNothingPre.m: -------------------------------------------------------------------------------- 1 | function [r, ack] = memd_doNothingPre(r, aux) 2 | ack = fliplr(aux); 3 | disp(aux); 4 | fprintf('That did nothing!\n'); 5 | 6 | -------------------------------------------------------------------------------- /EMDOS.tar/faux/stop_emd.m: -------------------------------------------------------------------------------- 1 | % tests if there are enough (3) extrema to continue the decomposition 2 | % Written by Gabriel Rilling 3 | function stop = stop_EMD(r) 4 | [indmin,indmax] = extr(r); 5 | ner = length(indmin) + length(indmax); 6 | stop = ner < 3; 7 | end 8 | -------------------------------------------------------------------------------- /minimalEMDOS/stop_emd.m: -------------------------------------------------------------------------------- 1 | % tests if there are enough (3) extrema to continue the decomposition 2 | % Written by Gabriel Rilling 3 | function stop = stop_EMD(r) 4 | [indmin,indmax] = extr(r); 5 | ner = length(indmin) + length(indmax); 6 | stop = ner < 3; 7 | end 8 | -------------------------------------------------------------------------------- /miniEMD/memd_stop_emd.m: -------------------------------------------------------------------------------- 1 | % tests if there are enough (3) extrema to continue the decomposition 2 | % Written by Gabriel Rilling 3 | function stop = memd_stop_emd(r) 4 | [indmin,indmax] = memd_extr(r); 5 | ner = length(indmin) + length(indmax); 6 | stop = ner < 3; 7 | end 8 | -------------------------------------------------------------------------------- /minimalEMDOS/io.m: -------------------------------------------------------------------------------- 1 | function ort = io(x,imf) 2 | % ort = IO(x,imf) computes the index of orthogonality 3 | % 4 | % inputs : - x : analyzed signal 5 | % - imf : empirical mode decomposition 6 | 7 | n = size(imf,1); 8 | 9 | s = 0; 10 | 11 | for i = 1:n 12 | for j =1:n 13 | if i~=j 14 | s = s + abs(sum(imf(i,:).*conj(imf(j,:)))/sum(x.^2)); 15 | end 16 | end 17 | end 18 | 19 | ort = 0.5*s; 20 | end -------------------------------------------------------------------------------- /EMDOS.tar/faux/io.m: -------------------------------------------------------------------------------- 1 | function ort = io(x,imf) 2 | % ort = IO(x,imf) computes the index of orthogonality 3 | % 4 | % inputs : - x : analyzed signal 5 | % - imf : empirical mode decomposition 6 | 7 | n = size(imf,1); 8 | 9 | s = 0; 10 | 11 | for i = 1:n 12 | for j =1:n 13 | if i~=j 14 | s = s + abs(sum(imf(i,:).*conj(imf(j,:)))/sum(x.^2)); 15 | end 16 | end 17 | end 18 | 19 | ort = 0.5*s; 20 | end -------------------------------------------------------------------------------- /miniEMD/memd_io.m: -------------------------------------------------------------------------------- 1 | function ort = memd_io(x,imf) 2 | % ort = IO(x,imf) computes the index of orthogonality 3 | % 4 | % inputs : - x : analyzed signal 5 | % - imf : empirical mode decomposition 6 | 7 | n = size(imf,1); 8 | 9 | s = 0; 10 | 11 | for i = 1:n 12 | for j =1:n 13 | if i~=j 14 | s = s + abs(sum(imf(i,:).*conj(imf(j,:)))/sum(x.^2)); 15 | end 16 | end 17 | end 18 | 19 | ort = 0.5*s; 20 | end -------------------------------------------------------------------------------- /EMDOS.tar/faux/madiff.m: -------------------------------------------------------------------------------- 1 | function deriv = madiff(s,t,k) 2 | %% deriv : gives the k-order derivative. Completion with constants. Uses a 4-th order formula. 3 | 4 | mykern = [-1 8 0 -8 1]; 5 | deriv = s; 6 | 7 | for i=1:k 8 | aux = conv(deriv,mykern); 9 | deriv = aux; 10 | deriv = deriv/(12*(t(2)-t(1))); 11 | end 12 | 13 | deriv=deriv(2*k+1:end-2*k); 14 | deriv(1:2*k) = deriv(2*k+1); 15 | deriv(end-2*k+1:end) = deriv(2*k); 16 | 17 | 18 | end -------------------------------------------------------------------------------- /EMDOS.tar/plot_imf.m: -------------------------------------------------------------------------------- 1 | function plot_imf(imf,t,suffixe) 2 | 3 | if nargin<2 4 | t = linspace(0,1,size(imf,2)); 5 | suffixe = ''; 6 | elseif nargin<3 7 | suffixe = ''; 8 | end 9 | 10 | M = size(imf,1); 11 | 12 | if abs(t-1)<=eps 13 | t = 1:size(imf,2); 14 | end 15 | 16 | subplot(M,1,1); 17 | hold on; 18 | title(['The decomposition by ' suffixe]); 19 | 20 | for i=1:M 21 | if i>1 22 | subplot(M,1,i); 23 | end 24 | plot(t,imf(i,:)); 25 | xlabel('time'); 26 | ylabel(['IMF ' num2str(i)]); 27 | end 28 | 29 | end -------------------------------------------------------------------------------- /EMDOS.tar/faux/matrice_pass.m: -------------------------------------------------------------------------------- 1 | function res = matrice_pass(sub,tau2,spo) 2 | % matrice_pass : computes the matrix res such that, for any spline h(t) on a subdivision x and with 3 | % coefficients the vector H, 4 | % (M H)_j = h(x_i) 5 | % 6 | % Thomas Oberlin 7 | % 12.2011 8 | % thomas.oberlin@imag.fr 9 | 10 | 11 | % Nombre de splines 12 | N = length(sub)-spo; 13 | M = length(tau2); 14 | 15 | res = zeros(M,N); 16 | 17 | % Création matrice 18 | for i=1:N 19 | % Création bspline j 20 | sp1 = spmak(sub(i:i+spo),1); 21 | res(:,i) = fnval(sp1,tau2'); 22 | 23 | end 24 | 25 | 26 | end 27 | 28 | -------------------------------------------------------------------------------- /HuangEMD/medianfilter.m: -------------------------------------------------------------------------------- 1 | %function y=medianfilter(x,n) 2 | % 3 | % The function MEDIANFILTER returns data smoothed by n-point median filter. 4 | % 5 | % Calling sequence- 6 | % y=medianfilter(x,n) 7 | % 8 | % Input- 9 | % x - column vector of input data x(n) 10 | % n - number of smoothing points 11 | % Output- 12 | % y - column vector of filtered data y(n) 13 | % 14 | 15 | function y=medianfilter(x,n) 16 | 17 | %----- Define the smoothing range indexes 18 | lbound = floor(n/2); 19 | rbound = n-lbound-1; 20 | 21 | y=x; 22 | 23 | %----- Run the filter 24 | for i=lbound+1:length(x)-rbound-1 25 | y(i)=median(x(i-lbound:i+rbound)); 26 | end 27 | -------------------------------------------------------------------------------- /HuangEMD/ratio1.m: -------------------------------------------------------------------------------- 1 | function r = ratio1(c) 2 | 3 | % The function RATIO1 calculates the non-orthogonal leakage of components, 4 | % where r is calculated according to the following rule being applied 5 | % to the case when c is the 2-D array. 6 | % r= c(1)*c(2)+...+c(k-1)*c(k)) / ( c(1)+...c(k) )^2. 7 | % 8 | % Calling sequence- 9 | % r=ratio1(c) 10 | % 11 | % Input- 12 | % c - 2-D matrix c(n,k) of IMF components 13 | % excluding the trend component. 14 | % Output- 15 | % r - value of the ratio 16 | % 17 | 18 | %----- Get dimensions and initialize 19 | 20 | [npt,knb] = size(c); 21 | a1=0; 22 | 23 | %----- Calculate the ratio 24 | for i=1:knb-1, 25 | for j=i+1:knb, 26 | a1=a1+sum(c(:,i).*c(:,j)); 27 | end 28 | end 29 | fprintf('%f\n',a1); 30 | 31 | c=sum(rot90(c)); 32 | c=c.*c; 33 | a2=sum(c); 34 | r=a1/a2; 35 | 36 | -------------------------------------------------------------------------------- /miniEMD/memd_sumdumw.m: -------------------------------------------------------------------------------- 1 | function w = memd_sumdumw(r, envmax, envmin, alpha, crit) 2 | % A dumb test function to determine whether the 'localEMDfunc', 3 | % 'localEMDparam' mechanism works. works the same as 'stop' in 4 | % 'memd_emd'. 5 | 6 | switch(crit) 7 | case 'f' 8 | disp('Flandrin et al. criterion'); 9 | envmoy = (envmin+envmax)/2; 10 | amp = mean(abs(envmax-envmin))/2; 11 | sx = abs(envmoy)./amp; 12 | stop_sift = ~(mean(sx > alpha) > 0.05 | any(sx > 10*alpha)); 13 | case 'h' 14 | disp('Hello, Ferd. This is the Huang.'); 15 | envmoy = (envmin+envmax)/2; 16 | nr = r - envmoy; 17 | stop_sift = norm(nr-r)/(norm(r)+eps) < alpha; 18 | end 19 | 20 | if stop_sift 21 | w = zeros(size(r)); 22 | else 23 | w = ones(size(r)); 24 | end 25 | 26 | -------------------------------------------------------------------------------- /miniEMD/memd_flandrin_weights.m: -------------------------------------------------------------------------------- 1 | function w = memd_flandrin_weights(r, envmax, envmin, alpha) 2 | % Weighting function for local EMD which uses the criterion that sifting 3 | % must continue in any section where there are negative frequencies coming 4 | % out of the Hilbert transform. The local part of the Flandrin criterion 5 | % is also applied to ensure that at least some sifting is done on all 6 | % sections of signal. 7 | % is the half-width of the Hanning window used to smooth . I 8 | % think it might be unavoidable to have an a priori scale setting parameter 9 | % like this in any local EMD weighting function. 10 | 11 | width = 10; 12 | 13 | amp = mean(abs(envmax-envmin))/2; 14 | sx = abs((envmin+envmax)/2)./amp; 15 | isbad = sx > 2*alpha; 16 | if sum(isbad) > length(isbad)/2 % Checking that more than half of indices are bad. 17 | w = ones(size(r)); 18 | else 19 | w = memd_smoothBinSeries(isbad, width); 20 | end 21 | 22 | -------------------------------------------------------------------------------- /git_test_text_master.m: -------------------------------------------------------------------------------- 1 | %This is a test text file to pull from master branch 2 | 3 | %Write your name here: 4 | 5 | %Lara 6 | %Dan - edited in Matlab, now attempting to commit to my own branch. 7 | % Oh, I see. That's impossible. Having edited the version from the 8 | % Master branch, I have to commit it back to the Master branch before I 9 | % can even so much as *look* at another branch. But we probably don't 10 | % want to actually use branches anyway (point for discussion). 11 | % Hmmm, I wonder what happens if I click on "sync"? Well, doing 12 | % another Commit did indeed raise my count of "commits ahead" from 1 to 2 13 | % as I imagined it might, and my "commits behind" remains at zero. It 14 | % seems to be a two-layered commit process whereby local commits 15 | % presumably do something locally without changing the remote repos, and 16 | % then clicking on "sync" commits all the local commits at once to the 17 | % repos. 18 | %Mikio - let's see how this goes. -------------------------------------------------------------------------------- /EMDOS.tar/faux/build_lambda.m: -------------------------------------------------------------------------------- 1 | function C = build_lambda(ti,k) 2 | %% build_lambda : internal function of EMDOS. Computes the matrix C st: 3 | % C*h(xi) = lambda_i 4 | % where h(xi) is the vector of the spline evaluated at the extrema xi, and 5 | % lambda_i is the symmetric points defined in [1].% 6 | % 7 | % Thomas Oberlin 8 | % 12.2011 9 | % thomas.oberlin@imag.fr 10 | 11 | n=k-1; 12 | r = k/2; 13 | 14 | 15 | ti1 = ti(1:2:end); 16 | ti2 = ti(2:2:end); 17 | 18 | 19 | C1 = spcol([ti1(1)*ones(1,r) ti1 ti1(end)*ones(1,r)],k,ti2); 20 | C2 = spcol([ti2(1)*ones(1,r) ti2 ti2(end)*ones(1,r)],k,ti1); 21 | % "Mélange" des matrices C1 et C2. 22 | C = zeros(size(C1)+size(C2)); 23 | for i=r:size(C1,1)-r 24 | % On commence par C1 25 | C(2*i,2*(i-r)+1:2:2*(i+r)) = C1(i,i-r+1:i+r); 26 | C(2*i+1,2*i+1-n:2:2*i+1+n) = C2(i,i-r+1:i+r); 27 | end 28 | % On complète (ordre 1) 29 | for i=1:2*r-1 30 | C(i,i+1)=1; 31 | end 32 | for i=size(C,1)-2*r+2:size(C,1) 33 | C(i,i-1)=1; 34 | end 35 | 36 | 37 | 38 | end -------------------------------------------------------------------------------- /EMDOS.tar/paperfig.m: -------------------------------------------------------------------------------- 1 | function paperfig() 2 | %PAPERFIG draws the figures of the paper [1]. 3 | % WARNING : due to extremly fine discretization in some figures, the 4 | % execution of this function is VERY SLOW... You had better to execute only 5 | % a part of this. You can also change parameters to get similar figures 6 | % with lower resolution in a more reasonable time. 7 | % 8 | % The functions called are all located in the folder 'figfun' 9 | 10 | 11 | % Fig. 2 : Evaluation of the procedure which determine the differentiation order. 12 | fig_diff(); 13 | 14 | % Fig 3 : the test-signal and its component 15 | [s s1 s2 s3] = gen_tests(1); 16 | 17 | % Fig 4 : Sensitivity to the spline order 18 | sensitiv_spo(); 19 | 20 | % Fig 5 : Sensitivity to the initial guess 21 | sensitiv_init(); 22 | 23 | % Fig 6 : Orthogonality index 24 | sensitiv_io(); 25 | 26 | % Fig 7 : separation plots in the (a,f)-plane 27 | separTest(); %%% WARNING NOT WORKING!! 28 | 29 | % Fig 8 : 30 | 31 | 32 | 33 | end 34 | 35 | -------------------------------------------------------------------------------- /miniEMD/memd_nonegfreq.m: -------------------------------------------------------------------------------- 1 | function w = memd_nonegfreq(r, envmax, envmin, alpha, width) 2 | % Weighting function for local EMD which uses the criterion that sifting 3 | % must continue in any section where there are negative frequencies coming 4 | % out of the Hilbert transform. 5 | % is the half-width of the Hanning window used to smooth . I 6 | % think it might be unavoidable to have an a priori scale setting parameter 7 | % like this in any local EMD weighting function. 8 | 9 | hfunc = hilbert(r); 10 | th = unwrap(angle(hfunc)); 11 | freq = [NaN diff(th)]; 12 | % first segments containing freq < 0 and fill any short gaps between them: 13 | isbad = freq < 0; 14 | if sum(isbad) > length(isbad)/2 15 | w = ones(size(r)); 16 | else 17 | % O'barf: I have no idea how to create a nicely smoothed waveform from 18 | % a random rectangular wave without doing a nonlinear iteration of some 19 | % kind. 20 | hw = hanning(2*width+1)'; 21 | w = conv(double(freq < 0), hw, 'same'); 22 | end 23 | 24 | -------------------------------------------------------------------------------- /HuangEMD/ratioa.m: -------------------------------------------------------------------------------- 1 | function r = ratioa(c) 2 | 3 | % The function RATIOA calculates the local non-orthogonal leakage for pair of adjoining components, 4 | % where r is calculated according to the following rule 5 | % being applied to the case when c is the 2-D array. % r(i)= | c(i)*c(j) | / (c(i)^2 + c(j)^2), i=1,k j=1,k 6 | % and k is the number of IMF components. 7 | % 8 | % Calling sequence- 9 | % r=ratioa(c) 10 | % 11 | % Input- 12 | % c - 2-D matrix c(n,k) of IMF components 13 | % excluding the trend component. 14 | % Output- 15 | % r - vector r(k) of the ratio for adjoining components 16 | % - r(i) = | (c(1,i)*c(1,j)+...+c(n,i)*c(n,j)) | / % ( c(1,i)^2+c(1,j)^2+...+c(n,i)^2+c(n,j)^2 ) 17 | % 18 | 19 | %----- Get dimensions and initialize 20 | 21 | [npt,knb] = size(c); 22 | r1=zeros(knb-1,1); 23 | 24 | %----- Calculate the ratio for each pair 25 | for i=1:knb-1, 26 | j=i+1; 27 | a1=abs(sum(c(:,i).*c(:,j))); 28 | b1=sum(c(:,i).*c(:,i))+sum(c(:,j).*c(:,j)); 29 | r1(i)=a1/b1; 30 | end 31 | %r=mean(r1); 32 | %s=std(r1); 33 | %q=max(r1-r); 34 | r=r1; 35 | -------------------------------------------------------------------------------- /HuangEMD/emax.m: -------------------------------------------------------------------------------- 1 | %function [ma, ta]=emax(x) 2 | % 3 | % 4 | % Input- 5 | % x - vector of input data x(n) 6 | % Output- 7 | % ma - vector that specifies max points 8 | % ta - vector that specifies the coordinates of max points 9 | % 10 | %NOTE: 11 | % give a 1D data-x 12 | % The function EMAX returns the max points and their coordinates. 13 | % 14 | % Reference: 15 | % Association: NFAM, NFAMSM, NFAMSMMULTI 16 | % 17 | % 18 | % code writer:Norden Huang (NASA GSFC) November 11, 1999 19 | % footnote:S.C.Su 2009/05/14 20 | 21 | function [ma, ta]=emax(x) 22 | 23 | % Calling sequence- 24 | % [ma, ta]=emax(x) 25 | % 26 | 27 | %----- Get dimensions 28 | n=length(x); 29 | 30 | %----- Initialize 31 | n_x=1; 32 | ma=0; 33 | ta=0; 34 | 35 | %----- Extract the set of max points and their coordinates 36 | for i=2:n-1 37 | if (x(i-1)=x(i+1)) 38 | ma=[ma x(i)]; 39 | ta=[ta i]; 40 | n_x=n_x+1; 41 | 42 | end 43 | end 44 | 45 | %----- Define the output 46 | ma=ma(2:n_x); 47 | ta=ta(2:n_x); 48 | 49 | ma=ma'; 50 | ta=ta'; -------------------------------------------------------------------------------- /HuangEMD/emin.m: -------------------------------------------------------------------------------- 1 | %function [mi, ti]=emin(x) 2 | % 3 | % 4 | % Input- 5 | % x - vector of input data x(n) 6 | % Output- 7 | % mi - vector that specifies min points 8 | % ti - vector that specifies the coordinates of min points 9 | % 10 | %NOTE: 11 | % give a 1D data-x 12 | % The function EMIN returns the min points and their coordinates. 13 | % 14 | % Reference: 15 | % Association: NFAM, NFAMSM, NFAMSMMULTI 16 | % 17 | % 18 | % code writer:Norden Huang (NASA GSFC) November 11, 1999 19 | % footnote:S.C.Su 2009/05/14 20 | % 21 | function [mi, ti]=emin(x) 22 | 23 | % Calling sequence- 24 | % [mi, ti]=emin(x) 25 | % 26 | % Initial 27 | 28 | %----- Get dimensions 29 | n=length(x); 30 | 31 | %----- Initialize 32 | n_x=1; 33 | mi=0; 34 | ti=0; 35 | 36 | %----- Extract the set of max points and their coordinates 37 | for i=2:n-1 38 | if (x(i-1)>x(i))&(x(i)<=x(i+1)) 39 | mi=[mi x(i)]; 40 | ti=[ti i]; 41 | n_x=n_x+1; 42 | 43 | end 44 | end 45 | 46 | %----- Define the output 47 | mi=mi(2:n_x); 48 | ti=ti(2:n_x); 49 | 50 | mi=mi'; 51 | ti=ti'; -------------------------------------------------------------------------------- /EMDOS.tar/faux/matrice_der.m: -------------------------------------------------------------------------------- 1 | function res = matrice_der(sub,order,spo) 2 | %% MATRICE_DER : internal function of EMDOS. Computes the "differentiation matrix" for b-splines: 3 | % res(i,j)=int B^(d)_i(t) B^(d)_j(t) dt 4 | % where B^(d)_j is the d-derivative of the j-th B-spline 5 | % INPUTS: 6 | % sub : knots subdivision 7 | % order : differentiation order 8 | % spo : spline order 9 | % 10 | % Thomas Oberlin 11 | % 12.2011 12 | % thomas.oberlin@imag.fr 13 | 14 | % Nombre de splines 15 | N = length(sub)-spo; 16 | 17 | res = zeros(N,N); 18 | 19 | for i=1:N 20 | % Création bspline i 21 | sp1 = spmak(sub(i:i+spo),1); 22 | sp1 = fnder(sp1,order); 23 | 24 | for j=i:min(i+spo-1,N) 25 | % Création spline 2 26 | sp2 = spmak(sub(j:j+spo),1); 27 | sp2 = fnder(sp2,order); 28 | aux = fncmb(sp1,'*',sp2); 29 | res(i,j) = diff(fnval(fnint(aux),[sub(i) sub(i+spo)]))/N^order; 30 | end 31 | end 32 | 33 | % Complétion 34 | 35 | % Triangle inférieur (par symétrie) 36 | for i=2:N 37 | for j=max(1,i-spo-1):i-1 38 | res(i,j)=res(j,i); 39 | end 40 | end 41 | 42 | end 43 | -------------------------------------------------------------------------------- /HuangEMD/test.m: -------------------------------------------------------------------------------- 1 | Y='LOD78'; 2 | 3 | function allmode=eemd(Y,Nstd,NE) 4 | xsize=length(Y); 5 | dd=1:1:xsize; 6 | Ystd=std(Y); 7 | Y=Y/Ystd; 8 | 9 | TNM=fix(log2(xsize))-1; 10 | TNM2=TNM+2; 11 | for kk=1:1:TNM2, 12 | for ii=1:1:xsize, 13 | allmode(ii,kk)=0.0; 14 | end 15 | end 16 | 17 | for iii=1:1:NE, 18 | for i=1:xsize, 19 | temp=randn(1,1)*Nstd; 20 | X1(i)=Y(i)+temp; 21 | end 22 | 23 | for jj=1:1:xsize, 24 | mode(jj,1) = Y(jj); 25 | end 26 | 27 | xorigin = X1; 28 | xend = xorigin; 29 | 30 | nmode = 1; 31 | while nmode <= TNM, 32 | xstart = xend; 33 | iter = 1; 34 | 35 | while iter<=10, 36 | [spmax, spmin, flag]=extrema(xstart); 37 | upper= spline(spmax(:,1),spmax(:,2),dd); 38 | lower= spline(spmin(:,1),spmin(:,2),dd); 39 | mean_ul = (upper + lower)/2; 40 | xstart = xstart - mean_ul; 41 | iter = iter +1; 42 | end 43 | xend = xend - xstart; 44 | 45 | nmode=nmode+1; 46 | 47 | for jj=1:1:xsize, 48 | mode(jj,nmode) = xstart(jj); 49 | end 50 | end 51 | 52 | for jj=1:1:xsize, 53 | mode(jj,nmode+1)=xend(jj); 54 | end 55 | 56 | allmode=allmode+mode; 57 | 58 | end 59 | 60 | allmode=allmode/NE; 61 | allmode=allmode*Ystd; -------------------------------------------------------------------------------- /EMDOS.tar/README.txt: -------------------------------------------------------------------------------- 1 | PACKAGE EMDOS : EMD by Optimization on Splines, 2 | 3 | Implements the OS method described in [1]. 4 | 5 | 6 | 7 | DESCRIPTION 8 | Contains 2 folders: 9 | - faux/ contains internal function used in the decomposition. 10 | - figfun/ contains functions which create all the figures of [1] 11 | 12 | Contains 2 main functions 13 | - emdos is the main function for computing the EMD by the OS method. For use make 'help emdos'. 14 | - paperfig is a script that creates all the figure of [1]. 15 | 16 | 17 | WARNING 18 | This code needs the Spline Toolbox, which is included in the Curve Fitting Toolbox since version 7.11 of Matlab 19 | 20 | 21 | REFERENCES 22 | [1] T. Oberlin, S. Meignen and V. Perrier, "An Alternative Formulation 23 | for the Empirical Mode Decomposition", IEEE Trans. on Signal 24 | Prcessing, to appear. 25 | [2] N. Huang, Z. Shen, S. Long, M. Wu, H. Shih, Q. Zheng, N. Yen, C. Tung, and H. Liu, "The empirical mode decomposition 26 | and the Hilbert spectrum for nonlinear and non-stationary time series analysis," Proceedings of the Royal Society : 27 | Mathematical, Physical and Engineering Sciences, vol. 454, no. 1971, pp. 903–995, 1998. 28 | [3] G. Rilling, P. Flandrin, and P. Gonçalvès, "On empirical mode decomposition and its algorithms," in IEEE-EURASIP 29 | workshop on nonlinear signal and image processing NSIP-03, Grado (I), 2003. 30 | 31 | 32 | 33 | 34 | Thomas Oberlin 35 | 12.2011 36 | thomas.oberlin@imag.fr 37 | -------------------------------------------------------------------------------- /EMDOS.tar/figfun/gen_tests.m: -------------------------------------------------------------------------------- 1 | %% gen_tests : generates a multicomponent test signal 2 | function [s s1 s2 s3] = gen_tests(doplot) 3 | 4 | if nargin<2 5 | doplot = 0; 6 | end 7 | 8 | % Param 9 | N = 4*1024; 10 | T = 1; 11 | t = linspace(0,T-T/N,N); 12 | un = ones(size(t)); 13 | 14 | 15 | a1 = 2*un+1.3*cos(4*pi*t); 16 | phi1 = 2*pi*77; 17 | f1=30*sin(3*pi*t); 18 | fi1 = phi1+30*3*pi*cos(3*pi*t); 19 | 20 | a2 = 1*un+0.5*sin(3*pi*t+0.2*un); 21 | phi2 = 2*pi*46; 22 | f2=21*sin(3*pi*t); 23 | fi2 = phi2+21*3*pi*cos(3*pi*t); 24 | 25 | a3 = 2*exp(-22*(t-T/2*un).^2); 26 | phi3 = 2*pi*8*un; 27 | a3 = 2*exp(-22*(t-T/2*un).^2); 28 | phi3 = 2*pi*6*un; 29 | 30 | s1 = a1.*sin(phi1*t+f1); 31 | s2 = a2.*sin(phi2*t+f2); 32 | s3 = a3.*sin(phi3.*t); 33 | s = s1+s2+s3; 34 | 35 | 36 | if doplot 37 | 38 | indplot = 1:12:N; 39 | 40 | figure(); 41 | hold on; 42 | scatter(t(indplot),fi1(indplot)/(2*pi),10,a1(indplot)); 43 | scatter(t(indplot),fi2(indplot)/(2*pi),10,a2(indplot)); 44 | scatter(t(indplot),phi3(indplot)/(2*pi),10,a3(indplot)); 45 | xlabel('time'); 46 | ylabel('frequency'); 47 | colormap(flipud((gray(128)))); 48 | hc = colorbar('XTick',1,'XTickLabel','Amplitude'); 49 | title('Test signal s'); 50 | 51 | 52 | 53 | figure(); 54 | subplot(4,1,1);plot(t,s);xlabel('t');ylabel('s'); 55 | subplot(4,1,2);plot(t,s1);xlabel('t');ylabel('s_1'); 56 | subplot(4,1,3);plot(t,s2);xlabel('t');ylabel('s_2'); 57 | subplot(4,1,4);plot(t,s3);xlabel('t');ylabel('s_3'); 58 | 59 | end 60 | 61 | end -------------------------------------------------------------------------------- /HuangEMD/skiphilbt_m.m: -------------------------------------------------------------------------------- 1 | %function [bx, ex] = skiphilbt_m(imf) 2 | % 3 | % INPUT: 4 | % IMF: an IMF 1D matrix 5 | % OUTPUT: 6 | % bx: when the value=1,need no modification for Gibbs phenomenon in the begginning of this imf 7 | % ex: when the value=1,need no modification for Gibbs phenomenon in the endding of this imf 8 | % 9 | % NOTE: 10 | % 1.check if intermittency is at endpoints and skip appending waves if so 11 | % 2.This code performs starting and endding segment checking 12 | % when first or last value of imf are 0,0,0 ,this means no need for modification. 13 | % [0,0,0]means value=0,slope=0,curvature=0 ,the Gibbs effect is already fixed. 14 | % 3.before using hilbtm.m to perform a extending segment to the original imf 15 | % in the beginning and endding part, one should use skiphilbt_m to check the necessarity. 16 | %% 17 | % References: 18 | % 19 | % code writer: Karin Blank (karin.blank@nasa.gov) 20 | % footnote:S.C.Su 2009/06/20 21 | % 22 | % Association: hilbtm.m 23 | % this function is a check procedure before hilbtm.m 24 | %hilbtm,m is an improved edition used for calculating the Hilbert-Transform. 25 | % matlab original function disadvantage is been solved. 26 | % 27 | % Concerned function: hilbtm.m 28 | % above mentioned m file must be put together 29 | % 30 | % 31 | 32 | function [bx, ex] = skiphilbt_m(imf) 33 | 34 | %initiate with 0 35 | bx = 0; 36 | ex = 0; 37 | 38 | %Are 3 values in a row zero? 39 | %the head 40 | if(imf(1) == 0 & imf(2) == 0 & imf(3) == 0) 41 | bx = 1; 42 | end 43 | 44 | %the end 45 | if(imf(end) == 0 & imf(end-1) == 0 & imf(end-2) == 0) 46 | ex = 1; 47 | end 48 | 49 | return; -------------------------------------------------------------------------------- /HuangEMD/FAhilbert.m: -------------------------------------------------------------------------------- 1 | %function [f, a] = FAhilbert(data,dt) 2 | 3 | % The function FAhilbert calculates an improved Hilbert frequency and amplitude 4 | % of data(n,k), where n specifies the length of time series, and 5 | % k is the number of IMF components. 6 | % Regular Hilbert transform is used to perform a Hilbert transform. 7 | % 8 | % Non MATLAB Library routine used in the function is: HILBTM. 9 | % Note: FAH allows the instantaneous frequency to be negative. 10 | % 11 | % Calling sequence- 12 | % [f,a] = fah(data,dt) 13 | % 14 | % Input- 15 | % data - 2-D matrix data(n,k) of IMF components 16 | % dt - sampling period in seconds 17 | % Output- 18 | % f - 2-D matrix f(n,k) that specifies the Hilbert frequency in Hz 19 | % a - 2-D matrix a(n,k) that specifies the Hilbert amplitude 20 | % 21 | % Used by- 22 | % FA, NSPABMUN. 23 | % 24 | %written by 25 | % Norden Huang (NASA GSFC) Junw 2,2002 :Initial 26 | % Xianyao Chen, september, 2008 : modified 27 | % S.C.Su ,Sep ,2009, rename all the functions 28 | % 29 | function [f, a] = FAhilbert(data,dt) 30 | 31 | %----- Get the dimension 32 | %[n,m] = size(data); 33 | [nIMF,npt] = size(data); 34 | flip=0; 35 | if nIMF>npt 36 | data=data'; 37 | [nIMF,npt] = size(data); 38 | flip=1; 39 | end 40 | 41 | 42 | %h=hilbert(data);%NG here 43 | 44 | %----- Apply improved Hilbert transform 45 | %----- Get the instantaneous frequency 46 | 47 | f(1:nIMF,1:npt)=0; 48 | for j1=1:nIMF 49 | h1=hilbert(data(j1,:)); 50 | temp=diff(unwrap(angle(h1)))./(2*pi*dt); 51 | 52 | %----- Duplicate last points to make dimensions equal 53 | tta=[temp temp(end)]; 54 | f(j1,1:npt)=tta(1:npt); 55 | a(j1,1:npt)=abs(h1); 56 | clear tta temp h1 57 | end 58 | 59 | %----- Get the amplitude 60 | 61 | if flip ==1 62 | f=f'; 63 | a=a'; 64 | end 65 | -------------------------------------------------------------------------------- /HuangEMD/FAimphilbert.m: -------------------------------------------------------------------------------- 1 | %function [f, a] = FAimphilbert(data,dt) 2 | 3 | % The function FAimphilbert calculates frequency and amplitude 4 | % of data(n,k) using an improved Hilbert method, where n specifies the length 5 | % of time series, and k is the number of IMF components. 6 | % hilbt.m is used to perform Hilbert transform instead of hilbert.m, 7 | % which mainly reduce the Gipps impacts from the end points. 8 | % 9 | % hilbtm.m is used to perform an improved Hilbert transform.(Gibbs phenomena is fixed) 10 | % Note: FAH allows the instantaneous frequency to be negative. 11 | % 12 | % Calling sequence- 13 | % [f,a] = FAimphilbert(data,dt) 14 | % 15 | % Input- 16 | % data - 2-D matrix data(n,k) of IMF components 17 | % dt - sampling period in seconds 18 | % Output- 19 | % f - 2-D matrix f(n,k) that specifies the Hilbert frequency in Hz 20 | % a - 2-D matrix a(n,k) that specifies the Hilbert amplitude 21 | % 22 | % Used by- 23 | % FA, NSPABMUN. 24 | % 25 | %written by 26 | % Norden Huang (NASA GSFC) Junw 2,2002 :Initial 27 | % Xianyao Chen, september, 2008 : modified 28 | % S.C.Su ,Sep ,2009, rename all the functions 29 | % 30 | function [f, a] = FAimphilbert(data,dt) 31 | 32 | %----- Get the dimension 33 | %[nPoints, nIMF] = size(data); 34 | [nIMF,npt] = size(data); 35 | flip=0; 36 | if nIMF>npt 37 | data=data'; 38 | [nIMF,npt] = size(data); 39 | flip=1; 40 | end 41 | 42 | %----- Apply improved Hilbert transform 43 | h=hilbtm(data); 44 | 45 | %----- Get the instantaneous frequency 46 | f(1:nIMF,1:npt)=0; 47 | for j1=1:nIMF 48 | temp=diff(unwrap(angle(h(j1,:))))./(2*pi*dt); 49 | 50 | %----- Duplicate last points to make dimensions equal 51 | tta=[temp temp(end)]; 52 | f(j1,1:npt)=tta(1:npt); 53 | clear tta temp 54 | end 55 | 56 | %----- Get the amplitude 57 | a=abs(h); 58 | if flip ==1 59 | f=f'; 60 | a=a'; 61 | end 62 | -------------------------------------------------------------------------------- /HuangEMD/hilbertnormalize.m: -------------------------------------------------------------------------------- 1 | %function [ndata,a]=hilbertnormalize(data) 2 | 3 | % The function HILBERTNORMALIZE normalizes data using its own Hilbert transformation. 4 | % 5 | % Non MATLAB Library function used is: HILBTM 6 | % First argument is required. If not passed, second argument defaults to 0. 7 | % If flipping of data is desired r should be set to 1. 8 | % 9 | % Calling sequence- 10 | % [ndata,a]=hilbertnormalize(data) 11 | % 12 | % Input- 13 | % data - 2-D matrix data(n,m) that specifies the IMF components 14 | % 15 | % Output- 16 | % ndata - 2-D normalized data (FM) 17 | % a - 2-D Hilbert envelope(AM) 18 | % 19 | % Used by- 20 | % FA 21 | 22 | %written by 23 | % Kenneth Arnold (NASA GSFC) Summer 2003 Initial 24 | % NEH replaced HILBT with HILBTM 23 March 2005 25 | % S.C.Su-change some syntax (for the footnote only)2009 26 | %footnote: S.C.Su (2009/09/01) 27 | % 1.read the data,check input matrix 28 | % 2.Do the normalization using NEH hilbert code-AM 29 | % 3.normalize the IMF,AM been devided out 30 | % 31 | function [ndata,a]=hilbertnormalize(data) 32 | 33 | % 1.read the data,check input matrix 34 | %----- Get the dimension 35 | [n,m] = size(data); 36 | 37 | %----- Initialize and flip data if needed 38 | flipped=0; 39 | if (n 2 12 | x1=x(1:m-1); 13 | x2=x(2:m); 14 | indzer = find(x1.*x2<0); 15 | 16 | if any(x == 0) 17 | iz = find( x==0 ); 18 | indz = []; 19 | if any(diff(iz)==1) 20 | zer = x == 0; 21 | dz = diff([0 zer 0]); 22 | debz = find(dz == 1); 23 | finz = find(dz == -1)-1; 24 | indz = round((debz+finz)/2); 25 | else 26 | indz = iz; 27 | end 28 | indzer = sort([indzer indz]); 29 | end 30 | end 31 | 32 | d = diff(x); 33 | 34 | n = length(d); 35 | d1 = d(1:n-1); 36 | d2 = d(2:n); 37 | indmin = find(d1.*d2<0 & d1<0)+1; 38 | indmax = find(d1.*d2<0 & d1>0)+1; 39 | 40 | 41 | % when two or more successive points have the same value we consider only one extremum in the middle of the constant area 42 | % (only works if the signal is uniformly sampled) 43 | 44 | if any(d==0) 45 | 46 | imax = []; 47 | imin = []; 48 | 49 | bad = (d==0); 50 | dd = diff([0 bad 0]); 51 | debs = find(dd == 1); 52 | fins = find(dd == -1); 53 | if debs(1) == 1 54 | if length(debs) > 1 55 | debs = debs(2:end); 56 | fins = fins(2:end); 57 | else 58 | debs = []; 59 | fins = []; 60 | end 61 | end 62 | if length(debs) > 0 63 | if fins(end) == m 64 | if length(debs) > 1 65 | debs = debs(1:(end-1)); 66 | fins = fins(1:(end-1)); 67 | 68 | else 69 | debs = []; 70 | fins = []; 71 | end 72 | end 73 | end 74 | lc = length(debs); 75 | if lc > 0 76 | for k = 1:lc 77 | if d(debs(k)-1) > 0 78 | if d(fins(k)) < 0 79 | imax = [imax round((fins(k)+debs(k))/2)]; 80 | end 81 | else 82 | if d(fins(k)) > 0 83 | imin = [imin round((fins(k)+debs(k))/2)]; 84 | end 85 | end 86 | end 87 | end 88 | 89 | if length(imax) > 0 90 | indmax = sort([indmax imax]); 91 | end 92 | 93 | if length(imin) > 0 94 | indmin = sort([indmin imin]); 95 | end 96 | 97 | end 98 | end 99 | -------------------------------------------------------------------------------- /EMDOS.tar/faux/extr.m: -------------------------------------------------------------------------------- 1 | %extracts the indices of extrema 2 | % Writtezn by Gabriel Rilling 3 | function [indmin, indmax, indzer] = extr(x,t) 4 | 5 | if(nargin==1) 6 | t=1:length(x); 7 | end 8 | 9 | m = length(x); 10 | 11 | if nargout > 2 12 | x1=x(1:m-1); 13 | x2=x(2:m); 14 | indzer = find(x1.*x2<0); 15 | 16 | if any(x == 0) 17 | iz = find( x==0 ); 18 | indz = []; 19 | if any(diff(iz)==1) 20 | zer = x == 0; 21 | dz = diff([0 zer 0]); 22 | debz = find(dz == 1); 23 | finz = find(dz == -1)-1; 24 | indz = round((debz+finz)/2); 25 | else 26 | indz = iz; 27 | end 28 | indzer = sort([indzer indz]); 29 | end 30 | end 31 | 32 | d = diff(x); 33 | 34 | n = length(d); 35 | d1 = d(1:n-1); 36 | d2 = d(2:n); 37 | indmin = find(d1.*d2<0 & d1<0)+1; 38 | indmax = find(d1.*d2<0 & d1>0)+1; 39 | 40 | 41 | % when two or more successive points have the same value we consider only one extremum in the middle of the constant area 42 | % (only works if the signal is uniformly sampled) 43 | 44 | if any(d==0) 45 | 46 | imax = []; 47 | imin = []; 48 | 49 | bad = (d==0); 50 | dd = diff([0 bad 0]); 51 | debs = find(dd == 1); 52 | fins = find(dd == -1); 53 | if debs(1) == 1 54 | if length(debs) > 1 55 | debs = debs(2:end); 56 | fins = fins(2:end); 57 | else 58 | debs = []; 59 | fins = []; 60 | end 61 | end 62 | if length(debs) > 0 63 | if fins(end) == m 64 | if length(debs) > 1 65 | debs = debs(1:(end-1)); 66 | fins = fins(1:(end-1)); 67 | 68 | else 69 | debs = []; 70 | fins = []; 71 | end 72 | end 73 | end 74 | lc = length(debs); 75 | if lc > 0 76 | for k = 1:lc 77 | if d(debs(k)-1) > 0 78 | if d(fins(k)) < 0 79 | imax = [imax round((fins(k)+debs(k))/2)]; 80 | end 81 | else 82 | if d(fins(k)) > 0 83 | imin = [imin round((fins(k)+debs(k))/2)]; 84 | end 85 | end 86 | end 87 | end 88 | 89 | if length(imax) > 0 90 | indmax = sort([indmax imax]); 91 | end 92 | 93 | if length(imin) > 0 94 | indmin = sort([indmin imin]); 95 | end 96 | 97 | end 98 | end 99 | -------------------------------------------------------------------------------- /EMDOS.tar/figfun/sensitiv_io.m: -------------------------------------------------------------------------------- 1 | function sensitiv_io() 2 | % Teste l'IO pour plusieurs methodes et k 3 | 4 | 5 | % Signal 6 | % Param 7 | N = 1024*8; 8 | T = 1; 9 | t = linspace(0,T-T/N,N); 10 | un = ones(size(t)); 11 | 12 | 13 | %% Signal s 14 | a1 = 2*un+1.1*cos(4*pi*t); 15 | phi1 = 2*pi*90; 16 | f1=30*sin(3*pi*t); 17 | fi1 = phi1+30*3*pi*cos(3*pi*t); 18 | 19 | a2 = 1*un+0.4*sin(3*pi*t+0.2*un); 20 | phi2 = 2*pi*49; 21 | f2=21*sin(3*pi*t); 22 | fi2 = phi2+21*3*pi*cos(3*pi*t); 23 | 24 | a3 = 2*exp(-22*(t-T/2*un).^2); 25 | phi3 = 2*pi*8*un; 26 | a3 = 2*exp(-22*(t-T/2*un).^2); 27 | phi3 = 2*pi*6*un; 28 | 29 | s1 = a1.*sin(phi1*t+f1); 30 | s2 = a2.*sin(phi2*t+f2); 31 | s3 = a3.*sin(phi3.*t); 32 | s = s1+s2+s3;%+sin(2*pi*180*t); 33 | sred = s((N/4):(3*N/4)); 34 | 35 | 36 | 37 | % General options 38 | opt.t = t; 39 | opt.maxmodes = 2; 40 | opt.stop = 'f'; 41 | opt.orderder = [2 2]; % Selection derivee 42 | opt.liss = [0]; 43 | 44 | 45 | 46 | alpha = 0.02:0.05:0.3; 47 | emd = zeros(size(alpha)); 48 | os4 = zeros(size(alpha)); 49 | os6 = zeros(size(alpha)); 50 | os8 = zeros(size(alpha)); 51 | 52 | 53 | opt.method = 'emd'; 54 | for i=1:length(alpha) 55 | opt.alpha = alpha(i); 56 | [imf,ort,nder] = emdos(s,opt); 57 | emd(i) = io(sred,imf(1:3,(N/4):(3*N/4))); 58 | end 59 | 60 | 61 | 62 | opt.method = 'os'; 63 | opt.sporder = 6; 64 | for i=1:length(alpha) 65 | opt.alpha = alpha(i); 66 | [imf,ort,nder] = emdos(s,opt);disp(nder); 67 | os4(i) = io(sred,imf(1:3,(N/4):(3*N/4))); 68 | end 69 | 70 | 71 | opt.sporder = 8; 72 | for i=1:length(alpha) 73 | opt.alpha = alpha(i); 74 | [imf,ort,nder] = emdos(s,opt);disp(nder); 75 | os6(i) = io(sred,imf(1:3,(N/4):(3*N/4))); 76 | end 77 | 78 | 79 | opt.sporder = 10; 80 | for i=1:length(alpha) 81 | opt.alpha = alpha(i); 82 | [imf,ort,nder] = emdos(s,opt);disp(nder); 83 | os8(i) = io(sred,imf(1:3,(N/4):(3*N/4))); 84 | end 85 | 86 | 87 | 88 | 89 | % Affichage 90 | figure(); 91 | plot(alpha,emd,'ko-',alpha,os4,'rs-',alpha,os6,'b*-',alpha,os8,'g+-'); 92 | legend('EMD','OS k=6','OS k=8','OS k=10'); 93 | xlabel('\alpha');ylabel('Orthogonality Index'); 94 | title('Orthogonality index for different spline orders'); 95 | 96 | -------------------------------------------------------------------------------- /EMDOS.tar/figfun/fig_diff.m: -------------------------------------------------------------------------------- 1 | function [a f er] = fig_diff(ares,fres) 2 | % Computes the function ac = h(f) which is the curve separating the domains 3 | % - D1 : s and s'' have the same number of extrema. 4 | % - D2 : s and s'''' have the same number of extrema. 5 | % - D3 : not (D1 or D2) 6 | % ares, fres : horizontal and vertical resolution (default : 0.01 and 7 | % 0.005). 8 | 9 | if nargin<2 10 | ares = 0.005; 11 | fres = 0.01; 12 | end 13 | 14 | 15 | T = 8*1024; 16 | seuil= 4; 17 | 18 | a = 10.^(-2:ares:2); 19 | f = 0.01:fres:1; 20 | t = linspace(0,T,128*T); 21 | intval = 2*T:128*T; 22 | 23 | er = zeros(length(a),length(f)); 24 | x1 = cos(2*pi*t); 25 | 26 | for i =1:length(a) 27 | for j = 1:length(f), 28 | 29 | s = x1+a(i)*cos(2*pi*f(j)*t); 30 | 31 | sder2 = madiff(s,t,2); 32 | sder4 = madiff(s,t,4); 33 | 34 | 35 | 36 | s = s(intval); 37 | sder2 = sder2(intval); 38 | sder4 = sder4(intval); 39 | 40 | 41 | % Par rapport aux extrema de s, s'' ou s4 42 | [indmin, indmax, indzer] = extr(s,t); 43 | ls0 = length(indmin) + length(indmax); 44 | 45 | [indmin, indmax, indzer] = extr(sder2); 46 | ls2 = length(indmin) + length(indmax); 47 | 48 | [indmin, indmax, indzer] = extr(sder4); 49 | ls4 = length(indmin) + length(indmax); 50 | 51 | if ls2<=ls0+seuil 52 | % On choisit s 53 | er(i,j)=0; 54 | elseif ls4 <=ls2+seuil 55 | % On choisit s2 56 | er(i,j)=1; 57 | else 58 | % On choisit s4 59 | er(i,j)=2; 60 | end 61 | 62 | if max(ls0,max(ls2,ls4))>=length(s)/5 63 | error('Pb de discretisation dans le calculm de la derivee'); 64 | end 65 | 66 | 67 | end 68 | end 69 | 70 | er=er'; 71 | 72 | figure(); 73 | imagesc(log(a)/log(10),(f),er); 74 | set(gca,'YDir','normal'); 75 | colormap('gray'); 76 | xlabel('a'); 77 | ylabel('f'); 78 | hold on; 79 | plot((-2:0.1:2),(10.^(-2:0.1:2)).^(-1/1),'color',0.3*ones(1,3),'LineWidth',2); 80 | plot((-2:0.1:2),(10.^(-2:0.1:2)).^(-1/3),'color',0.5*ones(1,3),'LineWidth',2); 81 | plot((-2:0.1:2),(10.^(-2:0.1:2)).^(-1/5),'color',0.7*ones(1,3),'LineWidth',2); 82 | legend('af=1','af^3=1','af^5=1'); 83 | set(gca,'xtick',-2:2,'xticklabel',{'10^{-2}','10^{-1}','1','10^{1}','10^{2}',}); 84 | 85 | 86 | end 87 | -------------------------------------------------------------------------------- /ViewDataEMD(2).m: -------------------------------------------------------------------------------- 1 | clear all;close all; 2 | %% Data 3 | load('2sample_CA1_DG_LFP_10s') 4 | Nt = length(sample_CA1_theta); 5 | fs = 1e3; 6 | dt = 1/fs; 7 | t = [0:Nt-1]*dt; 8 | 9 | CA1theta = sample_CA1_theta; 10 | DGLFP = sample_DG_complexLFP; 11 | clear sample_DG_complexLFP sample_CA1_theta 12 | 13 | figure; 14 | subplot(211);plot(t,CA1theta); 15 | xlabel('time (sec');title('CA1 LFP');axis tight 16 | subplot(212);plot(t,DGLFP);axis tight 17 | xlabel('time (sec');title('DG LFP');axis tight 18 | 19 | %% Gabor spectrograms 20 | L = 10; 21 | NFFT = 2^L; 22 | tw = -NFFT/2+1:NFFT/2; 23 | sigma = .2;%[sec] 24 | sigSamp = sigma*fs; 25 | w = sqrt(sqrt(2)/sigSamp)*exp(-pi*tw.*tw/sigSamp/sigSamp); 26 | overlap = NFFT-1; 27 | [SpecCA1 T F]=spectrogram(CA1theta,w,overlap,NFFT,fs);%deriv gaussian windowed spectrogram 28 | figure;imagesc(F,T,abs(SpecCA1));set(gca,'YDir','normal'); 29 | ylim([0 150]) 30 | set(gca,'YTick',[0:10:150]) 31 | grid on 32 | xlabel('time (sec)');ylabel('Hz') 33 | title('Gabor spectrogram CA1 LFP \theta') 34 | 35 | [SpecDG F T]=spectrogram(DGLFP,w,overlap,NFFT,fs);%deriv gaussian windowed spectrogram 36 | figure;imagesc(T,F,abs(SpecDG));set(gca,'YDir','normal'); 37 | ylim([0 150]) 38 | set(gca,'YTick',[0:10:150]) 39 | grid on 40 | xlabel('time (sec)');ylabel('Hz') 41 | title('Gabor spectrogram DG LFP') 42 | 43 | %% Morlet wavelet spectrograms 44 | sig1 = struct('val',CA1theta,'period',dt); 45 | cwtS1 = cwtft(sig1,'scales',1./[1:1:200]); 46 | scales = cwtS1.scales; 47 | MorletFourierFactor = 4*pi/(6+sqrt(2+6^2)); 48 | freq = 1./(scales.*MorletFourierFactor); 49 | figure;imagesc(t,freq,abs(cwtS1.cfs));set(gca,'YDir','normal'); 50 | xlabel('time (sec)'); ylabel('Pseudo-frequency'); 51 | title('Morlet spectrogram CA1 LFP \theta') 52 | set(gca,'YTick',[0:10:150]) 53 | grid on 54 | ylim([0 150]) 55 | 56 | sig1 = struct('val',DGLFP,'period',dt); 57 | cwtS1 = cwtft(sig1,'scales',1./[1:1:200]); 58 | scales = cwtS1.scales; 59 | MorletFourierFactor = 4*pi/(6+sqrt(2+6^2)); 60 | freq = 1./(scales.*MorletFourierFactor); 61 | figure;imagesc(t,freq,abs(cwtS1.cfs));set(gca,'YDir','normal'); 62 | xlabel('time (sec)'); ylabel('Pseudo-frequency'); 63 | title('Morlet spectrogram DG LFP \theta') 64 | set(gca,'YTick',[0:10:150]) 65 | grid on 66 | ylim([0 150]) 67 | -------------------------------------------------------------------------------- /HuangEMD/dist_value.m: -------------------------------------------------------------------------------- 1 | % function PDF = dist_value(yPos, yBar, nDof) 2 | % 3 | % INPUT: 4 | % yPos: An input array at which PDF values are calculated---y value 5 | % yPos is an 1D 5000 pt matrix for y value in interval-[yUpper,yLower] 6 | % yBar: The mean value of y ---------------------------exp(yBar)=E-bar 7 | % nDof: The number of degree of freedom----------------Ndof=Npt 8 | % OUTPUT: 9 | % PDF: a normalized output array -about the PDF distribution 10 | % 11 | % NOTE: This is a utility program being called by "confidenceLine.m". 12 | % this code calculate PDF value under yPos range 13 | % within the mean value of y(y-bar) of a chi-square distribution 14 | % here main job is calculating equation(3.4)--PDF formula from the reference paper 15 | % 16 | % References: 17 | % 'A study of the characteristics of white noise using the empirical mode decomposition method' 18 | % Zhaohua,Wu and Norden E. Huang 19 | % Proc. R. Soc. Lond. A (2004) 460,1597-1611 20 | % 21 | % code writer: zhwu@cola.iges.org 22 | % footnote:S.C.Su 2009/05/31 23 | % 24 | % 1.start to form equation(3.4)--PDF formula 25 | % 2.calculate PDF value for every y value 26 | % 3.to ensure the converge of the calculation,divide by rscale 27 | % 28 | % Association: 29 | % this function is called by confidenceLine.m 30 | % calculate the PDF value for 'sigline' 31 | % 32 | % Concerned function: confidenceLine.m 33 | % the others are matlab functions. 34 | % 35 | 36 | function PDF = dist_value(yPos, yBar, nDof) 37 | 38 | ylen = length(yPos); 39 | 40 | %1.start to form equation(3.4)--PDF formula 41 | eBar = exp(yBar);%E-bar 42 | evalue=exp(yPos);%E 43 | 44 | %2.calculate PDF value for every y value 45 | for i=1:ylen, 46 | tmp1 = evalue(i)/eBar-yPos(i);%calculate---tmp1=(E/E-bar)-y 47 | tmp2 = -tmp1*nDof*eBar/2;%calculate--------tmp2=(-1/2)*E-bar*Ndof*tmp1 48 | tmp3(i) = 0.5*nDof*eBar*log(nDof) + tmp2; 49 | end 50 | 51 | %3.to ensure the converge of the calculation,divide by rscale 52 | % because the PDF is a relative value,not a absolute value 53 | rscale = max(tmp3); 54 | 55 | tmp4 = tmp3 - rscale;%minus means divide,after we take expontial 56 | PDF= exp(tmp4); -------------------------------------------------------------------------------- /HuangEMD/hilbert.m: -------------------------------------------------------------------------------- 1 | function x = hilbert(xr,n) 2 | %HILBERT Discrete-time analytic signal via Hilbert transform. 3 | % X = HILBERT(Xr) computes the so-called discrete-time analytic signal 4 | % X = Xr + i*Xi such that Xi is the Hilbert transform of real vector Xr. 5 | % If the input Xr is complex, then only the real part is used: Xr=real(Xr). 6 | % If Xr is a matrix, then HILBERT operates along the columns of Xr. 7 | % 8 | % HILBERT(Xr,N) computes the N-point Hilbert transform. Xr is padded with 9 | % zeros if it has less than N points, and truncated if it has more. 10 | % 11 | % For a discrete-time analytic signal X, the last half of fft(X) is zero, 12 | % and the first (DC) and center (Nyquist) elements of fft(X) are purely real. 13 | % 14 | % Example: 15 | % Xr = [1 2 3 4]; 16 | % X = hilbert(Xr) 17 | % produces X=[1+1i 2-1i 3-1i 4+1i] such that Xi=imag(X)=[1 -1 -1 1] is the 18 | % Hilbert transform of Xr, and Xr=real(X)=[1 2 3 4]. Note that the last half 19 | % of fft(X)=[10 -4+4i -2 0] is zero (in this example, the last half is just 20 | % the last element). Also note that the DC and Nyquist elements of fft(X) 21 | % (10 and -2) are purely real. 22 | % 23 | % See also FFT, IFFT. 24 | 25 | % Copyright 1988-2004 The MathWorks, Inc. 26 | % $Revision: 1.10.4.3 $ $Date: 2007/12/14 15:05:05 $ 27 | 28 | % References: 29 | % [1] Alan V. Oppenheim and Ronald W. Schafer, Discrete-Time 30 | % Signal Processing, 2nd ed., Prentice-Hall, Upper Saddle River, 31 | % New Jersey, 1998. 32 | % 33 | % [2] S. Lawrence Marple, Jr., Computing the discrete-time analytic 34 | % signal via FFT, IEEE Transactions on Signal Processing, Vol. 47, 35 | % No. 9, September 1999, pp.2600--2603. 36 | 37 | if nargin<2, n=[]; end 38 | if ~isreal(xr) 39 | warning(generatemsgid('Ignore'),'HILBERT ignores imaginary part of input.') 40 | xr = real(xr); 41 | end 42 | % Work along the first nonsingleton dimension 43 | [xr,nshifts] = shiftdim(xr); 44 | if isempty(n) 45 | n = size(xr,1); 46 | end 47 | x = fft(xr,n,1); % n-point FFT over columns. 48 | h = zeros(n,~isempty(x)); % nx1 for nonempty. 0x0 for empty. 49 | if n > 0 && 2*fix(n/2) == n 50 | % even and nonempty 51 | h([1 n/2+1]) = 1; 52 | h(2:n/2) = 2; 53 | elseif n>0 54 | % odd and nonempty 55 | h(1) = 1; 56 | h(2:(n+1)/2) = 2; 57 | end 58 | x = ifft(x.*h(:,ones(1,size(x,2)))); 59 | 60 | % Convert back to the original shape. 61 | x = shiftdim(x,-nshifts); 62 | -------------------------------------------------------------------------------- /HuangEMD/significanceIMF.m: -------------------------------------------------------------------------------- 1 | % function logep = significanceIMF(imfs) 2 | % 3 | % INPUT: 4 | % imfs: a 2D (Npts,Nimf)IMF matrix after EMD or EEMD(Wu's suggestion) processed. 5 | % The first IMF must be included for it is used to obtain the relative mean 6 | % energy for other 1IMFs. The trend is not included. 7 | % OUTPUT: 8 | % logep: a 2D , with the first column the natural 9 | % logarithm of mean period, and the second column the 10 | % natural logarithm of mean energy for all IMFs 11 | % 12 | % NOTE: this code calculate [lnE,lnT] value for IMFs 13 | % 14 | % References: 15 | % 'A study of the characteristics of white noise using the empirical mode decomposition method' 16 | % Zhaohua,Wu and Norden E. Huang 17 | % Proc. R. Soc. Lond. A (2004) 460,1597-1611 18 | % 19 | % code writer: zhwu@cola.iges.org 20 | % code writer: S.C.Su-separate significance.m into 3 parts.2009/05/31 21 | % this part is to calculate [lnE,lnT] value for IMFs 22 | % footnote:S.C.Su 2009/05/31 23 | % 24 | % Concerned function: ifndq.m ,extrema.m 25 | % the others are matlab functions. 26 | % 27 | 28 | function logep = significanceIMF(imfs) 29 | 30 | nDof = length(imfs(:,1));%number of data points=degree of freedom 31 | 32 | %calculating (lnE,lnT) values 33 | %the energy of IMF--sum(square of the data/Ndof) 34 | %1.start to calculate the energy for every IMF 35 | columns=length(imfs(1,:)); 36 | for i=1:columns, 37 | logep(i,2)=0; 38 | logep(i,1)=0; 39 | for j=1:nDof, 40 | logep(i,2)=logep(i,2)+imfs(j,i)*imfs(j,i); 41 | end 42 | logep(i,2)=logep(i,2)/nDof; 43 | 44 | end 45 | 46 | %set the first IMF as a whiteNoise,set it as sfactor ,to be the beginning point 47 | sfactor=logep(1,2); 48 | for i=1:columns, 49 | logep(i,2)=0.5636*logep(i,2)/sfactor; % 0.6441 50 | end 51 | 52 | %2.start to calculate the period for every IMF 53 | % for IMF1~IMF3,direct count extrema number-Nextrema 54 | % lnT=Ndof/Nextrema 55 | for i=1:3, 56 | [spmax, spmin, flag]= extrema(imfs(:,i)); 57 | temp=length(spmax(:,1))-1; 58 | logep(i,1)=nDof/temp; 59 | end 60 | 61 | % for IMF4~end,calculate the IFvalue,determine how many rounds rotated already 62 | for i=4:columns, 63 | omega=ifndq(imfs(:,i),1); 64 | sumomega=0; 65 | for j=1:nDof, 66 | sumomega=sumomega+omega(j); 67 | end 68 | logep(i,1)=nDof*2*pi/sumomega; 69 | end 70 | 71 | %multiply the translation coeifficient ,1.4427=log(e)/log(2) 72 | logep=1.4427*log(logep); -------------------------------------------------------------------------------- /EMDOS.tar/faux/build_m02.m: -------------------------------------------------------------------------------- 1 | function [indmin indmax] = build_m02(r,t,ordersel,liss) 2 | %% build_h0 : builds the approximation m0 of the local mean of the signal s 3 | % INPUTS : 4 | % s : signal 5 | % t : time 6 | % ordersel : manual selection of derivation order 7 | % spo : order of the splines 8 | % OUTPUTS : 9 | % indexC : index of the extrema estimates 10 | % tau : points of the subdivision 11 | % m0 : estimation of the local mean : coefficients of the B-spline on tau 12 | % aux : effective order of serivation 13 | % 14 | % Thomas Oberlin 15 | % 12.2011 16 | % thomas.oberlin@imag.fr 17 | 18 | 19 | %% Lissage du signal 20 | noy = [1 2 1]/4; 21 | tmp=r; 22 | for j=1:liss 23 | tmp = conv(tmp,noy,'valid'); 24 | end 25 | s=tmp; 26 | 27 | 28 | %% Estimation of extrema of s 29 | sder2 = madiff(s,t,2); 30 | sder4 = madiff(s,t,4); 31 | switch(ordersel) 32 | case -1 33 | seuil=8; 34 | % Automative selection 35 | [indmin, indmax, indzer] = extr(s,t); 36 | ls0 = length(indmin) + length(indmax); 37 | [indmin, indmax, indzer] = extr(sder2); 38 | ls2 = length(indmin) + length(indmax); 39 | [indmin, indmax, indzer] = extr(sder4); 40 | ls4 = length(indmin) + length(indmax); 41 | if ls2<=ls0+seuil 42 | [indmin, indmax, indzer] = extr(s,t); 43 | aux = 0; 44 | elseif ls4 <=ls2+seuil 45 | [indmin, indmax, indzer] = extr(sder2,t); 46 | aux = 2; 47 | else 48 | [indmin, indmax, indzer] = extr(sder4,t); 49 | aux = 4; 50 | end 51 | % Case discretization pb 52 | if max(ls0,max(ls2,ls4))>=length(s)/4 53 | warning('Pb de discretisation dans le calcul de la derivee'); 54 | % Cas non C2 55 | if ls2<=ls0+seuil || ls2>=length(s)/4 56 | [indmin, indmax, indzer] = extr(s,t); 57 | aux = 0; 58 | else 59 | [indmin, indmax, indzer] = extr(sder2,t); 60 | aux = 2; 61 | end 62 | end 63 | 64 | case 0 65 | % Order 0 66 | [indmin, indmax, indzer] = extr(s); 67 | aux = 0; 68 | case 2 69 | % Order 2 70 | [indmin, indmax, indzer] = extr(sder2); 71 | aux = 2; 72 | case 4 73 | % Order 4 74 | [indmin, indmax, indzer] = extr(sder4); 75 | aux = 4; 76 | otherwise 77 | error('The ordersel field must be 0, 2 or 4'); 78 | end 79 | 80 | indmin = indmin+liss; 81 | indmax = indmax+liss; 82 | 83 | % Si bruit 84 | if 0%length(indmin)+length(indmax)>length(s)/2 85 | warning('Using sure SHRINKAGE'); 86 | sd = wden(s,'heursure','s','one',3,'sym8'); 87 | [indmin indmax] = extr(s-sd); 88 | %indexC = sort([indmin indmax]); 89 | end 90 | 91 | end 92 | 93 | -------------------------------------------------------------------------------- /HuangEMD/local_max.m: -------------------------------------------------------------------------------- 1 | %function [local_max_x, local_max_y] = local_max(data_x, data_y) 2 | % 3 | % The function LOCAL_MAX returns local max point, their coordinates and the number of values. 4 | % Identify local maxima points of physical signal 5 | % Note: in the code initially the local variable flag = 0. 6 | % It changes its value: flag = 0 when a slope is rising, 7 | % and flag = 1 when a slope is descending. 8 | % 9 | % Calling sequence- 10 | % [local_max_x,local_max_y] = local_max(data_x, data_y) 11 | % 12 | % Input- 13 | % data_x - input vector of coordinates 14 | % data_y - input vector of corresponding values 15 | % Output- 16 | % local_max_x - vector that specifies the coordinates 17 | % of max values in the order found 18 | % local_max_y - corresponding max values 19 | % 20 | % Jelena Marshak (NASA GSFC) May 8, 2004 Modified 21 | % (added variable flag initialization to 0) 22 | % Footnote:S.C.Su 23 | 24 | function [local_max_x, local_max_y] = local_max(data_x, data_y) 25 | 26 | 27 | flag = 0; %flag = 0 when slope rising, 1 when slope descending 28 | 29 | local_max_y = []; 30 | local_max_x = []; 31 | num_extrema = 0; 32 | Y = []; 33 | X = []; 34 | 35 | %----- Find the extrema values 36 | %loop start ---check every value in the series 37 | for i=2:length(data_y)-1; 38 | 39 | % when slope descending (flag=1) ,the value of i position =left or right hand side value 40 | if(((data_y(i) == data_y(i+1)) | (data_y(i) == data_y(i-1))) & (flag == 1)) 41 | %mark this position 42 | Y = [Y, data_y(i)]; 43 | X = [X, data_x(i)]; 44 | end 45 | 46 | %first time start here, because initial setting flag=0 47 | % when slope rising (flag=0) ,the value of i position >left hand side value 48 | if((data_y(i) > data_y(i-1)) & (flag == 0) & ~(data_y(i) == data_y(i-1))) 49 | %set slope descending (flag=1),mark the position 50 | flag = 1; 51 | Y = data_y(i); 52 | X = data_x(i); 53 | end 54 | 55 | % when slope descending (flag=1) ,the value of i position right hand side value 64 | if((data_y(i) > data_y(i+1) & (flag == 1))) 65 | %set slope rising,mark the position 66 | flag = 0; 67 | local_max_y = [local_max_y, Y]; 68 | local_max_x = [local_max_x, X]; 69 | Y = []; 70 | X = []; 71 | num_extrema = num_extrema+1; 72 | end 73 | 74 | %loop end ---check every value in the series 75 | end 76 | 77 | 78 | -------------------------------------------------------------------------------- /miniEMD/memd_extr.m: -------------------------------------------------------------------------------- 1 | %extracts the indices of extrema 2 | % Writtezn by Gabriel Rilling 3 | function [indmin, indmax, indzer] = memd_extr(x,t) 4 | 5 | if(nargin==1) 6 | t=1:length(x); 7 | end 8 | 9 | m = length(x); 10 | 11 | % Finding zeros. 12 | 13 | if nargout > 2 14 | x1=x(1:m-1); 15 | x2=x(2:m); 16 | indzer = find(x1.*x2<0); % Zeros are places where x changes from positive to negative. 17 | 18 | if any(x == 0) 19 | iz = find( x==0 ); % Zeros are places where x equals zero. 20 | indz = []; 21 | if any(diff(iz)==1) % Unless x equals zero for more than one time point. 22 | zer = x == 0; 23 | dz = diff([0 zer 0]); % Zero-one vector; equals one where x equals zero. 24 | debz = find(dz == 1); % Indices where x becomes zero after being nonzero. 25 | finz = find(dz == -1)-1; % Indices where x is zero before becoming nonzero. 26 | indz = round((debz+finz)/2); % Middle index of a stretch of zeros. 27 | else 28 | indz = iz; 29 | end 30 | indzer = sort([indzer indz]); % Concatenates places where x equals zero and places where x changes sign. 31 | end 32 | end 33 | 34 | % Now maxima & minima... 35 | 36 | d = diff(x); % Looking at "derivative". 37 | 38 | n = length(d); 39 | d1 = d(1:n-1); 40 | d2 = d(2:n); 41 | indmin = find(d1.*d2<0 & d1<0)+1; % Places where derivative changes sign from negative to positive are minima. 42 | indmax = find(d1.*d2<0 & d1>0)+1; % Places where derivative changes sign from positive to negative are maxima. 43 | 44 | 45 | % when two or more successive points have the same value we consider only one extremum in the middle of the constant area 46 | % (only works if the signal is uniformly sampled) 47 | 48 | if any(d==0) 49 | 50 | imax = []; 51 | imin = []; 52 | 53 | bad = (d==0); 54 | dd = diff([0 bad 0]); 55 | debs = find(dd == 1); 56 | fins = find(dd == -1); 57 | if debs(1) == 1 58 | if length(debs) > 1 59 | debs = debs(2:end); 60 | fins = fins(2:end); 61 | else 62 | debs = []; 63 | fins = []; 64 | end 65 | end 66 | if length(debs) > 0 67 | if fins(end) == m 68 | if length(debs) > 1 69 | debs = debs(1:(end-1)); 70 | fins = fins(1:(end-1)); 71 | 72 | else 73 | debs = []; 74 | fins = []; 75 | end 76 | end 77 | end 78 | lc = length(debs); 79 | if lc > 0 80 | for k = 1:lc 81 | if d(debs(k)-1) > 0 82 | if d(fins(k)) < 0 83 | imax = [imax round((fins(k)+debs(k))/2)]; 84 | end 85 | else 86 | if d(fins(k)) > 0 87 | imin = [imin round((fins(k)+debs(k))/2)]; 88 | end 89 | end 90 | end 91 | end 92 | 93 | if length(imax) > 0 94 | indmax = sort([indmax imax]); 95 | end 96 | 97 | if length(imin) > 0 98 | indmin = sort([indmin imin]); 99 | end 100 | 101 | end 102 | end 103 | -------------------------------------------------------------------------------- /EMDOS.tar/faux/init.m: -------------------------------------------------------------------------------- 1 | function [s,stop,alpha,maxmodes,t,method,orderder,liss,sporder] = init(varargin) 2 | %% INIT : internal function for the initialization of the parameters. 3 | 4 | 5 | s = varargin{1}; 6 | N = max(size((s))); 7 | if nargin == 2 8 | if isstruct(varargin{2}) 9 | inopts = varargin{2}; 10 | else 11 | error('when using 2 arguments the first one is the analyzed signal and the second one is a struct object describing the options') 12 | end 13 | elseif nargin > 2 14 | try 15 | inopts = struct(varargin{2:end}); 16 | catch 17 | error('bad argument syntax') 18 | end 19 | end 20 | 21 | % Paramètres par défaut 22 | defopts.stop = 'f'; 23 | defopts.alpha = 0.05; 24 | defopts.maxmodes = 8; 25 | defopts.t = 1:max(size(s)); 26 | defopts.method = 'os'; 27 | defopts.orderder=-1; 28 | defopts.liss = 0; 29 | defopts.sporder = 4; 30 | opt_fields = {'stop','alpha','maxmodes','t','method','orderder','liss','sporder'}; 31 | opts = defopts; 32 | 33 | if(nargin==1) 34 | inopts = defopts; 35 | elseif nargin == 0 36 | error('not enough arguments') 37 | end 38 | 39 | % Some checking 40 | names = fieldnames(inopts); 41 | for nom = names' 42 | if ~any(strcmpi(char(nom), opt_fields)) 43 | error(['bad option field name: ',char(nom)]) 44 | end 45 | % Et modification des paramètres rentrés 46 | if ~isempty(eval(['inopts.',char(nom)])) % empty values are discarded 47 | eval(['opts.',lower(char(nom)),' = inopts.',char(nom),';']) 48 | end 49 | end 50 | 51 | % Mise à jour 52 | stop = opts.stop; 53 | alpha = opts.alpha; 54 | maxmodes = opts.maxmodes; 55 | t = opts.t; 56 | method = opts.method; 57 | orderder = opts.orderder; 58 | liss = opts.liss; 59 | sporder = opts.sporder; 60 | 61 | %% Syntax check 62 | % s 63 | if ~isvector(s) 64 | error('The signal S must have only one row or one column') 65 | end 66 | if size(s,1) > 1 67 | s = s.'; 68 | end 69 | 70 | % t 71 | if ~isvector(t) 72 | error('option field T must have only one row or one column') 73 | end 74 | if ~isreal(t) 75 | error('time instants T must be a real vector') 76 | end 77 | if size(t,1) > 1 78 | t = t'; 79 | end 80 | if (length(t)~=length(s)) 81 | error('X and option field T must have the same length') 82 | end 83 | 84 | % orderder 85 | if max(size(orderder))>1 86 | if size(orderder,1)>1 87 | orderder=orderder'; 88 | end 89 | if size(orderder,2)1 99 | if size(liss,1)>1 100 | liss=liss'; 101 | end 102 | if size(liss,2) 2 14 | try 15 | inopts = struct(varargin{2:end}); 16 | catch 17 | error('bad argument syntax') 18 | end 19 | end 20 | 21 | % Paramètres par défaut 22 | defopts.stop = 'f'; 23 | defopts.alpha = 0.05; 24 | defopts.maxmodes = 8; 25 | defopts.t = 1:max(size(s)); 26 | defopts.method = 'os'; 27 | defopts.orderder=-1; 28 | defopts.liss = 0; 29 | defopts.sporder = 4; 30 | opt_fields = {'stop','alpha','maxmodes','t','method','orderder','liss','sporder'}; 31 | opts = defopts; 32 | 33 | if(nargin==1) 34 | inopts = defopts; 35 | elseif nargin == 0 36 | error('not enough arguments') 37 | end 38 | 39 | % Some checking 40 | names = fieldnames(inopts); 41 | for nom = names' 42 | if ~any(strcmpi(char(nom), opt_fields)) 43 | error(['bad option field name: ',char(nom)]) 44 | end 45 | % Et modification des paramètres rentrés 46 | if ~isempty(eval(['inopts.',char(nom)])) % empty values are discarded 47 | eval(['opts.',lower(char(nom)),' = inopts.',char(nom),';']) 48 | end 49 | end 50 | 51 | % Mise à jour 52 | stop = opts.stop; 53 | alpha = opts.alpha; 54 | maxmodes = opts.maxmodes; 55 | t = opts.t; 56 | method = opts.method; 57 | orderder = opts.orderder; 58 | liss = opts.liss; 59 | sporder = opts.sporder; 60 | 61 | %% Syntax check 62 | % s 63 | if ~isvector(s) 64 | error('The signal S must have only one row or one column') 65 | end 66 | if size(s,1) > 1 67 | s = s.'; 68 | end 69 | 70 | % t 71 | if ~isvector(t) 72 | error('option field T must have only one row or one column') 73 | end 74 | if ~isreal(t) 75 | error('time instants T must be a real vector') 76 | end 77 | if size(t,1) > 1 78 | t = t'; 79 | end 80 | if (length(t)~=length(s)) 81 | error('X and option field T must have the same length') 82 | end 83 | 84 | % orderder 85 | if max(size(orderder))>1 86 | if size(orderder,1)>1 87 | orderder=orderder'; 88 | end 89 | if size(orderder,2)1 99 | if size(liss,1)>1 100 | liss=liss'; 101 | end 102 | if size(liss,2) x(indmin(1)) 17 | lmax = fliplr(indmax(2:min(end,nbsym+1))); 18 | lmin = fliplr(indmin(1:min(end,nbsym))); 19 | lsym = indmax(1); 20 | else 21 | lmax = fliplr(indmax(1:min(end,nbsym))); 22 | lmin = [fliplr(indmin(1:min(end,nbsym-1))),1]; 23 | lsym = 1; 24 | end 25 | else 26 | 27 | if x(1) < x(indmax(1)) 28 | lmax = fliplr(indmax(1:min(end,nbsym))); 29 | lmin = fliplr(indmin(2:min(end,nbsym+1))); 30 | lsym = indmin(1); 31 | else 32 | lmax = [fliplr(indmax(1:min(end,nbsym-1))),1]; 33 | lmin = fliplr(indmin(1:min(end,nbsym))); 34 | lsym = 1; 35 | end 36 | end 37 | 38 | if indmax(end) < indmin(end) 39 | if x(end) < x(indmax(end)) 40 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 41 | rmin = fliplr(indmin(max(end-nbsym,1):end-1)); 42 | rsym = indmin(end); 43 | else 44 | rmax = [lx,fliplr(indmax(max(end-nbsym+2,1):end))]; 45 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 46 | rsym = lx; 47 | end 48 | else 49 | if x(end) > x(indmin(end)) 50 | rmax = fliplr(indmax(max(end-nbsym,1):end-1)); 51 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 52 | rsym = indmax(end); 53 | else 54 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 55 | rmin = [lx,fliplr(indmin(max(end-nbsym+2,1):end))]; 56 | rsym = lx; 57 | end 58 | end 59 | 60 | tlmin = 2*t(lsym)-t(lmin); 61 | tlmax = 2*t(lsym)-t(lmax); 62 | trmin = 2*t(rsym)-t(rmin); 63 | trmax = 2*t(rsym)-t(rmax); 64 | 65 | % in case symmetrized parts do not extend enough 66 | if tlmin(1) > t(1) || tlmax(1) > t(1) 67 | if lsym == indmax(1) 68 | lmax = fliplr(indmax(1:min(end,nbsym))); 69 | else 70 | lmin = fliplr(indmin(1:min(end,nbsym))); 71 | end 72 | if lsym == 1 73 | error('bug') 74 | end 75 | lsym = 1; 76 | tlmin = 2*t(lsym)-t(lmin); 77 | tlmax = 2*t(lsym)-t(lmax); 78 | end 79 | 80 | if trmin(end) < t(lx) || trmax(end) < t(lx) 81 | if rsym == indmax(end) 82 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 83 | else 84 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 85 | end 86 | if rsym == lx 87 | error('bug') 88 | end 89 | rsym = lx; 90 | trmin = 2*t(rsym)-t(rmin); 91 | trmax = 2*t(rsym)-t(rmax); 92 | end 93 | 94 | zlmax =z(lmax); 95 | zlmin =z(lmin); 96 | zrmax =z(rmax); 97 | zrmin =z(rmin); 98 | 99 | tmin = [tlmin t(indmin) trmin]; 100 | tmax = [tlmax t(indmax) trmax]; 101 | zmin = [zlmin z(indmin) zrmin]; 102 | zmax = [zlmax z(indmax) zrmax]; 103 | end -------------------------------------------------------------------------------- /minimalEMDOS/boundary_conditions.m: -------------------------------------------------------------------------------- 1 | %--------------------------------------------------------------------------------------- 2 | % defines new extrema points to extend the interpolations at the edges of the 3 | % signal (mainly mirror symmetry) 4 | % Written by Gabriel Rilling 5 | function [tmin,tmax,zmin,zmax] = boundary_conditions(indmin,indmax,t,x,z,nbsym) 6 | 7 | lx = length(x); 8 | 9 | if (length(indmin) + length(indmax) < 3) 10 | error('not enough extrema') 11 | end 12 | 13 | % boundary conditions for interpolations : 14 | 15 | if indmax(1) < indmin(1) 16 | if x(1) > x(indmin(1)) 17 | lmax = fliplr(indmax(2:min(end,nbsym+1))); 18 | lmin = fliplr(indmin(1:min(end,nbsym))); 19 | lsym = indmax(1); 20 | else 21 | lmax = fliplr(indmax(1:min(end,nbsym))); 22 | lmin = [fliplr(indmin(1:min(end,nbsym-1))),1]; 23 | lsym = 1; 24 | end 25 | else 26 | 27 | if x(1) < x(indmax(1)) 28 | lmax = fliplr(indmax(1:min(end,nbsym))); 29 | lmin = fliplr(indmin(2:min(end,nbsym+1))); 30 | lsym = indmin(1); 31 | else 32 | lmax = [fliplr(indmax(1:min(end,nbsym-1))),1]; 33 | lmin = fliplr(indmin(1:min(end,nbsym))); 34 | lsym = 1; 35 | end 36 | end 37 | 38 | if indmax(end) < indmin(end) 39 | if x(end) < x(indmax(end)) 40 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 41 | rmin = fliplr(indmin(max(end-nbsym,1):end-1)); 42 | rsym = indmin(end); 43 | else 44 | rmax = [lx,fliplr(indmax(max(end-nbsym+2,1):end))]; 45 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 46 | rsym = lx; 47 | end 48 | else 49 | if x(end) > x(indmin(end)) 50 | rmax = fliplr(indmax(max(end-nbsym,1):end-1)); 51 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 52 | rsym = indmax(end); 53 | else 54 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 55 | rmin = [lx,fliplr(indmin(max(end-nbsym+2,1):end))]; 56 | rsym = lx; 57 | end 58 | end 59 | 60 | tlmin = 2*t(lsym)-t(lmin); 61 | tlmax = 2*t(lsym)-t(lmax); 62 | trmin = 2*t(rsym)-t(rmin); 63 | trmax = 2*t(rsym)-t(rmax); 64 | 65 | % in case symmetrized parts do not extend enough 66 | if tlmin(1) > t(1) || tlmax(1) > t(1) 67 | if lsym == indmax(1) 68 | lmax = fliplr(indmax(1:min(end,nbsym))); 69 | else 70 | lmin = fliplr(indmin(1:min(end,nbsym))); 71 | end 72 | if lsym == 1 73 | error('bug') 74 | end 75 | lsym = 1; 76 | tlmin = 2*t(lsym)-t(lmin); 77 | tlmax = 2*t(lsym)-t(lmax); 78 | end 79 | 80 | if trmin(end) < t(lx) || trmax(end) < t(lx) 81 | if rsym == indmax(end) 82 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 83 | else 84 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 85 | end 86 | if rsym == lx 87 | error('bug') 88 | end 89 | rsym = lx; 90 | trmin = 2*t(rsym)-t(rmin); 91 | trmax = 2*t(rsym)-t(rmax); 92 | end 93 | 94 | zlmax =z(lmax); 95 | zlmin =z(lmin); 96 | zrmax =z(rmax); 97 | zrmin =z(rmin); 98 | 99 | tmin = [tlmin t(indmin) trmin]; 100 | tmax = [tlmax t(indmax) trmax]; 101 | zmin = [zlmin z(indmin) zrmin]; 102 | zmax = [zlmax z(indmax) zrmax]; 103 | end -------------------------------------------------------------------------------- /EMDOS.tar/figfun/sensitiv_spo.m: -------------------------------------------------------------------------------- 1 | function sensitiv_spo() 2 | % Test function, creates figure 4 of [1]. 3 | 4 | 5 | N = 4*1024; 6 | [s s1 s2 s3] = gen_tests(); 7 | 8 | 9 | figure(1);hold on; 10 | figure(2);hold on; 11 | 12 | p_alpha = [0.02:0.003:0.06 0.06:0.01:0.2]; 13 | index = N/4+1:3*N/4; % remove boundary effects 14 | 15 | 16 | %% EMD 17 | 18 | % General options 19 | opt.maxmodes = 1; 20 | opt.sporder = 8; 21 | opt.alpha= 0.2; 22 | opt.stop = 'f'; 23 | opt.orderder = [0 0 0]; % Selection derivee 24 | opt.method = 'emdni'; 25 | opt.liss = [0]; 26 | 27 | 28 | 29 | err = zeros(size(p_alpha)); 30 | errp = zeros(size(p_alpha)); 31 | oldimf=zeros(1,N); 32 | for j=1:length(p_alpha) 33 | opt.alpha = p_alpha(j); 34 | [imf,ort,nder] = emdos(s,opt); 35 | errp(j) = norm(oldimf(1,index)-imf(1,index)); 36 | oldimf = imf; 37 | err(j) = norm(s1(index)-imf(1,index)); 38 | end 39 | 40 | figure(1); 41 | plot(p_alpha,log(err*(1/norm(s))),'ko--'); 42 | figure(2); 43 | plot(p_alpha(2:end),1/sqrt(N/2)*(errp(2:end)./diff(p_alpha)),'ko--'); 44 | 45 | 46 | 47 | %% EMDOS 48 | 49 | % General options 50 | opt.maxmodes = 1; 51 | opt.sporder = 8; 52 | opt.alpha= 0.06; 53 | opt.stop = 'f'; 54 | opt.orderder = [2 0 0]; % Selection derivee 55 | opt.method = 'os'; 56 | opt.liss = [0]; 57 | 58 | 59 | 60 | err6 = zeros(size(p_alpha)); 61 | errp6 = zeros(size(p_alpha)); 62 | err8 = zeros(size(p_alpha)); 63 | errp8 = zeros(size(p_alpha)); 64 | err10 = zeros(size(p_alpha)); 65 | errp10 = zeros(size(p_alpha)); 66 | oldimf6=zeros(1,N); 67 | oldimf8=zeros(1,N); 68 | oldimf10=zeros(1,N); 69 | for j=1:length(p_alpha) 70 | opt.alpha = p_alpha(j); 71 | % k=6 72 | opt.sporder=6; 73 | [imf,ort,nder] = emdos(s,opt); 74 | errp6(j) = norm(oldimf6(1,index)-imf(1,index)); 75 | oldimf6 = imf; 76 | err6(j) = norm(s1(index)-imf(1,index)); 77 | % k=8 78 | opt.sporder=8; 79 | [imf,ort,nder] = emdos(s,opt); 80 | errp8(j) = norm(oldimf8(1,index)-imf(1,index)); 81 | oldimf8 = imf; 82 | err8(j) = norm(s1(index)-imf(1,index)); 83 | % k=6 84 | opt.sporder=10; 85 | [imf,ort,nder] = emdos(s,opt); 86 | errp10(j) = norm(oldimf10(1,index)-imf(1,index)); 87 | oldimf10 = imf; 88 | err10(j) = norm(s1(index)-imf(1,index)); 89 | end 90 | 91 | figure(1); 92 | plot(p_alpha,log(err6/norm(s)),'rs-'); 93 | plot(p_alpha,log(err8/norm(s)),'b*-'); 94 | plot(p_alpha,log(err10/norm(s)),'g+-'); 95 | figure(2); 96 | plot(p_alpha(2:end),1/sqrt(N/2)*(errp6(2:end)./diff(p_alpha)),'rs-'); 97 | plot(p_alpha(2:end),1/sqrt(N/2)*(errp8(2:end)./diff(p_alpha)),'b*-'); 98 | plot(p_alpha(2:end),1/sqrt(N/2)*(errp10(2:end)./diff(p_alpha)),'g+-'); 99 | 100 | 101 | 102 | 103 | %% MISE EN FORME 104 | figure(1); 105 | legend('EMD','OS k=6','OS k=8','OS k=10'); 106 | xlabel('\alpha'); 107 | set(findall(0,'type','text'),'FontSize',15); 108 | set(gca,'FontSize',15); 109 | set(findall(0,'type','line'),'linewidth',2); 110 | 111 | figure(2); 112 | legend('EMD','OS k=6','OS k=8','OS k=10'); 113 | xlabel('\alpha'); 114 | set(findall(0,'type','text'),'FontSize',15); 115 | set(gca,'FontSize',15); 116 | set(findall(0,'type','line'),'linewidth',2); 117 | 118 | 119 | end -------------------------------------------------------------------------------- /miniEMD/memd_boundary_conditions.m: -------------------------------------------------------------------------------- 1 | %------------------------------------------------------------------------- 2 | % defines new extrema points to extend the interpolations at the edges of 3 | % the signal (mainly mirror symmetry) 4 | % Written by Gabriel Rilling 5 | % Modified by D. Gibson 6 | function [tmin,tmax,zmin,zmax] = memd_boundary_conditions(indmin,indmax,t,x,z,nbsym) 7 | tmin = NaN; 8 | tmax = NaN; 9 | zmin = NaN; 10 | zmax = NaN; 11 | 12 | lx = length(x); 13 | 14 | if (length(indmin) + length(indmax) < 3) 15 | warning('not enough extrema') 16 | return 17 | end 18 | 19 | % boundary conditions for interpolations : 20 | 21 | if indmax(1) < indmin(1) 22 | if x(1) > x(indmin(1)) 23 | lmax = fliplr(indmax(2:min(end,nbsym+1))); 24 | lmin = fliplr(indmin(1:min(end,nbsym))); 25 | lsym = indmax(1); 26 | else 27 | lmax = fliplr(indmax(1:min(end,nbsym))); 28 | lmin = [fliplr(indmin(1:min(end,nbsym-1))),1]; 29 | lsym = 1; 30 | end 31 | else 32 | 33 | if x(1) < x(indmax(1)) 34 | lmax = fliplr(indmax(1:min(end,nbsym))); 35 | lmin = fliplr(indmin(2:min(end,nbsym+1))); 36 | lsym = indmin(1); 37 | else 38 | lmax = [fliplr(indmax(1:min(end,nbsym-1))),1]; 39 | lmin = fliplr(indmin(1:min(end,nbsym))); 40 | lsym = 1; 41 | end 42 | end 43 | 44 | if indmax(end) < indmin(end) 45 | if x(end) < x(indmax(end)) 46 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 47 | rmin = fliplr(indmin(max(end-nbsym,1):end-1)); 48 | rsym = indmin(end); 49 | else 50 | rmax = [lx,fliplr(indmax(max(end-nbsym+2,1):end))]; 51 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 52 | rsym = lx; 53 | end 54 | else 55 | if x(end) > x(indmin(end)) 56 | rmax = fliplr(indmax(max(end-nbsym,1):end-1)); 57 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 58 | rsym = indmax(end); 59 | else 60 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 61 | rmin = [lx,fliplr(indmin(max(end-nbsym+2,1):end))]; 62 | rsym = lx; 63 | end 64 | end 65 | 66 | tlmin = 2*t(lsym)-t(lmin); 67 | tlmax = 2*t(lsym)-t(lmax); 68 | trmin = 2*t(rsym)-t(rmin); 69 | trmax = 2*t(rsym)-t(rmax); 70 | 71 | % in case symmetrized parts do not extend enough 72 | if tlmin(1) > t(1) || tlmax(1) > t(1) 73 | if lsym == indmax(1) 74 | lmax = fliplr(indmax(1:min(end,nbsym))); 75 | else 76 | lmin = fliplr(indmin(1:min(end,nbsym))); 77 | end 78 | if lsym == 1 79 | error('bug') 80 | end 81 | lsym = 1; 82 | tlmin = 2*t(lsym)-t(lmin); 83 | tlmax = 2*t(lsym)-t(lmax); 84 | end 85 | 86 | if trmin(end) < t(lx) || trmax(end) < t(lx) 87 | if rsym == indmax(end) 88 | rmax = fliplr(indmax(max(end-nbsym+1,1):end)); 89 | else 90 | rmin = fliplr(indmin(max(end-nbsym+1,1):end)); 91 | end 92 | if rsym == lx 93 | error('bug') 94 | end 95 | rsym = lx; 96 | trmin = 2*t(rsym)-t(rmin); 97 | trmax = 2*t(rsym)-t(rmax); 98 | end 99 | 100 | zlmax =z(lmax); 101 | zlmin =z(lmin); 102 | zrmax =z(rmax); 103 | zrmin =z(rmin); 104 | 105 | tmin = [tlmin t(indmin) trmin]; 106 | tmax = [tlmax t(indmax) trmax]; 107 | zmin = [zlmin z(indmin) zrmin]; 108 | zmax = [zlmax z(indmax) zrmax]; 109 | end -------------------------------------------------------------------------------- /EMDOS.tar/faux/build_m0.m: -------------------------------------------------------------------------------- 1 | function [indexC tau m0 m0eval aux] = build_m0(r,t,ordersel,sporder) 2 | %% build_h0 : builds the approximation m0 of the local mean of the signal s 3 | % INPUTS : 4 | % r : signal 5 | % t : time 6 | % ordersel : manual selection of derivation order 7 | % sporder : order of the splines 8 | % OUTPUTS : 9 | % indexC : index of the extrema estimates 10 | % tau : points of the subdivision 11 | % m0 : estimation of the local mean : coefficients of the B-spline on tau 12 | % m0eval : the B-spline evaluated on t 13 | % aux : effective order of derivation 14 | % 15 | % Thomas Oberlin 16 | % 12.2011 17 | % thomas.oberlin@imag.fr 18 | 19 | s=r; 20 | 21 | %% Estimation of extrema of s 22 | sder2 = madiff(s,t,2); 23 | sder4 = madiff(s,t,4); 24 | switch(ordersel) 25 | case -1 26 | seuil=8; 27 | % Automative selection (see [1]) 28 | [indmin, indmax, indzer] = extr(s,t); 29 | ls0 = length(indmin) + length(indmax); 30 | [indmin, indmax, indzer] = extr(sder2); 31 | ls2 = length(indmin) + length(indmax); 32 | [indmin, indmax, indzer] = extr(sder4); 33 | ls4 = length(indmin) + length(indmax); 34 | if ls2<=ls0+seuil 35 | [indmin, indmax, indzer] = extr(s,t); 36 | aux = 0; 37 | elseif ls4 <=ls2+seuil 38 | [indmin, indmax, indzer] = extr(sder2,t); 39 | aux = 2; 40 | else 41 | [indmin, indmax, indzer] = extr(sder4,t); 42 | aux = 4; 43 | end 44 | % Case discretization pb 45 | if max(ls0,max(ls2,ls4))>=length(s)/4 46 | warning('Pb de discretisation dans le calcul de la derivee'); 47 | % Case not C2 48 | if ls2<=ls0+seuil || ls2>=length(s)/4 49 | [indmin, indmax, indzer] = extr(s,t); 50 | aux = 0; 51 | else 52 | [indmin, indmax, indzer] = extr(sder2,t); 53 | aux = 2; 54 | end 55 | end 56 | 57 | case 0 58 | % Order 0 59 | [indmin, indmax, indzer] = extr(s); 60 | aux = 0; 61 | case 2 62 | % Order 2 63 | [indmin, indmax, indzer] = extr(sder2); 64 | aux = 2; 65 | case 4 66 | % Order 4 67 | [indmin, indmax, indzer] = extr(sder4); 68 | aux = 4; 69 | otherwise 70 | error('The ordersel field must be 0, 2 or 4'); 71 | end 72 | 73 | 74 | %% Index of extremas 75 | s=r; 76 | indexC = sort([indmin indmax]); 77 | 78 | L = length(indexC); 79 | 80 | % Hong formula 81 | xbar = zeros(1,L+1); 82 | tbar = zeros(1,L+1); 83 | for i=1:L-1 84 | xbar(i+1) = mean(s(indexC(i):indexC(i+1))); 85 | tbar(i+1) = mean(t(indexC(i):indexC(i+1)) .* ((s(indexC(i):indexC(i+1)) -xbar(i+1)).^2))/... 86 | mean((s(indexC(i):indexC(i+1)) -xbar(i+1)).^2); 87 | end 88 | 89 | % Symmetry 90 | tbar(1) = 2*t(1)-tbar(2); 91 | xbar(1) = xbar(2); 92 | tbar(L+1) = 2*t(end)-tbar(L); 93 | xbar(L+1) = xbar(L); 94 | 95 | 96 | tau = tbar; 97 | knots = aptknt(tau,sporder); 98 | m0sp = spapi(knots,tau,[xbar(1) 0 xbar(3:end-1) 0]); 99 | m0eval = fnval(m0sp,t); 100 | m0 = m0sp.coefs; 101 | 102 | end 103 | 104 | -------------------------------------------------------------------------------- /EMDOS.tar/faux/build_m0.m~: -------------------------------------------------------------------------------- 1 | function [indexC tau m0 m0eval aux] = build_m0(r,t,ordersel,sporder) 2 | %% build_h0 : builds the approximation m0 of the local mean of the signal s 3 | % INPUTS : 4 | % r : signal 5 | % t : time 6 | % ordersel : manual selection of derivation order 7 | % sporder : order of the splines 8 | % OUTPUTS : 9 | % indexC : index of the extrema estimates 10 | % tau : points of the subdivision 11 | % m0 : estimation of the local mean : coefficients of the B-spline on tau 12 | % m0eval : the B-spline evaluated on t 13 | % aux : effective order of derivation 14 | % 15 | % Thomas Oberlin 16 | % 12.2011 17 | % thomas.oberlin@imag.fr 18 | 19 | s=r; 20 | 21 | %% Estimation of extrema of s 22 | sder2 = madiff(s,t,2); 23 | sder4 = madiff(s,t,4); 24 | switch(ordersel) 25 | case -1 26 | seuil=8; 27 | % Automative selection (see [1]) 28 | [indmin, indmax, indzer] = extr(s,t); 29 | ls0 = length(indmin) + length(indmax); 30 | [indmin, indmax, indzer] = extr(sder2); 31 | ls2 = length(indmin) + length(indmax); 32 | [indmin, indmax, indzer] = extr(sder4); 33 | ls4 = length(indmin) + length(indmax); 34 | if ls2<=ls0+seuil 35 | [indmin, indmax, indzer] = extr(s,t); 36 | aux = 0; 37 | elseif ls4 <=ls2+seuil 38 | [indmin, indmax, indzer] = extr(sder2,t); 39 | aux = 2; 40 | else 41 | [indmin, indmax, indzer] = extr(sder4,t); 42 | aux = 4; 43 | end 44 | % Case discretization pb 45 | if max(ls0,max(ls2,ls4))>=length(s)/4 46 | warning('Pb de discretisation dans le calcul de la derivee'); 47 | % Case not C2 48 | if ls2<=ls0+seuil || ls2>=length(s)/4 49 | [indmin, indmax, indzer] = extr(s,t); 50 | aux = 0; 51 | else 52 | [indmin, indmax, indzer] = extr(sder2,t); 53 | aux = 2; 54 | end 55 | end 56 | 57 | case 0 58 | % Order 0 59 | [indmin, indmax, indzer] = extr(s); 60 | aux = 0; 61 | case 2 62 | % Order 2 63 | [indmin, indmax, indzer] = extr(sder2); 64 | aux = 2; 65 | case 4 66 | % Order 4 67 | [indmin, indmax, indzer] = extr(sder4); 68 | aux = 4; 69 | otherwise 70 | error('The ordersel field must be 0, 2 or 4'); 71 | end 72 | 73 | 74 | %% Index of extremas 75 | s=r; 76 | indexC = sort([indmin indmax]); 77 | 78 | L = length(indexC); 79 | 80 | % Hong formula 81 | xbar = zeros(1,L+1); 82 | tbar = zeros(1,L+1); 83 | for i=1:L-1 84 | xbar(i+1) = mean(s(indexC(i):indexC(i+1))); 85 | tbar(i+1) = mean(t(indexC(i):indexC(i+1)) .* ((s(indexC(i):indexC(i+1)) -xbar(i+1)).^2))/... 86 | mean((s(indexC(i):indexC(i+1)) -xbar(i+1)).^2); 87 | end 88 | 89 | % Symmetry 90 | tbar(1) = 2*t(1)-tbar(2); 91 | xbar(1) = xbar(2); 92 | tbar(L+1) = 2*t(end)-tbar(L); 93 | xbar(L+1) = xbar(L); 94 | 95 | 96 | tau = tbar; 97 | knots = aptknt(tau,sporder); 98 | m0sp = spapi(knots,[tau(1) tau tau(end)],[xbar(1) 0 xbar(2 0]); 99 | m0eval = fnval(m0sp,t); 100 | m0 = m0sp.coefs; 101 | 102 | end 103 | 104 | -------------------------------------------------------------------------------- /dg_AMFMdecomp.m: -------------------------------------------------------------------------------- 1 | function [A, F] = dg_AMFMdecomp(imf, method, verboseflag) 2 | %[A, F] = dg_AMFMdecomp(imf) Norden Huang's empirical AM-FM decomposition 3 | % as described in Huang et al 4 | %INPUTS 5 | % imf: a row vector containing an individual IMF, as defined by Liang et al 6 | % method: optional param, see Matlab 'interp1' function. Default = 7 | % 'spline'. 8 | 9 | %REFERENCES 10 | % 11 | % Liang H, Bressler SL, Buffalo EA, Desimone R, and Fries P, "Empirical 12 | % mode decomposition of field potentials from macaque V4 in visual spatial 13 | % attention", Biol. Cybern. (2005) 92: 380-392 14 | % 15 | % Huang NE, Wu Z, Long SR, Arnold KC, Chen X, Blank K, "On Instaneous 16 | % Frequency", Advances in Adaptive Data Analysis Vol. 1, No. 2 (2009) 17 | % 177?229 18 | 19 | if nargin < 2 20 | method = 'spline'; 21 | end 22 | if nargin < 3 23 | verboseflag = false; 24 | end 25 | 26 | pospeaks = dg_findpks(imf); 27 | negpeaks = dg_findpks(-imf); 28 | peaks = union(pospeaks, negpeaks); 29 | if isempty(peaks) 30 | error('dg_AMFMdecomp:nopeaks', ... 31 | 'There are no extrema in the IMF'); 32 | end 33 | if abs(imf(1)) > abs(imf(peaks(1))) 34 | startval = abs(imf(1)); 35 | else 36 | startval = abs(imf(peaks(1))); 37 | end 38 | if abs(imf(end)) > abs(imf(peaks(end))) 39 | endval = abs(imf(end)); 40 | else 41 | endval = abs(imf(peaks(end))); 42 | end 43 | A = interp1([0 peaks length(imf)+1], ... 44 | [startval abs(imf(peaks)) endval], ... 45 | 1:length(imf), method); 46 | F = imf ./ A; 47 | loopcnt = 1; 48 | newpospeaks = dg_findpks(F); 49 | newnegpeaks = dg_findpks(-F); 50 | if ~isequal(newpospeaks, pospeaks) || ~isequal(newnegpeaks, negpeaks) 51 | posadded = setdiff(newpospeaks, pospeaks); 52 | poslost = setdiff(pospeaks, newpospeaks); 53 | negadded = setdiff(newnegpeaks, negpeaks); 54 | neglost = setdiff(negpeaks, newnegpeaks); 55 | pospeaks = newpospeaks; 56 | negpeaks = newnegpeaks; 57 | peaks = union(pospeaks, negpeaks); 58 | end 59 | while any(abs(abs(F(peaks)) - 1) > eps) 60 | % If values at start or end are causing trouble, include them in the 61 | % new envelope; otherwise, propagate the values from the nearest peak. 62 | if abs(F(1)) > abs(F(peaks(1))) 63 | startval = abs(F(1)); 64 | else 65 | startval = abs(F(peaks(1))); 66 | end 67 | if abs(F(end)) > abs(F(peaks(end))) 68 | endval = abs(F(end)); 69 | else 70 | endval = abs(F(peaks(end))); 71 | end 72 | e = interp1([1 peaks length(F)+1], ... 73 | [startval abs(F(peaks)) endval], 1:length(F), method); 74 | F = F ./ e; 75 | A = A .* e; 76 | newpospeaks = dg_findpks(F); 77 | newnegpeaks = dg_findpks(-F); 78 | if ~isequal(newpospeaks, pospeaks) || ~isequal(newnegpeaks, negpeaks) 79 | pospeaks = newpospeaks; 80 | negpeaks = newnegpeaks; 81 | peaks = union(pospeaks, negpeaks); 82 | end 83 | peakvals{loopcnt} = reshape(F(peaks), 1, []); 84 | loopcnt = loopcnt + 1; 85 | if loopcnt > 100 86 | warning('dg_AMFMdecomp:loopcnt', ... 87 | 'Quitting due to loopcnt = %d', loopcnt); 88 | return 89 | end 90 | end 91 | if verboseflag 92 | fprintf('normal termination of dg_AMFMdecomp after %d iterations\n', ... 93 | loopcnt); 94 | end 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /eemdRF.m: -------------------------------------------------------------------------------- 1 | function [modos its]=eemd(x,Nstd,NR,MaxIter) 2 | %-------------------------------------------------------------------------- 3 | %WARNING: this code needs to include in the same 4 | %directoy the file emd.m developed by Rilling and Flandrin. 5 | %This file is available at %http://perso.ens-lyon.fr/patrick.flandrin/emd.html 6 | %We use the default stopping criterion. 7 | %We use the last modification: 3.2007 8 | % ------------------------------------------------------------------------- 9 | % OUTPUT 10 | % modos: contain the obtained modes in a matrix with the rows being the modes 11 | % its: contain the iterations needed for each mode for each realization 12 | % 13 | % INPUT 14 | % x: signal to decompose 15 | % Nstd: noise standard deviation 16 | % NR: number of realizations 17 | % MaxIter: maximum number of sifting iterations allowed. 18 | % ------------------------------------------------------------------------- 19 | % Syntax 20 | % 21 | % modos=eemd(x,Nstd,NR,MaxIter) 22 | % [modos its]=eemd(x,Nstd,NR,MaxIter) 23 | % ------------------------------------------------------------------------- 24 | % NOTE: if Nstd=0 and NR=1, the EMD decomposition is obtained. 25 | % ------------------------------------------------------------------------- 26 | % EEMD was introduced in 27 | % Wu Z. and Huang N. 28 | % "Ensemble Empirical Mode Decomposition: A noise-assisted data analysis method". 29 | % Advances in Adaptive Data Analysis. vol 1. pp 1-41, 2009. 30 | %-------------------------------------------------------------------------- 31 | % The present EEMD implementation was used in 32 | % M.E.TORRES, M.A. COLOMINAS, G. SCHLOTTHAUER, P. FLANDRIN, 33 | % "A complete Ensemble Empirical Mode decomposition with adaptive noise," 34 | % IEEE Int. Conf. on Acoust., Speech and Signal Proc. ICASSP-11, pp. 4144-4147, Prague (CZ) 35 | % 36 | % in order to compare the performance of the new method CEEMDAN with the performance of the EEMD. 37 | % 38 | % ------------------------------------------------------------------------- 39 | % Date: June 06,2011 40 | % Authors: Torres ME, Colominas MA, Schlotthauer G, Flandrin P. 41 | % For problems with the code, please contact the authors: 42 | % To: macolominas(AT)bioingenieria.edu.ar 43 | % CC: metorres(AT)santafe-conicet.gov.ar 44 | % ------------------------------------------------------------------------- 45 | % This version was run on Matlab 7.10.0 (R2010a) 46 | %-------------------------------------------------------------------------- 47 | 48 | desvio_estandar=std(x); 49 | x=x/desvio_estandar; 50 | xconruido=x+Nstd*randn(size(x)); 51 | [modos, o, it]=emd(xconruido,'MAXITERATIONS',MaxIter); 52 | modos=modos/NR; 53 | iter=it; 54 | if NR>=2 55 | for i=2:NR 56 | xconruido=x+Nstd*randn(size(x)); 57 | [temp, ort, it]=emd(xconruido,'MAXITERATIONS',MaxIter); 58 | temp=temp/NR; 59 | lit=length(it); 60 | [p liter]=size(iter); 61 | if litalto 74 | modos=[modos; zeros(abs(diferencia),ancho)]; 75 | end; 76 | if alto>filas 77 | temp=[temp;zeros(abs(diferencia),ancho)]; 78 | end; 79 | 80 | modos=modos+temp; 81 | end; 82 | end; 83 | its=iter; 84 | modos=modos*desvio_estandar; -------------------------------------------------------------------------------- /HuangEMD/FAcosfor.m: -------------------------------------------------------------------------------- 1 | %function [f,a] = FAcosfor(data, dt) 2 | % 3 | % The function FAcosfor generates a frequency and amplitude 4 | % of previously normalized data(n,k) using cosine formula method, 5 | % where n is the length of the time series and k is the number of IMF components. 6 | % 7 | % FAcosfor finds the frequency by applying 8 | % the cosine-angle-sum identity to the normalized data. 9 | % The special point of this method is "no trigonometric function value is calculated" 10 | % the sample rate of the data must be high enough---this is the assumption 11 | % 12 | % Calling sequence- 13 | % [f,a] = FAcosfor(data, dt) 14 | % 15 | % Input- 16 | % data - 2-D matrix of IMF components 17 | % dt - time increment per point 18 | % Output- 19 | % f - 2-D matrix f(n,k) that specifies frequency 20 | % a - 2-D matrix a(n,k) that specifies amplitude 21 | %Note: 22 | % Used by- 23 | % FA 24 | %Reference: 25 | % 26 | %written by 27 | % Zhaohua Wu (NCU RCADA) Summer 2007, Initial 28 | % adapted by S.C.Su and Men-Tzung Lo 29 | %footnote: S.C.Su (2009/09/12) 30 | % 31 | %1.read the data,check input matrix 32 | %2.Do the normalization for an IMF ---loop A start 33 | % 3.compute the phase angle by arc-cosine function 34 | % 4.take difference for the phase angle 35 | %2.Do the normalization for an IMF ---loop A end 36 | % 37 | 38 | function [f,a] = FAcosfor(data, dt) 39 | 40 | disp('WARNING: Sample rate must high enough for this method.'); 41 | 42 | %1.read the data,check input matrix 43 | %----- Get dimensions 44 | [npts,nimf] = size(data); 45 | 46 | %----- Flip data if necessary 47 | flipped=0; 48 | if (npts < nimf) 49 | flipped=1; 50 | data=data'; 51 | [npts,nimf] = size(data); 52 | end 53 | 54 | %----- Input is normalized, so assume that amplitude is always 1 55 | a = ones(npts,nimf); 56 | 57 | 58 | %2.Do the calculation for an IMF ---loop A start 59 | %----- Process each IMF 60 | for c=1:nimf 61 | 62 | %3.compute the phase angle by cosine formula method 63 | rangetop=0.90; 64 | rangebot=-rangetop; 65 | for i=2:npts-1 66 | if data(i,c)>rangebot & data(i,c) < rangetop 67 | omgcos(i)=abs(data(i+1,c)-data(i-1,c))*0.5/sqrt(1-data(i,c).*data(i,c)); 68 | else 69 | omgcos(i)=-9999; 70 | end 71 | end 72 | omgcos(1)=-9999; 73 | omgcos(npts)=-9999; 74 | 75 | %9.remove those marked outliner 76 | jj=1; 77 | for i=1:npts 78 | if omgcos(i)>-1000 79 | ddd(jj)=i; 80 | temp(jj)=omgcos(i); 81 | jj=jj+1; 82 | end 83 | end 84 | 85 | %10.use cubic spline to smooth the instantaneous frequency values 86 | dd=1:1:npts; 87 | temp2=spline(ddd,temp,dd); 88 | omgcos=temp2; 89 | clear ddd temp dd temp2 90 | %clear syntax is important for this kind of algorithm-S.C.Su 91 | %choose some value with out clear makes chaos 92 | 93 | %11.return the values back 94 | for i=1:npts, 95 | omega(i)=omgcos(i); 96 | end 97 | 98 | pi2=pi*2; 99 | f(:,c)=omega/dt/pi2; 100 | 101 | %2.Do the normalization for an IMF ---loop A end 102 | end 103 | 104 | %----- Flip again if data was flipped at the beginning 105 | if (flipped) 106 | f=f'; 107 | a=a'; 108 | end -------------------------------------------------------------------------------- /EMDOS.tar/figfun/separTest.m: -------------------------------------------------------------------------------- 1 | function separTest() 2 | %% separTest : test of separation power, comparison with the original EMD 3 | 4 | %% Paramètres 5 | N = 1048*8; 6 | T = 32; 7 | t = linspace(0,T,N); 8 | s1 = sin(2*pi*t); 9 | intval = N/4:3*N/4; % remove border effects 10 | 11 | % General options 12 | opt.t = t; 13 | opt.MAXMODES = 1; 14 | opt.alpha= 0.01; 15 | opt.sporder=10; 16 | opt.orderder = -1; 17 | 18 | f = 0.01:0.01:0.99; 19 | %f = 0.01:0.1:0.99; 20 | P = length(f); 21 | 22 | a = 10.^(-2:0.025:2); 23 | %a = 10.^(-2:0.5:-0.5); 24 | M = length(a); 25 | 26 | ordOS = zeros(P,M); % OS 27 | ordEMD = zeros(P,M); % EMD 28 | ordEMDNI = zeros(P,M); % EMDNI 29 | 30 | 31 | for i=1:P 32 | for j=1:M 33 | s2 = a(j)*sin(2*pi*f(i)*t); 34 | %s2 = 0.2*sin(2*pi*0.3*t); 35 | s = s1+s2; 36 | 37 | % OS 38 | opt.method='os'; 39 | imf = emdos(s,opt); 40 | imf1=imf(1,:); 41 | ordOS(i,j) = norm(imf1(intval)-s1(intval))/norm(s2(intval)); 42 | %figure();plot_imf(imf,t,'');title('os'); disp(ordOS(i,j)); 43 | 44 | % EMDNI 45 | opt.method='emdni'; 46 | imf = emdos(s,opt); 47 | imf1=imf(1,:); 48 | ordEMDNI(i,j) = norm(imf1(intval)-s1(intval))/norm(s2(intval)); 49 | 50 | % EMD 51 | opt.method='emd'; 52 | imf = emdos(s,opt); 53 | imf1=imf(1,:); 54 | ordEMD(i,j) = norm(imf1(intval)-s1(intval))/norm(s2(intval)); 55 | %figure();plot_imf(imf,t,'');disp(ordOS(i,j));title('emd'); 56 | 57 | end 58 | end 59 | 60 | xx = -2:0.05:2; 61 | 62 | figure(); 63 | imagesc(log(a)/log(10),f,ordOS); 64 | set(gca,'YDir','normal') 65 | colormap('gray'); 66 | xlabel('a (log_{10})'); 67 | ylabel('f'); 68 | zlabel('erreur normalisee (log)'); 69 | title('Separation power, OS, automatic selection of derivation order'); 70 | h=findall(gcf,'type','line');delete(h);h=findall(0,'type','legend');delete(h);set(gca,'CLim',[0 1.1]);hold on;plot(xx,10.^(-1/1*xx),'color',0.9*ones(1,3),'LineWidth',2);ylim([0 1]);xlim([-2 2]);plot(xx,10.^(-1/2*xx),'color',0.7*ones(1,3),'LineWidth',2);plot(xx,10.^(-1/3*xx),'color',0.4*ones(1,3),'LineWidth',2);plot(xx,10.^(-1/4*xx),'color',0.1*ones(1,3),'LineWidth',2);legend('af=1','af^2=1','af^3=1','af^4=1'); 71 | colorbar; 72 | 73 | figure(); 74 | imagesc(log(a)/log(10),f,ordEMD); 75 | set(gca,'YDir','normal') 76 | colormap('gray'); 77 | xlabel('a (log_{10})'); 78 | ylabel('f'); 79 | zlabel('erreur normalisee (log)'); 80 | title('Separation power, Standard EMD with stopping criteria'); 81 | h=findall(gcf,'type','line');delete(h);h=findall(0,'type','legend');delete(h);set(gca,'CLim',[0 1.1]);hold on;plot(xx,10.^(-1/1*xx),'color',0.9*ones(1,3),'LineWidth',2);ylim([0 1]);xlim([-2 2]);plot(xx,10.^(-1/2*xx),'color',0.7*ones(1,3),'LineWidth',2);plot(xx,10.^(-1/3*xx),'color',0.4*ones(1,3),'LineWidth',2);plot(xx,10.^(-1/4*xx),'color',0.1*ones(1,3),'LineWidth',2);legend('af=1','af^2=1','af^3=1','af^4=1'); 82 | colorbar; 83 | 84 | figure(); 85 | imagesc(log(a)/log(10),f,ordEMDNI); 86 | set(gca,'YDir','normal') 87 | colormap('gray'); 88 | xlabel('a (log_{10})'); 89 | ylabel('f'); 90 | zlabel('erreur normalisee (log)'); 91 | title('Separation power, EMD-NI'); 92 | h=findall(gcf,'type','line');delete(h);h=findall(0,'type','legend');delete(h);set(gca,'CLim',[0 1.1]);hold on;plot(xx,10.^(-1/1*xx),'color',0.9*ones(1,3),'LineWidth',2);ylim([0 1]);xlim([-2 2]);plot(xx,10.^(-1/2*xx),'color',0.7*ones(1,3),'LineWidth',2);plot(xx,10.^(-1/3*xx),'color',0.4*ones(1,3),'LineWidth',2);plot(xx,10.^(-1/4*xx),'color',0.1*ones(1,3),'LineWidth',2);legend('af=1','af^2=1','af^3=1','af^4=1'); 93 | colorbar; 94 | 95 | 96 | end -------------------------------------------------------------------------------- /HuangEMD/findcriticalpoints.m: -------------------------------------------------------------------------------- 1 | %function [allx, ally] = findcriticalpoints(datay) 2 | % 3 | % 4 | % Input- 5 | % datay - input vector of values 6 | % Output- 7 | % allx - vector that specifies the coordinates of max, min 8 | % and zero crossing values in the order found 9 | % ally - corresponding max, min and zero values 10 | % 11 | %Note: 12 | % The function FINDCRITICALPOINTS returns max, min and zero crossing values 13 | % and their coordinates in the order found in datay(n), 14 | % where n specifies the dimension of array datay. 15 | % The value datay(i) is considered to have a local minimum in i, 16 | % if datay(i-1) > datay(i) < datay(i+1); 17 | % The value datay(i) is considered to have a local maximum in i, 18 | % if datay(i-1) < datay(i) > datay(i+1); 19 | % The value datay(i) is considered to have a zero crossing, if 20 | % datay(i) and datay(i+1) have different signs. The coordinate 21 | % of datay(i) is linearly interpolated. 22 | % 23 | % 24 | %Reference: 25 | % 26 | % 27 | % code writer:Karin Blank (NASA GSFC) March 26, 2003 Initial 28 | % Jelena Marshak (NASA GSFC) July 16, 2004 Modified 29 | % (Replaced '&&' by '&' in zero-crossing calculation section.) 30 | % (Corrected zero-crossing calculation.) 31 | % footnote:S.C.Su 2009/05/14 32 | % 33 | %1.checking point by point----loop start 34 | % ----- Find local maximum 35 | % ----- Find local minimum 36 | % ----- Find zero crossings 37 | %1.checking point by point----loop end% 38 | % 39 | % Association: 40 | % 41 | % Concerned function: 42 | % 43 | % Notes- 44 | % Used by FAZ, FAZPLUS 45 | % 46 | % Calling sequence- 47 | 48 | function [allx, ally] = findcriticalpoints(datay): 49 | 50 | %----- Initialize values 51 | allx = []; 52 | ally = []; 53 | Ymax = 0; 54 | Ymin = 0; 55 | Xmax = 0; 56 | Xmin = 0; 57 | flagmax = 0; 58 | flagmin = 0; 59 | 60 | %1.checking point by point----loop start 61 | for i=2:length(datay)-1; 62 | %----- Find local maximum 63 | if ((datay(i) > datay(i-1)) & (flagmax == 0)) 64 | flagmax = 1; 65 | 66 | Xmax = i; 67 | Ymax = datay(i); 68 | end 69 | 70 | if((datay(i) < datay(i+1)) & (flagmax == 1)) 71 | flagmax = 0; 72 | end 73 | 74 | if((datay(i) > datay(i+1)) & (flagmax == 1)) 75 | flagmax = 0; 76 | 77 | ally = [ally, Ymax]; 78 | 79 | allx = [allx, Xmax]; 80 | end 81 | 82 | %----- Find local minimum 83 | if((datay(i) < datay(i-1)) & (flagmin == 0)) 84 | flagmin = 1; 85 | Xmin = i; 86 | Ymin = datay(i); 87 | end 88 | 89 | if((datay(i) > datay(i+1)) & (flagmin == 1)) 90 | flagmin = 0; 91 | end 92 | 93 | if((datay(i) < datay(i+1)) & (flagmin == 1)) 94 | flagmin = 0; 95 | 96 | allx = [allx, Xmin]; 97 | ally = [ally, Ymin]; 98 | end 99 | 100 | %----- Find zero crossings 101 | if(((datay(i) > 0) & (datay(i+1) < 0)) | ((datay(i) < 0) & (datay(i+1) > 0))) 102 | %----- Estimate where zero crossing actually occurs 103 | slope = (datay(i+1) - datay(i)); %(delta x = 1) 104 | b = datay(i+1) - (slope * (i+1)); 105 | x = -b/slope; 106 | 107 | allx = [allx, x]; 108 | ally = [ally, 0]; 109 | elseif(datay(i) == 0.) 110 | x=i; 111 | allx = [allx, x]; 112 | ally = [ally, 0]; 113 | % zero_cross = [zero_cross2, i]; 114 | end 115 | end 116 | %1.checking point by point----loop end -------------------------------------------------------------------------------- /HuangEMD/FAacos.m: -------------------------------------------------------------------------------- 1 | %function [f,a] = FAacos(data, dt) 2 | % 3 | % The function FAacos generates an arccosine frequency and amplitude 4 | % of previously normalized data(n,k), where n is the length of the 5 | % time series and k is the number of IMF components. 6 | % 7 | % FAacos finds the frequency by applying 8 | % the arc-cosine function to the normalized data and 9 | % checking the points where the slope of arc-cosine phase changes. 10 | % Nature of the procedure suggests not to use the function 11 | % to process the residue component. 12 | % 13 | % Calling sequence- 14 | % [f,a] = FAacos(data, dt) 15 | % 16 | % Input- 17 | % data - 2-D matrix of IMF components 18 | % dt - time increment per point 19 | % Output- 20 | % f - 2-D matrix f(n,k) that specifies frequency 21 | % a - 2-D matrix a(n,k) that specifies amplitude 22 | %Note: 23 | % Used by- 24 | % FA 25 | %Reference: 26 | % 27 | %written by 28 | % Kenneth Arnold (NASA GSFC) Summer 2003, Initial 29 | %footnote: S.C.Su (2009/09/12) 30 | % 31 | %1.read the data,check input matrix 32 | %2.Do the normalization for an IMF ---loop A start 33 | % 3.compute the phase angle by arc-cosine function 34 | % 4.take difference for the phase angle 35 | %2.Do the normalization for an IMF ---loop A end 36 | % 37 | 38 | function [f,a] = FAacos(data, dt) 39 | 40 | disp('WARNING: Applying the function to the residue can cause an error.'); 41 | 42 | %1.read the data,check input matrix 43 | %----- Get dimensions 44 | [npts,nimf] = size(data); 45 | 46 | %----- Flip data if necessary 47 | flipped=0; 48 | if (npts < nimf) 49 | flipped=1; 50 | data=data'; 51 | [npts,nimf] = size(data); 52 | end 53 | 54 | %----- Input is normalized, so assume that amplitude is always 1 55 | a = ones(npts,nimf); 56 | 57 | %----- Mark points that are above 1 as invalid (Not a Number) 58 | data(find(abs(data)>1)) = NaN; 59 | 60 | %2.Do the normalization for an IMF ---loop A start 61 | %----- Process each IMF 62 | for c=1:nimf 63 | %----- Compute "phase" by arc-cosine function 64 | acphase = acos(data(:,c)); 65 | 66 | %3.compute the phase angle by arc-cosine function 67 | %----- Mark points where slope of arccosine phase changes as invalid 68 | %after difference the arc-cosine function will be discontinuous at those points 69 | for i=2:npts-1 70 | prev = data(i-1,c);%value of 'i-1' position 71 | cur = data(i,c); %value of 'i' position 72 | next = data(i+1,c);%value of 'i+1' position 73 | 74 | %Check for local max and local min, set them as NaN 75 | if (prev < cur & cur > next) | (prev > cur & cur < next) 76 | acphase(i) = NaN; 77 | 78 | end 79 | end 80 | 81 | clear prev cur next 82 | %clear syntax is important for this kind of algorithm-S.C.Su 83 | %choose some value with out clear makes chaos 84 | %4.take difference for the phase angle 85 | %----- Get phase differential frequency--calculate I.F. values 86 | acfreq = abs(diff(acphase))/(2*pi*dt); 87 | 88 | %----- Mark points with negative frequency as invalid 89 | acfreq(find(acfreq<0)) = NaN; 90 | 91 | %----- Fill in invalid points using a spline 92 | legit = find(~isnan(acfreq));%legit=allvalues-NaN 93 | %interpolate for NaN positions 94 | if (length(legit) < npts) 95 | f(:,c) = spline(legit, acfreq(legit), 1:npts)'; 96 | else 97 | f(:,c) = acfreq; 98 | end 99 | %2.Do the normalization for an IMF ---loop A end 100 | end 101 | 102 | %----- Flip again if data was flipped at the beginning 103 | if (flipped) 104 | f=f'; 105 | a=a'; 106 | end 107 | -------------------------------------------------------------------------------- /ceemdan.m: -------------------------------------------------------------------------------- 1 | function [modes its]=ceemdan(x,Nstd,NR,MaxIter) 2 | 3 | % WARNING: for this code works it is necessary to include in the same 4 | %directoy the file emd.m developed by Rilling and Flandrin. 5 | %This file is available at %http://perso.ens-lyon.fr/patrick.flandrin/emd.html 6 | %We use the default stopping criterion. 7 | %We use the last modification: 3.2007 8 | % 9 | % This version was run on Matlab 7.10.0 (R2010a) 10 | %---------------------------------------------------------------------- 11 | % INPUTs 12 | % x: signal to decompose 13 | % Nstd: noise standard deviation 14 | % NR: number of realizations 15 | % MaxIter: maximum number of sifting iterations allowed. 16 | % 17 | % OUTPUTs 18 | % modes: contain the obtained modes in a matrix with the rows being the modes 19 | % its: contain the sifting iterations needed for each mode for each realization (one row for each realization) 20 | % ------------------------------------------------------------------------- 21 | % Syntax 22 | % 23 | % modes=ceemdan(x,Nstd,NR,MaxIter) 24 | % [modes its]=ceemdan(x,Nstd,NR,MaxIter) 25 | % 26 | %-------------------------------------------------------------------------- 27 | % This algorithm was presented at ICASSP 2011, Prague, Czech Republic 28 | % Plese, if you use this code in your work, please cite the paper where the 29 | % algorithm was first presented. 30 | % If you use this code, please cite: 31 | % 32 | % M.E.TORRES, M.A. COLOMINAS, G. SCHLOTTHAUER, P. FLANDRIN, 33 | % "A complete Ensemble Empirical Mode decomposition with adaptive noise," 34 | % IEEE Int. Conf. on Acoust., Speech and Signal Proc. ICASSP-11, pp. 4144-4147, Prague (CZ) 35 | % 36 | % ------------------------------------------------------------------------- 37 | % Date: June 06,2011 38 | % Authors: Torres ME, Colominas MA, Schlotthauer G, Flandrin P. 39 | % For problems with the code, please contact the authors: 40 | % To: macolominas(AT)bioingenieria.edu.ar 41 | % CC: metorres(AT)santafe-conicet.gov.ar 42 | % ------------------------------------------------------------------------- 43 | 44 | x=x(:)'; 45 | desvio_x=std(x); 46 | x=x/desvio_x; 47 | 48 | modes=zeros(size(x)); 49 | temp=zeros(size(x)); 50 | aux=zeros(size(x)); 51 | acum=zeros(size(x)); 52 | iter=zeros(NR,round(log2(length(x))+5)); 53 | 54 | for i=1:NR 55 | white_noise{i}=randn(size(x));%creates the noise realizations 56 | end; 57 | 58 | for i=1:NR 59 | modes_white_noise{i}=emd(white_noise{i});%calculates the modes of white gaussian noise 60 | end; 61 | 62 | for i=1:NR %calculates the first mode 63 | temp=x+Nstd*white_noise{i}; 64 | [temp, o, it]=emd(temp,'MAXMODES',1,'MAXITERATIONS',MaxIter); 65 | temp=temp(1,:); 66 | aux=aux+temp/NR; 67 | iter(i,1)=it; 68 | end; 69 | 70 | modes=aux; %saves the first mode 71 | k=1; 72 | aux=zeros(size(x)); 73 | acum=sum(modes,1); 74 | 75 | while nnz(diff(sign(diff(x-acum))))>2 %calculates the rest of the modes 76 | for i=1:NR 77 | tamanio=size(modes_white_noise{i}); 78 | if tamanio(1)>=k+1 79 | noise=modes_white_noise{i}(k,:); 80 | noise=noise/std(noise); 81 | noise=Nstd*noise; 82 | try 83 | [temp, o, it]=emd(x-acum+std(x-acum)*noise,'MAXMODES',1,'MAXITERATIONS',MaxIter); 84 | temp=temp(1,:); 85 | catch 86 | it=0; 87 | temp=x-acum; 88 | end; 89 | else 90 | [temp, o, it]=emd(x-acum,'MAXMODES',1,'MAXITERATIONS',MaxIter); 91 | temp=temp(1,:); 92 | end; 93 | aux=aux+temp/NR; 94 | iter(i,k+1)=it; 95 | end; 96 | modes=[modes;aux]; 97 | aux=zeros(size(x)); 98 | acum=zeros(size(x)); 99 | acum=sum(modes,1); 100 | k=k+1; 101 | end; 102 | modes=[modes;(x-acum)]; 103 | [a b]=size(modes); 104 | iter=iter(:,1:a); 105 | modes=modes*desvio_x; 106 | its=iter; 107 | 108 | 109 | -------------------------------------------------------------------------------- /HuangEMD/FSPHSP.m: -------------------------------------------------------------------------------- 1 | %function [HESPw,HESPv,FESPw,FESPv]=FSPHSP(file,samplerate,freqsol,timesol,hsp_fre1,noiselevel,Nensemble) 2 | % 3 | % 4 | %INPUT 5 | % file----------full path file name,for instance,'c:\example\lod78.csv' 6 | % samplerate----sample rate of data 7 | % freqsol-------frequency axis bin number when plotting an Hilbert Energy Spectrum 8 | % timesol-------time axis bin number when plotting an Hilbert Energy Spectrum 9 | % hsp_fre1------maximun frequency when plotting an Hilbert Energy Spectrum 10 | % noiselevel----EEMD parameters--add noise level 11 | % Nensemble-----EEMD parameters--number of ensemble 12 | % 13 | %OUTPUT 14 | % HESPw---------frequency values of Marginal Spectrum (from Hilbert Energy Spectrum) 15 | % HESPv---------Energy values of Marginal Spectrum (from Hilbert Energy Spectrum) 16 | % FESPw---------frequency values of Fourier Energy Spectrum 17 | % FESPw---------Energy values of Fourier Energy Spectrum 18 | % 19 | %NOTE: 20 | % give an 1D data set 21 | % 1.this program performs EEMD ,do Hilbert-Spectrum, do Marginal-Spectrum 22 | % 2.this program performs Pwelch --Fourier Energy Spectrum 23 | % 3.Calculate comparison coefficients ,and multiply to those spectrum 24 | % 4.plot 1. and 2. together 25 | % 26 | %Note: 27 | % the translation coefficients are formulaed by N.E.H(Norden E. Huang) in HHT class 2008,NCU 28 | % the code is written by Student --S.C.Su(Sheng-Chung Su) 29 | % footnote:S.C.Su 30 | 31 | function [HESPw,HESPv,FESPw,FESPv]=FSPHSP(file,samplerate,freqsol,timesol,hsp_fre1,noiselevel,Nensemble) 32 | 33 | %0.load data 34 | datafile=load(file); 35 | 36 | %1.this program performs EEMD ,do Hilbert-Spectrum, do Marginal-Spectrum 37 | %do eemd and show result 38 | EMDIMF=eemd(datafile,noiselevel,Nensemble); 39 | figure(1) 40 | anoiselevel=num2str(noiselevel); 41 | aNensemble=num2str(Nensemble); 42 | strips(EMDIMF);title(['EEMD result ,noise level=',anoiselevel,' Number of ensemble=',aNensemble]); 43 | %plot Hilbert-Energy-SPectrum 44 | 45 | % emd result counting 46 | au=size(EMDIMF);nIMF=au(2)-2;nPT=au(1)-1; 47 | totalt=(nPT+1)/samplerate; 48 | Xlow=1/totalt; 49 | Xhig=samplerate/2; 50 | 51 | %plot NNSPE 52 | [nt,ta,fa]=nnspe(EMDIMF(1:nPT,2:nIMF-1), 0, totalt, freqsol, timesol, 0.00001, hsp_fre1,0,totalt,'hilbtm','spline',5); 53 | %calculate the marginal spectrum 54 | ms=sum(nt,2); 55 | %Smooth the pic ,use an Gaussian Filter "q",let nt been filtered twice,HESP spectrum will be good for reading 56 | q=fspecial('gaussian', 7, 0.6); 57 | nsu=filter2(q, nt);nsu=filter2(q, nsu); 58 | %Plot the HESP and MS 59 | figure(2); 60 | imagesc(ta,fa,nsu.^.5);axis xy;set(gca,'FontSize',6);title('Time-frequency analysis-Hilbert Spectrum','FontSize',9);xlabel('Time(sec)','FontSize',7,'VerticalAlignment','middle');ylabel('Freq(hz)','FontSize',7);ylim([Xlow hsp_fre1]); 61 | 62 | % 2.this program performs Pwelch --Fourier Energy Spectrum 63 | 64 | %plot Fourier-Energy-SPectrum 65 | stroridata='Pwelch'; 66 | [Pxx,w] =pwelch(datafile,[],[],[],samplerate,'onesided'); 67 | 68 | %3.Calculate comparison coefficients ,and multiply to those spectrum 69 | 70 | %Calculate the specrum to bin=400,max freq=100hz 71 | fco=2*samplerate*(nPT+1)/2/freqsol; 72 | hco=samplerate/2/hsp_fre1; 73 | %4.plot two different kind of spectrum together 74 | figure(5) 75 | loglog(w(1:end),fco*Pxx(1:end),'g',fa(1:end),hco*ms(1:end),'r');set(gca,'FontSize',6);title('Spectrum Comparison-Energy -and -frequency','FontSize',9);xlabel('Hz(1/sec)','FontSize',7,'VerticalAlignment','middle');ylabel('Energy','FontSize',7);xlim([Xlow hsp_fre1]);legend('FESP','MESP'); 76 | 77 | %return those values 78 | HESPw=fa; 79 | HESPv=hco*ms; 80 | FESPw=w; 81 | FESPv=fco*Pxx; -------------------------------------------------------------------------------- /HuangEMD/ifndq.m: -------------------------------------------------------------------------------- 1 | % function omega = ifndq(vimf, dt) 2 | % 3 | % 4 | % INPUT: 5 | % vimf: an IMF; 6 | % dt: time interval of the imputted data 7 | % OUTPUT: 8 | % omega: instantanesous frequency, which is 2*PI/T, where T 9 | % is the period of an ascillation 10 | % NOTE: 11 | % this is a function to calculate instantaneous based on EMD method-- 12 | % normalize the absolute values and find maximum envelope for 5 times 13 | % then calculate the Quadrature ,Phase angle,then take difference to them 14 | % finally,the instantaneous frequency values of an IMF is found. 15 | % 16 | %Reference: 17 | % 18 | % 19 | % code writer:Zhaohua Wu,mailbox:zhwu@cola.iges.org 20 | % footnote:S.C.Su 2009/05/14 21 | % 22 | % 1.set initial parameters 23 | % 2.find absolute values 24 | % 3.find the spline envelope for AM,take those out-loop start 25 | % 4.Normalize the envelope out (for 5 times) 26 | % 3.find the spline envelope for AM,take those out-loop end 27 | % 5.flip back those negative values after AM been removed 28 | % 6.Calculate the quadrature values 29 | % 7.Calculate the differece of the phase angle and give +/- sign 30 | % 8.create a algorithm to remove those outliner 31 | % 9.remove those marked outliner 32 | %10.use cubic spline to smooth the instantaneous frequency values 33 | %11.return the values back 34 | % 35 | % 36 | % Association: those procedure of HHT need this code 37 | %1.EMD 38 | %2.EEMD 39 | % 40 | % Concerned function: no 41 | % 42 | % 43 | 44 | function omega = ifndq(vimf, dt) 45 | % 46 | %1.set initial parameters 47 | Nnormal=5;%number of spline envelope normalization for AM 48 | rangetop=0.90; %the threshold of outliner remove for instantaneous frequency values 49 | vlength = max( size(vimf) ); 50 | vlength_1 = vlength -1; 51 | 52 | %2.find absolute values 53 | for i=1:vlength, 54 | abs_vimf(i)=vimf(i); 55 | if abs_vimf(i) < 0 56 | abs_vimf(i)=-vimf(i); 57 | end 58 | end 59 | 60 | %3.find the spline envelope for AM,take those out-loop start 61 | for jj=1:Nnormal, 62 | [spmax, spmin, flag]=extrema(abs_vimf); 63 | dd=1:1:vlength; 64 | upper= spline(spmax(:,1),spmax(:,2),dd); 65 | 66 | %4.Normalize the envelope out 67 | for i=1:vlength, 68 | abs_vimf(i)=abs_vimf(i)/upper(i); 69 | end 70 | end 71 | %3.find the spline envelope for AM,take those out-loop end 72 | 73 | %5.flip back those negative values after AM been removed 74 | for i=1:vlength, 75 | nvimf(i)=abs_vimf(i); 76 | if vimf(i) < 0; 77 | nvimf(i)=-abs_vimf(i); 78 | end 79 | end 80 | 81 | %6.Calculate the quadrature values 82 | for i=1:vlength, 83 | dq(i)=sqrt(1-nvimf(i)*nvimf(i)); 84 | end 85 | 86 | %7.Calculate the differece of the phase angle and give +/- sign 87 | for i=2:vlength_1, 88 | devi(i)=nvimf(i+1)-nvimf(i-1); 89 | if devi(i)>0 & nvimf(i)<1 90 | dq(i)=-dq(i); 91 | end 92 | end 93 | 94 | %8.create a algorithm to remove those outliner 95 | rangebot=-rangetop; 96 | for i=2:(vlength-1), 97 | if nvimf(i)>rangebot & nvimf(i) < rangetop 98 | %good original value,direct calculate instantaneous frequency 99 | omgcos(i)=abs(nvimf(i+1)-nvimf(i-1))*0.5/sqrt(1-nvimf(i)*nvimf(i)); 100 | else 101 | %bad original value,direct set -9999,mark them 102 | omgcos(i)=-9999; 103 | end 104 | end 105 | omgcos(1)=-9999; 106 | omgcos(vlength)=-9999; 107 | 108 | %9.remove those marked outliner 109 | jj=1; 110 | for i=1:vlength, 111 | if omgcos(i)>-1000 112 | ddd(jj)=i; 113 | temp(jj)=omgcos(i); 114 | jj=jj+1; 115 | end 116 | end 117 | 118 | %10.use cubic spline to smooth the instantaneous frequency values 119 | temp2=spline(ddd,temp,dd); 120 | omgcos=temp2; 121 | 122 | %11.return the values back 123 | for i=1:vlength, 124 | omega(i)=omgcos(i); 125 | end 126 | pi2=pi*2; 127 | omega=omega/dt; -------------------------------------------------------------------------------- /HuangEMD/linearnormalize.m: -------------------------------------------------------------------------------- 1 | %function [ndata,a]=linearnormalize(data) 2 | 3 | % The function LINEARNORMALIZE normalizes with linear interpolation (interp1), 4 | % which mainly to avoid the overshot of cubic spline interpolation, 5 | % especially when using the EEMD method, 6 | % the data(n,m) where n specifies the number of time points, 7 | % and m is the number of IMF components. 8 | % The normalization is carried out with both maxima and minima; hence emax 9 | % is taken as the absolute sign. 10 | % 11 | % Calling sequence- 12 | % [ndata,a]=linearnormalize(data) 13 | % 14 | % Input- 15 | % data - 2-D matrix data(n,m) that specifies the IMF components 16 | % Output- 17 | % ndata - normalized data (FM) 18 | % a - splined envelope(AM) 19 | % 20 | % Used by- 21 | % FA 22 | 23 | %written by 24 | % Xianyao Chen Sep. 20 created following the splinenormalize.m 25 | %%footnote: S.C.Su (2009/09/02) 26 | % 27 | % There are two loops in this code ,it's dealing with IMF by IMF . 28 | % 0. set the default value about the end-process methodtype 29 | % 1.read the data,check input matrix 30 | % 2.Do the normalization for an IMF ---loop A start 31 | % 3.find maximun for the asbolute value 32 | % 4.add check for trend--loop B start 33 | % 5.Do end-process for bouble sides 34 | % 6.linear interpolation for the envelope-AM 35 | % 7.normalize the IMF,AM been devided out 36 | % 4. add check for trend--loop B end 37 | % 2.add stop algorithm-for the normalize procedure-loop A end 38 | % 39 | function [ndata,a]=linearnormalize(data) 40 | 41 | % 0. set the default value about the end-process methodtype 42 | %----- Get the dimension 43 | [npt,ncol] = size(data); 44 | 45 | %----- Initialize and flip data if needed 46 | flipped=0; 47 | if (ncol > npt) 48 | data=data'; 49 | [npt,ncol] = size(data); 50 | flipped=1; 51 | end 52 | 53 | te=(1:npt)'; %a series of total number 54 | 55 | %2.Do the normalization for an IMF ---loop A start 56 | %----- Process each IMF component 57 | for c=1:ncol % loop-a start here 58 | 59 | %3.find maximun for the asbolute value 60 | %----- Extract the set of max points and their coordinates 61 | [mx, tx]=emax(abs(data(:,c))); 62 | %4. add check for trend--loop B start 63 | %----- Fix the end to prevent wide swaying in spline 64 | %----- by assigning the te(1) and te(n) 65 | %----- the same values as the first and last tx and mx. 66 | nExtrema=length(mx); 67 | if nExtrema > 1 68 | %5.Do end-process for bouble sides 69 | tx=[1;tx;npt]; 70 | %----- Fix the ends at the same as the next point 71 | mx=[mx(1);mx;mx(nExtrema)]; 72 | 73 | %6.linaer interpolation for the envelope-AM 74 | %find linaer envelope as AM 75 | a(:,c)=interp1(tx,mx,te); 76 | %***linear interpolation backup in case spline error. Added by KBB. 77 | %Experimental 78 | if(any(a(:, c) < data(:,c))) 79 | a2 = interp1(tx, mx, te); 80 | 81 | mask_sp = ones(length(a), 1); 82 | 83 | for(i=1:length(tx)-1) 84 | if(any(a(tx(i):tx(i+1),c) < data(tx(i):tx(i+1),c))) 85 | mask_sp(tx(i):tx(i+1)) = 0; 86 | end 87 | end 88 | 89 | % mask_sp = (a(:,c) >= data(:, c)); 90 | 91 | a(:, c) = (a(:,c) .* mask_sp) + (a2 .* ~mask_sp); 92 | end 93 | %end KBB 94 | 95 | %7.normalize the IMF,AM been devided out 96 | %----- % Normalize the data by linaer envelope 97 | ndata(:,c)=data(:,c)./a(:,c); 98 | 99 | 100 | else 101 | %----- Leave data unchanged 102 | a(:,c)=ones(npt,1); 103 | ndata(:,c)=0*ones(npt,1); 104 | disp('Watch! there is a trend in the input Matrix') 105 | end 106 | %4. add check for trend--loop B end 107 | end 108 | %2.Do the normalization for an IMF ---loop A end 109 | 110 | %----- Flip data back if needed 111 | if (flipped) 112 | ndata=ndata'; 113 | a=a'; 114 | end -------------------------------------------------------------------------------- /HuangEMD/pchipnormalize.m: -------------------------------------------------------------------------------- 1 | %function [ndata,a]=hermitenormalize(data) 2 | 3 | % The function HERMITENORMALIZE normalizes with cubic hermite interpolation (pchip), 4 | % which mainly to avoid the overshot of cubic spline interpolation, 5 | % especially when using the EEMD method, 6 | % the data(n,m) where n specifies the number of time points, 7 | % and m is the number of IMF components. 8 | % The normalization is carried out with both maxima and minima; hence emax 9 | % is taken as the absolute sign. 10 | % 11 | % Calling sequence- 12 | % [ndata,a]=hermitenormalize(data) 13 | % 14 | % Input- 15 | % data - 2-D matrix data(n,m) that specifies the IMF components 16 | % Output- 17 | % ndata - normalized data (FM) 18 | % a - splined envelope(AM) 19 | % 20 | % Used by- 21 | % FA 22 | 23 | %written by 24 | % Xianyao Chen Sep. 20 created following the splinenormalize.m 25 | %footnote: S.C.Su (2009/09/02) 26 | % 27 | % There are two loops in this code ,it's dealing with IMF by IMF . 28 | % 0. set the default value about the end-process methodtype 29 | % 1.read the data,check input matrix 30 | % 2.Do the normalization for an IMF ---loop A start 31 | % 3.find maximun for the asbolute value 32 | % 4.add check for trend--loop B start 33 | % 5.Do end-process for bouble sides 34 | % 6.cubic Hermite spline interpolation for the envelope-AM 35 | % 7.normalize the IMF,AM been devided out 36 | % 4. add check for trend--loop B end 37 | % 2.add stop algorithm-for the normalize procedure-loop A end 38 | % 39 | function [ndata,a]=hermitenormalize(data) 40 | 41 | % 0. set the default value about the end-process methodtype 42 | %----- Get the dimension 43 | [npt,ncol] = size(data); 44 | 45 | %----- Initialize and flip data if needed 46 | flipped=0; 47 | if (ncol > npt) 48 | flipped=1; 49 | data=data'; 50 | [npt,ncol] = size(data); 51 | end 52 | 53 | te=(1:npt)'; %a series of total number 54 | 55 | %2.Do the normalization for an IMF ---loop A start 56 | %----- Process each IMF component 57 | for c=1:ncol % loop-a start here 58 | 59 | %3.find maximun for the asbolute value 60 | %----- Extract the set of max points and their coordinates 61 | [mx, tx]=emax(abs(data(:,c))); 62 | %4. add check for trend--loop B start 63 | %----- Fix the end to prevent wide swaying in spline 64 | %----- by assigning the te(1) and te(n) 65 | %----- the same values as the first and last tx and mx. 66 | nExtrema=length(mx); 67 | if nExtrema > 1 68 | %5.Do end-process for bouble sides 69 | tx=[1;tx;npt]; 70 | %----- Fix the ends at the same as the next point 71 | mx=[mx(1);mx;mx(nExtrema)]; 72 | 73 | %6.cubic Hermite spline interpolation for the envelope-AM 74 | %find cubic Hermite spline envelope as AM 75 | a(:,c)=pchip(tx,mx,te); 76 | %***linear interpolation backup in case spline error. Added by KBB. 77 | %Experimental 78 | if(any(a(:, c) < data(:,c))) 79 | a2 = interp1(tx, mx, te); 80 | 81 | mask_sp = ones(length(a), 1); 82 | 83 | for(i=1:length(tx)-1) 84 | if(any(a(tx(i):tx(i+1),c) < data(tx(i):tx(i+1),c))) 85 | mask_sp(tx(i):tx(i+1)) = 0; 86 | end 87 | end 88 | 89 | % mask_sp = (a(:,c) >= data(:, c)); 90 | 91 | a(:, c) = (a(:,c) .* mask_sp) + (a2 .* ~mask_sp); 92 | end 93 | %end KBB 94 | 95 | %7.normalize the IMF,AM been devided out 96 | %----- % Normalize the data by splined envelope 97 | ndata(:,c)=data(:,c)./a(:,c); 98 | 99 | 100 | else 101 | %----- Leave data unchanged 102 | a(:,c)=ones(npt,1); 103 | ndata(:,c)=0*ones(npt,1); 104 | disp('Watch! there is a trend in the input Matrix') 105 | end 106 | %4. add check for trend--loop B end 107 | end 108 | %2.Do the normalization for an IMF ---loop A end 109 | 110 | %----- Flip data back if needed 111 | if (flipped) 112 | ndata=ndata'; 113 | a=a'; 114 | end -------------------------------------------------------------------------------- /HuangEMD/splinenormalize.m: -------------------------------------------------------------------------------- 1 | %function [ndata,a]=splinenormalize(data) 2 | 3 | % The function SPLINENORMALIZE normalizes with splines, 4 | % the data(n,m) where n specifies the number of time points, 5 | % and m is the number of IMF components. 6 | % The normalization is carried out with both maxima and minima; hence emax 7 | % is taken as the absolute sign. 8 | % 9 | % Calling sequence- 10 | % [ndata,a]=splinenormalize(data) 11 | % 12 | % Input- 13 | % data - 2-D matrix data(n,m) that specifies the IMF components 14 | % Output- 15 | % ndata - normalized data (FM) 16 | % a - splined envelope(AM) 17 | % 18 | % Used by- 19 | % FA 20 | 21 | %written by 22 | % K. Arnold (for NASA GSFC) Jan. 28 2004 Modified 23 | % (fixed a bug where a "random" point 24 | % could get thrown somewhere in the middle 25 | % of the dataset and the endpoints 26 | % were not controlled). 27 | % Modified Karin Blank March 22, 2005 28 | %footnote: S.C.Su (2009/09/02) 29 | % 30 | % There are two loops in this code ,it's dealing with IMF by IMF . 31 | % 0. set the default value about the end-process methodtype 32 | % 1.read the data,check input matrix 33 | % 2.Do the normalization for an IMF ---loop A start 34 | % 3.find maximun for the asbolute value 35 | % 4.add check for trend--loop B start 36 | % 5.Do end-process for bouble sides 37 | % 6.spline interpolation for the envelope-AM 38 | % 7.normalize the IMF,AM been devided out 39 | % 4. add check for trend--loop B end 40 | % 2.add stop algorithm-for the normalize procedure-loop A end 41 | % 42 | function [ndata,a]=splinenormalize(data) 43 | 44 | % 0. set the default value about the end-process methodtype 45 | %----- Get the dimension 46 | [npt,ncol] = size(data); 47 | 48 | %----- Initialize and flip data if needed 49 | flipped=0; 50 | if (ncol > npt) 51 | data=data'; 52 | [npt,ncol] = size(data); 53 | flipped=1; 54 | end 55 | 56 | te=(1:npt)'; %a series of total number 57 | 58 | %2.Do the normalization for an IMF ---loop A start 59 | %----- Process each IMF component 60 | for c=1:ncol % loop-a start here 61 | 62 | %3.find maximun for the asbolute value 63 | %----- Extract the set of max points and their coordinates 64 | [mx, tx]=emax(abs(data(:,c))); 65 | %4. add check for trend--loop B start 66 | %----- Fix the end to prevent wide swaying in spline 67 | %----- by assigning the te(1) and te(n) 68 | %----- the same values as the first and last tx and mx. 69 | nExtrema=length(mx); 70 | if nExtrema > 1 71 | %5.Do end-process for bouble sides 72 | tx=[1;tx;npt]; 73 | %----- Fix the ends at the same as the next point 74 | mx=[mx(1);mx;mx(nExtrema)]; 75 | 76 | %6.spline interpolation for the envelope-AM 77 | %find spline envelope as AM 78 | a(:,c)=spline(tx,mx,te); 79 | %***linear interpolation backup in case spline error. Added by KBB. 80 | %Experimental 81 | if(any(a(:, c) < data(:,c))) 82 | a2 = interp1(tx, mx, te); 83 | 84 | mask_sp = ones(length(a), 1); 85 | 86 | for(i=1:length(tx)-1) 87 | if(any(a(tx(i):tx(i+1),c) < data(tx(i):tx(i+1),c))) 88 | mask_sp(tx(i):tx(i+1)) = 0; 89 | end 90 | end 91 | 92 | % mask_sp = (a(:,c) >= data(:, c)); 93 | 94 | a(:, c) = (a(:,c) .* mask_sp) + (a2 .* ~mask_sp); 95 | end 96 | %end KBB 97 | 98 | %7.normalize the IMF,AM been devided out 99 | %----- % Normalize the data by splined envelope 100 | ndata(:,c)=data(:,c)./a(:,c); 101 | 102 | 103 | else 104 | %----- Leave data unchanged 105 | a(:,c)=ones(npt,1); 106 | ndata(:,c)=0*ones(npt,1); 107 | disp('Watch! there is a trend in the input Matrix') 108 | end 109 | %4. add check for trend--loop B end 110 | end 111 | %2.Do the normalization for an IMF ---loop A end 112 | 113 | %----- Flip data back if needed 114 | if (flipped) 115 | ndata=ndata'; 116 | a=a'; 117 | end -------------------------------------------------------------------------------- /HuangEMD/splinenormalizeep.m: -------------------------------------------------------------------------------- 1 | %function [ndata,a]=splinenormalizeep(data,methodtype) 2 | 3 | % The function SPLINENORMALIZEEP normalizes with splines with end-process, 4 | % the data(n,m) where n specifies the number of time points, 5 | % and m is the number of IMF components. 6 | % The normalization is carried out with both maxima and minima; hence emax 7 | % is taken as the absolute sign. 8 | % 9 | % Calling sequence- 10 | % [ndata,a]=splinenormalizeep(data,methodtype) 11 | % 12 | % Input- 13 | % data - 2-D matrix data(n,m) that specifies the IMF components 14 | % methodtype- end-process method selection 15 | % 'A'-for 5 point extend 16 | % 'B'-for first and last extrema copy once 17 | % 'C'-for neighbor peak slope extend out 18 | % Output- 19 | % ndata - normalized data (FM) 20 | % a - splined envelope(AM) 21 | % 22 | % Used by- 23 | % FA 24 | % 25 | %written by 26 | % K. Arnold (for NASA GSFC) Jan. 28 2004 Modified 27 | % (fixed a bug where a "random" point 28 | % could get thrown somewhere in the middle 29 | % of the dataset and the endpoints 30 | % were not controlled). 31 | % Modified Karin Blank March 22, 2005 32 | % modified S.C.Su Sep 1st,2009 33 | % Collected different type end-process method,put them in selection. 34 | %footnote: S.C.Su (2009/09/01) 35 | % 36 | % There are two loops in this code ,it's dealing with IMF by IMF . 37 | % 0. set the default value about the end-process methodtype 38 | % 1.read the data,check input matrix 39 | % 2.Do the normalization for an IMF ---loop A start 40 | % 3.find maximun for the asbolute value 41 | % 4.add check for trend--loop B start 42 | % 5.Do end-process for spline bouble sides 43 | % 6.spline interpolation for the envelope-AM 44 | % 7.normalize the IMF,AM been devided out 45 | % 4. add check for trend--loop B end 46 | % 2.add stop algorithm-for the normalize procedure-loop A end 47 | % 48 | function [ndata,a]=splinenormalizeep(data,methodtype) 49 | 50 | %0. set the default value about the end-process methodtype 51 | if nargin<2 52 | methodtype = []; 53 | end 54 | if isempty(methodtype) 55 | methodtype = 'C'; 56 | end 57 | 58 | 59 | % 1.read the data,check input matrix 60 | %----- Get the dimension 61 | [npt,ncol] = size(data); 62 | 63 | %----- Initialize and flip data if needed 64 | flipped=0; 65 | if (ncol > npt) 66 | flipped=1; 67 | data=data'; 68 | [npt,ncol] = size(data); 69 | end 70 | 71 | te=(1:npt)'; %a series of total number 72 | 73 | %2.Do the normalization for an IMF ---loop A start 74 | %----- Process each IMF component 75 | for c=1:ncol % loop-a start here 76 | 77 | %3.find maximun for the asbolute value 78 | %----- Extract the set of max points and their coordinates 79 | [mx, tx]=emax(abs(data(:,c))); 80 | %4. add check for trend--loop B start 81 | nExtrema=length(mx); 82 | if nExtrema > 1 83 | %5.Do end-process for spline bouble sides 84 | 85 | %----- Fix the ends with endprocess1.m 86 | [beg_x,beg_y,end_x,end_y,begx2,begy2,endx2,endy2] = endprocess1(abs(data(:,c)),10,methodtype); 87 | tx1 = [beg_x; tx; end_x]; 88 | mx1 = [beg_y; mx; end_y]; 89 | 90 | 91 | %6.spline interpolation for the envelope-AM 92 | %find spline envelope as AM 93 | a(:,c)=spline(tx1,mx1,te); 94 | %***linear interpolation backup in case spline error. Added by KBB. 95 | %Experimental 96 | if(any(a(:, c) < data(:,c))) 97 | a2 = interp1(tx1, mx1, te); 98 | 99 | mask_sp = ones(length(a), 1); 100 | 101 | for(i=1:length(tx)-1) 102 | if(any(a(tx(i):tx(i+1),c) < data(tx(i):tx(i+1),c))) 103 | mask_sp(tx(i):tx(i+1)) = 0; 104 | end 105 | end 106 | 107 | % mask_sp = (a(:,c) >= data(:, c)); 108 | 109 | a(:, c) = (a(:,c) .* mask_sp) + (a2 .* ~mask_sp); 110 | end 111 | %end KBB 112 | 113 | %7.normalize the IMF,AM been devided out 114 | %----- % Normalize the data by splined envelope 115 | ndata(:,c)=data(:,c)./a(:,c); 116 | 117 | 118 | else 119 | %----- Leave data unchanged 120 | a(:,c)=ones(npt,1); 121 | ndata(:,c)=0*ones(npt,1); 122 | disp('Watch! there is a trend in the input Matrix') 123 | end 124 | %4. add check for trend--loop B end 125 | end 126 | %2.Do the normalization for an IMF ---loop A end 127 | 128 | %----- Flip data back if needed 129 | if (flipped) 130 | ndata=ndata'; 131 | a=a'; 132 | end -------------------------------------------------------------------------------- /HuangEMD/blocknormalize.m: -------------------------------------------------------------------------------- 1 | %function [ndata,a]=blocknormalize(data) 2 | 3 | % The function BLOCKNORMALIZE normalizes each block of the input signal, 4 | % as defined by zero crossings, to have a maximum amplitude of 1. 5 | % 6 | % Suggestion: if the routine is needed than use it with caution. 7 | disp('Using block-normalize ,are you sure?!') 8 | disp('Slope discontinuous will happen in left and right side about a zero') 9 | pause(2); 10 | % 11 | % Calling sequence- 12 | % [ndata,a]=blocknormalize(data) 13 | % 14 | % Input- 15 | % data - 2-D matrix data(npt,ncol) 16 | % Output- 17 | % ndata - 2-D matrix of normalized data (FM) 18 | % a - 2-D matrix of envelope (AM) 19 | % 20 | % Used by- 21 | % FA 22 | 23 | %written by 24 | % Kenneth Arnold (NASA GSFC) Summer 2003 Initial 25 | % Kenneth Arnold (NASA GSFC) June 6, 2004 Tweaked 26 | %footnote: S.C.Su (2009/09/01) 27 | %this code is with complex structure,but easy meanings 28 | %just remember about some easy sentences 29 | %"Blocks are defined as those segments between the zero-crossing points" 30 | %"Finding the extreme value in each block,the value is the amplitude of this block" 31 | % 32 | %1.read the data,check input matrix 33 | %2.Do the normalization for an IMF ---loop A start 34 | %3.checking each data value from the beginning point of each block----loop B start 35 | %4.read the sign (+,-,0) form starting of every block 36 | %5. make judgement block by block,start from point 1-----loop C start 37 | %6.judge the value about its sign,different sign means the end of this block 38 | %7.judge the value about its sign,the same sign means still in the same block 39 | %5. make judgement block by block,start from point 1-----loop C end 40 | %3.checking each data value from the beginning point of each block----loop B end 41 | %2.Do the normalization for an IMF ---loop A end 42 | % 43 | function [ndata,a]=blocknormalize(data) 44 | 45 | %1.read the data,check input matrix 46 | %----- Get the dimension 47 | [npt,ncol] = size(data); 48 | 49 | %----- Flip data if needed 50 | flipped=0; 51 | if (ncol > npt) 52 | data=data'; 53 | [npt,ncol] = size(data); 54 | flipped=1; 55 | end 56 | 57 | %----- Initialize amplitude matrix 58 | a = ones(npt,ncol); 59 | 60 | %2.Do the normalization for an IMF ---loop A start 61 | %----- Process each column of data 62 | for col=1:ncol 63 | 64 | i=1; 65 | %3.checking each data value from the beginning point of each block----loop B start 66 | while (i<=npt) 67 | %4.read the sign (+,-,0) form starting of every block 68 | blockSign = sign(data(i,col)); %get blocksign : + or -,or 0,find it first 69 | 70 | % when zero is meet,use right-hand side sign as the blocksign for it 71 | while (blockSign == 0 & i < npt) % handle zero case 72 | i=i+1; 73 | blockSign = sign(data(i,col)); 74 | end 75 | 76 | 77 | if (i==npt) 78 | break; %after all values are checked,this if help to stop "while" 79 | end 80 | 81 | %5. make judgement block by block,start from point 1-----loop C start 82 | blockExtr = 0; % extreme value (maximum or minimum) 83 | 84 | 85 | %----- Find the end of this block and its extreme value 86 | for (j=i:npt) %this block from (i~j) 87 | j; %the jth value is the value that we are checking now 88 | cur = data(j,col); 89 | 90 | %6.judge the value about its sign,different sign means the end of this block 91 | if (sign(cur) ~= blockSign) 92 | j=j-1; 93 | break; % jump out,because meet next block 94 | end 95 | 96 | %7.judge the value about its sign,the same sign means still in the same block 97 | if ((blockSign == 1 & cur > blockExtr) | (blockSign == -1 & cur < blockExtr)) 98 | blockExtr = cur; %stay in,because still in the same block 99 | %collect the biggest or smallest value for this block 100 | end 101 | end 102 | %5. make judgement block by block,start from point 1-----loop C end 103 | if (blockExtr ~= 0) 104 | %normalize this block with its own extreme value 105 | ndata(i:j,col) = data(i:j,col) / blockExtr * blockSign; 106 | a(i:j,col) = a(i:j,col) * blockExtr * blockSign; 107 | end 108 | 109 | i=j+1; %return this block endding position 110 | %go back to while loop 111 | %next block start from last endding position 112 | end 113 | %3.checking each data value from the beginning point of each block----loop B end 114 | end 115 | %2.Do the normalization for an IMF ---loop A end 116 | 117 | %----- Flip data back if needed 118 | if (flipped) 119 | ndata=ndata'; 120 | a=a'; 121 | end -------------------------------------------------------------------------------- /ViewDataEMD.m: -------------------------------------------------------------------------------- 1 | clear all;close all; 2 | %% Data 3 | load('2sample_CA1_DG_LFP_10s') 4 | Nt = length(sample_CA1_theta); 5 | fs = 1e3; 6 | dt = 1/fs; 7 | t = [0:Nt-1]*dt; 8 | 9 | CA1theta = sample_CA1_theta; 10 | DGLFP = sample_DG_complexLFP; 11 | clear sample_DG_complexLFP sample_CA1_theta 12 | figure; 13 | subplot(211);plot(t,CA1theta); 14 | xlabel('time (sec');title('CA1 LFP');axis tight 15 | subplot(212);plot(t,DGLFP);axis tight 16 | xlabel('time (sec');title('DG LFP');axis tight 17 | 18 | %% Gabor spectrograms 19 | L = 10; 20 | NFFT = 2^L; 21 | tw = -NFFT/2+1:NFFT/2; 22 | sigma = .2;%[sec] 23 | sigSamp = sigma*fs; 24 | w = sqrt(sqrt(2)/sigSamp)*exp(-pi*tw.*tw/sigSamp/sigSamp); 25 | overlap = NFFT-1; 26 | [SpecCA1, T, F]=spectrogram(CA1theta,w,overlap,NFFT,fs);%deriv gaussian windowed spectrogram 27 | figure;imagesc(F,T,abs(SpecCA1));set(gca,'YDir','normal'); 28 | ylim([0 150]) 29 | set(gca,'YTick',[0:10:150]) 30 | grid on 31 | xlabel('time (sec)');ylabel('Hz') 32 | title('Gabor spectrogram CA1 LFP \theta') 33 | 34 | [SpecDG, F, T]=spectrogram(DGLFP,w,overlap,NFFT,fs);%deriv gaussian windowed spectrogram 35 | figure;imagesc(T,F,abs(SpecDG));set(gca,'YDir','normal'); 36 | ylim([0 150]) 37 | set(gca,'YTick',[0:10:150]) 38 | grid on 39 | xlabel('time (sec)');ylabel('Hz') 40 | title('Gabor spectrogram DG LFP') 41 | 42 | %% Morlet wavelet spectrograms 43 | sig1 = struct('val',CA1theta,'period',dt); 44 | cwtS1 = cwtft(sig1,'scales',1./[1:1:200]); 45 | scales = cwtS1.scales; 46 | MorletFourierFactor = 4*pi/(6+sqrt(2+6^2)); 47 | freq = 1./(scales.*MorletFourierFactor); 48 | figure;imagesc(t,freq,abs(cwtS1.cfs));set(gca,'YDir','normal'); 49 | xlabel('time (sec)'); ylabel('Pseudo-frequency'); 50 | title('Morlet spectrogram CA1 LFP \theta') 51 | set(gca,'YTick',[0:10:150]) 52 | grid on 53 | ylim([0 150]) 54 | 55 | sig1 = struct('val',DGLFP,'period',dt); 56 | cwtS1 = cwtft(sig1,'scales',1./[1:1:200]); 57 | scales = cwtS1.scales; 58 | MorletFourierFactor = 4*pi/(6+sqrt(2+6^2)); 59 | freq = 1./(scales.*MorletFourierFactor); 60 | figure;imagesc(t,freq,abs(cwtS1.cfs));set(gca,'YDir','normal'); 61 | xlabel('time (sec)'); ylabel('Pseudo-frequency'); 62 | title('Morlet spectrogram DG LFP \theta') 63 | set(gca,'YTick',[0:10:150]) 64 | grid on 65 | ylim([0 150]) 66 | 67 | %% HHT IF diagram 68 | 69 | % %Do EMD using conventional algorithm 70 | % IMFCA1 = emdos(CA1theta,'method','emd'); 71 | % IMFDG = emdos(DGLFP,'method','emd'); 72 | 73 | % %Do EMD using local EMD, with Dan's "remove negative frequencies" weights. 74 | % emd_opts=struct('localEMDfunc',@memd_nonegfreq); 75 | % IMFCA1 = memd_emd(CA1theta,emd_opts); 76 | % IMFDG = memd_emd(DGLFP,emd_opts); 77 | 78 | %Do EMD using local EMD, with Flandrin-style weights. 79 | emd_opts=struct('localEMDfunc',@memd_flandrin_weights); 80 | IMFCA1 = memd_emd(CA1theta,emd_opts); 81 | IMFDG = memd_emd(DGLFP,emd_opts); 82 | 83 | figure;plot_imf(IMFCA1,t,'CA1') 84 | figure;plot_imf(IMFDG,t,'DG') 85 | 86 | %plot CWT magnitude of IMFs 87 | nIMF = 7; 88 | for j = 1:nIMF 89 | % subplot(nIMF,1,j) 90 | % psd(Hs,IMFCA1(j,:),'Fs',fs) 91 | sig1 = struct('val',IMFDG(j,:),'period',dt); 92 | cwtS1 = cwtft(sig1,'scales',1./[1:1:200]); 93 | scales = cwtS1.scales; 94 | MorletFourierFactor = 4*pi/(6+sqrt(2+6^2)); 95 | freq = 1./(scales.*MorletFourierFactor); 96 | figure;imagesc(t,freq,abs(cwtS1.cfs));set(gca,'YDir','normal'); 97 | xlabel('time (sec)'); ylabel('Pseudo-frequency'); 98 | title('Morlet spectrogram DG LFP \theta') 99 | set(gca,'YTick',[0:10:150]) 100 | grid on 101 | ylim([0 150]) 102 | end 103 | for j = 1:nIMF 104 | % subplot(nIMF,1,j) 105 | % psd(Hs,IMFCA1(j,:),'Fs',fs) 106 | sig1 = struct('val',IMFCA1(j,:),'period',dt); 107 | cwtS1 = cwtft(sig1,'scales',1./[1:1:200]); 108 | scales = cwtS1.scales; 109 | MorletFourierFactor = 4*pi/(6+sqrt(2+6^2)); 110 | freq = 1./(scales.*MorletFourierFactor); 111 | figure;imagesc(t,freq,abs(cwtS1.cfs));set(gca,'YDir','normal'); 112 | xlabel('time (sec)'); ylabel('Pseudo-frequency'); 113 | title('Morlet spectrogram DG LFP \theta') 114 | set(gca,'YTick',[0:10:150]) 115 | grid on 116 | ylim([0 150]) 117 | end 118 | 119 | % %plot multitaper spectra of each IMF 120 | % TW = 5; 121 | % Hs=spectrum.mtm(TW); 122 | % figure; 123 | % for j = 1:nIMF 124 | % subplot(nIMF,1,j) 125 | % psd(Hs,IMFDG(j,:),'Fs',fs) 126 | % end 127 | 128 | % return 129 | % %Plot instantaneous frequency of each IMF 130 | % xIMFCA1 = hilbert(IMFCA1'); 131 | % xIMFDG = hilbert(IMFDG'); 132 | % PhiIMFCA1 = angle(xIMFCA1'); 133 | % PhiIMFDG = angle(xIMFDG); 134 | % MagIMFCA1 = abs(xIMFCA1);figure;plot(t,MagIMFCA1) 135 | % MagIMFDG = abs(xIMFDG); 136 | % IF_IMFCA1 = diff(PhiIMFCA1); 137 | % IF_IMFDG = diff(PhiIMFDG); 138 | % figure;plot(t(2:end),IF_IMFCA1(:,1:3)) 139 | % figure;plot(t(2:end),IF_IMFDG(:,1:3)) 140 | -------------------------------------------------------------------------------- /HuangEMD/extrema.m: -------------------------------------------------------------------------------- 1 | % function [spmax, spmin, flag]= extrema(in_data) 2 | % 3 | % This is a utility program for cubic spline envelope, 4 | % the code is to find out max values and max positions 5 | % min values and min positions 6 | % (then use matlab function spline to form the spline) 7 | % 8 | % function [spmax, spmin, flag]= extrema(in_data) 9 | % 10 | % INPUT: 11 | % in_data: Inputted data, a time series to be sifted; 12 | % OUTPUT: 13 | % spmax: The locations (col 1) of the maxima and its corresponding 14 | % values (col 2) 15 | % spmin: The locations (col 1) of the minima and its corresponding 16 | % values (col 2) 17 | % 18 | % NOTE: 19 | % EMD uses Cubic Spline to be the Maximun and Minimum Envelope for 20 | % the data.Besides finding spline,end points should be noticed. 21 | % 22 | %References: ? which paper? 23 | % 24 | % 25 | % 26 | % code writer: Zhaohua Wu. 27 | % footnote:S.C.Su 28 | % 29 | % There are two seperste loops in this code . 30 | % part1.-- find out max values and max positions 31 | % process the start point and end point 32 | % part2.-- find out min values and max positions 33 | % process the start point and end point 34 | % Those parts are similar. 35 | % 36 | % Association:eemd.m 37 | % this function ususally used for finding spline envelope 38 | % 39 | % Concerned function: no 40 | % (all matlab internal function) 41 | 42 | function [spmax, spmin, flag]= extrema(in_data) 43 | 44 | flag=1; 45 | dsize=length(in_data); 46 | 47 | %part1.--find local max value and do end process 48 | 49 | %start point 50 | %spmax(1,1)-the first 1 means first point max value,the second 1 means first index 51 | %spmax(1,2)-the first 1 means first point max value,the second 2 means first index 52 | %spmax(1,1)-for position of max 53 | %spmax(1,2)-for value of max 54 | 55 | spmax(1,1) = 1; 56 | spmax(1,2) = in_data(1); 57 | 58 | %Loop --start find max by compare the values 59 | %when [ (the jj th value > than the jj-1 th value ) AND (the jj th value > than the jj+1 th value ) 60 | %the value jj is the position of the max 61 | %the value in_data (jj) is the value of the max 62 | %do the loop by index-jj 63 | %after the max value is found,use index -kk to store in the matrix 64 | %kk=1,the start point 65 | %the last value of kk ,the end point 66 | 67 | jj=2; 68 | kk=2; 69 | while jj=in_data(jj+1) ) 71 | spmax(kk,1) = jj; 72 | spmax(kk,2) = in_data (jj); 73 | kk = kk+1; 74 | end 75 | jj=jj+1; 76 | end 77 | 78 | %end point 79 | spmax(kk,1)=dsize; 80 | spmax(kk,2)=in_data(dsize); 81 | 82 | %End point process-please see reference about spline end effect 83 | %extend the slpoe of neighbor 2 max value ---as extend value 84 | %original value of end point -----as original value 85 | %compare extend and original value 86 | 87 | if kk>=4 88 | slope1=(spmax(2,2)-spmax(3,2))/(spmax(2,1)-spmax(3,1)); 89 | tmp1=slope1*(spmax(1,1)-spmax(2,1))+spmax(2,2); 90 | if tmp1>spmax(1,2) 91 | spmax(1,2)=tmp1; 92 | end 93 | 94 | slope2=(spmax(kk-1,2)-spmax(kk-2,2))/(spmax(kk-1,1)-spmax(kk-2,1)); 95 | tmp2=slope2*(spmax(kk,1)-spmax(kk-1,1))+spmax(kk-1,2); 96 | if tmp2>spmax(kk,2) 97 | spmax(kk,2)=tmp2; 98 | end 99 | else 100 | flag=-1; 101 | end 102 | 103 | %these 4 sentence seems useless. 104 | msize=size(in_data); 105 | dsize=max(msize); 106 | xsize=dsize/3; 107 | xsize2=2*xsize; 108 | 109 | 110 | %part2.--find local min value and do end process 111 | %the syntax are all similar with part1. 112 | %here-explan with beginning local max-find upper starting envelope 113 | %the end process procedure-find out the neighbor 2 local extrema value 114 | %connect those 2 local extrema and extend the line to the end 115 | %make judgement with 1).line extend value 2).original data value 116 | %the bigger value is chosen for upper envelope end control point 117 | 118 | %local max 119 | spmin(1,1) = 1; 120 | spmin(1,2) = in_data(1); 121 | jj=2; 122 | kk=2; 123 | while jj=in_data(jj) & in_data(jj)<=in_data(jj+1)) 125 | spmin(kk,1) = jj; 126 | spmin(kk,2) = in_data (jj); 127 | kk = kk+1; 128 | end 129 | jj=jj+1; 130 | end 131 | 132 | 133 | %local min 134 | spmin(kk,1)=dsize; 135 | spmin(kk,2)=in_data(dsize); 136 | 137 | if kk>=4 138 | slope1=(spmin(2,2)-spmin(3,2))/(spmin(2,1)-spmin(3,1)); 139 | tmp1=slope1*(spmin(1,1)-spmin(2,1))+spmin(2,2); 140 | if tmp1 that are of length 4*width 6 | % or less are simply replaced with ones. 7 | %INPUTS 8 | % x: a logical row vector. 9 | % width: an integer specifying the half-width of the Hanning window. 10 | %OUTPUTS 11 | % y: a double vector of same size as . 12 | 13 | y = double(x); 14 | firstfalseidx = find([~x(1) x(1:end-1) & ~x(2:end)]); 15 | firsttrueidx = find([x(1) ~x(1:end-1) & x(2:end)]); 16 | 17 | % Match lengths, using "length(x) + 1" to denote that the run of same 18 | % values goes all the way to the end of the vector. Note that if either 19 | % length is strictly greater than the other one, then x must both start and 20 | % end with that value. 21 | if length(firstfalseidx) > length(firsttrueidx) 22 | firsttrueidx(end+1) = length(x) + 1; 23 | end 24 | if length(firsttrueidx) > length(firstfalseidx) 25 | firstfalseidx(end+1) = length(x) + 1; 26 | end 27 | 28 | % Find lengths strings of : 29 | if firstfalseidx(1) == 1 30 | % We have runs of false marked by firstfalseidx, firsttrueidx. 31 | falselengths = firsttrueidx - firstfalseidx; 32 | else 33 | % We have runs of true marked by firsttrueidx, firstfalseidx. That 34 | % means the runs of false are in between. 35 | falselengths = firsttrueidx(2:end) - firstfalseidx(1:end-1); 36 | % ...and it also means that there might be a trailing run of false at 37 | % the end that we didn't compute yet: 38 | if firstfalseidx(end) <= length(x) 39 | falselengths(end+1) = length(x) - firstfalseidx(end) + 1; 40 | end 41 | end 42 | 43 | % Fill in the short strings of and remove from index lists. 44 | % is an index into , whose length is either 45 | % length(firstfalseidx) or one shorter. 46 | isshortidx = find(falselengths <= 4 * width); 47 | false2delete = zeros(size(isshortidx)); 48 | true2delete = zeros(size(isshortidx)); 49 | for k = 1:length(isshortidx) 50 | if firstfalseidx(1) == 1 51 | % We have runs of false marked by firstfalseidx, firsttrueidx. 52 | % Whether the last run of false runs to the end or not, it ends at 53 | % firsttrueidx(end) - 1, so there is nothing to worry about. 54 | y(firstfalseidx(isshortidx(k)) : ... 55 | firsttrueidx(isshortidx(k)) - 1) = 1; 56 | false2delete(k) = isshortidx(k); 57 | true2delete(k) = isshortidx(k); 58 | else 59 | % We have runs of true marked by firsttrueidx, firstfalseidx. That 60 | % means that a run of false that goes all the way to the end will 61 | % need special handling. It also means that length(falselengths) 62 | % is length(firstfalseidx) - 1. 63 | if isshortidx(k) < length(falselengths) 64 | % Not the last run of false, nothing to worry about. 65 | y(firstfalseidx(isshortidx(k)) : ... 66 | firsttrueidx(isshortidx(k) + 1) - 1) = 1; 67 | false2delete(k) = isshortidx(k); 68 | true2delete(k) = isshortidx(k) + 1; 69 | else 70 | % isshortidx(k) is the very last entry in falselengths. 71 | % Since we are dealing with runs of true, then either 72 | % this run of false goes all the way to the end, in which 73 | % case firstfalseidx(end) <= length(x); or there is another run 74 | % of true that goes all the way to the end, in which case 75 | % firstfalseidx(end) = length(x) + 1. 76 | if firstfalseidx(end) <= length(x) 77 | y(firstfalseidx(isshortidx(k)) : end) = 1; 78 | % instead of deleting the marker for the start of the run 79 | % of false that we are filling in, we move the marker off 80 | % the end of the vector: 81 | firstfalseidx(end) = length(x) + 1; 82 | else 83 | % there is another run of true 84 | y(firstfalseidx(isshortidx(k)) : ... 85 | firsttrueidx(isshortidx(k) + 1) - 1) = 1; 86 | false2delete(k) = isshortidx(k); 87 | true2delete(k) = isshortidx(k) + 1; 88 | end 89 | end 90 | end 91 | end 92 | false2delete(false2delete==0) = []; 93 | true2delete(true2delete==0) = []; 94 | firstfalseidx(false2delete) = []; 95 | firsttrueidx(true2delete) = []; 96 | 97 | % Now smooth the transitions: 98 | hw = hanning(2*width+1)'; 99 | for k = 1:length(firstfalseidx) 100 | if firstfalseidx(k) ~= 1 101 | % Add a right-half hanning starting at firstfalseidx(k): 102 | yidx = firstfalseidx(k) : min(length(y), firstfalseidx(k) + width - 1); 103 | hwidx = width + (2 : length(yidx) + 1); 104 | y(yidx) = hw(hwidx); 105 | end 106 | if firsttrueidx(k) <= length(x) 107 | % Add a left-half hanning ending at firsttrueidx(k) - 1: 108 | yidx = max(1, firsttrueidx(k) - width) : firsttrueidx(k) - 1; 109 | hwidx = width - length(yidx) + 1 : width; 110 | y(yidx) = hw(hwidx); 111 | end 112 | end 113 | 114 | 115 | -------------------------------------------------------------------------------- /HuangEMD/ex02d.m: -------------------------------------------------------------------------------- 1 | %ex02 2 | %topic-HOW TO 3 | %"RUN EEMD or EMD and use the result to 4 | %PLOT Hilbert-Energy or Hilbert-Energy Spectrum" 5 | clear;close all 6 | clc 7 | %load data file 8 | datafile=csvread('lod78.csv'); 9 | 10 | %==============Run EEMD================================= 11 | %give noise level 12 | noiselevel=0; 13 | %give Number of ensemble 14 | Nensemble=1; 15 | %do eemd and show result 16 | EEMDIMF=eemd(datafile,noiselevel,Nensemble); 17 | %plot the EEMD result 18 | figure(1) 19 | anoiselevel=num2str(noiselevel); 20 | aNensemble=num2str(Nensemble); 21 | strips(EEMDIMF);title(['EEMD result ,noise level=',anoiselevel,' Number of ensemble=',aNensemble]); 22 | clear noiselevel Nensemble 23 | disp('===== EEMD calculation complete! =====') 24 | 25 | % %==============Run EMD================================= 26 | % %give noise level 27 | % noiselevel=0; 28 | % %give Number of ensemble 29 | % Nensemble=1; 30 | % %do eemd and show result 31 | % EMDIMF=eemd(datafile,noiselevel,Nensemble); 32 | % %plot the EMD result 33 | % figure(4) 34 | % anoiselevel=num2str(noiselevel); 35 | % aNensemble=num2str(Nensemble); 36 | % strips(EMDIMF);title(['EMD result ,noise level=',anoiselevel,' Number of ensemble=',aNensemble]); 37 | % disp('===== EMD calculation complete! =====') 38 | 39 | 40 | %==============Run Hilbert-Energy Spectrum for EEMD ======================= 41 | %give samplerate 42 | samplerate=365; 43 | %give frequency-axis resolution for hilbert-spectrum 44 | freqsol=400; 45 | %give time-axis resolution for hilbert-spectrum 46 | timesol=800; 47 | %give frequency-axis maximun value 48 | hsp_fre1=40; 49 | 50 | % dealing with EEMD result 51 | au=size(EEMDIMF);nIMF=au(2)-2;nPT=au(1)-1; 52 | totalt=(nPT+1)/samplerate; 53 | Xlow=1/totalt; 54 | Xhig=samplerate/2; 55 | 56 | %Calculate Hilbert-Energy-Spectrum(NNSPE.m) 57 | [nte,tae,fae]=nnspe(EEMDIMF(1:nPT,2:nIMF+1), 0, totalt, freqsol, timesol, 0, hsp_fre1,0,totalt); 58 | %nnspe(EEMDIMF(1:nPT,2:nIMF-1), 0, totalt, freqsol, timesol, 0.00001,%hsp_fre1,0,totalt); 59 | 60 | %Smooth the pic ,use an Gaussian Filter "q",let nt been filtered twice,HESP spectrum will be good for reading 61 | q=fspecial('gaussian', 7, 0.6); 62 | nse=filter2(q, nte);nsue=filter2(q, nse); 63 | 64 | %Plot the HESP and MS 65 | figure(2); 66 | imagesc(tae,fae,nsue.^.5);axis xy;set(gca,'FontSize',6);title('Hilbert Energy Spectrum- for EEMD','FontSize',9);xlabel('Time(sec)','FontSize',7,'VerticalAlignment','middle');ylabel('Freq(hz)','FontSize',7);ylim([Xlow hsp_fre1]); 67 | 68 | % %==============Run Hilbert-Amplitude Spectrum for EEMD ======================= 69 | % %Calculate Hilbert-Amplitude-Spectrum(NNSPE.m) 70 | % [nta,taa,faa]=nnspa(EEMDIMF(1:nPT,2:nIMF+1), 0, totalt, freqsol, timesol, 0, hsp_fre1,0,totalt); 71 | % 72 | % %Smooth the pic ,use an Gaussian Filter "q",let nt been filtered twice,HESP spectrum will be good for reading 73 | % nsa=filter2(q, nta);nsua=filter2(q, nsa); 74 | % 75 | % %Plot the HESP and MS 76 | % figure(3); 77 | % imagesc(taa,faa,nsua.^.5);axis xy;set(gca,'FontSize',6);title('Hilbert Amplitude Spectrum- for EEMD','FontSize',9);xlabel('Time(sec)','FontSize',7,'VerticalAlignment','middle');ylabel('Freq(hz)','FontSize',7);ylim([Xlow hsp_fre1]); 78 | % 79 | % 80 | % 81 | % 82 | % 83 | % clear samplerate freqsol timesol hsp_fre1 au totalt Xlow Xhig nta taa faa nte tae fae 84 | % %==============Run Hilbert-Energy Spectrum for EMD ======================= 85 | % %give samplerate 86 | % samplerate=365; 87 | % %give frequency-axis resolution for hilbert-spectrum 88 | % freqsol=400; 89 | % %give time-axis resolution for hilbert-spectrum 90 | % timesol=800; 91 | % %give frequency-axis maximun value 92 | % hsp_fre1=40; 93 | % 94 | % % dealing with EEMD result 95 | % au=size(EMDIMF);nIMF=au(2)-2;nPT=au(1)-1; 96 | % totalt=(nPT+1)/samplerate; 97 | % Xlow=1/totalt; 98 | % Xhig=samplerate/2; 99 | % %Calculate Hilbert-Amplitude-Spectrum(NNSPE.m) 100 | % [nte,tae,fae]=nnspe(EMDIMF(1:nPT,2:nIMF+1), 0, totalt, freqsol, timesol, 0, hsp_fre1,0,totalt); 101 | % 102 | % %Smooth the pic ,use an Gaussian Filter "q",let nt been filtered twice,HESP spectrum will be good for reading 103 | % nse=filter2(q, nte);nsue=filter2(q, nse); 104 | % 105 | % %Plot the HESP and MS 106 | % figure(5); 107 | % imagesc(tae,fae,nsue.^.5);axis xy;set(gca,'FontSize',6);title('Hilbert Energy Spectrum- for EMD','FontSize',9);xlabel('Time(sec)','FontSize',7,'VerticalAlignment','middle');ylabel('Freq(hz)','FontSize',7);ylim([Xlow hsp_fre1]); 108 | % 109 | % %==============Run Hilbert-Amplitude Spectrum for EEMD ======================= 110 | % %Calculate Hilbert-Amplitude-Spectrum(NNSPE.m) 111 | % [nta,taa,faa]=nnspa(EMDIMF(1:nPT,2:nIMF+1), 0, totalt, freqsol, timesol, 0, hsp_fre1,0,totalt); 112 | % 113 | % %Smooth the pic ,use an Gaussian Filter "q",let nt been filtered twice,HESP spectrum will be good for reading 114 | % nsa=filter2(q, nta);nsua=filter2(q, nsa); 115 | % 116 | % %Plot the HESP and MS 117 | % figure(6); 118 | % imagesc(taa,faa,nsua.^.5);axis xy;set(gca,'FontSize',6);title('Hilbert Amplitude Spectrum- for EMD','FontSize',9);xlabel('Time(sec)','FontSize',7,'VerticalAlignment','middle');ylabel('Freq(hz)','FontSize',7);ylim([Xlow hsp_fre1]); 119 | % 120 | 121 | -------------------------------------------------------------------------------- /HuangEMD/eemd.m: -------------------------------------------------------------------------------- 1 | %function allmode=eemd(Y,Nstd,NE) 2 | % 3 | % This is an EMD/EEMD program 4 | % 5 | % INPUT: 6 | % Y: Inputted data;1-d data only 7 | % Nstd: ratio of the standard deviation of the added noise and that of Y; 8 | % NE: Ensemble number for the EEMD 9 | % OUTPUT: 10 | % A matrix of N*(m+1) matrix, where N is the length of the input 11 | % data Y, and m=fix(log2(N))-1. Column 1 is the original data, columns 2, 3, ... 12 | % m are the IMFs from high to low frequency, and comlumn (m+1) is the 13 | % residual (over all trend). 14 | % 15 | % NOTE: 16 | % It should be noted that when Nstd is set to zero and NE is set to 1, the 17 | % program degenerates to a EMD program.(for EMD Nstd=0,NE=1) 18 | % This code limited sift number=10 ,the stoppage criteria can't change. 19 | % 20 | % References: 21 | % Wu, Z., and N. E Huang (2008), 22 | % Ensemble Empirical Mode Decomposition: a noise-assisted data analysis method. 23 | % Advances in Adaptive Data Analysis. Vol.1, No.1. 1-41. 24 | % 25 | % code writer: Zhaohua Wu. 26 | % footnote:S.C.Su 2009/03/04 27 | % 28 | % There are three loops in this code coupled together. 29 | % 1.read data, find out standard deviation ,devide all data by std 30 | % 2.evaluate TNM as total IMF number--eq1. 31 | % TNM2=TNM+2,original data and residual included in TNM2 32 | % assign 0 to TNM2 matrix 33 | % 3.Do EEMD NE times-------------------------------------------------------------loop EEMD start 34 | % 4.add noise 35 | % 5.give initial values before sift 36 | % 6.start to find an IMF------------------------------------------------IMF loop start 37 | % 7.sift 10 times to get IMF--------------------------sift loop start and end 38 | % 8.after 10 times sift --we got IMF 39 | % 9.subtract IMF from data ,and let the residual to find next IMF by loop 40 | % 6.after having all the IMFs---------------------------------------------IMF loop end 41 | % 9.after TNM IMFs ,the residual xend is over all trend 42 | % 3.Sum up NE decomposition result-------------------------------------------------loop EEMD end 43 | % 10.Devide EEMD summation by NE,std be multiply back to data 44 | % 45 | % Association: no 46 | % this function ususally used for doing 1-D EEMD with fixed 47 | % stoppage criteria independently. 48 | % 49 | % Concerned function: extrema.m 50 | % above mentioned m file must be put together 51 | 52 | function allmode=eemd(Y,Nstd,NE) 53 | 54 | %part1.read data, find out standard deviation ,devide all data by std 55 | xsize=length(Y); 56 | dd=1:1:xsize; 57 | Ystd=std(Y); 58 | Y=Y/Ystd; 59 | 60 | %part2.evaluate TNM as total IMF number,ssign 0 to TNM2 matrix 61 | TNM=fix(log2(xsize))-1; 62 | TNM2=TNM+2; 63 | for kk=1:1:TNM2, 64 | for ii=1:1:xsize, 65 | allmode(ii,kk)=0.0; 66 | end 67 | end 68 | 69 | %part3 Do EEMD -----EEMD loop start 70 | for iii=1:1:NE, %EEMD loop -NE times EMD sum together 71 | 72 | %part4 --Add noise to original data,we have X1 73 | for i=1:xsize, 74 | temp=randn(1,1)*Nstd; 75 | X1(i)=Y(i)+temp; 76 | end 77 | 78 | %part4 --assign original data in the first column 79 | for jj=1:1:xsize, 80 | mode(jj,1) = Y(jj); 81 | end 82 | 83 | %part5--give initial 0 to xorigin and xend 84 | xorigin = X1; 85 | xend = xorigin; 86 | 87 | %part6--start to find an IMF-----IMF loop start 88 | nmode = 1; 89 | while nmode <= TNM, 90 | xstart = xend; %last loop value assign to new iteration loop 91 | %xstart -loop start data 92 | iter = 1; %loop index initial value 93 | 94 | %part7--sift 10 times to get IMF---sift loop start 95 | while iter<=10, 96 | [spmax, spmin, flag]=extrema(xstart); %call function extrema 97 | %the usage of spline ,please see part11. 98 | upper= spline(spmax(:,1),spmax(:,2),dd); %upper spline bound of this sift 99 | lower= spline(spmin(:,1),spmin(:,2),dd); %lower spline bound of this sift 100 | mean_ul = (upper + lower)/2;%spline mean of upper and lower 101 | xstart = xstart - mean_ul;%extract spline mean from Xstart 102 | iter = iter +1; 103 | end 104 | %part7--sift 10 times to get IMF---sift loop end 105 | 106 | %part8--subtract IMF from data ,then let the residual xend to start to find next IMF 107 | xend = xend - xstart; 108 | 109 | nmode=nmode+1; 110 | 111 | %part9--after sift 10 times,that xstart is this time IMF 112 | for jj=1:1:xsize, 113 | mode(jj,nmode) = xstart(jj); 114 | end 115 | 116 | end 117 | %part6--start to find an IMF-----IMF loop end 118 | 119 | %part 10--after gotten all(TNM) IMFs ,the residual xend is over all trend 120 | % put them in the last column 121 | for jj=1:1:xsize, 122 | mode(jj,nmode+1)=xend(jj); 123 | end 124 | %after part 10 ,original +TNM-IMF+overall trend ---those are all in mode 125 | allmode=allmode+mode; 126 | 127 | end 128 | %part3 Do EEMD -----EEMD loop end 129 | 130 | %part10--devide EEMD summation by NE,std be multiply back to data 131 | allmode=allmode/NE; 132 | allmode=allmode*Ystd; 133 | 134 | %part11--the syntax of the matlab function spline 135 | %yy= spline(x,y,xx); this means 136 | %x and y are matrixs of n1 points ,use n1 set (x,y) to form the cubic spline 137 | %xx and yy are matrixs of n2 points,we want know the spline value yy(y-axis) in the xx (x-axis)position 138 | %after the spline is formed by n1 points ,find coordinate value on the spline for [xx,yy] --n2 position. 139 | 140 | -------------------------------------------------------------------------------- /HuangEMD/confidenceLine.m: -------------------------------------------------------------------------------- 1 | % function sigline= confidenceLine(percenta,Npt) 2 | % 3 | % INPUT: 4 | % percenta: a parameter having a value between 0.0 ~ 1.0, e.g., 0.05 5 | % represents 95% confidence level (upper bound); and 0.95 6 | % represents 5% confidence level (lower bound) 7 | % Npt: the data point number of original data 8 | % Npt means the degree of freedom (Ndof)of the timeseries. 9 | % OUTPUT: 10 | % sigline: a two column matrix, with the first column the natural 11 | % logarithm of mean period, and the second column the 12 | % natural logarithm of mean energy for significance line 13 | % For this program to work well, the minimum Npt is 36 14 | % 15 | % NOTE: 16 | % this code is used to obtain the "percenta" line based on Wu and Huang (2004). 17 | % 1.from the reference paper,our goal is to recognize the statistical meanings of IMF copoment. 18 | % First,they investigate into WhiteNoise signal about the PDF distribution on lnE-lnT plan. 19 | % When the IMF (lnT,lnE) values been plot with the whiteNoise values,we can say that 20 | % some IMFs have the same pattern with WhiteNoise,those IMFs might be meaningless signals. 21 | % 2.WhiteNoise distribution on lnE-lnT plan is calsulated in this code.Here we calculate all 22 | % the values from log(1)~log(Ndof) ----the x axis values. 23 | % 3.Use the principle of 'slope=-1' for lnE-lnT value,y-bar=-x 24 | % upper and lower bound of y value for the chi-square distribution for every x-axis values. 25 | % the yUpper=0,yLower=-3-xx,between those upper and lower-[y range],the line been devided into 5000 segments. 26 | % then [y range,y-bar,Ndof]be the parameter to calculate the probability density function use dist_value.m 27 | % 4.after knowing the PDF for every y,under every x,we can think that's z--the PDF 28 | % 5.connect the PDF for the confidence level,that's the answer. 29 | % percenta-it's the width coefficients of the PDF confidence limit. 30 | % 31 | % References: 32 | % 'A study of the characteristics of white noise using the empirical mode decomposition method' 33 | % Zhaohua,Wu and Norden E. Huang 34 | % Proc. R. Soc. Lond. A (2004) 460,1597-1611 35 | % 36 | % 37 | % code writer: zhwu@cola.iges.org 38 | % code writer: S.C.Su-separate significance.m into 3 parts.2009/05/31 39 | % this part is use Npt to form WhiteNoise confidence limt curve. 40 | % footnote:S.C.Su 2009/05/31 41 | % 42 | % 1.use input parameter to initialize 43 | % 2.form x axis(lnT) 44 | % 3.form y axis(lnE),by y=-x relationship 45 | % 4.because y value is chi-square distributed,form [yUpper,yLower] for every x value 46 | % 5.calculate the confidence limit position with each x value-loop start 47 | % 6.determine the percenta values by interpolation 48 | % 5.calculate the confidence limit position with each x value-loop end 49 | % 50 | % Association: 51 | % 1. this function only calculate confidence curve under Ndof on the lnT-lnE plane. 52 | % the result 'sigline' is a 2D matrix --the [x,y] coordinate values for the curve 53 | % 2. there should be another codes,finding [lnT,lnE] values of the IMF 54 | % 3. plot 1. and 2. values together ,determine the statistical meaning of IMF 55 | % 56 | % Concerned function: dist_value, 57 | % the others are matlab functions. 58 | % 59 | 60 | function sigline= confidenceLine(percenta,Npt) 61 | 62 | %1.use input parameter to initialize 63 | nDof = Npt;%Degree of freedom=number of data points 64 | pdMax = fix(log(nDof))+1;%pdMax-max X axis value 65 | % 66 | %2.form x axis(lnT) 67 | pdIntv = linspace(1,pdMax,100);%divide [1~pdMaxX] into 100 segments,form the whole x axis values 68 | %3.form y axis(lnE),by y=-x relationship 69 | yBar = -pdIntv;%determine y-bar by slpoe,yBar=-x 70 | %4.because y value is chi-square distributed,form [yUpper,yLower] for every x value 71 | for i=1:100, 72 | %for every x value,define [yUpper,yLower] by chi-square 73 | yUpper(i)=0;%chi-square start from 0 74 | yLower(i)= -3-pdIntv(i)*pdIntv(i);%chi-square decayed to zero,Wu use -3-xx as lower bound 75 | end 76 | 77 | %5.calculate the confidence limit position with each x value---loop start 78 | for i=1:100, 79 | sigline(i,1)=pdIntv(i);%x-dir values assign to the line 80 | % divide [yUpper,yLowe] into 5000segments as the domain to investigate PDF for WhiteNoise 81 | yPos=linspace(yUpper(i),yLower(i),5000); 82 | dyPos=yPos(1)-yPos(2); 83 | yPDF=dist_value(yPos,yBar(i),nDof);%Call dist_value.m to calculate exact PDF distribution in y-dir 84 | 85 | %6.determine the percenta values by interpolation 86 | 87 | %determine the sum of PDF 88 | sum = 0.0; 89 | for jj=1:5000, 90 | sum = sum + yPDF(jj);%total accumulated PDF value 91 | end 92 | 93 | %inorder to find percenta--for some accurate value 94 | %but the [yUpper,yLower] is changing with x value 95 | %so it needs interpolation 96 | jj1=0; 97 | jj2=1; 98 | psum1=0.0; 99 | psum2=yPDF(1); 100 | pratio1=psum1/sum; 101 | pratio2=psum2/sum; 102 | 103 | %interpolate 104 | while pratio2 < percenta, 105 | jj1=jj1+1; 106 | jj2=jj2+1; 107 | psum1=psum1+yPDF(jj1); 108 | psum2=psum2+yPDF(jj2); 109 | pratio1=psum1/sum; 110 | pratio2=psum2/sum; 111 | yref=yPos(jj1); 112 | end 113 | sigline(i,2) = yref + dyPos*(pratio2-percenta)/(pratio2-pratio1);%the interpolation result 114 | %the values are extra modified to more accurate position by Dr.Wu 115 | sigline(i,2) = sigline(i,2) + 0.066*pdIntv(i) + 0.12; 116 | %confidence values assign to the line 117 | end 118 | %5.calculate the confidence limit position with each x value-loop end 119 | 120 | sigline=1.4427*sigline; %multiply the translation coeifficient ,1.4427=log(e)/log(2) -------------------------------------------------------------------------------- /HuangEMD/FAzc.m: -------------------------------------------------------------------------------- 1 | %function [f,a]=FAzc(data,dt) 2 | % 3 | % The function FAzc generates a frequency and amplitude using zero-crossing method 4 | % applied to data(n,k), where n specifies the length of time series, 5 | % and k is the number of IMFs. 6 | % Non MATLAB Library routine used in the function is: FINDCRITICALPOINTS. 7 | % 8 | % Calling sequence- 9 | % [f,a]=fazc(data,dt) 10 | % 11 | % Input- 12 | % data - 2-D matrix of IMF components 13 | % dt - time increment per point 14 | % Output- 15 | % f - 2-D matrix f(n,k) that specifies frequency 16 | % a - 2-D matrix a(n,k) that specifies amplitude 17 | % 18 | % Used by- 19 | % FA 20 | % See also- 21 | % FAZPLUS, which in addition to frequency and amplitude, outputs 22 | % other fields. 23 | 24 | %written by 25 | % Kenneth Arnold (NASA GSFC) Summer 2003, Modified 26 | % Xianyao Chen Sep. 20 created following the splinenormalize.m 27 | % S.C.Su Sep. 2009(NCU Rcada)rename faz.m as fazc.m 28 | % in order to integrate all m file in a group type 29 | %%footnote: S.C.Su (2009/09/07) 30 | % 31 | % There are two loops in this code ,it's dealing with IMF by IMF .checking wave by wave 32 | % 0. set the default value and initialize 33 | % 1. process IMF by IMF ---loop A start 34 | % 2. Find all critical points (max,min,zeros) in an IMF 35 | % 3. integrate those information from previous waveform 36 | % 4. calculate by zero-crossing method----loop B start 37 | % 4.1 estimate for the quarter waves(weighting coefficients included already) 38 | % 4.2 estimate for the half waves(weighting coefficients included already) 39 | % 4.3 estimate for the whole waves(weighting coefficients included already) 40 | % 5. add-in those information from previous waveform 41 | % 6. calculate current frequency and amplitude 42 | % 7. pass current information to next waveform calculation 43 | % 4. calculate by zero-crossing method----loop B end 44 | % 1. process IMF by IMF ---loop A end 45 | % 46 | % 47 | 48 | function [f,a]=FAzc(data,dt) 49 | 50 | % 0. set the default value and initialize 51 | %----- Get dimensions 52 | [nPoints, nIMF] = size(data); 53 | 54 | %----- Flip data if necessary 55 | flipped=0; 56 | if nPoints < nIMF 57 | %----- Flip data set 58 | data = data'; 59 | [nPoints, nIMF] = size(data); 60 | flipped=1; 61 | end 62 | 63 | %----- Inverse dt 64 | idt = 1/dt; 65 | 66 | %----- Preallocate arrays 67 | f = zeros(nPoints,nIMF); 68 | a = f; 69 | 70 | %1. process IMF by IMF ---loop A start 71 | %----- Process each IMF 72 | for c=1:nIMF %loop A--start 73 | 74 | %2. Find all critical points (max,min,zeros) in an IMF 75 | %The function FINDCRITICALPOINTS returns max, min and zero crossing values and their coordinates in the order found 76 | [allX, allY] = findcriticalpoints(data(:,c)); 77 | nCrit = length(allX); %number of critical points 78 | 79 | if nCrit <= 1 80 | %----- Too few critical points; keep looping 81 | continue; 82 | end 83 | 84 | %3. integrate those information from previous waveform 85 | %----- Initialize previous calculated frequencies 86 | f2prev1 = NaN; 87 | f4prev1 = NaN; 88 | f4prev2 = NaN; 89 | f4prev3 = NaN; 90 | 91 | %----- Initialize previous calculated amplitudes 92 | a2prev1 = NaN; 93 | a4prev1 = NaN; 94 | a4prev2 = NaN; 95 | a4prev3 = NaN; 96 | 97 | %4. calculate by zero-crossing method----loop B start 98 | %start to calculate the zero-crossing frequency and amplitude 99 | %exam them position by position 100 | for i=1:nCrit-1 101 | 102 | %----- Estimate current frequency 103 | cx = allX(i); %current value at i position of critical points 104 | 105 | %4.1 estimate for the quarter waves(weighting coefficients included already) 106 | f1 = idt / (allX(i+1)-cx); %f1 is the frequency calculated by quarter wave 107 | a1 = 4*max(abs(allY(i:i+1))); %a1 is the amplitude calculated by quarter wave 108 | npt = 4; 109 | ftotal = f1; 110 | atotal = a1; 111 | 112 | %4.2 estimate for the half waves(weighting coefficients included already) 113 | if i+2<=nCrit 114 | f2cur = idt / (allX(i+2)-cx); %f2cur is the frequency calculated by half wave 115 | range = allY(i:i+2); 116 | ext = range(range~=0); %findout position index for the max or min value 117 | a2cur = 2*mean(abs(ext)); %a2cur is the amplitude calculated by half wave 118 | npt = npt+2; %jump to next half wave 119 | ftotal = ftotal+f2cur; 120 | atotal = atotal+a2cur; 121 | else 122 | f2cur=NaN; 123 | end 124 | 125 | %4.3 estimate for the whole waves(weighting coefficients included already) 126 | if i+4<=nCrit 127 | f4cur = idt / (allX(i+4)-cx); %f4cur is the frequency calculated by whole wave 128 | range = allY(i:i+4); 129 | ext = range(range~=0); %findout position index for the max or min value 130 | a4cur = mean(abs(ext)); %a4cur is the amplitude calculated by whole wave 131 | npt = npt+1; %jump to next whole wave 132 | ftotal = ftotal+f4cur; 133 | atotal=atotal+a4cur; 134 | else 135 | f4cur=NaN; 136 | end 137 | 138 | %5. add-in those information from previous waveform 139 | %----- Add previous points if they are valid 140 | if ~isnan(f2prev1) 141 | npt=npt+2; 142 | ftotal=ftotal+f2prev1; 143 | atotal=atotal+a2prev1; 144 | end 145 | if ~isnan(f4prev1) 146 | npt=npt+1; 147 | ftotal=ftotal+f4prev1; 148 | atotal=atotal+a4prev1; 149 | end 150 | if ~isnan(f4prev2) 151 | npt=npt+1; 152 | ftotal=ftotal+f4prev2; 153 | atotal=atotal+a4prev2; 154 | end 155 | if ~isnan(f4prev3) 156 | npt=npt+1; 157 | ftotal=ftotal+f4prev3; 158 | atotal=atotal+a4prev3; 159 | end 160 | 161 | %6. calculate current frequency and amplitude 162 | %total npt times wave been included ,so divided by npt 163 | %General formula for frequency and amplitude when using zero-crossing method 164 | f(ceil(allX(i)):floor(allX(i+1)),c) = ftotal/npt; 165 | a(ceil(allX(i)):floor(allX(i+1)),c) = atotal/npt; 166 | 167 | %7. pass current information to next waveform calculation 168 | %assign the frequency values for next position calculation 169 | f2prev1=f2cur; 170 | f4prev3=f4prev2; 171 | f4prev2=f4prev1; 172 | f4prev1=f4cur; 173 | 174 | a2prev1=a2cur; 175 | a4prev3=a4prev2; 176 | a4prev2=a4prev1; 177 | a4prev1=a4cur; 178 | 179 | %exam them position by position----loop B end 180 | end 181 | %4. calculate by zero-crossing method----loop B end 182 | 183 | %----- Fill in ends for amplitude anbd frequency 184 | f(1:ceil(allX(1))-1,c) = f(ceil(allX(1)),c); 185 | f(floor(allX(nCrit))+1:nPoints,c) = f(floor(allX(nCrit)),c); 186 | 187 | a(1:ceil(allX(1))-1,c) = a(ceil(allX(1)),c); 188 | a(floor(allX(nCrit))+1:nPoints,c) = a(floor(allX(nCrit)),c); 189 | 190 | %loop A--end 191 | end 192 | %1. process IMF by IMF ---loop A end 193 | 194 | %----- Flip again if data was flipped at the beginning 195 | if (flipped) 196 | f=f'; 197 | a=a'; 198 | end 199 | -------------------------------------------------------------------------------- /EMDOS.tar/emdos.m: -------------------------------------------------------------------------------- 1 | function [imf,ort,nder] = emdos(varargin) 2 | %% EMDOS : Computes the EMD by Optimisation on Splines, implements the method described in [1]. Uses either the standard EMD [2,3] or the OS algorithm [1]. 3 | % 4 | % SYNTAX 5 | % imf = emdos(s) 6 | % imf = emdos(s,...,'Option_name',Option_value,...) 7 | % imf = emdos(s,opt) 8 | % [imf,ort,order_der] = emdos(...) 9 | % 10 | % INPUTS 11 | % s : one-dimensional real signal 12 | % opt : field of optional parameters. They are listed below together with 13 | % the default value 14 | % method <'os'> : 'emd' or 'emdni' or 'os' 15 | % stop <'f'> : Kind of stopping criterion : 'h' for the Huang one [2] and 16 | % 'f' for Flandrin and Rilling's one [3]. 17 | % alpha <0.05> : Stopping criterion parameter [3]. 18 | % maxmodes <10> : Maximum number of IMFs 19 | % t : Time vector t 20 | % orderder <-1> : Differentiation order for each mode : either a vector or a scalar. 21 | % -1 : automatic procedure described in [1] 22 | % 0, 2 or 4 : uses the 0th, 2th or 4th 23 | % derivative respectively. 24 | % sporder <8> : Order of the splines used in the os method. 25 | % sporder>=6 and <=12, must be even. 26 | % 27 | % OUTPUTS 28 | % imf : (number of IMFs +1) X length(s) array which contains each IMFs 29 | % and a residue 30 | % ort : orthogonality index defined in [3,1] 31 | % nder : derivation order used for each IMF (array of length the 32 | % number of IMFs). 33 | % EXAMPLE 34 | % n = 1024; 35 | % t = linspace(0,1,n); 36 | % s = cos(60*pi*(t+1).^2) + (1+exp(-18*(t-0.5).^2)).*sin(50*pi*t); 37 | % opt.maxmodes = 1; 38 | % opt.sporder = 8; 39 | % opt.alpha= 0.05; 40 | % opt.stop = 'f'; 41 | % opt.orderder = [2 0] 42 | % opt.method = 'os'; 43 | % opt.t = t; 44 | % [imf ort nder] = emdos(s,opt); 45 | % plot_imf(imf,t,''); 46 | % 47 | % REFERENCES 48 | % [1] T. Oberlin, S. Meignen and V. Perrier, “An Alternative Formulation 49 | % for the Empirical Mode Decomposition”, IEEE Trans. on Signal 50 | % Prcessing, to appear. 51 | % [2] N. Huang, Z. Shen, S. Long, M. Wu, H. Shih, Q. Zheng, N. Yen, C. Tung, and H. Liu, “The empirical mode decomposition 52 | % and the Hilbert spectrum for nonlinear and non-stationary time series analysis,” Proceedings of the Royal Society : 53 | % Mathematical, Physical and Engineering Sciences, vol. 454, no. 1971, pp. 903–995, 1998. 54 | % [3] G. Rilling, P. Flandrin, and P. Gonc¸alv`es, “On empirical mode decomposition and its algorithms,” in IEEE-EURASIP 55 | % workshop on nonlinear signal and image processing NSIP-03, Grado (I), 2003. 56 | % 57 | % Thomas Oberlin 58 | % 12.2011 59 | % thomas.oberlin@imag.fr 60 | 61 | 62 | 63 | 64 | 65 | % Gets the parameter 66 | [s,stop,alpha,maxmodes,t,method,orderder,liss,sporder] = init(varargin{:}); 67 | 68 | k = 1; 69 | r=s; 70 | imf = []; 71 | 72 | %main loop : requires at least 3 extrema to proceed 73 | while ~stop_emd(r) && (k < maxmodes+1 || maxmodes == 0) 74 | ar=r; 75 | switch(method) 76 | 77 | % OS method 78 | case 'os' 79 | % Computation of m0 80 | [indexC tau m0 m0eval aux] = build_m0(r,t,orderder(k),sporder); 81 | nder(k) = aux; 82 | knots = aptknt(tau,sporder); 83 | L = length(indexC); 84 | m0 = m0'; 85 | ti = t(indexC); 86 | D = matrice_der(knots,2,sporder); 87 | pass = matrice_pass(knots,ti,sporder); 88 | A = build_lambda(ti,sporder); 89 | A1 = (eye(L,L)+A)*pass; 90 | b1 = alpha*abs((eye(L,L)-A)*(r(indexC)'-pass*m0)); 91 | auxb1 = (eye(L,L)+A)*(r(indexC)'); 92 | bb=vertcat(b1-auxb1,b1+auxb1); 93 | AA=vertcat(-A1,A1); 94 | 95 | % Computes x0 in the set of constraints 96 | x0 = pass\(s(indexC)'); 97 | if sum(isnan(x0))>=eps 98 | x0 = []; 99 | end 100 | 101 | optis = optimset('MaxIter',20000,'LargeScale','off'); 102 | [m,fval,exitflag] = quadprog(D,[],AA,bb,[],[],[],[],x0,optis); 103 | 104 | % Evaluation 105 | msp = spmak(knots,m'); 106 | meval = fnval(msp,t); 107 | m = meval; 108 | r = r-m; 109 | 110 | 111 | case 'emd' 112 | tmp = r; 113 | % padding 114 | [indmin,indmax] = extr(tmp); 115 | indmin=indmin+floor(liss(k)); 116 | indmax=indmax+floor(liss(k)); 117 | 118 | % Sifting 119 | stop_sift=0; 120 | aux=0; 121 | 122 | while ~stop_sift 123 | [tmin,tmax,mmin,mmax] = boundary_conditions(indmin,indmax,t,r,r,6); 124 | envmin = interp1(tmin,mmin,t,'spline'); 125 | envmax = interp1(tmax,mmax,t,'spline'); 126 | envmoy = (envmin+envmax)/2; 127 | nr = r-envmoy; 128 | 129 | 130 | switch(stop) 131 | case 'f' 132 | % Flandrin 133 | amp = mean(abs(envmax-envmin))/2; 134 | sx = abs(envmoy)./amp; 135 | stop_sift = ~(mean(sx > alpha) > 0.05 | any(sx > 10*alpha)); 136 | case 'h' 137 | % Huang 138 | stop_sift = norm(nr-r)/(norm(r)+eps) < alpha; 139 | end 140 | 141 | if ~stop_sift 142 | r=nr; 143 | aux=aux+1; 144 | [indmin,indmax] = extr(r); 145 | end 146 | nder(k)=aux; 147 | end 148 | 149 | 150 | case 'emdni' 151 | % Computation of m0 152 | [indmin indmax] = build_m02(r,t,orderder(k),liss(k)); 153 | 154 | % Sifting 155 | stop_sift=0; 156 | aux=0; 157 | 158 | while ~stop_sift 159 | [tmin,tmax,mmin,mmax] = boundary_conditions(indmin,indmax,t,r,r,6); 160 | envmin = interp1(tmin,mmin,t,'spline'); 161 | envmax = interp1(tmax,mmax,t,'spline'); 162 | envmoy = (envmin+envmax)/2; 163 | nr = r-envmoy; 164 | 165 | 166 | switch(stop) 167 | case 'f' 168 | % Flandrin 169 | amp = mean(abs(envmax-envmin))/2; 170 | sx = abs(envmoy)./amp; 171 | stop_sift = ~(mean(sx > alpha) > 0.05 | any(sx > 10*alpha)); 172 | case 'h' 173 | % Huang 174 | stop_sift = norm(nr-r)/(norm(r)+eps) < alpha; 175 | end 176 | 177 | if ~stop_sift 178 | r=nr; 179 | aux=aux+1; 180 | %[indmin,indmax] = extr(r); % Sans lissage 181 | [indmin indmax] = build_m02(r,t,orderder(k),liss(k)); % Avec lissage 182 | end 183 | nder(k)=aux; 184 | end 185 | 186 | 187 | otherwise 188 | error('Attention : wrong value of field ''method'''); 189 | end 190 | 191 | imf(k,:) = r'; 192 | r=ar-r; 193 | k=k+1; 194 | end 195 | ort = io(s,imf); 196 | 197 | % Residu 198 | imf(k,:) = r'; 199 | 200 | end -------------------------------------------------------------------------------- /minimalEMDOS/emdos.m: -------------------------------------------------------------------------------- 1 | function [imf,ort,nder] = emdos(varargin) 2 | %% EMDOS : Computes the EMD by Optimisation on Splines, implements the method described in [1]. Uses either the standard EMD [2,3] or the OS algorithm [1]. 3 | % 4 | % SYNTAX 5 | % imf = emdos(s) 6 | % imf = emdos(s,...,'Option_name',Option_value,...) 7 | % imf = emdos(s,opt) 8 | % [imf,ort,order_der] = emdos(...) 9 | % 10 | % INPUTS 11 | % s : one-dimensional real signal 12 | % opt : field of optional parameters. They are listed below together with 13 | % the default value 14 | % method <'os'> : 'emd' or 'emdni' or 'os' 15 | % stop <'f'> : Kind of stopping criterion : 'h' for the Huang one [2] and 16 | % 'f' for Flandrin and Rilling's one [3]. 17 | % alpha <0.05> : Stopping criterion parameter [3]. 18 | % maxmodes <10> : Maximum number of IMFs 19 | % t : Time vector t 20 | % orderder <-1> : Differentiation order for each mode : either a vector or a scalar. 21 | % -1 : automatic procedure described in [1] 22 | % 0, 2 or 4 : uses the 0th, 2th or 4th 23 | % derivative respectively. 24 | % sporder <8> : Order of the splines used in the os method. 25 | % sporder>=6 and <=12, must be even. 26 | % 27 | % OUTPUTS 28 | % imf : (number of IMFs +1) X length(s) array which contains each IMFs 29 | % and a residue 30 | % ort : orthogonality index defined in [3,1] 31 | % nder : derivation order used for each IMF (array of length the 32 | % number of IMFs). 33 | % EXAMPLE 34 | % n = 1024; 35 | % t = linspace(0,1,n); 36 | % s = cos(60*pi*(t+1).^2) + (1+exp(-18*(t-0.5).^2)).*sin(50*pi*t); 37 | % opt.maxmodes = 1; 38 | % opt.sporder = 8; 39 | % opt.alpha= 0.05; 40 | % opt.stop = 'f'; 41 | % opt.orderder = [2 0] 42 | % opt.method = 'os'; 43 | % opt.t = t; 44 | % [imf ort nder] = emdos(s,opt); 45 | % plot_imf(imf,t,''); 46 | % 47 | % REFERENCES 48 | % [1] T. Oberlin, S. Meignen and V. Perrier, “An Alternative Formulation 49 | % for the Empirical Mode Decomposition”, IEEE Trans. on Signal 50 | % Prcessing, to appear. 51 | % [2] N. Huang, Z. Shen, S. Long, M. Wu, H. Shih, Q. Zheng, N. Yen, C. Tung, and H. Liu, “The empirical mode decomposition 52 | % and the Hilbert spectrum for nonlinear and non-stationary time series analysis,” Proceedings of the Royal Society : 53 | % Mathematical, Physical and Engineering Sciences, vol. 454, no. 1971, pp. 903–995, 1998. 54 | % [3] G. Rilling, P. Flandrin, and P. Gonc¸alv`es, “On empirical mode decomposition and its algorithms,” in IEEE-EURASIP 55 | % workshop on nonlinear signal and image processing NSIP-03, Grado (I), 2003. 56 | % 57 | % Thomas Oberlin 58 | % 12.2011 59 | % thomas.oberlin@imag.fr 60 | 61 | 62 | 63 | 64 | 65 | % Gets the parameter 66 | [s,stop,alpha,maxmodes,t,method,orderder,liss,sporder] = init(varargin{:}); 67 | 68 | k = 1; 69 | r=s; 70 | imf = []; 71 | 72 | %main loop : requires at least 3 extrema to proceed 73 | while ~stop_emd(r) && (k < maxmodes+1 || maxmodes == 0) 74 | ar=r; 75 | switch(method) 76 | 77 | % OS method 78 | case 'os' 79 | % Computation of m0 80 | [indexC tau m0 m0eval aux] = build_m0(r,t,orderder(k),sporder); 81 | nder(k) = aux; 82 | knots = aptknt(tau,sporder); 83 | L = length(indexC); 84 | m0 = m0'; 85 | ti = t(indexC); 86 | D = matrice_der(knots,2,sporder); 87 | pass = matrice_pass(knots,ti,sporder); 88 | A = build_lambda(ti,sporder); 89 | A1 = (eye(L,L)+A)*pass; 90 | b1 = alpha*abs((eye(L,L)-A)*(r(indexC)'-pass*m0)); 91 | auxb1 = (eye(L,L)+A)*(r(indexC)'); 92 | bb=vertcat(b1-auxb1,b1+auxb1); 93 | AA=vertcat(-A1,A1); 94 | 95 | % Computes x0 in the set of constraints 96 | x0 = pass\(s(indexC)'); 97 | if sum(isnan(x0))>=eps 98 | x0 = []; 99 | end 100 | 101 | optis = optimset('MaxIter',20000,'LargeScale','off'); 102 | [m,fval,exitflag] = quadprog(D,[],AA,bb,[],[],[],[],x0,optis); 103 | 104 | % Evaluation 105 | msp = spmak(knots,m'); 106 | meval = fnval(msp,t); 107 | m = meval; 108 | r = r-m; 109 | 110 | 111 | case 'emd' 112 | tmp = r; 113 | % padding 114 | [indmin,indmax] = extr(tmp); 115 | indmin=indmin+floor(liss(k)); 116 | indmax=indmax+floor(liss(k)); 117 | 118 | % Sifting 119 | stop_sift=0; 120 | aux=0; 121 | 122 | while ~stop_sift 123 | [tmin,tmax,mmin,mmax] = boundary_conditions(indmin,indmax,t,r,r,6); 124 | envmin = interp1(tmin,mmin,t,'spline'); 125 | envmax = interp1(tmax,mmax,t,'spline'); 126 | envmoy = (envmin+envmax)/2; 127 | nr = r-envmoy; 128 | 129 | 130 | switch(stop) 131 | case 'f' 132 | % Flandrin 133 | amp = mean(abs(envmax-envmin))/2; 134 | sx = abs(envmoy)./amp; 135 | stop_sift = ~(mean(sx > alpha) > 0.05 | any(sx > 10*alpha)); 136 | case 'h' 137 | % Huang 138 | stop_sift = norm(nr-r)/(norm(r)+eps) < alpha; 139 | end 140 | 141 | if ~stop_sift 142 | r=nr; 143 | aux=aux+1; 144 | [indmin,indmax] = extr(r); 145 | end 146 | nder(k)=aux; 147 | end 148 | 149 | 150 | case 'emdni' 151 | % Computation of m0 152 | [indmin indmax] = build_m02(r,t,orderder(k),liss(k)); 153 | 154 | % Sifting 155 | stop_sift=0; 156 | aux=0; 157 | 158 | while ~stop_sift 159 | [tmin,tmax,mmin,mmax] = boundary_conditions(indmin,indmax,t,r,r,6); 160 | envmin = interp1(tmin,mmin,t,'spline'); 161 | envmax = interp1(tmax,mmax,t,'spline'); 162 | envmoy = (envmin+envmax)/2; 163 | nr = r-envmoy; 164 | 165 | 166 | switch(stop) 167 | case 'f' 168 | % Flandrin 169 | amp = mean(abs(envmax-envmin))/2; 170 | sx = abs(envmoy)./amp; 171 | stop_sift = ~(mean(sx > alpha) > 0.05 | any(sx > 10*alpha)); 172 | case 'h' 173 | % Huang 174 | stop_sift = norm(nr-r)/(norm(r)+eps) < alpha; 175 | end 176 | 177 | if ~stop_sift 178 | r=nr; 179 | aux=aux+1; 180 | %[indmin,indmax] = extr(r); % Sans lissage 181 | [indmin indmax] = build_m02(r,t,orderder(k),liss(k)); % Avec lissage 182 | end 183 | nder(k)=aux; 184 | end 185 | 186 | 187 | otherwise 188 | error('Attention : wrong value of field ''method'''); 189 | end 190 | 191 | imf(k,:) = r'; 192 | r=ar-r; 193 | k=k+1; 194 | end 195 | ort = io(s,imf); 196 | 197 | % Residu 198 | imf(k,:) = r'; 199 | 200 | end -------------------------------------------------------------------------------- /dg_mkwave.m: -------------------------------------------------------------------------------- 1 | function [wave, subwaves] = dg_mkwave(wavespec, Fs) 2 | % Creates a waveform by summing an arbitrary number of components specified 3 | % by . The components are created one at a time, and previously 4 | % created components can be used as modulation sources for later 5 | % components. Does not do any sanity checks on specified values, so 6 | % stupid results, crashes, etc. can potentially result. 7 | %INPUTS 8 | % wavespec: a vector of struct with the following fields: 9 | % form - can be 'sin', 'sqr', 'tri', 'table', 'pink'. The value 'table' 10 | % indicates that the waveform is specified by interpolating values 11 | % from a table. The value 'pink' specifies pink noise. 12 | % param - for 'sin' this is the starting phase in cycles; for 'sqr' 13 | % and 'tri', it is the duty cycle; for 'table' it is a vector of 14 | % sample values for one full cycle. For 'pink' it is the gain to be 15 | % applied to the previous value when integrating the signal (1 = 16 | % perfect integrator, 0 = no integration at all, so noise is white). 17 | % Looked on as a single-pole highpass filtering of perfectly 18 | % integrated white noise, the half-power frequency is fcutoff = 19 | % -1/(2*pi/(Fs*ln())); conversely, = 20 | % exp(-2*pi*fcutoff/Fs). 21 | % freq - a vector of sample-by-sample instantaneous frequency in Hz, or a 22 | % scalar for constant frequency. There must be at least one 23 | % component where 'freq' is a vector, and its length specifies the 24 | % number of samples to be produced. All vector values must be the 25 | % same length. 26 | % amp - a vector of sample-by-sample instantaneous amplitude, or a scalar 27 | % for constant amplitude. 28 | % freqmod - a two-element vector where the first is an index into 29 | % specifying a component to use as a modulation source, 30 | % and the second is a modulation gain by which to multiply that 31 | % component before adding it to to determine the final 32 | % frequency. If the modulation gain is zero, or the field is empty, 33 | % there is no modulation. The modulation source must have a lower 34 | % index than the current channel so that the modulation waveform will 35 | % have a value when the current waveform is computed. 36 | % ampmod - same as 'freqmod', but for amplitude. 37 | % Fs - sampling frequency in Hz. 38 | %OUTPUTS 39 | % wave: column vector containing the summed waveform. 40 | % subwaves: the separate components, in samples X components format. 41 | %NOTES 42 | % Does not make any attempt to cope with roundoff problems at extremely 43 | % large numbers of samples. 44 | % The fractional precision of timing of the last two samples is given by 45 | % (eps / (numsamp/(numsamp-1) - 1)), so with eps = 2.22044604925031e-016, 46 | % you would be down to 1 part in 1000 precision when numsamp = 47 | % -1/(1/(eps*1000 + 1) - 1) = 4.503600e+012, which is 142 years at 1 kHz. 48 | % No worries. 49 | 50 | %$Rev: 189 $ 51 | %$Date: 2014-01-24 18:48:11 -0500 (Fri, 24 Jan 2014) $ 52 | %$Author: dgibson $ 53 | 54 | % Find number of samples. 55 | nsamp = 1; 56 | for cidx = 1:length(wavespec) 57 | if length(wavespec(cidx).freq) > 1 58 | if nsamp == 1 59 | nsamp = length(wavespec(cidx).freq); 60 | elseif nsamp ~= length(wavespec(cidx).freq) 61 | error('dg_mkwave:nsamp', ... 62 | 'Component %d has an inconsistent number of samples.', ... 63 | cidx); 64 | end 65 | wavespec(cidx).freq = reshape(wavespec(cidx).freq, [], 1); 66 | wavespec(cidx).amp = reshape(wavespec(cidx).amp, [], 1); 67 | end 68 | end 69 | 70 | subwaves = zeros(nsamp, length(wavespec)); 71 | Ts = 1/Fs; 72 | for cidx = 1:length(wavespec) 73 | if isempty(wavespec(cidx).freqmod) || wavespec(cidx).freqmod(2) == 0 74 | modidx = 1; 75 | modgain = 0; 76 | else 77 | modidx = wavespec(cidx).freqmod(1); 78 | modgain = wavespec(cidx).freqmod(2); 79 | end 80 | % is in cycles. 81 | phase = cumsum((wavespec(cidx).freq + ... 82 | modgain * subwaves(:, modidx)) * Ts); 83 | if isempty(wavespec(cidx).ampmod) || wavespec(cidx).ampmod(2) == 0 84 | modidx = 1; 85 | modgain = 0; 86 | else 87 | modidx = wavespec(cidx).ampmod(1); 88 | modgain = wavespec(cidx).ampmod(2); 89 | end 90 | amp = wavespec(cidx).amp + modgain * subwaves(:, modidx); 91 | switch wavespec(cidx).form 92 | case 'sin' 93 | subwaves(:, cidx) = amp .* ... 94 | sin(2*pi*(wavespec(cidx).param + phase)); 95 | case 'sqr' 96 | isupstate = mod(phase,1) < wavespec(cidx).param; 97 | subwaves(isupstate, cidx) = 1; 98 | subwaves(~isupstate, cidx) = -1; 99 | subwaves(:, cidx) = amp .* subwaves(:, cidx); 100 | case 'tri' 101 | isupstate = mod(phase,1) < wavespec(cidx).param; 102 | startsamp = [1 103 | find(isupstate(2:end) & ~isupstate(1:end-1)) + 1]; 104 | endsamp = find(~isupstate(2:end) & isupstate(1:end-1)); 105 | if length(endsamp) < length(startsamp) 106 | endsamp(end+1) = nsamp; %#ok 107 | end 108 | for k = 1:length(startsamp) 109 | subwaves(startsamp(k):endsamp(k), cidx) = reshape( ... 110 | linspace(-1, 1, endsamp(k) - startsamp(k) + 1), ... 111 | [], 1 ); 112 | if k < length(startsamp) 113 | subwaves(endsamp(k)+1:startsamp(k+1), cidx) = reshape( ... 114 | linspace(1, -1, startsamp(k+1) - endsamp(k)), ... 115 | [], 1 ); 116 | else 117 | subwaves(endsamp(k)+1:nsamp, cidx) = ... 118 | reshape( linspace(1, -1, nsamp - endsamp(k)), ... 119 | [], 1 ); 120 | end 121 | end 122 | subwaves(:, cidx) = amp .* subwaves(:, cidx); 123 | case 'table' 124 | phase = mod(phase,1); 125 | startsamp = [1; find(phase(2:end) < phase(1:end-1)) + 1]; 126 | for k = 1:length(startsamp) 127 | if k < length(startsamp) 128 | endsamp = startsamp(k+1); 129 | else 130 | endsamp = nsamp; 131 | end 132 | subwaves(startsamp(k):endsamp, cidx) = interp1(reshape( ... 133 | linspace(0, 1, length(wavespec(cidx).param)), [], ... 134 | 1 ), reshape(wavespec(cidx).param, [], 1 ), ... 135 | phase(startsamp(k):endsamp) ); 136 | end 137 | subwaves(:, cidx) = amp .* subwaves(:, cidx); 138 | case 'pink' 139 | noise = randn(nsamp,1); 140 | if wavespec(cidx).param == 0 141 | subwaves(:, cidx) = amp .* noise; 142 | elseif wavespec(cidx).param == 1 143 | subwaves(:, cidx) = amp .* cumsum(noise); 144 | else 145 | subwaves(1, cidx) = noise(1); 146 | for k = 2:nsamp 147 | subwaves(k, cidx) = ... 148 | wavespec(cidx).param * subwaves(k-1, cidx) ... 149 | + noise(k); 150 | end 151 | subwaves(:, cidx) = amp .* subwaves(:, cidx); 152 | end 153 | otherwise 154 | warning( 'dg_mkwave:form', ... 155 | 'Unknown waveform "%s"; setting component to zero.', ... 156 | wavespec(cidx).form ); 157 | subwaves(:, cidx) = 0; 158 | end 159 | end 160 | 161 | % And finally... 162 | wave = sum(subwaves, 2); 163 | 164 | 165 | -------------------------------------------------------------------------------- /HuangEMD/NCU2009V1.txt: -------------------------------------------------------------------------------- 1 | NCU2009 folder ---40 m files 2 | ==============total-list==================================== 3 | 2009/10/08 AM 01:34 4,316 blocknormalize.m 4 | 2009/10/08 PM 04:07 5,770 confidenceLine.m 5 | 2009/10/08 PM 04:07 2,115 dist_value.m 6 | 2009/10/08 PM 03:59 5,276 eemd.m 7 | 2009/10/08 PM 03:37 853 emax.m 8 | 2009/10/08 PM 03:36 867 emin.m 9 | 2009/10/08 PM 04:52 9,997 endprocess1.m 10 | 2009/10/13 PM 09:40 5,152 ex02d.m 11 | 2009/10/08 PM 03:59 4,361 extrema.m 12 | 2009/10/27 AM 04:03 7,935 fa.m 13 | 2009/10/08 AM 01:39 3,356 FAacos.m 14 | 2009/10/21 PM 04:40 3,013 FAcosfor.m 15 | 2009/10/08 AM 01:42 1,528 FAhilbert.m 16 | 2009/10/27 AM 04:11 1,603 FAimphilbert.m 17 | 2009/10/08 AM 01:42 7,896 FAquadrature.m 18 | 2009/10/08 AM 01:38 6,695 FAzc.m 19 | 2009/10/08 PM 03:46 3,103 findcriticalpoints.m 20 | 2009/10/08 PM 04:41 6,536 findEE.m 21 | 2009/10/12 AM 01:41 11,957 findEEfsp.m 22 | 2009/10/11 PM 12:08 13,153 fspecial.m 23 | 2009/10/12 AM 12:01 3,794 FSPHSP.m 24 | 2009/10/11 PM 04:47 2,207 hilbert.m 25 | 2009/10/08 AM 01:37 1,683 hilbertnormalize.m 26 | 2009/10/27 AM 04:06 18,846 hilbtm.m 27 | 2009/10/08 PM 03:58 3,502 ifndq.m 28 | 2009/10/08 AM 01:36 3,610 linearnormalize.m 29 | 2009/10/09 AM 12:36 2,614 local_max.m 30 | 2009/10/08 PM 04:52 515 medianfilter.m 31 | 2009/10/27 AM 04:48 8,575 nnspa.m 32 | 2009/10/27 AM 04:39 8,085 nnspe.m 33 | 2009/10/09 AM 12:20 7,539 nspplota.m 34 | 2009/10/09 AM 12:18 7,520 nspplote.m 35 | 2009/10/08 AM 01:35 3,651 pchipnormalize.m 36 | 2009/10/11 PM 04:16 664 ratio1.m 37 | 2009/10/11 PM 04:16 914 ratioa.m 38 | 2009/10/08 PM 04:06 2,420 significanceIMF.m 39 | 2009/10/08 PM 04:07 2,730 signiplotIMF.m 40 | 2009/10/08 PM 03:50 1,525 skiphilbt_m.m 41 | 2009/10/08 AM 01:35 3,645 splinenormalize.m 42 | 2009/10/08 AM 01:34 4,204 splinenormalizeep.m 43 | =============================================================== 44 | Ver 1.0 ,Release from NCU RCADA 45 | Director:Norden E. ,Huang 46 | code and footnote:Sheng-Chung ,Su 47 | This version of HHT matlab code is for Education Purpose 48 | DATE:2009/10/27 49 | 50 | What's new with NCU2009? 51 | 1.M files with complex structures are equipped with footnotes. 52 | 2.Cosine-formula method is in volved in fa.m 53 | 3.Testrun examples are prepared in "NCU2009-Run" folder 54 | 4.M-files for "Significance for IMFs" are ready. 55 | 5.M-files for "Excessive Extrema" are ready. 56 | 6.A set of "Run matlab code and Learn HHT" examples are ready: 57 | ==============Frequently asked question========================= 58 | 6.1. compare the difference of HHT and FFT 59 | Q.01-how to show the difference -------ex01.m 60 | 61 | ==============basic HHT calculation============================= 62 | 6.2.Do EEMD 63 | Q.02-how to run eemd-------------------ex02.m 64 | Q.03-how to run emd--------------------ex02.m 65 | 6.3.Plot Hilbert Spectrum 66 | how to plot Hilbert-Energy spectrum----ex02.m 67 | how to plot Hilbert-Energy spectrum----ex02.m 68 | 69 | ==============detail IMF Properties calculation================= 70 | 6.4.Significance of IMF 71 | how to Show the significance of IMF----ex05.m 72 | 6.5.I.F.(Instantaneous Frequency) of IMF 73 | how to Show the I.F. values of IMF-----ex03.m 74 | 6.6.E.E(Excessive Extrema) value of IMF 75 | how to Show the E.E. values of IMF-----ex04.m 76 | 77 | 7.Detail test for "Hilbert-Transform calculation" m-file are ready: 78 | ==============detail Hilbert-Transform calculation============== 79 | 7.1 Improved Hilbert--Lena 80 | how to Show the difference of Hilbert and Improved Hilbert -----testfile 81 | 82 | 7.2.calculate f(t),a(t) by fa.m--Lena 83 | How to Run Hilbert by my favorite method-----fa.m --------------019 84 | 85 | 7.2.1 Calculationfa for Instantaneous Frequency 86 | How to do IF by acos method -----------------------------------020 87 | How to do IF by cosine formula method -------------------------021 88 | How to do IF by matlab Hilbert method -------------------------022 89 | How to do IF Improved Hilbert method---------------------------023 90 | How to do IF Quadrature method---------------------------------024 91 | How to do IF by Zero-Crossing method---------------------------025 92 | 7.2.2 functions called by fa for Normalization 93 | How to do normalize by Block-normalize method------------------026 94 | How to do normalize by Hilbert method--------------------------027 95 | How to do normalize by Linear-normalize method-----------------028 96 | How to do normalize by Hermite-Spline-normalize method---------029 97 | How to do normalize by Spline-normalize method-----------------030 98 | How to do normalize by Spline-normalize (with favorite end-process) method--031 99 | 100 | 8.Detail test for the others important m-file are ready: 101 | ==============Frequently asked question========================= 102 | 8.1. HSP and FSP Comparison--Huang 103 | FSPHSP.m----------------------------------001 104 | 105 | ==============basic HHT calculation============================= 106 | 8.2.Do EEMD--Wu 107 | eemd.m------------------------------------002 108 | extrema.m---------------------------------003 109 | 8.3.Plot Hilbert Spectrum-lena 110 | nnspa.m-----------------------------------004 111 | nnspe.m-----------------------------------005 112 | nspplota.m--------------------------------006 113 | nspplote.m--------------------------------007 114 | 115 | ==============detail IMF Properties calculation================= 116 | 8.4.Significance of IMF --Wu 117 | confidenceLine.m--------------------------008 118 | dist_value.m------------------------------009 119 | significanceIMF.m-------------------------010 120 | signiplotIMF.m----------------------------011 121 | 8.5.I.F.(Instantaneous Frequency) of IMF --Wu 122 | ifndq.m-----------------------------------012 123 | 8.6.E.E(Excessive Extrema) value of IMF--Huang 124 | findEE.m----------------------------------013 125 | findEEfsp.m-------------------------------014 126 | ratio1.m----------------------------------015 127 | ratioa.m----------------------------------016 128 | 129 | ==============detail Hilbert-Transform calculation============== 130 | 8.7. Improved Hilbert--Lena 131 | hilbtm.m----------------------------------017 132 | skiphilbt_m.m-----------------------------018 133 | 8.8.calculate f(t),a(t) by fa.m--Lena 134 | fa.m--------------------------------------019 135 | 8.8.1 functions called by fa for Instantaneous Frequency 136 | FAacos.m---------------------------------020 137 | FAcosfor.m-------------------------------021 138 | FAhilbert.m------------------------------022 139 | FAimphilbert.m---------------------------023 140 | FAquadrature.m---------------------------024 141 | FAzc.m-----------------------------------025 142 | 8.8.2 functions called by fa for Normalization 143 | blocknormalize.m-------------------------026 144 | hilbertnormalize.m-----------------------027 145 | linearnormalize.m------------------------028 146 | pchipnormalize.m-------------------------029 147 | splinenormalize.m------------------------030 148 | splinenormalizeep.m----------------------031 149 | 8.8.3 other functions called by fa 150 | emax.m-----------------------------------032 151 | emin.m-----------------------------------033 152 | endprocess1.m----------------------------034 153 | findcriticalpoints.m---------------------035 154 | local_max.m------------------------------036 155 | medianfilter.m---------------------------037 -------------------------------------------------------------------------------- /HuangEMD/nspplote.m: -------------------------------------------------------------------------------- 1 | %function [nt,tscale,fscale]=nspplote(f,a,t0,t1,fres,tres,fw0,fw1,tw0,tw1,lscale) 2 | % 3 | % Calling sequence- 4 | % [nt,tscale,fscale]= nspplote(f,a[,t0][,t1][,fres][,tres][,fw0][,fw1][,tw0][,tw1][,lscale]) 5 | % Example, [nt,tscale,fscale]=nspplote(f,a,1,1000,500,300,0,1,1,1000). 6 | % All arguments are required, WITHOUT default values. 7 | % 8 | % Input- 9 | % f - 2-D matrix that specifies the frequency values 10 | % a - 2-D matrix that specifies the amplitude values 11 | % t0 - true start time 12 | % t1 - true end time 13 | % fres - frequency resolution 14 | % tres - time resolution 15 | % fw0 - minimum frequency 16 | % fw1 - maximum frequency 17 | % tw0 - minimum time for zooming 18 | % tw1 - maximum time for zooming, if no zooming, tw0=t0, tw1=t1 19 | % lscale - when value=0 means linear axis scale,value= other integer means log axis scale 20 | % 21 | % Output- 22 | % nt - 2-D matrix that specifies the spectrum 23 | % tscale - vector that specifies the time axis values 24 | % fscale - vector that specifies the frequency axis values 25 | % 26 | % NOTE 27 | % NSPPLOTE.m compute the Hilbert-Huang Spectrum of energy. 28 | % The function NSPPLOTE generates and plots the HHT Energy spectrum of data (or log data) 29 | % in time-frequency (or time-log frequency) space based on given frequency 30 | % f(n,k) and amplitude a(n,k), where n specifies the length of time series, and 31 | % k is the number of IMF components. 32 | % 33 | % 34 | % References: 35 | % N. E Huang (2008),NCU hht class lecture 36 | % 6 Instantaneous Frequency.ppt 37 | % 38 | % code writer: Kenneth Arnold (NASA GSFC) Summer, 2003 Initial 39 | % code writer: Jelena Marshak (NASA GSFC) July 30, 2004 Modified 40 | % added the LOG scale option 41 | % code writer: Xianyao Chen September, 2008 42 | % nspplote is update version of nspplot. 43 | % It is called by nnspe to compute the Hilbert-Huang spectrum of energy. 44 | % The default input of each arguments are removed for better control of the program 45 | % code writer: S.C.su,August,2009 46 | % Add log scale calculation back (Norden E. Huang think those values are helpful for users) 47 | % Add check procedure for the matrix dimension,it's helpful when doing transfer function. 48 | % When the dimension of the matrixs are stable,transfer function can calculate one by one divison. 49 | % footnote:S.C.Su 2009/08/10 50 | % 51 | % This code is used when FM and AM is already been separated. 52 | % Code nspplote.m plots "Time-Frequency relationship" of those AM and FM signals. 53 | % The key concept is "Fill in those AM values in the corresponding FM positions" 54 | % 55 | % The structure of this code: 56 | % 1. read data and check input 57 | % 2.start to construct the 2D grid 58 | % 3.mapping the FM values into the grid 59 | % 4.when the FM values are correct,fill in the corresponding AM 60 | % 5. form the x,y axis coordinate values 61 | % 6.plot the 2d figure (optional) 62 | % 63 | 64 | function [nt,tscale,fscale]=nspplote(f,a,t0,t1,fres,tres,fw0,fw1,tw0,tw1,lscale) 65 | 66 | % 1. read data and check input 67 | %----- Check the input arguments 68 | if nargin<11 69 | lscale=[]; 70 | end 71 | if isempty(lscale) 72 | lscale=0; 73 | end 74 | if nargin < 10 75 | help nspplote 76 | error('The values of all parameters should be given.') 77 | end 78 | if size(f) ~= size(a) 79 | error('nspplot: frequency and amplitude matrix sizes differ'); 80 | end 81 | %----- check input 82 | if (t1 < t0 | fw1 < fw0 | tw1 < tw0 | tw0 < t0 | tw1 > t1) 83 | error('check the region: t0 t1, tw0 tw1, and fw0 fw1.') 84 | end 85 | 86 | %----- Check the frequency range 87 | if abs(fw0-fw1)/fres<1e-10 88 | warning('nspplot: frequency is nearly constant; giving an artificial range of +/- 1'); 89 | fw0=fw0-.5; 90 | fw1=fw1+.5; 91 | end 92 | 93 | %----- Get dimensions 94 | [npt,nimf]=size(f); 95 | 96 | %----- Set the log scale values if requested 97 | if lscale ~= 0; 98 | if((fw0<=0)|(fw1<=0)) 99 | disp('=========================================================='); 100 | disp('WARNING: min (or max) frequency range is less or equal to 0'); 101 | disp('SUGGESTION: Change the frequency range and run again'); 102 | disp('=========================================================='); 103 | end 104 | fw2=min(min(f)); 105 | if (fw2 <= 0) 106 | warning('NSPPLOT:zero/negative frequency encountered, check the range'); 107 | fw2 108 | end 109 | for i=1:nimf; 110 | % Apply LOG to a frequency 111 | for j=1:npt; 112 | if (f(j,i) > 0); 113 | f(j,i)=log(f(j,i)); 114 | end 115 | end 116 | end 117 | fw0=log(fw0); 118 | fw1=log(fw1); 119 | end 120 | 121 | %----- Flip frequency and amplitude if necessary 122 | if npt < nimf 123 | f=f'; 124 | a=a'; 125 | [npt,nimf]=size(f); 126 | end 127 | 128 | %2.start to construct the 2D grid 129 | %----- Get local frequency and time 130 | fw=fw1-fw0; 131 | tw=tw1-tw0; 132 | 133 | %----- Get time interval 134 | dt=(t1-t0)/(npt-1); 135 | 136 | %----- Construct the ploting matrix in time axis(time axis grid) 137 | sidx=floor((tw0-t0)/dt)+1; 138 | eidx=ceil((tw1-t0)/dt)+1; 139 | 140 | % The following algorithm is needed when doing transfer function for time-freq spectrum 141 | % confirm the total grid number in time axis ! 142 | if ( t0 == tw0 & t1 == tw1) 143 | sidx=1; 144 | eidx=npt 145 | end 146 | 147 | nidx=eidx-sidx+1; 148 | 149 | %-----check for the time axis grid number 150 | %condition : data point not enough ,user should reduce time resolution! 151 | 152 | if tres>nidx 153 | disp('data point in time axis not enough ! Please stop and reduce time resolution!') 154 | disp('if you continue doing this,the time resolution is changed ,the value becomes Npt ') 155 | tres=nidx; 156 | end 157 | 158 | %----- initial the matrix 159 | nt=zeros(tres,fres); 160 | 161 | %3.mapping the FM values into the grid 162 | %----- Construct the ploting matrix in frequency axis(freq axis grid) 163 | %P is the mapping position of frequency values into the freq axis grid 164 | p=round((fres-1)*(f-fw0)/fw)+1; 165 | 166 | %----- Map vector from point space into block space 167 | %t is the mapping position of time values into the time axis grid 168 | t=ceil((1:nidx)*tres/nidx); 169 | 170 | %4.when the FM values are correct,fill in the corresponding AM 171 | %the most important part-"Fill in those AM values in the corresponding FM positions" 172 | %because this calculate energy ,so AM square! 173 | % the following loop checking FM about its position 174 | % when the position is correct ,put its corresponding energy in that position. 175 | for x=sidx:eidx %checking every FM values-loop A start 176 | for imf=1:nimf %checking the FM values for every IMF-loop B start 177 | freqidx=p(x,imf); % use P as the FM position index ,called freqidx 178 | if (freqidx >= 1 & freqidx <= fres) %checking the position is 'inside' or 'outside' the grid 179 | tx = t(x-sidx+1); %tx is the final position of frequency on time axis 180 | nt(tx,freqidx)=nt(tx,freqidx)+a(x,imf)^2; %put energy(AM*AM) in its position 181 | end 182 | end %checking the FM values for every IMF-loop B end 183 | end %checking every FM values-loop A end 184 | 185 | %----- Define the output and mask out the neg ampl in case of log scale 186 | nt=abs(nt); 187 | if (lscale ~=0) 188 | for i=1:tres 189 | for j=1:fres 190 | if(nt(i,j) == 0.) 191 | nt(i,j)=-999.; 192 | else 193 | nt(i,j)=log(nt(i,j)); 194 | end 195 | end 196 | end 197 | end 198 | 199 | % 5. form the x,y axis coordinate values 200 | %form freq-axis grid value 201 | fscale=linspace(fw0,fw1,fres)'; 202 | %form time-axis grid value 203 | tscale=linspace(tw0,tw1,tres)'; 204 | nt=flipud(rot90((nt))); 205 | 206 | %6.plot the 2d figure (optional) 207 | %----- Plot if no output arguments are passed 208 | if nargout == 0 209 | img(tscale,fscale,nt); 210 | end 211 | --------------------------------------------------------------------------------