function [analytic_signal, inst_amplitude, inst_phase, inst_freq] = HilbertT (Fsignal, Fs)
% Hilbert transform computation and calculation of instantaneous parameters: Amplitude, Phase and Frequency  
%
% @file  HilbertT.m
%
%
% This function applies Hilbert transformation and performs the following
% operations: 
%  1 - Hilbert Transform computation
%  2 - Plot of real and imaginar components
%  3 - Calculate instantaneous parameters: Amplitude, Phase and Frequency
%
%
%
%  [analytic_signal, inst_amplitude, inst_phase, inst_freq] = HilbertT (Fsignal, Fs)
%  
%  INPUTS:
%
%   Fsignal:             Data/Signal to be processed. Signals should be filtered
%                        previously if that step is required.  
%                        * A Butterworth filter is provided on @file Butt_highpass.m
%
%   Fs:                  Sampling Frequency    
%
%
%  OUTPUTS:
%
%  analytic_signal:      Hilbert transform for the input sequence Fsignal ;
%                        Result is a complex result with the same Fsignal length 
%
%  inst_amplitude:       Instantaneous magnitude of the analytic signal
%
%  inst_phase:           Instantaneous phase of the analytic signal
%
%  inst_freq:            Instantaneous frequency of the analytic signal  
%
%
%
% Beatriz Jacinto Barros (beatriz.j.barros@inesctec.pt)
% BRAIN, INESC-TEC Porto, October 2022
% 

%% Hilbert Transform computation
T = 1/Fs;                                           % Sampling period  
t = (0:length(Fsignal)-1)*T;                        % Time vector    
analytic_signal = hilbert(Fsignal);                 % Compute the analytical signal through Hilbert Transform

%% Plot real and imaginary parts 
% the real part of the analytic signal should be the same as the original signal and 
% the imaginary part of the analytic signal a -90º phase shifted version of the original signal.

plot(t,real(analytic_signal),t,imag(analytic_signal))
legend('real','imaginary')
title('Hilbert Function')
xlabel('Time (s)')

%% Compute instantaneous parameters
inst_amplitude = abs(analytic_signal);              % Instantaneous amplitude (complex envelope of the original signal)
inst_phase = unwrap(angle(analytic_signal));        % Instantaneous phase

