-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEG02_channelPreprocessing.m
executable file
·99 lines (79 loc) · 4.95 KB
/
EEG02_channelPreprocessing.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%% Preprocessing : phase 2
% - Remove EMG and AUX channels
% - Remove or interpolate bad channels (comment line 78 to remove and 79 to interpolate)
% - Save
%
% Note that channel to remove should be indicated as channel number
%
% Tip: place a stopper at line 65 so that you can inspect channels before being
% asked which ones you would like to remove
close all;
clear all;
clc;
% Declare paths
pathData = '/Volumes/Seagate/project_rhythmicBrain/DATA/'; %Folder where all data is
addpath('/Users/claraziane/Documents/Académique/Informatique/MATLAB/eeglab2021.1'); %EEGLAB toolbox
% Load matrix where all rejected channels are stored (comment if running script for the 1st time)
load('/Volumes/Seagate/project_rhythmicBrain/Results/subAll/chanReject.mat')
% Load file containing EEG electrode coordinates as some blocks do not have the right coordinates
load('/Volumes/Seagate/project_rhythmicBrain/DATA/chanLocs.mat')
Participants = {'sub-001'; 'sub-002'; 'sub-003'; 'sub-004'; 'sub-005'; 'sub-006'; 'sub-007'; 'sub-008'; 'sub-009'; 'sub-010'; 'sub-011'; 'sub-012'; 'sub-013'; 'sub-014'; 'sub-015'; 'sub-016'; 'sub-017'; 'sub-018'; 'sub-019'; 'sub-020'};
Blocks = {'run-01'; 'run-02'; 'run-03'; 'run-04'; 'run-05'; 'run-06'; 'run-07'; 'run-08'; 'run-09'; 'run-10'; 'run-11'; 'run-12'; 'run-13'; 'run-14'; 'run-15'; 'run-16'; 'run-17'; 'run-18'; 'run-19'};
extensionRoot = '.set';
extensionFinal = '_chanClean.set';
[ALLEEG EEG CURRENTSET ALLCOM] = eeglab;
for iParticipant = 1:length(Participants)
directory = fullfile(pathData, Participants{iParticipant}, 'eeg');
participantStr = strcat('SUB', Participants{iParticipant}(end-2:end));
for iBlock = 1:length(Blocks)
conditionStr = strcat('Run', Blocks{iBlock}(end-1:end));
fileRead = [Participants{iParticipant} '_task-AudioCueWalkingStudy_' Blocks{iBlock} extensionRoot];
fileWrite = [Participants{iParticipant} '_task-AudioCueWalkingStudy_' Blocks{iBlock} extensionFinal];
% To deal with different number of blocks per participant
if exist(fullfile(directory, fileRead), 'file') ~= 0
EEG = pop_loadset('filename', fileRead,'filepath', directory); % Loads an EEG dataset
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 0,'gui','off'); % Edits/saves EEG dataset structure information
% Keep only EEG channels
if sum(strcmpi('EMG',{EEG.chanlocs.type})) ~= 0 || sum(strcmpi('AUX',{EEG.chanlocs.type})) ~= 0
chanEEG = strcmpi('EEG',{EEG.chanlocs.type});
nChan = sum(chanEEG);
EEG = pop_select(EEG, 'nochannel', [nChan+1 : length(EEG.chanlocs)]);
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 1,'overwrite','on','gui','off');
EEG = eeg_checkset(EEG);
end
% Use same EEG electrode coordinate system for all blocks
if strcmpi(ALLEEG.chaninfo.filename, '/data/projects/ramon/johanna_eeg2bids/data_mfiles/channel_labels_plusEMG.locs')
ALLEEG.chanlocs = chanLocs;
EEG.chanlocs = chanLocs;
end
% Identify bad channels
iChan = 1;
chanReject.(participantStr).(conditionStr)(iChan) = NaN;
while(1)
chanRemove = input('Should some channels be removed from the data ?, Y/N [Y]:', 's');
if chanRemove == 'Y'
chanReject.(participantStr).(conditionStr)(iChan) = input('Which channel should be removed from the data ?');
iChan = iChan + 1;
elseif chanRemove == 'N'
break
end
end
% Remove/Interpolate identified bad channels
if ~isnan(chanReject.(participantStr).(conditionStr)(1))
chanReject.(participantStr).(conditionStr) = sort(chanReject.(participantStr).(conditionStr));
EEG = pop_interp(EEG, chanReject.(participantStr).(conditionStr)(1:iChan-1), 'spherical'); %Comment this line if you want to remove bad channels without interpolating
% EEG = pop_select( EEG, 'nochannel', chanReject.(participantStr).(conditionStr)(1:iChan-1)); %Comment this line if you want to interpolate bad channels
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 1,'overwrite','on','gui','off');
EEG = eeg_checkset(EEG);
% Rereference
nChan = length(EEG.chanlocs);
EEG = pop_reref(EEG, [],'exclude',[109-(length(chanReject.(participantStr).(conditionStr))-1):nChan]);
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 1,'overwrite','on','gui','off');
end
% Save new .set file
EEG = pop_saveset(EEG, 'filename', fileWrite, 'filepath', [directory '/']); %pop_saveset saves one or more EEG dataset structures
ALLEEG = [];
save('/Volumes/Seagate/project_rhythmicBrain/Results/subAll/chanReject', 'chanReject');
end
end
end