% ecg_preprocessing 
% -----------------
% Curso HEP Granada 2018
% Pandelis Perakakis, Luis Ciria
% peraka@ugr.es, lciria@ugr.es
% ------------------------------
%
% extract the ECG signal from the raw data file
% 
% load the EEG file 'ENT_1_raw.set' and extract the ECG channel using the
% eeglab GUI
% to visualise the signal use:
% figure; plot(EEG.data(66,:))
%
% to do the same from the cmd window use:
EEG = pop_loadset('filename','ENT_1_raw.set','filepath',cd); % load EEG
EEG = pop_select( EEG,'channel',{'EXG2'}); % select the ECG channel
ecg = EEG.data; % extract ECG signal from EEG variable
% alternatively you can use: ecg = EEG.data(66,:);
% 
% filter the ECG signal using the ecg_filt function
ecgout = ecg_filt(double(ecg),1024); % filtered ecg signal
%
% prepare ECG .mat file for ecglab
% the variables have to be called 'ecg' and 'fs'
% ecg must be an Nx1 vector
ecg = ecgout';
fs = 1024; % sampling frequency
save ('filename.mat','ecg','fs');
%
% load the ECG .mat file called ENT_1_sampleECG.mat to ecglabRR
% use the ecglabRR GUI to generate the ENT_1_sampleECG_rr.mat file