EMD算法的matlab程序介绍

合集下载

matlab使用经验模式分解emd对信号进行去噪

matlab使用经验模式分解emd对信号进行去噪

matlab使⽤经验模式分解emd对信号进⾏去噪:对于这个例⼦,考虑由具有明显频率变化的正弦波组成的⾮平稳连续信号。

⼿提钻的振动或烟花声是⾮平稳连续信号的例⼦。

以采样频率加载⾮平稳信号数据fs,并可视化混合正弦信号。

1.load('sinusoidalSignalExampleData.mat','X','fs');2.3.xlabel('Time(s)');观察到混合信号包含具有不同幅度和频率值的正弦波。

为了创建希尔伯特谱图,您需要信号的IMF。

执⾏经验模式分解以计算信号的固有模式函数和残差。

由于信号不平滑,请指定' pchip'作为Interpolation⽅法。

[imf,residual,info] = emd(X,'Interpolation','pchip');1.⽬前的IMF | #Sift Iter | 相对Tol | 停⽌标准命中2.1 |2 | 0.026352 | SiftMaxRelativeTolerance3.2 | 2 | 0.0039573 | SiftMaxRelativeTolerance4.3 | 1 | 0.024838 | SiftMaxRelativeTolerance5.4 | 2 | 0.05929 | SiftMaxRelativeTolerance6.5 | 2 | 0.11317 | SiftMaxRelativeTolerance7.6 | 2 | 0.12599 | SiftMaxRelativeTolerance8.7 | 2 | 0.13802 | SiftMaxRelativeTolerance9.8 | 3 | 0.15937 | SiftMaxRelativeTolerance10.9 | 2 | 0.15923 | SiftMaxRelativeTolerance11.分解停⽌是因为残留信号的极值数⼩于'MaxNumExtrema'。

EMD与EEMD程序

EMD与EEMD程序

%%%%%%%%%%%载入信号x=load('1.txt');%产生信号N=length(x);%采样点数fs=2000;%采样频率dt=1/fs;%采样时间间隔t=(0:N-1)*dt;%产生时间序列%%%%%%%%EMDimf=emd(x);%EMD.M(EMD程序)%G.Rilling,July2002%%computesEMD(EmpiricalModeDecomposition)accordingto:%%N.E.Huangetal.,"Theempiricalmodedecompositionandthe%Hilbertspectrumfornon-linearandnonstationarytimeseriesanalysis,"%Proc.RoyalSoc.LondonA,Vol.454,pp.903-995,1998%%withvariationsreportedin:%%G.Rilling,P.FlandrinandP.Gon?alvès %"OnEmpiricalModeDecompositionanditsalgorithms"%IEEE-EURASIPWorkshoponNonlinearSignalandImageProcessing%NSIP-03,Grado(I),June2003%%stoppingcriterionforsifting:%ateachpoint:meanamplitude<threshold2*envelopeamplitude%&%meanofbooleanarray((meanamplitude)/(envelopeamplitude)>threshold)<tolerance %&%|#zeros-#extrema|<=1%%inputs:-x:analysedsignal(linevector)%-t(optional):samplingtimes(linevector)(default:1:length(x))%-stop(optional):threshold,threshold2andtolerance(optional)%forsiftingstoppingcriterion%default:[0.05,0.5,0.05]%-tst(optional):ifequalsto1showssiftingstepswithpause%ifequalsto2nopause%%outputs:-imf:intrinsicmodefunctions(lastline=residual)%-ort:indexoforthogonality%-nbits:numberofiterationsforeachmode%%calls:-extr(findsextremaandzero-crossings)%-io:computestheindexoforthogonalityfunction[imf,ort,nbits]=emd(x,t,stop,tst);%defaultforstoppingdefstop=[0.05,0.5,0.05];if(nargin==1)t=1:length(x);stop=defstop;tst=0;endif(nargin==2)stop=defstop;tst=0;endif(nargin==3)tst=0;endS=size(x);if((S(1)>1)&(S(2)>1))|(length(S)>2)error('xmusthaveonlyoneroworonecolumn')endif S(1)>1x=x';endS=size(t);if((S(1)>1)&(S(2)>1))|(length(S)>2)error('tmusthaveonlyoneroworonecolumn')endif S(1)>1t=t';endif(length(t)~=length(x))error('xandtmusthavethesamelength')endS=size(stop);if((S(1)>1)&(S(2)>1))|(S(1)>3)|(S(2)>3)|(length(S)>2)error('stopmusthaveonlyoneroworonecolumnofmaxthreeelements') endif S(1)>1stop=stop';S=size(stop);endif S(2)<3stop(3)=defstop(3);endif S(2)<2stop(2)=defstop(2);endsd=stop(1);sd2=stop(2);tol=stop(3);if tstfigureend%maximumnumberofiterationsMAXITERATIONS=2000;%maximumnumberofsymmetrizedpointsforinterpolationsNBSYM=2;lx=length(x);sdt(lx)=0;sdt=sdt+sd;sd2t(lx)=0;sd2t=sd2t+sd2;%maximumnumberofextremaandzero-crossingsinresidualner=lx;nzr=lx;r=x;imf=[];k=1;%iterationscounterforextractionof1modenbit=0;%totaliterationscounterNbIt=0;while ner>2%currentmodem=r;%modeatpreviousiterationmp=m;sx=sd+1;%testsifenoughextrematoproceedtest=0;[indmin,indmax,indzer]=extr(m);lm=length(indmin);lM=length(indmax);nem=lm+lM;nzm=length(indzer);j=1;%siftingloopwhile(mean(sx>sd)>tol|any(sx>sd2)|(abs(nzm-nem)>1))&(test==0)&nbit<MAXITERATIONS if(nbit>MAXITERATIONS/5&mod(nbit,floor(MAXITERATIONS/10))==0)disp(['mode',int2str(k),'nombrediterations:',int2str(nbit)])disp(['stopparametermeanvalue:',num2str(s)])end%boundaryconditionsforinterpolations:if indmax(1)<indmin(1)if m(1)>m(indmin(1))lmax=fliplr(indmax(2:min(end,NBSYM+1)));lmin=fliplr(indmin(1:min(end,NBSYM)));lsym=indmax(1);elselmax=fliplr(indmax(1:min(end,NBSYM)));lmin=[fliplr(indmin(1:min(end,NBSYM-1))),1];lsym=1;endelseif m(1)<m(indmax(1))lmax=fliplr(indmax(1:min(end,NBSYM)));lmin=fliplr(indmin(2:min(end,NBSYM+1)));lsym=indmin(1);elselmax=[fliplr(indmax(1:min(end,NBSYM-1))),1];lmin=fliplr(indmin(1:min(end,NBSYM)));lsym=1;endendif indmax(end)<indmin(end)if m(end)<m(indmax(end))rmax=fliplr(indmax(max(end-NBSYM+1,1):end));rmin=fliplr(indmin(max(end-NBSYM,1):end-1));rsym=indmin(end);elsermax=[lx,fliplr(indmax(max(end-NBSYM+2,1):end))]; rmin=fliplr(indmin(max(end-NBSYM+1,1):end));rsym=lx;endelseif m(end)>m(indmin(end))rmax=fliplr(indmax(max(end-NBSYM,1):end-1));rmin=fliplr(indmin(max(end-NBSYM+1,1):end));rsym=indmax(end);elsermax=fliplr(indmax(max(end-NBSYM+1,1):end));rmin=[lx,fliplr(indmin(max(end-NBSYM+2,1):end))]; rsym=lx;endendtlmin=2*t(lsym)-t(lmin);tlmax=2*t(lsym)-t(lmax);trmin=2*t(rsym)-t(rmin);trmax=2*t(rsym)-t(rmax);%incasesymmetrizedpartsdonotextendenoughif tlmin(1)>t(1)|tlmax(1)>t(1)if lsym==indmax(1)lmax=fliplr(indmax(1:min(end,NBSYM)));elselmin=fliplr(indmin(1:min(end,NBSYM)));endif lsym==1error('bug')endlsym=1;tlmin=2*t(lsym)-t(lmin);tlmax=2*t(lsym)-t(lmax);endif trmin(end)<t(lx)|trmax(end)<t(lx)if rsym==indmax(end)rmax=fliplr(indmax(max(end-NBSYM+1,1):end));elsermin=fliplr(indmin(max(end-NBSYM+1,1):end));endif rsym==lxerror('bug')endrsym=lx;trmin=2*t(rsym)-t(rmin);trmax=2*t(rsym)-t(rmax);endmlmax=m(lmax);mlmin=m(lmin);mrmax=m(rmax);mrmin=m(rmin);%definitionofenvelopesfrominterpolationenvmax=interp1([tlmaxt(indmax)trmax],[mlmaxm(indmax)mrmax],t,'spline'); envmin=interp1([tlmint(indmin)trmin],[mlminm(indmin)mrmin],t,'spline'); envmoy=(envmax+envmin)/2;m=m-envmoy;[indmin,indmax,indzer]=extr(m);lm=length(indmin);lM=length(indmax);nem=lm+lM;nzm=length(indzer);%evaluationofmeanzerosx=2*(abs(envmoy))./(abs(envmax-envmin));s=mean(sx);%displayif tstsubplot(4,1,1)plot(t,mp);hold on;plot(t,envmax,'--k');plot(t,envmin,'--k');plot(t,envmoy,'r');title(['IMF',int2str(k),';iteration',int2str(nbit),'beforesifting']);set(gca,'XTick',[])hold offsubplot(4,1,2)plot(t,sx)hold onplot(t,sdt,'--r')plot(t,sd2t,':k')title('stopparameter')set(gca,'XTick',[])hold offsubplot(4,1,3)plot(t,m)title(['IMF',int2str(k),';iteration',int2str(nbit),'aftersifting']);set(gca,'XTick',[])subplot(4,1,4);plot(t,r-m)title('residue');disp(['stopparametermeanvalue:',num2str(s)])if tst==2pause(0.01)elsepauseendend%endloop:stopsifnotenoughextremaif nem<3test=1;endmp=m;nbit=nbit+1;NbIt=NbIt+1;if(nbit==(MAXITERATIONS-1))warning(['forcedstopofsifting:toomanyiterations...mode',int2str(k),'.stopparametermea nvalue:',num2str(s)])endendimf(k,:)=m;if tstdisp(['mode',int2str(k),'enregistre'])nbits(k)=nbit;k=k+1;r=r-m;[indmin,indmax,indzer]=extr(r);ner=length(indmin)+length(indmax);nzr=length(indzer);nbit=1;if(max(r)-min(r))<(1e-10)*(max(x)-min(x))if ner>2warning('forcedstopofEMD:toosmallamplitude')elsedisp('forcedstopofEMD:toosmallamplitude')endbreakendendimf(k,:)=r;ort=io(x,imf);if tstcloseendEEMD程序%ThisisanEMD/EEMDprogram%%functionallmode=eemd(Y,Nstd,NE)%%INPUT:%Y:Inputteddata;(输入数据)%Nstd:ratioofthestandarddeviationoftheaddednoiseandthatofY;(添加噪声和Y的标准偏差的比率)%NE:EnsemblenumberfortheEEMD(EEMD集合的数量)%OUTPUT:%AmatrixofN*(m+1)matrix,whereNisthelengthoftheinput%dataY,andm=fix(log2(N))-1.Column1istheoriginaldata,columns2,3,...(一个N*(m+1)矩阵,其中N是输入数据Y的长度,和m=fix(log2(N))-1。

emd算法仿真 光谱数据去噪

emd算法仿真 光谱数据去噪

emd算法仿真光谱数据去噪电磁脉冲(EMD)算法(Empirical Mode Decomposition)是一种用于信号处理的自适应时频分析方法,它可以将信号分解为若干个固有模态函数(Intrinsic Mode Functions,IMFs)。

EMD 在信号去噪中有一定的应用,但直接应用于光谱数据去噪可能需要考虑一些特定的问题。

以下是一个使用 Python 中的 EMD 算法库 PyEMD 对光谱数据进行去噪的示例:首先,确保你已经安装了 PyEMD 库。

你可以使用以下命令进行安装:pip install EMD-signal然后,使用下面的 Python 代码演示如何在光谱数据上应用 EMD 进行去噪:import numpy as npimport matplotlib.pyplot as pltfrom PyEMD import EMD# 生成一些示例光谱数据np.random.seed(42)x = np.linspace(0, 10, 1000)spectrum_data = np.sin(x) + 0.2 * np.random.randn(1000)# 使用 EMD 进行信号分解emd = EMD()IMFs = emd(spectrum_data)# 选择保留的 IMFs(去噪效果可能取决于保留的 IMFs 数量)selected_IMFs = [1, 2, 3] # 根据实际情况选择保留的 IMFs denoised_spectrum = np.sum(IMFs[selected_IMFs], axis=0)# 绘制原始光谱和去噪后的光谱plt.figure(figsize=(10, 6))plt.subplot(2, 1, 1)plt.plot(x, spectrum_data, label='Original Spectrum')plt.title('Original Spectrum')plt.subplot(2, 1, 2)plt.plot(x, denoised_spectrum, label='Denoised Spectrum', color='orange')plt.title('Denoised Spectrum')plt.tight_layout()plt.show()这个示例中,我们使用PyEMD 库中的EMD 类来对光谱数据进行信号分解,然后选择一些IMFs(固有模态函数)来重构去噪后的光谱。

matlab中的ceemd函数

matlab中的ceemd函数

MATLAB (Matrix Laboratory) 是一种用于算法开发、数据可视化、数据分析和数值计算的高级技术计算语言和交互式环境。

它是MATLAB公司MathWorks的主要产品之一,被广泛应用于工程、科学和数学等领域。

CEEMD (Complete Ensemble Empirical Mode Dposition) 是一种数据分解方法,用于分解非平稳和非线性数据,以揭示数据中的潜在信息。

在MATLAB中,CEEMD可以通过CEEMD 函数来实现。

本文将介绍MATLAB中的CEEMD函数的基本原理、用法和实际应用。

1. CEEMD函数的基本原理CEEMD是一种基于经验模态分解 (EMD) 的数据分解方法,它通过将原始信号分解成一组本征模态函数 (EMD) 和残差的方法来揭示信号内部的结构特征。

CEEMD函数首先对原始信号进行一系列轮次的随机取样和平滑处理,然后将这些处理后的信号进行集成,得到最终的本征模态函数和残差。

这种方法可以有效地处理非线性和非平稳数据,并且不需要对数据进行先验的假设,因此在各种领域都有广泛的应用。

2. CEEMD函数的用法在MATLAB中,CEEMD函数的用法非常简单。

用户需要将原始信号传入CEEMD函数,并设置一些参数,如轮次、随机取样次数等。

CEEMD函数会自动进行数据分解,并输出本征模态函数和残差。

用户可以根据实际需求对输出的结果进行进一步的分析和处理,如提取特征、去噪等。

3. CEEMD函数的实际应用CEEMD函数在实际应用中有着广泛的应用。

在信号处理领域,CEEMD函数可以用于处理非平稳和非线性信号,如地震信号、生物信号等。

在金融领域,CEEMD函数可以用于分解股票价格的波动,揭示其内在的规律。

在医学领域,CEEMD函数可以用于分析医学图像和生物信号,帮助医生进行诊断和治疗。

MATLAB中的CEEMD函数是一种强大的数据分解工具,它可以帮助用户从非平稳和非线性数据中提取有用的信息,广泛应用于工程、科学、金融和医学等领域。

二维数据bemd分解matlab

二维数据bemd分解matlab

一、概述在科学研究和工程应用中,二维数据分析和处理是非常常见的问题。

其中,bemd分解(Bivariate Empirical Mode Dposition)是一种用于对二维数据进行分解的有效方法。

在本文中,我们将重点介绍如何使用Matlab工具进行二维数据的bemd分解,以及该方法在实际应用中的意义和作用。

二、二维数据bemd分解的原理和方法bemd是一种基于经验模态分解(Empirical Mode Dposition,简称EMD)的技术,在处理二维数据时是非常有用的。

该方法的基本原理是将二维数据分解为一系列二维本征模态函数(Intrinsic Mode Function,简称IMF),从而实现数据的局部化分析和处理。

在进行bemd分解时,通常会使用Hilbert-Huang变换来进行辅助处理,以确保得到的IMF函数具有较好的时频局部性质。

三、Matlab工具在二维数据bemd分解中的应用Matlab是一种广泛应用于科学计算和数据分析的工具,它提供了丰富的函数库和工具包,可以方便地进行各种数据处理和分析。

在进行二维数据bemd分解时,我们可以借助Matlab中的相关函数和工具来实现较为高效的计算和分析。

通过调用Matlab中的emd、hilbert等函数,可以很容易地实现二维数据的bemd分解。

四、二维数据bemd分解在实际应用中的意义和作用二维数据bemd分解在实际应用中有着广泛的意义和作用。

在信号处理领域中,bemd分解可以用于对图像、声音等二维信号进行分析和处理,从而提取出其中的局部特征和信息。

在地震学、气象学等领域中,bemd分解也可以用于对地震波形、气象数据等二维空时信号进行处理,以便进行地震监测、气象预测等工作。

五、结论通过本文的介绍,我们了解了二维数据bemd分解的原理和方法,以及在Matlab中进行bemd分解的具体步骤和技术。

我们还深入探讨了bemd分解在实际应用中的意义和作用。

EMD信号处理方法在LabVIEW和MATLAB中的实现

EMD信号处理方法在LabVIEW和MATLAB中的实现
② MATLAB Scrip t节点具有多输入 、多输出的 特点 ,可以一次处理较大的信息量 。MATLAB 脚本 可以先在 MATLAB 环境下调试 ,运行无误后再导入 到 MATLAB Scrip t节点中 。MATLAB Scrip t节点对 输入 、输 出 数 据 的 类 型 有 明 确 的 要 求 , 只 有 Lab2 V IEW 中的数据类型与 MATLAB 中的数据型相匹 配 ,才能进行数据传输 。使用 MATLAB Scrip t节点 的方法快捷方便 ,但不利于较大的应用程序开发 。 当需要使用时 ,可将其模块化 ,采用主程序动态加 载。
LabV IEW 设计用户图形界面 ,负责数据采集和网络 通信 ;由 MATLAB 在后台提供算法供 LabV IEW 调 用。
1 基于 EMD 的信号处理方法
基于模态分解的时频分析方法于 1998 年由美 国国家宇航局的 Norden E. Huang提出 ,他是运用基 于经验的模态分解方法 ,将一个时间序列信号分解 成有限个不同时间尺度的内禀模态函数 IMF ( intrin2 sic mode function) ,然后将每个 IM F 进行 H ilbert Huang变换 ,得到时频平面上的能量分布谱图 ,用来 分析信号时频谱特征的信号分析方法 。 H ilbert Huang变换适合处理非线性 、非平稳信号 。用这种 方法进行分析可更准确有效地掌握原信号的特征信 息 。此方法包括两个过程 :经验模态分解和 H ilbert 变换 ,其中最关键的部分是 EMD 方法 。 EMD 方法 基于信号的局部特征时间尺度 ,能把复杂信号函数 分解为有限的内禀模态函数之和 ,每一个 IM F所包 含的频率成分不仅与分析频率有关 ,而且最重要的 是随信号本身变化而变化 ,因此 , EMD 方法是自适 应的信号处理方法 。 1. 1 EMD 基本理论

(完整word版)EM算法matlab程序

(完整word版)EM算法matlab程序

X=zeros(600,2);X(1:200,:)= normrnd(0,1,200,2);X(201:400,:)= normrnd(0,2,200,2);X(401:600,:)= normrnd(0,3,200,2);[W,M,V,L] = EM_GM(X,3,[],[],1,[])下面是程序源码:打印帮助function[W,M,V,L]= EM_GM(X,k,ltol,maxiter,pflag,Init)%[W,M,V,L]= EM_GM(X,k,ltol,maxiter,pflag,Init)%% EM algorithm for k multidimensional Gaussian mixture estimation%%Inputs:%X(n,d)- input data,n=number of observations,d=dimension of variable % k - maximum number of Gaussian components allowed%ltol - percentage of the log likelihood difference between 2 iterations ([] for none) % maxiter — maximum number of iteration allowed ([] for none)%pflag — 1 for plotting GM for 1D or 2D cases only,0 otherwise ([]for none) % Init — structure of initial W,M, V: Init。

W,Init。

M, Init。

V ([] for none)%%Ouputs:% W(1,k)— estimated weights of GM% M(d,k) — estimated mean vectors of GM%V(d,d,k) — estimated covariance matrices of GM%L — log likelihood of estimates%%Written by%Patrick P。

MATLAB之经验模态分解EMD

MATLAB之经验模态分解EMD

MATLAB之经验模态分解EMDfunction [imf,ort,nbits] = emd(varargin)[x,t,sd,sd2,tol,MODE_COMPLEX,ndirs,display_sifting,sdt,sd2t, r,imf,k,nbit,NbIt,MAXITERATIONS,FIXE,FIXE_H,MAXMODES,INTE RP,mask] = init(varargin{:});if display_siftingfig_h = figure;endwhile ~stop_EMD(r,MODE_COMPLEX,ndirs) && (k < maxmodes+1="" ||="" maxmodes="=" 0)="" &&=""> m = r;mp = m;if FIXE % 如果设定了迭代次数[stop_sift,moyenne] = stop_sifting_fixe(t,m,INTERP,MODE_COMPLEX,ndirs);elseif FIXE_Hstop_count = 0;[stop_sift,moyenne] = stop_sifting_fixe_h(t,m,INTERP,stop_count,FIXE_H,MODE_COMPLEX,ndirs);else[stop_sift,moyenne] = stop_sifting(m,t,sd,sd2,tol,INTERP,MODE_COMPLEX,ndirs);endif (max(abs(m))) <>if ~stop_siftwarning('emd:warning','forced stop of EMD : too small amplitude')elsedisp('forced stop of EMD : too small amplitude')endbreakendwhile ~stop_sift && nbit<>if(~MODE_COMPLEX && nbit>MAXITERATIONS/5 && mod(nbit,floor(MAXITERATIONS/10))==0 && ~FIXE && nbit > 100)disp(['mode ',int2str(k),', iteration ',int2str(nbit)])if exist('s','var')disp(['stop parameter mean value : ',num2str(s)])end[im,iM] = extr(m);disp([int2str(sum(m(im) > 0)),' minima > 0; ',int2str(sum(m(iM) < 0)),'="" maxima=""><>endm = m - moyenne;if FIXE[stop_sift,moyenne] =stop_sifting_fixe(t,m,INTERP,MODE_COMPLEX,ndirs);elseif FIXE_H[stop_sift,moyenne,stop_count] = stop_sifting_fixe_h(t,m,INTERP,stop_count,FIXE_H,MODE_COMPL EX,ndirs);else[stop_sift,moyenne,s] = stop_sifting(m,t,sd,sd2,tol,INTERP,MODE_COMPLEX,ndirs);endif display_sifting && ~MODE_COMPLEXNBSYM = 2;[indmin,indmax] = extr(mp);[tmin,tmax,mmin,mmax] = boundary_conditions(indmin,indmax,t,mp,mp,NBSYM);envminp = interp1(tmin,mmin,t,INTERP);envmaxp = interp1(tmax,mmax,t,INTERP);envmoyp = (envminp+envmaxp)/2;if FIXE || FIXE_Hdisplay_emd_fixe(t,m,mp,r,envminp,envmaxp,envmoyp,nbit, k,display_sifting)elsesxp = 2*(abs(envmoyp))./(abs(envmaxp-envminp));sp = mean(sxp);display_emd(t,m,mp,r,envminp,envmaxp,envmoyp,s,sp,sxp,s dt,sd2t,nbit,k,display_sifting,stop_sift)endendmp = m;nbit = nbit+1;NbIt = NbIt+1;if (nbit==(MAXITERATIONS-1) && ~FIXE && nbit > 100)if exist('s','var')warning('emd:warning',['forced stop of sifting : too many iterations... mode ',int2str(k),'. stop parameter mean value : ',num2str(s)])elsewarning('emd:warning',['forced stop of sifting : too many iterations... mode ',int2str(k),'.'])endendendimf(k,:) = m;if display_siftingdisp(['mode ',int2str(k),' stored'])endnbits(k) = nbit;k = k+1;r = r - m;nbit = 0;endif any(r) && ~any(mask)imf(k,:) = r;endort = io(x,imf);if display_siftingcloseendendfunction stop = stop_EMD(r,MODE_COMPLEX,ndirs)if MODE_COMPLEXfor k = 1:ndirsphi = (k-1)*pi/ndirs;[indmin,indmax] = extr(real(exp(i*phi)*r));ner(k) = length(indmin) + length(indmax);endstop = any(ner <>else[indmin,indmax] = extr(r);ner = length(indmin) + length(indmax);stop = ner <>endendfunction [envmoy,nem,nzm,amp] = mean_and_amplitude(m,t,INTERP,MODE_COMPLEX,ndirs) NBSYM = 2;if MODE_COMPLEXswitch MODE_COMPLEXcase 1for k = 1:ndirsphi = (k-1)*pi/ndirs;y = real(exp(-i*phi)*m);nem(k) = length(indmin)+length(indmax);nzm(k) = length(indzer);[tmin,tmax,zmin,zmax] = boundary_conditions(indmin,indmax,t,y,m,NBSYM);envmin(k,:) = interp1(tmin,zmin,t,INTERP);envmax(k,:) = interp1(tmax,zmax,t,INTERP);endenvmoy = mean((envmin+envmax)/2,1);if nargout > 3amp = mean(abs(envmax-envmin),1)/2;endcase 2for k = 1:ndirsphi = (k-1)*pi/ndirs;y = real(exp(-i*phi)*m);[indmin,indmax,indzer] = extr(y);nem(k) = length(indmin)+length(indmax);nzm(k) = length(indzer);[tmin,tmax,zmin,zmax] = boundary_conditions(indmin,indmax,t,y,y,NBSYM);envmin(k,:) = exp(i*phi)*interp1(tmin,zmin,t,INTERP);envmax(k,:) = exp(i*phi)*interp1(tmax,zmax,t,INTERP);endenvmoy = mean((envmin+envmax),1);if nargout > 3amp = mean(abs(envmax-envmin),1)/2;endendelsenem = length(indmin)+length(indmax);nzm = length(indzer);[tmin,tmax,mmin,mmax] = boundary_conditions(indmin,indmax,t,m,m,NBSYM);envmin = interp1(tmin,mmin,t,INTERP);envmax = interp1(tmax,mmax,t,INTERP);envmoy = (envmin+envmax)/2;if nargout > 3amp = mean(abs(envmax-envmin),1)/2; % 计算包络幅度endendendfunction [stop,envmoy,s] = stop_sifting(m,t,sd,sd2,tol,INTERP,MODE_COMPLEX,ndirs) try[envmoy,nem,nzm,amp] = mean_and_amplitude(m,t,INTERP,MODE_COMPLEX,ndirs);sx = abs(envmoy)./amp;s = mean(sx);stop = ~((mean(sx > sd) > tol | any(sx > sd2)) & (all(nem > 2)));if ~MODE_COMPLEXstop = stop && ~(abs(nzm-nem)>1);endcatchstop = 1;envmoy = zeros(1,length(m));s = NaN;endendfunction [stop,moyenne]= stop_sifting_fixe(t,m,INTERP,MODE_COMPLEX,ndirs)trymoyenne = mean_and_amplitude(m,t,INTERP,MODE_COMPLEX,ndirs);stop = 0;catchmoyenne = zeros(1,length(m));stop = 1;endendfunction [stop,moyenne,stop_count]= stop_sifting_fixe_h(t,m,INTERP,stop_count,FIXE_H,MODE_COMPL EX,ndirs)try[moyenne,nem,nzm] = mean_and_amplitude(m,t,INTERP,MODE_COMPLEX,ndirs);if (all(abs(nzm-nem)>1))stop = 0;stop_count = 0;elsestop_count = stop_count+1;stop = (stop_count == FIXE_H);endcatchmoyenne = zeros(1,length(m));stop = 1;endendfunctiondisplay_emd(t,m,mp,r,envmin,envmax,envmoy,s,sb,sx,sdt,sd2t,nb it,k,display_sifting,stop_sift)subplot(4,1,1)plot(t,mp);hold on;plot(t,envmax,'--k');plot(t,envmin,'--k');plot(t,envmoy,'r');title(['IMF ',int2str(k),'; iteration ',int2str(nbit),' before sifting']);set(gca,'XTick',[])hold offsubplot(4,1,2)plot(t,sx)hold onplot(t,sdt,'--r')plot(t,sd2t,':k')title('stop parameter')set(gca,'XTick',[])hold offsubplot(4,1,3)plot(t,m)title(['IMF ',int2str(k),'; iteration ',int2str(nbit),' after sifting']);set(gca,'XTick',[])subplot(4,1,4);plot(t,r-m)title('residue');disp(['stop parameter mean value : ',num2str(sb),' before sifting and ',num2str(s),' after'])if stop_siftdisp('last iteration for this mode')endif display_sifting == 2pause(0.01)elsepauseendendfunctiondisplay_emd_fixe(t,m,mp,r,envmin,envmax,envmoy,nbit,k,display _sifting)subplot(3,1,1)plot(t,mp);hold on;plot(t,envmax,'--k');plot(t,envmin,'--k');plot(t,envmoy,'r');title(['IMF ',int2str(k),'; iteration ',int2str(nbit),' before sifting']);set(gca,'XTick',[])hold offsubplot(3,1,2)plot(t,m)title(['IMF ',int2str(k),'; iteration ',int2str(nbit),' after sifting']);set(gca,'XTick',[])subplot(3,1,3);plot(t,r-m)title('residue');if display_sifting == 2pause(0.01)elsepauseendendfunction [tmin,tmax,zmin,zmax] = boundary_conditions(indmin,indmax,t,x,z,nbsym)% 实数情况下,x = zlx = length(x);% 判断极值点个数if (length(indmin) + length(indmax) <>error('not enough extrema')end% 插值的边界条件if indmax(1) <> % 第一个极值点是极大值if x(1) > x(indmin(1)) % 以第一个极大值为对称中心lmax = fliplr(indmax(2:min(end,nbsym+1)));lmin = fliplr(indmin(1:min(end,nbsym)));lsym = indmax(1);else % 如果第一个采样值小于第一个极小值,则将认为该值是一个极小值,以该点为对称中心lmax = fliplr(indmax(1:min(end,nbsym)));lmin = [fliplr(indmin(1:min(end,nbsym-1))),1];lsym = 1;endelseif x(1) <> % 以第一个极小值为对称中心lmax = fliplr(indmax(1:min(end,nbsym)));lmin = fliplr(indmin(2:min(end,nbsym+1)));lsym = indmin(1);else % 如果第一个采样值大于第一个极大值,则将认为该值是一个极大值,以该点为对称中心lmax = [fliplr(indmax(1:min(end,nbsym-1))),1];lmin = fliplr(indmin(1:min(end,nbsym)));lsym = 1;endend% 序列末尾情况与序列开头类似if indmax(end) <>if x(end) <>rmax = fliplr(indmax(max(end-nbsym+1,1):end));rmin = fliplr(indmin(max(end-nbsym,1):end-1));rsym = indmin(end);elsermax = [lx,fliplr(indmax(max(end-nbsym+2,1):end))];rmin = fliplr(indmin(max(end-nbsym+1,1):end));rsym = lx;endelseif x(end) > x(indmin(end))rmax = fliplr(indmax(max(end-nbsym,1):end-1));rmin = fliplr(indmin(max(end-nbsym+1,1):end));rsym = indmax(end);elsermax = fliplr(indmax(max(end-nbsym+1,1):end));rmin = [lx,fliplr(indmin(max(end-nbsym+2,1):end))];rsym = lx;endend% 将序列根据对称中心,镜像到两边tlmin = 2*t(lsym)-t(lmin);tlmax = 2*t(lsym)-t(lmax);trmin = 2*t(rsym)-t(rmin);trmax = 2*t(rsym)-t(rmax);% 如果对称的部分没有足够的极值点if tlmin(1) > t(1) || tlmax(1) > t(1) % 对折后的序列没有超出原序列的范围if lsym == indmax(1)lmax = fliplr(indmax(1:min(end,nbsym)));elselmin = fliplr(indmin(1:min(end,nbsym)));endif lsym == 1 % 这种情况不应该出现,程序直接中止error('bug')endlsym = 1; % 直接关于第一采样点取镜像tlmin = 2*t(lsym)-t(lmin);tlmax = 2*t(lsym)-t(lmax);end% 序列末尾情况与序列开头类似if trmin(end) < t(lx)="" ||="" trmax(end)=""><> if rsym == indmax(end)rmax = fliplr(indmax(max(end-nbsym+1,1):end)); elsermin = fliplr(indmin(max(end-nbsym+1,1):end)); endif rsym == lxerror('bug')endrsym = lx;trmin = 2*t(rsym)-t(rmin);trmax = 2*t(rsym)-t(rmax);end% 延拓点上的取值zlmax = z(lmax);zlmin = z(lmin);zrmax = z(rmax);zrmin = z(rmin);% 完成延拓tmin = [tlmin t(indmin) trmin];tmax = [tlmax t(indmax) trmax];zmin = [zlmin z(indmin) zrmin];zmax = [zlmax z(indmax) zrmax];end%---------------------------------------------------------------------------------------------------% 极值点和过零点位置提取function [indmin, indmax, indzer] = extr(x,t)if(nargin==1)t = 1:length(x);endm = length(x);if nargout > 2x1 = x(1:m-1);x2 = x(2:m);indzer = find(x1.*x2<> % 寻找信号符号发生变化的位置if any(x == 0) % 考虑信号采样点恰好为0的位置iz = find( x==0 ); % 信号采样点恰好为0的位置indz = [];if any(diff(iz)==1) % 出现连0的情况zer = x == 0; % x=0处为1,其它地方为0dz = diff([0 zer 0]); % 寻找0与非0的过渡点debz = find(dz == 1); % 0值起点finz = find(dz == -1)-1; % 0值终点indz = round((debz+finz)/2); % 选择中间点作为过零点elseindz = iz; % 若没有连0的情况,该点本身就是过零点endindzer = sort([indzer indz]); % 全体过零点排序end% 提取极值点d = diff(x);n = length(d);d1 = d(1:n-1);d2 = d(2:n);indmin = find(d1.*d2<0 &="">0><> % 最小值indmax = find(d1.*d2<0 &="" d1="">0)+1; % 最大值% 当连续多个采样值相同时,把最中间的一个值作为极值点,处理方式与连0类似if any(d==0)imax = [];imin = [];bad = (d==0);dd = diff([0 bad 0]);debs = find(dd == 1);fins = find(dd == -1);if debs(1) == 1 % 连续值出现在序列开头if length(debs) > 1debs = debs(2:end);fins = fins(2:end);elsedebs = [];fins = [];endif length(debs) > 0if fins(end) == m % 连续值出现在序列末尾if length(debs) > 1debs = debs(1:(end-1));fins = fins(1:(end-1));elsedebs = [];fins = [];endendendlc = length(debs);if lc > 0for k = 1:lcif d(debs(k)-1) > 0 % 取中间值if d(fins(k)) <>imax = [imax round((fins(k)+debs(k))/2)]; endelseif d(fins(k)) > 0imin = [imin round((fins(k)+debs(k))/2)]; endendendendif length(imax) > 0indmax = sort([indmax imax]);if length(imin) > 0indmin = sort([indmin imin]);endendend%---------------------------------------------------------------------------------------------------function ort = io(x,imf)% ort = IO(x,imf) 计算正交指数%% 输入 : - x : 分析信号% - imf : IMF信号n = size(imf,1);s = 0;% 根据公式计算for i = 1:nfor j = 1:nif i ~= js = s + abs(sum(imf(i,:).*conj(imf(j,:)))/sum(x.^2));endendendort = 0.5*s;end%---------------------------------------------------------------------------------------------------% 函数参数解析function[x,t,sd,sd2,tol,MODE_COMPLEX,ndirs,display_sifting,sdt,sd2t,r,im f,k,nbit,NbIt,MAXITERATIONS,FIXE,FIXE_H,MAXMODES,INTERP,m ask] = init(varargin)x = varargin{1};if nargin == 2if isstruct(varargin{2})inopts = varargin{2};elseerror('when using 2 arguments the first one is the analyzed signal X and the second one is a struct object describing the options')endelseif nargin > 2tryinopts = struct(varargin{2:end});catcherror('bad argument syntax')endend% 默认停止条件defstop = [0.05,0.5,0.05];opt_fields = {'t','stop','display','maxiterations','fix','maxmodes','interp','fix_h',' mask','ndirs','complex_version'};% 时间序列,停止参数,是否演示,最大迭代次数,每一轮迭代次数,IMF个数,插值方法,每一轮迭代次数(带条件),mask信号,方向数,是否采用复数模式defopts.stop = defstop;defopts.display = 0;defopts.t = 1:max(size(x));defopts.maxiterations = 2000;defopts.fix = 0;defopts.maxmodes = 0;defopts.interp = 'spline';defopts.fix_h = 0;defopts.mask = 0;defopts.ndirs = 4;plex_version = 2;opts = defopts;if(nargin==1)inopts = defopts;elseif nargin == 0error('not enough arguments')endnames = fieldnames(inopts);for nom = names'if ~any(strcmpi(char(nom), opt_fields))error(['bad option field name: ',char(nom)])endif ~isempty(eval(['inopts.',char(nom)]))eval(['opts.',lower(char(nom)),' = inopts.',char(nom),';'])endendt = opts.t;stop = opts.stop;display_sifting = opts.display;MAXITERATIONS = opts.maxiterations;FIXE = opts.fix;MAXMODES = opts.maxmodes;INTERP = opts.interp;FIXE_H = opts.fix_h;mask = opts.mask;ndirs = opts.ndirs;complex_version = plex_version;if ~isvector(x)error('X must have only one row or one column')endif size(x,1) > 1x = x.';endif ~isvector(t)error('option field T must have only one row or one column')if ~isreal(t)error('time instants T must be a real vector')endif size(t,1) > 1t = t';endif (length(t)~=length(x))error('X and option field T must have the same length')endif ~isvector(stop) || length(stop) > 3error('option field STOP must have only one row or one column of max three elements')endif ~all(isfinite(x))error('data elements must be finite')endif size(stop,1) > 1stop = stop';endL = length(stop);if L <>stop(3) = defstop(3);if L <>stop(2) = defstop(2);endif ~ischar(INTERP) || ~any(strcmpi(INTERP,{'linear','cubic','spline'}))error('INTERP field must be ''linear'', ''cubic'', ''pchip'' or ''spline''')end% 使用mask信号时的特殊处理if any(mask)if ~isvector(mask) || length(mask) ~= length(x)error('masking signal must have the same dimension as the analyzed signal X')endif size(mask,1) > 1mask = mask.';endopts.mask = 0;imf1 = emd(x+mask, opts);imf2 = emd(x-mask, opts);if size(imf1,1) ~= size(imf2,1)warning('emd:warning',['the two sets of IMFs have different sizes: ',int2str(size(imf1,1)),' and ',int2str(size(imf2,1)),' IMFs.']) endS1 = size(imf1,1);S2 = size(imf2,1);if S1 ~= S2 % 如果两个信号分解得到的IMF个数不一致,调整顺序if S1 <>tmp = imf1;imf1 = imf2;imf2 = tmp;endimf2(max(S1,S2),1) = 0; % 将短的那个补零,达到长度一致endimf = (imf1+imf2)/2;endsd = stop(1);sd2 = stop(2);tol = stop(3);lx = length(x);sdt = sd*ones(1,lx);sd2t = sd2*ones(1,lx);if FIXEMAXITERATIONS = FIXE;if FIXE_Herror('cannot use both ''FIX'' and ''FIX_H'' modes')endendMODE_COMPLEX = ~isreal(x)*complex_version;if MODE_COMPLEX && complex_version ~= 1 && complex_version ~= 2error('COMPLEX_VERSION parameter must equal 1 or 2')end% 极值点和过零点的个数ner = lx;nzr = lx;r = x;if ~any(mask)imf = [];endk = 1;nbit = 0;NbIt = 0;end0>。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

%此版本为ALAN 版本的整合注释版
function imf = emd(x)
% Empiricial Mode Decomposition (Hilbert-Huang Transform)
% imf = emd(x)
% Func : findpeaks
x= transpose(x(:));%转置为行矩阵
imf = [];
while ~ismonotonic(x) %当x不是单调函数,分解终止条件
x1 = x;
sd = Inf;%均值
%直到x1满足IMF条件,得c1
while (sd > 0.1) | ~isimf(x1) %当标准偏差系数sd大于0.1或x1不是固有模态函数时,分量终止条件
s1 = getspline(x1);%上包络线
s2 = -getspline(-x1);%下包络线
x2 = x1-(s1+s2)/2;%此处的x2为文章中的h
sd = sum((x1-x2).^2)/sum(x1.^2);
x1 = x2;
end
imf{end+1} = x1;
x = x-x1;
end
imf{end+1} = x;
% FUNCTIONS
function u = ismonotonic(x)
%u=0表示x不是单调函数,u=1表示x为单调的
u1 = length(findpeaks(x))*length(findpeaks(-x));
if u1 > 0, u = 0;
else, u = 1; end
function u = isimf(x)
%u=0表示x不是固有模式函数,u=1表示x是固有模式函数
N = length(x);
u1 = sum(x(1:N-1).*x(2:N) < 0);
u2 = length(findpeaks(x))+length(findpeaks(-x));
if abs(u1-u2) > 1, u = 0;
else, u = 1; end
function s = getspline(x)
%三次样条函数拟合成元数据包络线
N = length(x);
p = findpeaks(x);
s = spline([0 p N+1],[0 x(p) 0],1:N);
-------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function n = findpeaks(x)
% Find peaks.找到极值,n为极值点所在位置
% n = findpeaks(x)
n = find(diff(diff(x) > 0) < 0);
u = find(x(n+1) > x(n));
n(u) = n(u)+1;
------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
function plot_hht00(x,Ts)
% 双边带调幅信号的EMD分解
% Plot the HHT.
% plot_hht(x,Ts)
%
% :: Syntax
% The array(列)x is the input signal and Ts is the sampling period(取样周期). % Example on use: [x,Fs] = wavread('Hum.wav');
% plot_hht(x(1:6000),1/Fs);
% Func : emd
% Get HHT.
clear all;
close all;
Ts=0.0005;
t=0:Ts:10; % 采样率2000HZ
% 调幅信号
%x=sin(2*pi*t).*sin(40*pi*t);
x=sin(2*pi*t);
s1 = getspline(x);%上包络线
s2 = -getspline(-x);%上包络线
x1 = (s1+s2)/2;%此处的x2为文章中的h
figure;
plot(t,x);xlabel('Time'), ylabel('Amplitude');title('双边带调幅信号');hold on;
plot(t,s1,'-r');
plot(t,s2,'-r');
plot(t,x1,'g');
imf = emd(x);
for k = 1:length(imf)
b(k) = sum(imf{k}.*imf{k});
th = angle(hilbert(imf{k}));
d{k} = diff(th)/Ts/(2*pi);
end
[u,v] = sort(-b);
b = 1-b/max(b);
% Set time-frequency plots.
N = length(x);
c = linspace(0,(N-2)*Ts,N-1);
%
figure;
for k = v(1:2)
plot(c,d{k},'k.','Color',b([k k k]),'MarkerSize',3); hold on;
set(gca,'FontSize',8,'XLim',[0 c(end)],'YLim',[0 50]);%设置x、y轴句柄xlabel('Time'), ylabel('Frequency');title('原信号时频图');
end
% Set IMF plots.
M = length(imf);
N = length(x);
c = linspace(0,(N-1)*Ts,N);
for k1 = 0:4:M-1
figure
for k2 = 1:min(4,M-k1),
subplot(4,1,k2),
plot(c,imf{k1+k2});
set(gca,'FontSize',8,'XLim',[0 c(end)]);
title('EMD分解结果');
end
xlabel('Time');
end。

相关文档
最新文档