% AUTHORS: Ana Marta Dias and Pedro Sousa
% Performs High Frequencies / Low Frequencies ratio, using Wavelet Transform
% Calculates duration of the tonic and clonic phases of the GTCS

function [ratio, max_HF, max_LF] = func_BicepsHFLF(emg, Fs)

L = length(emg);

filt = HFLF_Bandpass;
filtemg = filt(emg); % Bandpass Filtering 2Hz-260Hz

% Wavelet Tranform
fb = cwtfilterbank('SignalLength', L, 'VoicesPerOctave', 4, 'SamplingFrequency', Fs);
wavelet = wt(fb, filtemg);
freq = abs(wavelet);
[row, ~] = size(freq);

% Variable Initialization
HF = zeros(row, L);   % High-Frequency Components per Sample
LF = zeros(row, L);   % Low-Frequency Components per Sample

% -------------------------------------------

% High Frequency Components
HF_index = (freq > 150) & (freq < 260);
HF(HF_index) = freq(HF_index);
clear HF_index;
max_HF = max(HF);
max_HF = medfilt1(max_HF, 250); 

% Low Frequency Components
LF_index = (freq > 6) & (freq < 70);
LF(LF_index) = freq(LF_index);
clear LF_index;
max_LF = max(LF);
max_LF = medfilt1(max_LF, 250);

% Ratio Computation
ratio = max_HF./max_LF; % 1 x No. Of Samples vector containing the ratio for each sample
ratio = medfilt1(ratio, 250);      % Signal Suavization
ratio(max_LF == 0) = 0;

end

