Matlab實現(xiàn)HHT程序(源碼-非常珍貴)(共5頁)
精選優(yōu)質文檔-傾情為你奉上clear all;x=load ('.TXT');fs=;N=length(x);t=0:1/fs:(N-1)/fs;z=x;c=emd(z);%計算每個IMF分量及最后一個剩余分量residual與原始信號的相關性m,n=size(c);for i=1:m;a=corrcoef(c(i,:),z);xg(i)=a(1,2);endxg;for i=1:m-1%-%計算各IMF的方差貢獻率%定義:方差為平方的均值減去均值的平方%均值的平方%imfp2=mean(c(i,:),2).2%平方的均值%imf2p=mean(c(i,:).2,2)%各個IMF的方差mse(i)=mean(c(i,:).2,2)-mean(c(i,:),2).2;end;mmse=sum(mse);for i=1:m-1mse(i)=mean(c(i,:).2,2)-mean(c(i,:),2).2; %方差百分比,也就是方差貢獻率mseb(i)=mse(i)/mmse*100;%顯示各個IMF的方差和貢獻率end;%畫出每個IMF分量及最后一個剩余分量residual的圖形figure(1)for i=1:m-1disp('imf',int2str(i) ;disp(mse(i) mseb(i);end;subplot(m+1,1,1)plot(t,z)set(gca,'fontname','times New Roman')set(gca,'fontsize',14.0)ylabel('signal','Amplitude')for i=1:m-1subplot(m+1,1,i+1);set(gcf,'color','w')plot(t,c(i,:),'k')set(gca,'fontname','times New Roman')set(gca,'fontsize',14.0)ylabel('imf',int2str(i)endsubplot(m+1,1,m+1);set(gcf,'color','w')plot(t,c(m,:),'k')set(gca,'fontname','times New Roman')set(gca,'fontsize',14.0)ylabel('r',int2str(m-1)%畫出每個IMF分量及剩余分量residual的幅頻曲線figure(2)subplot(m+1,1,1)set(gcf,'color','w')f,z=fft(t,z);plot(f,z,'k')set(gca,'fontname','times New Roman')set(gca,'fontsize',14.0)ylabel('initial signal',int2str(m-1),'Amplitude')for i=1:m-1subplot(m+1,1,i+1);set(gcf,'color','w')f,z=fft(t,c(i,:);plot(f,z,'k')set(gca,'fontname','times New Roman')set(gca,'fontsize',14.0)ylabel('imf',int2str(i),'Amplitude')endsubplot(m+1,1,m+1);set(gcf,'color','w')f,z=fft(t,c(m,:);plot(f,z,'k')set(gca,'fontname','times New Roman')set(gca,'fontsize',14.0)ylabel('r',int2str(m-1),'Amplitude')hx=hilbert(z);xr=real(hx);xi=imag(hx);%計算瞬時振幅sz=sqrt(xr.2+xi.2);%計算瞬時相位sx=angle(hx);%計算瞬時頻率dt=diff(t);dx=diff(sx);sp=dx./dt;figure(6)plot(t(1:N-1),sp)title('瞬時頻率')%計算HHT時頻譜和邊際譜A,fa,tt=hhspectrum(c);E,tt1=toimage(A,fa,tt,length(tt);figure(3)disp_hhs(E,tt1) %二維圖顯示HHT時頻譜,E是求得的HHT譜pausefigure(4)for i=1:size(c,1)faa=fa(i,:);FA,TT1=meshgrid(faa,tt1);%三維圖顯示HHT時頻圖surf(FA,TT1,E)title('HHT時頻譜三維顯示')hold onendhold offE=flipud(E);for k=1:size(E,1)bjp(k)=sum(E(k,:)*1/fs; endf=(1:N-2)/N*(fs/2);figure(5)plot(f,bjp);xlabel('頻率 / Hz');ylabel('信號幅值');title('信號邊際譜')%要求邊際譜必須先對信號進行EMD分解function A,f,tt = hhspectrum(x,t,l,aff)error(nargchk(1,4,nargin);if nargin < 2t=1:size(x,2);endif nargin < 3l=1;endif nargin < 4aff = 0;endif min(size(x) = 1if size(x,2) = 1x = x'if nargin < 2t = 1:size(x,2);endendNmodes = 1;elseNmodes = size(x,1);endlt=length(t);tt=t(l+1):(lt-l);for i=1:Nmodesan(i,:)=hilbert(x(i,:)')'f(i,:)=instfreq(an(i,:)',tt,l)'A=abs(an(:,l+1:end-l);if affdisprog(i,Nmodes,max(Nmodes,100)endendfunction disp_hhs(im,t,inf)% DISP_HHS(im,t,inf)% displays in a new figure the spectrum contained in matrix "im"% (amplitudes in log).% inputs : - im : image matrix (e.g., output of "toimage")% - t (optional) : time instants (e.g., output of "toimage") % - inf (optional) : -dynamic range in dB (wrt max)% default : inf = -20% utilisation : disp_hhs(im) ; disp_hhs(im,t) ; disp_hhs(im,inf) % disp_hhs(im,t,inf)figurecolormap(bone)colormap(1-colormap);if nargin=1inf=-20;t = 1:size(im,2);endif nargin = 2if length(t) = 1inf = t;t = 1:size(im,2);elseinf = -20;endendif inf >= 0error('inf doit etre < 0')endM=max(max(im);im = log10(im/M+1e-300);inf=inf/10;imagesc(t,fliplr(1:size(im,1)/(2*size(im,1),im,inf,0);set(gca,'YDir','normal')xlabel('time')ylabel('normalized frequency')title('Hilbert-Huang spectrum')function f,z=fftfenxi(t,y)L=length(t);N=2nextpow2(L);%fft默認計算的信號是從0開始的t=linspace(t(1),t(L),N);deta=t(2)-t(1);m=0:N-1;f=1./(N*deta)*m;%下面計算的Y就是x(t)的傅里葉變換數(shù)值%Y=exp(i*4*pi*f).*fft(y)%將計算出來的頻譜乘以exp(i*4*pi*f)得到頻移后-2,2之間的頻譜值Y=fft(y);z=sqrt(Y.*conj(Y);專心-專注-專業(yè)