Contents

%%%#!/usr/local/bin/matlab%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%O
% MATLAB Script to read and preprocess NetStation EGI segmented Raw Data
% 1. Read segmented EGI Data (.raw files)
% 2. Import raw data and save as EEGLab dataset. Export Events (seconds).
% 3. Adapt the Events file exported above: Edit Triggers + Insert Auditory Cues
% 4. Insert Auditory Cues (InsertAuditoryCues.sh, and in turn AWK scripts)
% 5. Import Adapted Events File, containing adapted triggers and auditory cues
% 6. Filter the data -- We've adapted Katrin's version of this part
% 7. Re-reference the data to the linked mastoids
% 8. Deselect channels in the periphery...they're just way too noisy to make use of!
%
% Filter definition adapted from: Katrin Cunitz, with inputs from: Alessandro Tavano
%
% Author: R. Muralikrishnan -- 24.06.2016; Last modified: 07.03.2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%m
clear all;
close all;

eeglab;

V_Folder_RawData = '/home/muralir/Documents/AB/Experiments/NeuroFemi/Raw_NeuroFemi_RAW/';
V_Folder_EEGLabData = '/home/muralir/Documents/AB/Experiments/NeuroFemi/EEGLab-Data/';

V_Participant = 'filled_in_below';

% CA_x => CellArray
CA_Participants = {'NF01','NF02','NF03','NF04','NF05','NF06','NF07','NF08','NF09','NF10','NF11','NF12','NF13','NF14','NF15','NF16','NF17','NF18','NF19','NF20','NF21','NF22','NF23','NF24','NF25','NF26','NF27','NF28','NF29','NF30','NF31','NF32','NF33','NF34','NF35','NF37','NF38','NF39','NF40','NF41','NF42','NF43','NF44','NF45','NF46','NF47','NF48','NF49','NF50','NF51','NF52','NF53','NF54','NF55','NF56'};
%CA_Participants = {'NF01','NF02','NF03','NF05','NF06','NF07','NF09','NF10','NF11','NF12','NF13','NF14','NF15','NF16','NF20','NF21','NF22','NF23','NF24','NF25','NF27','NF28','NF30','NF32','NF33','NF34','NF37','NF38','NF39','NF42','NF43','NF45','NF46','NF47','NF48','NF49','NF51','NF52','NF53','NF54','NF55','NF56'};

Import raw data and save as EEGLab dataset. Export Events (seconds).

for V_Counter = 1:length(CA_Participants)
  V_Participant = CA_Participants{V_Counter};
  V_InputFile = fullfile(V_Folder_RawData, [V_Participant '001.raw']);

  % Create a participant folder
  V_Command = sprintf('mkdir %s', [V_Folder_EEGLabData, V_Participant]);
  system(V_Command);

  fprintf('########################################################\n\n');
  fprintf('Reading Raw Data for %s.\n\n', V_Participant);

  % Read segmented EGI file
  EEG = pop_readsegegi(V_InputFile);

  % The following does not work in combination with pop_readsegegi! Katrin
  % didn't use pop_readsegegi but a different approach, so we're not sure
  % if it is because of this that it worked for her and not for us.
  % BUT, pop_readsegegi calls readegilocs.m, which, based on the number of
  % electrodes, assings GSN129.sfp to be the correct one...which of course
  % in our case wasn't quite correct due to the location mismatches.
  % So we simply changed EEGLAB/functions/sigprocfunc/readegilocs.m such that GSN-HydroCel-129.sfp is
  % read, rather than GSN129.sfp.  The correct file GSN-HydroCel-129.sfp is copied and is in both
  % the AB-Scripts folder as well as EEGLAB/sample_locs folders.

  % Specify the channel positions file to use.  We use the identical file that Katrin used (see her preprocess.m)
  % EEG.chanlocs = pop_chanedit(EEG.chanlocs, 'load', {'/home/muralir/Documents/AB/Experiments/NeuroFemi/GSN-HydroCel-129.sfp', 'filetype','sfp'});

  %   for c = 1:EEG.nbchan                           % nbchan = number of channels
  %       EEG.chanlocs(c).chantype = 'EEG';
  %   end


  % Save the resultant file as an EEGLab .set

  EEG.filename = V_Participant;
  EEG.filepath = [V_Folder_EEGLabData, V_Participant];
  EEG.comments = char(V_Participant, '; Sampling Rate: 500 Hz;');
  pop_saveset(EEG, 'filename', EEG.filename, 'filepath', EEG.filepath);



  % Export the Events list for further processing (outside EEGLab)
  V_EventsFile = fullfile(V_Folder_EEGLabData, V_Participant, [V_Participant, '.events']);
  %pop_expevents(EEG, V_EventsFile, 'samples');
  % Exporting Event latencies as samples would mean that, the output always
  % depends upon the sampling frequency.  And in turn, all calculations in
  % InsertAuditoryTriggers etc. need to take into account the sampling
  % frequency. For instance, when the data is down-sampled, this must also
  % be changed inside Insert Auditory Triggers script.  To avoid these
  % interdependencies as well as to keep it more human-readable and similar
  % to .trg files, we export the events in seconds here (Not possible in GUI!).
  pop_expevents(EEG, V_EventsFile, 'seconds');

  fprintf('########################################################\n\n');
  fprintf('Saved imported data and exported events for ... %s.\n\n', V_Participant);

end;

Adapt the Events file exported above: Edit Triggers + Insert Auditory Cues

Run the ShellScript which runs the AWK script for each file to incorporate auditory cues as latencies.

fprintf('########################################################\n\n');
fprintf('Inserting Auditory Triggers / Adapting Events \n\n');

In case the events were exported as samples...

%fprintf('IMPORTANT: If THIS STEP DEPENDS ON THE SAMPLING FREQUENCY!!! \n\n');
%fprintf('         : Check the Shell Script to see if the sampling frequency\n');
%fprintf('         : used there matched that of the data\n');
%fprintf('         : THIS STEP IS BEST DONE BEFORE ANY RESAMPLING!!!\n\n');

Else if the events were exported in seconds, then sampling frequency

doesn't affect the scripts. We follow this, as usual.

system('sh Adapt-Triggers-in-EventsList.sh');
for V_Counter = 1:length(CA_Participants)

Import Adapted Events File, containing adapted triggers and auditory cues

In the GUI, we selected: headings = type latency; header = 1; Time = 1; Align = NaN; Auto adjust = unchecked = Off;

  V_Participant = CA_Participants{V_Counter};
  V_DataFile = fullfile(V_Folder_EEGLabData, V_Participant, [V_Participant, '.set']);

  EEG = pop_loadset('filename', V_DataFile);
  EEG = pop_importevent( EEG, 'append','no','event',fullfile(V_Folder_EEGLabData, V_Participant, [V_Participant '.trgcorcues']),'fields',{'type','latency'},'skipline',1,'timeunit',1,'align',NaN,'optimalign','off');

NO DOWNSAMPLING ANYMORE so ICA has more samples!!! Downsample the 500 Hz data to 250 Hz

  %EEG = pop_resample(EEG,250);

Filter the data -- We've adapted Katrin's version of this part

Define Filter Parameters

  A_fcutoff = [0.3 40];    % 0.3 to 40Hz Bandpass: Highpass: 0.3 Hz + Lowpass: 40 Hz
  V_tbw = A_fcutoff(1)/2;  % Transition Bandwidth???
  V_dev = 0.001;           % Filter Ripple???

  V_mhp_kaiser = pop_firwsord('kaiser',EEG.srate,V_tbw,V_dev);
  V_beta = pop_kaiserbeta(V_dev);

  [EEG,com,Bhp] = pop_firws(EEG, 'fcutoff', [A_fcutoff], 'ftype','bandpass', 'wtype', 'kaiser', 'warg', V_beta, 'forder', V_mhp_kaiser); % From A Widmann's firfilt 1.6.1 plugin

  % Additional Notchfilter around 50 Hz [48, 52] ...Katrin's comments: / adaptiv? (Olguin+, 2005)
  % EEG = pop_iirfilt(EEG, 48, 52, [], [1]); % From M Pozdin's Infinite impulse response filter iirfilt 1.02 plugin
  % Notch Filter not a good idea apparently: Ref: http://www.nitrc.org/projects/cleanline/
  % So we don't use it.  Also, we filter our data up to 45 Hz.
  % If necessary, cleanline could be used to achieve a better result.
  % OLD EEG = pop_firws(EEG, 'fcutoff', 1, 'forder', 454, 'ftype', 'highpass', 'wtype', 'kaiser', 'warg', 5.65326);%from A Widmann's firfilt 1.6.1 toolbox


  % Save the resultant file as an EEGLab .set

  EEG.filename = [V_Participant '_bp03_40'];
  EEG.filepath = [V_Folder_EEGLabData, V_Participant];
  EEG.comments = char([V_Participant, '_bp03_40'], '; Sampling Rate: 500 Hz; BPF: 0.3-40 Hz;');
  pop_saveset(EEG, 'filename', EEG.filename, 'filepath', EEG.filepath);

Re-reference the data to the linked mastoids

  %EEG = pop_reref(EEG, [], 'keepref', 'on'); % Common/Average reference
  EEG = pop_reref(EEG, [57 100], 'keepref', 'on');

Deselect channels in the periphery...they're just way too noisy to make use of!

  EEG = pop_select(EEG, 'nochannel', [8,1,121,114,95,89,82,74,69,64,44,38,32,25,120,113,107,99,94,88,81,73,68,63,56,49,43,119,48,125,128]);

  % Save the resultant file as an EEGLab .set

  EEG.filename = [V_Participant '_bp03_40r_'];
  EEG.filepath = [V_Folder_EEGLabData, V_Participant];
  EEG.comments = char([V_Participant, '_bp03_40r_'], '; Sampling Rate: 500 Hz; BPF: 0.3-40 Hz; ReRef: Linked Mastoids;');
  pop_saveset(EEG, 'filename', EEG.filename, 'filepath', EEG.filepath);

  fprintf('########################################################\n\n');
  fprintf('Saved Filter_ReRef data for ... %s.\n\n', V_Participant);


  %Om
end;

The dataset ABxx_bp03_40r_.set is the one that will be used in the next step!

%# See EEGLab_Rejection_PreICA.m