www.pudn.com > HHT_power-system_power-quality_disturbances-detect > hhspectrum.m, change:2010-04-26,size:2850b

```%HHSPECTRUM  compute Hilbert-Huang spectrum
%
% [A,f,tt] = HHSPECTRUM(x,t,l,aff) computes the Hilbert-Huang spectrum
%
% inputs:
%   - x   : matrix with one signal per row
%   - t   : time instants
%   - l   : estimation parameter for instfreq (integer >=1 (1:default))
%   - aff : if 1, displays the computation evolution
%
% outputs:
%   - A   : instantaneous amplitudes
%   - f   : instantaneous frequencies
%   - tt  : truncated time instants
%
% calls:
%   - hilbert  : computes the analytic signal
%   - instfreq : computes the instantaneous frequency
%   - disprog : displays the computation evolution
%
%Examples:
%
%s = randn(1,512);
%imf = emd(s);
%[A,f,tt] = hhspectrum(imf(1:end-1,:));
%
%s = randn(10,512);
%[A,f,tt] = hhspectrum(s,1:512,2,1);
%
% rem: need the Time-Frequency Toolbox (http://tftb.nongnu.org)
%
%  emd, toimage, disp_hhs
%
% G. Rilling, last modification 3.2007
% gabriel.rilling@ens-lyon.fr

function [A,f,tt] = hhspectrum(x,t,l,aff)

error(nargchk(1,4,nargin));

if nargin < 2

t=1:size(x,2);

end

if nargin < 3

l=1;

end

if nargin < 4

aff = 0;

end

if min(size(x)) == 1
if size(x,2) == 1
x = x';
if nargin < 2
t = 1:size(x,2);
end
end
Nmodes = 1;
else
Nmodes = size(x,1);
end

lt=length(t);

tt=t((l+1):(lt-l));

for i=1:Nmodes

an(i,:)=hilbert(x(i,:)')';
f(i,:)=instfreq(an(i,:)',tt,l)';
A=abs(an(:,l+1:end-l));

if aff
disprog(i,Nmodes,max(Nmodes,100))
end

end

function [fnormhat,t]=instfreq(x,t,L,trace);

if (nargin == 0),
error('At least one parameter required');
end;
[xrow,xcol] = size(x);
if (xcol~=1),
error('X must have only one column');
end

if (nargin == 1),
t=2:xrow-1; L=1; trace=0.0;
elseif (nargin == 2),
L = 1; trace=0.0;
elseif (nargin == 3),
trace=0.0;
end;

if L<1,
error('L must be >=1');
end
[trow,tcol] = size(t);
if (trow~=1),
error('T must have only one row');
end;

if (L==1),
if any(t==1)|any(t==xrow),
error('T can not be equal to 1 neither to the last element of X');
else
fnormhat=0.5*(angle(-x(t+1).*conj(x(t-1)))+pi)/(2*pi);
end;
else
H=kaytth(L);
if any(t<=L)|any(t+L>xrow),
error('The relation L<T<=length(X)-L must be satisfied');
else
for icol=1:tcol,
if trace, disprog(icol,tcol,10); end;
ti = t(icol); tau = 0:L;
R = x(ti+tau).*conj(x(ti-tau));
M4 = R(2:L+1).*conj(R(1:L));

diff=2e-6;
tetapred = H * (unwrap(angle(-M4))+pi);
while tetapred<0.0 , tetapred=tetapred+(2*pi); end;
while tetapred>2*pi, tetapred=tetapred-(2*pi); end;
iter = 1;
while (diff > 1e-6)&(iter<50),
M4bis=M4 .* exp(-j*2.0*tetapred);
teta = H * (unwrap(angle(M4bis))+2.0*tetapred);
while teta<0.0 , teta=(2*pi)+teta; end;
while teta>2*pi, teta=teta-(2*pi); end;
diff=abs(teta-tetapred);
tetapred=teta; iter=iter+1;
end;
fnormhat(icol,1)=teta/(2*pi);
end;
end;
end;
```