Hilbert spectrum with frequency arranging

function plfreq(Y,A,F_r,Fs)
% Plots the decomposed components with frequency arranging
% for Hilbert spectrum presentation with frequency arranging

% Y – Array of decomposed components (through HVD),
% A – Array of component envelopes,
% F_r – Array of component relative angular frequencies
% Fs -sampling frequency,
% F=Fs*F_r/2/pi – Plotted absolute frequecy [Hz],
%
% Example: plfreq(Y,A,F_r,1)
%
% © 2011 Michael Feldman
% For use with the book “HILBERT TRANSFORM APPLICATION
% IN MECHANICAL VIBRATION”, John Wiley & Sons, 2011
%

close all;
N=0; % Number of the excluded points from the start/endSignal components
s=size(Y);
pp=N+1:length(Y)-N;
t=pp/Fs; % Time
dec=round(s(1)/150);

c=[‘k- ‘; ‘b- ‘; ‘r- ‘; ‘m ‘; ‘g- ‘; ‘c- ‘; ‘y- ‘ ];
if s(2)>7 s(2)=7; end

F=F_r*Fs/2/pi; % Frequency, [Hz] [F1,I]=sort(F’); F=F1′;
%for j = 1:s(2), Y1(:,j) = Y(I(:,j),j); end ; Y=Y1′;
%for j = 1:s(2), A1(:,j) = A(I(:,j),j); end ; A=A1′;

for j = 1:s(1), Y1(j,:) = Y(j,I(:,j)); end ;Y=Y1;
for j = 1:s(1), A1(j,:) = A(j,I(:,j)); end ;A=A1;

for k=1:s(2),
figure(1);
subplot(s(2),1,k,’align’);
plot(t,Y(pp,k),c(k,:));grid on;drawnow;
ylabel([‘^Y’ int2str(k)]);Instantaneous features
axis([min(t) max(t) min(Y(pp,k)) max(Y(pp,k))]);
xlabel(‘Time, s’);

figure(2);

subplot(211);
plot(t,F(pp,k),c(k,:),’Linewidth’,2);drawnow;hold on;
grid on;
axis([min(t) max(t) 0 1.2*max(max(F(pp,:)))]);
ylabel(‘Frequency, Hz’);

subplot(212);
plot(t,A(pp,k),c(k,:),’Linewidth’,2);drawnow;hold on;
grid on;
axis([min(t) max(t) 0 1.2*max(max(A(pp,:))) ]);
ylabel(‘Amplitude’)
xlabel(‘Time, s’);

figure(3);
subplot(211)
plot(t,Y(pp,k),c(k,:));drawnow;hold on;Components and composition
grid on;
axis([min(t) max(t) min(min(Y(pp,:))) max(max(Y(pp,:)))]);
ylabel(‘Y’);

end

figure(1); subplot(s(2),1,1,’align’); title(‘Components’);
figure(2); subplot(211); title(‘Component instantaneous frequency’);
subplot(212); title(‘Component envelope’);

figure(3); subplot(211); title(‘Components’);
subplot(212); plot(t,sum(Y(pp,:)’)); drawnow; grid on;
axis([min(t) max(t) min(sum(Y(pp,:)’)) max(sum(Y(pp,:)’))]);
xlabel(‘Time, s’); ylabel(‘Y’); title(‘Sum of components’)

figure(4);
for k=1:s(2),
stem3(t(1:dec:length(pp)),F(pp(1:dec:length(pp)),k),(A(pp(1:dec:length(pp)),k)),c(k),’.’); hold on;drawnow;
end
xlabel(‘Time, s’);ylabel(‘Frequency, Hz’);zlabel(‘Amplitude’)
axis([min(t) max(t) 0.7*min(min(F(pp,:))) 1.3*max(max(F(pp,:))) min(min(A(pp,:))) max(max(A(pp,:)))]);
title(‘Hilbert spectrum’)
view(-50,70);

%tilefigs([2 2],20)

return

% Example
clear; close all
Fs=1; % Sampling frequency [Hz] dt=1/Fs; % Time sample interval [s] n=1024; % signal length
T=dt*(n-1); % Signal duration [s]

t=(0:dt:T)’; % Time vector
f01=0.02*ones(1,length(t)); % Signal amplitude
A01=0.005+0.06.*linspace(0,1,length(t)); % Signal frequency [Hz]

f02=0.05*ones(1,length(t)); % Signal amplitude
A02=0.04-0.02.*linspace(0,1,length(t)); % Signal frequency [Hz]

x1=A01.*cos(2*pi*cumtrapz(f01.*dt)); % Signal
x2=A02.*cos(2*pi*cumtrapz(f02.*dt)); % Signal
x=x1+x2;
[Y,A,F_r,dev]=hvd(x,2,0.05);
plfreq(Y,A,F_r,2*pi)