% SKRIPT SLOUŽÍ K NAHRÁNÍ DAT ZÍSKANÝCH Z FNKV
% POSTUPNĚ NAHRÁVÁ JEDNOLIVÉ SVODY EKG A PROVÁDÍ NA NICH
% VYHLAZENÍ POMOCÍ KUBICKÉHO SPLINU A PRVNÍ DERIVACI SIGNÁLU
% VÝSLEDNÝ SIGNÁL JE ULOŽEN VE FORMÁTU .CSV


close all, clc, clear all

%%
[annot,~,~] = xlsread('QRSDetekceALL_CurilaManual21102020.xlsx');
annot = annot(~isnan(annot));
annot = reshape(annot,[],4);

for i = 1:size (annot,1)
%% LOAD SIGNALS
temp = annot (i,1);
temp = int2str (temp);

% if is name of number < 1000
if length (temp) < 4
    temp = ['0',temp]
end

%load annotation
name =[pwd,'\source\', temp,'.mat'];
load(name)
load ('Annotation.mat')

%load signal
ecg = squeeze(VYSLTD{1,1}(1,:,1:14));
ecgStim = squeeze(VYSLTDs{1,1}(1,:,1:14));

% edit annotation
QRS_begin = annot(i,3);
QRS_end = annot(i,4);

QRS_begin = QRS_begin/1000/(1/fvz)+1000;
QRS_end = QRS_end/1000/(1/fvz)+1000;

Annotation = Annotation (1:length(ecg));
Annotation(1:length(ecg)) = 'n';
Annotation(QRS_begin:QRS_end)= 'QRS';


%% VERTIFICATION OF ANNOTATION

x = 1:size (ecg,1);
figure
plot (x,ecg (:,9))
hold on 
a = (QRS_begin:QRS_end);
b = ecg (QRS_begin:QRS_end,9);
plot (a,b)
legend('signal','annotation')

%% 
tempi = int2str(i);
typ = 'orig'
for j = 1:14
        
        data = [];
        patient = [];
        x = [];
        smooth = [];
        der_data = [];
        
        
        patient = s (:,j);
       
        x = 1:length(patient);
        smooth = csaps(x,patient,0.25) %smoothing with cubic spline
        der_data = diff(patient); %1. derivation
        
               
        smooth_data=smooth.coefs (:,4);
        smooth_data(end+1)= patient(end);
        der_data(end+1)=der_data(end);
        der_data=der_data';
       

        smooth_data = smooth_data';
        der_data = der_data';
        
        data = table;
        data{:,1} = x';
        data{:,2} = Annotation;
        data{:,3} = patient;
        data{:,4} = smooth_data';
        data{:,5} = der_data;
        
        data.Properties.VariableNames = {'index','annotation' 'signal' 'smooth' 'derivation'};
        
        
        tempj = int2str(j);
        name = [tempi,'\''FNKV_data_',typ,'_',tempi,'_',tempj,'.csv']
                
        writetable(data,name,'Delimiter',';')
        clear data
        
end

tempii = int2str(i+401);
mkdir(tempii)
typ = 'stim'

for j = 1:14
        
        data = [];
        patient = [];
        x = [];
        smooth = [];
        der_data = [];
        
        
        patient = s (:,j);
       
        x = 1:length(patient);
        smooth = csaps(x,patient,0.25) %smoothing with cubic spline
        der_data = diff(patient); %1. derivation
        
               
        smooth_data=smooth.coefs (:,4);
        smooth_data(end+1)= patient(end);
        der_data(end+1)=der_data(end);
        der_data=der_data';
       

        smooth_data = smooth_data';
        der_data = der_data';
        
        data = table;
        data{:,1} = x';
        data{:,2} = Annotation;
        data{:,3} = patient;
        data{:,4} = smooth_data';
        data{:,5} = der_data;
        
        data.Properties.VariableNames = {'index','annotation' 'signal' 'smooth' 'derivation'};
        
        
        tempj = int2str(j);
        name = [tempii,'\''FNKV_data_',typ,'_',tempi,'_',tempj,'.csv']
                
        writetable(data,name,'Delimiter',';')
        clear data
        
end



end
