% SKRIPT SLOUŽÍ K VYMEZENÍ QRS KOMPLEXŮ V EKG ZÁZNAMU
% JAKO VSTUPNÍ DATA JE NUTNÉ POUŽÍT .CSV SOUBOR ZÍSKANÝ Z SKRIPTU
% signal2csv.py

clc, clear all, close all,

%% SETUP
window = 0:9; % possible width of deviation  
% QT 0:9 FNKV 0:5:45 THEW 0:4:36
gap = 12; % gap of filling 0 points in QRS komplex
fvz = 250; % frequency in HZ QT 250 FNKV 5000 THEW 1000
patient =  1:10  ; % range of the record for evaluation

%TRAIN QT 1:126 FNKV 1:241 THEW 1:361
%VALID QT 127:168 FNKV 242:322 THEW 362:482
%TEST QT 169:210 FNKV (323:401 724:802)THEW 483:602 

on = zeros (length(patient), length(window));
off = zeros (length(patient), length(window));

tic

for g = 1: length(patient)
%% LOAD DATA
disp (g/length(patient))
pom = int2str(patient (g));
name = ['results_',pom,'.csv'];    
input = readtable(name);

signal = [];
pos = [];

signal(:,1) = input {:,2}; % predicted signal
signal(:,2) = input {:,3}; % ground truth

pos = find (signal~=1); % only QRS komplex
signal(pos)= 0;
signal = [0,0;signal;0,0]; % extension of the signal by the first and last point

%% GAP FILLING

signal = gap_filling (signal,gap);


%% ON/OFF QRS
on_QRS = zeros (size (signal));
last_detected_on = 0;% measures against samples  

off_QRS = zeros (size (signal));
last_detected_off = 0;

ON = false; 



for i = 2:(size(signal,1)- 1)
   %ON QRS
    if (signal((i-1),1) == 0) && (signal(i,1) == 1) && ON == 0
       
       if (i - last_detected_on) > (0.005*fvz)
            on_QRS(i,1) = 1; 
            ON = 1;
       end
       
       last_detected_on = i;        
    end

    if (signal((i-1),2) == 0) && (signal(i,2) == 1)
       on_QRS(i,2) = 1; 
    end
   
  % OFF QRS  
    if (signal((i),1) == 1) && (signal(i+1,1) == 0) && ON == 1 
       
       
       if (i - last_detected_off) > (0.005*fvz)
            off_QRS(i,1) = 1; 
            ON = 0; 
       end
       
       last_detected_off = i;     
    end

    if (signal((i),2) == 1) && (signal(i+1,2) == 0)
       off_QRS(i,2) = 1; 
    end
end


[on_QRS_result, on_correct_detected, on_detect_position, on_numb_pred, ...
    on_numb_ground, on_numb_diff] = on_off_QRS (on_QRS, window);

[off_QRS_result, off_correct_detected,off_detect_position, off_numb_pred, ...
    off_numb_ground, off_numb_diff] = on_off_QRS (off_QRS, window);



%% QRSd

temp = min ((size (on_detect_position,1)),(size (on_QRS_result,1)));
both_QRSd = zeros (temp,1);
both_QRSd_diff = zeros (temp, 1);

%temp = min ((size (on_detect_position,1)),(size (on_QRS_result,1)));
    for j = 1: length(both_QRSd)
 
        % correct detection one of position QRS
        if (on_detect_position (j,end) == 1) || (off_detect_position (j,end) == 1)
           both_QRSd(j) = off_QRS_result(j,1)- on_QRS_result(j,1); 
           both_QRSd_diff(j) =  both_QRSd(j) - (off_detect_position(j,1) - on_detect_position(j,1));
        end
        
    end


%% RESULTES

diff (g) = on_numb_diff;
detec(g) = on_numb_ground;

mean_abs_QRSd_ms (g) = sum (abs(both_QRSd_diff)*(1/fvz)*1000)/length(both_QRSd_diff);
mean_QRSd_ms (g) = sum ((both_QRSd_diff)*(1/fvz)*1000)/length(both_QRSd_diff);

on (g,:) = on_correct_detected / on_numb_ground;

off (g,:) = off_correct_detected / off_numb_ground; 

end

on(on>1) = 1
off(off>1) = 1

data = table;
data = table(patient',detec', diff', mean_abs_QRSd_ms',mean_QRSd_ms', on(:,1), on(:,2),...
    on(:,3), on(:,4), on(:,5), on(:,6), on(:,7), on(:,8),on(:,9), on (:,10), off(:,1), off(:,2),...
    off(:,3), off(:,4), off(:,5), off(:,6), off(:,7), off(:,8),off(:,9), off(:,10));

name = ['delineator.csv'];  

data.Properties.VariableNames = {'pacient', 'pocet_QRS_komplexu','pocet_nedekovanych_komplexu', 'prumerny_rozdil_QRSd_absolutni_hodnota',...
    'prumerny_rozdil_v_hodnotach_QRSd','on_0ms', 'on_4ms','on_8ms', 'on_12ms', 'on_16ms', 'on_20ms', 'on_24ms', 'on_28ms', 'on_32ms', 'on_36ms',...
    'off_0ms', 'off_4ms','off_8ms', 'off_12ms', 'off_16ms', 'off_20ms', 'off_24ms', 'off_28ms', 'off_32ms', 'off_36ms'};

writetable(data,name,'Delimiter',';')

toc

