% hep_epoch 
% -----------------
% Curso HEP Granada 2018
% Pandelis Perakakis, Luis Ciria
% peraka@ugr.es, lciria@ugr.es
% ------------------------------
%
% load the eeglab .set file called ENT_1_Revents.set using the eeglab GUI
% or the script below
[file, path] = uigetfile ('*.set','Pick an eeglab .set file',...
    'Multiselect','off'); % select the file
EEG = pop_loadset('filename',file,'filepath',path); % load the file
eeglab redraw % draw the eecglab GUI
%
% define variables
thresh = 200; % threshold for artifact rejection
elecs = 1:64; % elctrodes of interest
%
% extract R1 epochs
EEGOUT = pop_epoch( EEG, {'R1'}, [-0.2  1], 'epochinfo', 'yes');
trialsini = EEGOUT.trials; % No trials before rejection
EEGOUT = pop_eegthresh(EEGOUT, 1, elecs, -thresh, ...
    thresh, -0.2, 1, 0, 1); % reject abnormal values
trialsfin = EEGOUT.trials; % No trials after rejection
rmtrials = (trialsini-trialsfin)/trialsini; % percentage of rejected trials
pop_saveset( EEGOUT, 'filename','ENT_1_HEP1.set');
%
% extract R2 epochs
EEGOUT = pop_epoch( EEG, {'R2'}, [-0.2  1], 'epochinfo', 'yes');
trialsini = EEGOUT.trials;
EEGOUT = pop_eegthresh(EEGOUT, 1, elecs, -thresh, ...
    thresh, -0.2, 1, 0, 1);
trialsfin = EEGOUT.trials;
rmtrials = (trialsini-trialsfin)/trialsini;
pop_saveset( EEGOUT, 'filename','ENT_1_HEP2.set');