function[attributes_local] = FindAttributes(file)
% Funkce slouzi k vyhledavani priznaku. Priznaky jsou prirazeny kazdemu
% detekovanemu vrcholu v elektrogramu sini.
%
% Vstup: file - jeden objekt obsahujici data
%
% Vystup: attributes_local - matice příznaku, radky znaci jednotlive
%                            priznaky, sloupce znaci jednotlive cykly
%                  
% Funkce: Extraction - funkce pro extrakci vrcholu sinove aktivity
%         DetectionQRS - funkce pro extrakci vrcholu komorove aktivity
%
%% load

CS5 = file.data(13).content';
CS4 = file.data(14).content';
CS3 = file.data(15).content';
CS2 = file.data(16).content';
CS1 = file.data(17).content';
all_CS = [CS5; CS4; CS3; CS2; CS1]';

fvz = file.measurement.samplingFrequency; %sampling frequency

all_CS = all_CS - mean(all_CS); % subtraction of the effect of the DC component

%% atrial activity peaks

[~, idx_atr, beginning, ending] = Extraction(file); 

%% ventricular activity peaks

[idx_ventr] = DetectionQRS(file);

%% activation in CS
% beginnig, ending AA
% activity fisrt means that attribute is 0

idx = [];
for i = 1:length(beginning)
    [peaks_CS1(i), idx_CS1(i)] = max(CS1(beginning(i) : ending(i)));
    [peaks_CS2(i), idx_CS2(i)] = max(CS2(beginning(i) : ending(i)));
    [peaks_CS3(i), idx_CS3(i)] = max(CS3(beginning(i) : ending(i)));
    [peaks_CS4(i), idx_CS4(i)] = max(CS4(beginning(i) : ending(i)));
    [peaks_CS5(i), idx_CS5(i)] = max(CS5(beginning(i) : ending(i)));
    
    % zero for the first one
    early = min([idx_CS1(i), idx_CS2(i), idx_CS3(i), idx_CS4(i), idx_CS5(i)]);
    idx(:,i) = [idx_CS1(i)-early; idx_CS2(i)-early; idx_CS3(i)-early; idx_CS4(i)-early; idx_CS5(i)-early]; 
end

%% both in one

idx_signal = zeros(1, length(CS5)); % 0 for no antrial and no ventricular peak

idx_signal(idx_atr) = 1; % 1 for atrial peak
idx_signal(idx_ventr) = 2; % 2 for ventricular peak

%% QP - atrium to ventricular activity

QP_vz = []; % PQ intervals in samples

for i = 1 : length(idx_atr)
    ok = 0;
    j = idx_atr(i) - 1;
    while ok == 0
        if  j == 0 % none wave found before Q wave
            QP_vz = [QP_vz, 0];
            ok = 1;
        elseif idx_signal(j) == 1 % another P before P (start - index 1, not before)
            QP_vz = [QP_vz, 0];
            ok = 1;
        elseif idx_signal(j) == 2 % Q found before P
            QP_vz = [QP_vz, idx_atr(i) - j ];
            ok = 1;
        else
            j = j-1;
        end
    end
end

QP_sec = QP_vz./fvz; % QP intervals in sec


%% PQ interval - another atrial activity found after ventricular

PQ_vz = []; % PQ intervals in samples

for i = 1 : length(idx_atr)
    ok = 0;
    j = idx_atr(i) + 1;
    while ok == 0
        if  j >= length(idx_signal) + 1 % none wave found after P wave
            PQ_vz = [PQ_vz, 0];
            ok = 1;
        elseif idx_signal(j) == 1 % P found after P
            PQ_vz = [PQ_vz, 0];
            ok = 1;
        elseif idx_signal(j) == 2 % Q found after P
            PQ_vz = [PQ_vz, j - idx_atr(i)]; % length of another PQ
            ok = 1;
        else
            j = j+1;
        end
    end
end

PQ_sec = PQ_vz./fvz; % PQ intervals in sec

%% average rate for both activity - beats/min

msPerBeat = mean(diff(idx_atr));
atr_rate = 60*(fvz/msPerBeat);
msPerBeat_2 = mean(diff(idx_ventr));
ventr_rate = 60*(fvz/msPerBeat_2);

%% P : Q ratio

ratio_PQ = atr_rate / ventr_rate;

%% median voltage value throught all CS leads

volt = median(abs(all_CS), 'all'); 

%% short signals
% if there is not enough beats for RMSSD and A

if length(idx_atr) <= 1
    attributes_global = [ratio_PQ, 
        volt];
    
elseif length(idx_atr) < 4 
    attributes_global = [ratio_PQ, 
        volt];
    attributes_local = [idx;
         PQ_sec;
         QP_sec;
        NaN * ones(1,length(idx_atr));    
        NaN * ones(1,length(idx_atr))]; 
    
attributes_local(size(attributes_local,1)+1, 1:size(attributes_local, 2)) = attributes_global(1);
attributes_local(size(attributes_local,1)+1, 1:size(attributes_local, 2)) = attributes_global(2);

%% not short
% if there is enought beats for RMSSD and A
else
    %% PP - atrial to atrial activity
    
    PP_vz = diff(idx_atr); % PP intervals in samples
    PP_sec = PP_vz./fvz; % PP intervals in sec
    
    %PP_timeline = cumsum(PP_sec); % PP intervals on timeline (sec)
    %figure
    %stem(PP_timeline,PP_sec) % PP intervals on timeline
    
    %% CCM (Complex Correlation Measure)
    % triangle area on Poincare map
    
    %CCM = [];
    A = [];
    
    for i = 4 : length(PP_vz)
        D = det([PP_vz(i-3), PP_vz(i-2), 1; ...
            PP_vz(i-2), PP_vz(i-1), 1; ...
            PP_vz(i-1), PP_vz(i),   1]);
        A(i-3) = 0.5 * D;
    end
    
%     S = sum(A);
%     Cn = pi * SDs(1) * SDs(2);
%     N = length(PP_sec) - 1;
%     
%     CCM = (1 / (Cn * (N - 2))) * S;
    
    %% RMSSD for window length 3

    RMSSD = [];
    for j = 4 : length(PP_vz)
        section = PP_vz((j - 3) : (j));
        pom = [];
        for i = 1:(length(section)-1)
            pom(i) = (section(i+1)-section(i))^2;
        end
        RMSSD(j-3) = sqrt((1/(length(section)-1)*sum(pom))); %RMSSD (ms) – druhá odmocnina průměru druhých mocnin rozdílů mezi po sobě jdoucími NN intervaly
        %nRMSSD = RMSSD/mean(PP_sec);
    end
  
    %% attributes output (attributes_local)
    
    attributes_global = [ratio_PQ,
        volt];
    attributes_local = [idx(:, 5:length(idx_atr));
         PQ_sec(5:length(idx_atr));
         QP_sec(5:length(idx_atr));
        A;     % od tretiho po posledni interval
        RMSSD]; % od tretiho po posledni interval (prvni interval neeituje)
    
    attributes_local(size(attributes_local,1)+1, 1:size(attributes_local, 2)) = attributes_global(1);
    attributes_local(size(attributes_local,1)+1, 1:size(attributes_local, 2)) = attributes_global(2);

end

end