Singh2020 / scripts /Step1_EEG_Pedaling_PreProcess.m
jalauer's picture
Add files using upload-large-folder tool
03a0748 verified
%% Rest data preprocessing step..
%%%%%%%%%%%%%%% About Markers %%%%%%%%%%%%%%%%%%%%
% S 1 = Warning Cue
% S 2 = GO Cue
% get the EEGLAB and have Brain Vision import addon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ref is Pz and Fs = 500 Hz
clear; clc; close all;
cwd = pwd;
addpath(cwd);
datalocation= [cwd '\ALL_DATA\RAW_DATA\']; % Data are here
%%% load subject and Behaviour info from the excel sheet
[~, ~, SubjInfo] = xlsread([cwd '\Patients_MS.xlsx'],'AllPedaling');
savelocation= [cwd '\ALL_DATA\ALL_Processed_Data\']; % Data go here
cd(savelocation);
% add eeglab to the path
p = genpath('eeglab13_4_4b'); addpath(p);
%%
for nn = 1:size(SubjInfo,1)
type = SubjInfo{nn,1};
subj = SubjInfo{nn, 2};
disp([type,num2str(subj)]);
EEG = pop_loadbv(datalocation,[type,num2str(subj),'.vhdr'], [], []);
% Edit the event code
del = 0;
for b = 1:size(EEG.event,2)
if strcmp(EEG.event(b-del).type, 'boundary')
EEG.event(b-del) = []; del = del+1;
end
end
% get the time difference: S1 ---> S2
TimeDiff = (diff([EEG.event.latency])/EEG.srate)'; TimeDiff = ([0; TimeDiff]);
for b = 1:size(EEG.event,2); EEG.event(b).TimeDiff = TimeDiff(b); end
% Epoch data on the basis of GO cue
EEG = pop_epoch( EEG, { 'S 2' }, [-1 3], 'epochinfo', 'yes');
EEG = eeg_checkset( EEG );
%% Remove X,Y,Z accelerometer channels
if EEG.nbchan>63
EEG.X=squeeze(EEG.data(64,:,:));
EEG.Y=squeeze(EEG.data(65,:,:));
EEG.Z=squeeze(EEG.data(66,:,:));
EEG.data=EEG.data(1:63,:,:);
EEG.nbchan=63;
EEG.chanlocs(66)=[]; EEG.chanlocs(65)=[]; EEG.chanlocs(64)=[];
end
% Fix BV-specific issue - - - only needed for APPLE
for ai=1:size(EEG.urevent,2), EEG.urevent(ai).bvtime=EEG.urevent(ai).bvmknum; end
for ai=1:size(EEG.event,2), EEG.event(ai).bvtime=EEG.event(ai).bvmknum; end
for ai=1:size(EEG.epoch,2), EEG.epoch(ai).eventbvtime=EEG.epoch(ai).eventbvmknum; end
% Add Pz which is Ref
EEG = pop_chanedit(EEG,'append',63,'changefield',{64 'labels' 'Pz'});
% EEG = pop_chanedit(EEG,'lookup', locpath); %%%% if loading channels location then use this
% Re-Ref to Average Ref and recover Pz
EEG = pop_reref(EEG,[],'refloc',struct('labels',{'Pz'},'type',{''},'theta',{180},'radius',{0.2535},'X',{-60.7385},'Y',{-7.4383e-15},'Z',{59.4629},...
'sph_theta',{-180},'sph_phi',{44.3920},'sph_radius',{85},'urchan',{64},'ref',{''}),'keepref','on');
% Remove 1=Fp1 and 31=Fp2: Eye blink and artifacts; 26=FT10, 10=TP9 and 20=TP10: Ear muscle artifacts: Since Pz has been reconstructed from the total
EEG.data = EEG.data([2:9, 11:19, 21:25, 27:30, 32:64],:,:);
EEG.nbchan=59;
EEG.chanlocs(31)=[]; EEG.chanlocs(26)=[]; EEG.chanlocs(20)=[]; EEG.chanlocs(10)=[]; EEG.chanlocs(1)=[]; % Have to be in this order! It will delete channel locs
% Should probably re-ref to average again now that the contaminated channels are gone
EEG = pop_reref(EEG,[]);
% Remove mean
EEG = pop_rmbase(EEG,[],[]);
%% ----------------------
% Setup APPLE to interp chans, reject epochs, & ID bad ICs. Output will be Avg ref'd and ICA'd.
subno = subj;
VEOG = 0;
session = 1;
TASK = ['Ped_',type];
EEG_chans=1:EEG.nbchan;
Do_ICA=1;
Reref = 'FCz'; % Re-Ref to FCz [THIS IS FOR FASTER, which is a part of APPLE]
chanidx = find(strcmpi(Reref,{EEG.chanlocs.labels}));
ref_chan=chanidx;
EEG = pop_reref(EEG,ref_chan,'keepref','on');
% Run APPLE (will re-ref data to avg ref)
[EEG,bad_chans,bad_epochs,bad_ICAs]=APPLE_ActiveCap_v2(EEG,EEG_chans,ref_chan,Do_ICA,subno,VEOG,session,TASK);
%%% Save
save([savelocation,type,num2str(subj),'_Ped_Processed.mat'],'EEG','bad_chans','bad_epochs','bad_ICAs');
%%% for loop
clearvars -except datalocation savelocation SubjInfo
end