forked from fpellegrini/MotorImag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfp_preprocess.m
125 lines (94 loc) · 4.17 KB
/
fp_preprocess.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
cd ~/Dropbox/Franziska/Meditation/matlab/eeglab-develop/
eeglab
addpath('~/Dropbox/Franziska/Meditation/matlab/eeglab-develop/plugins/dipfit/standard_BEM/elec/');
addpath('~/Dropbox/Franziska/Meditation/matlab/eeglab-develop/plugins/roiconnect/libs/brainstorm/');
addpath('~/Dropbox/Franziska/Meditation/matlab/eeglab-develop/plugins/roiconnect/libs/export_fig/');
DIRIN = '/Users/franziskapellegrini/Dropbox/VitalBCI/mat/imag/';
DIROUT = '~/Dropbox/Franziska/MotorImag/Data_MI/';
if ~exist(DIROUT); mkdir(DIROUT); end
DIRFIG = '~/Dropbox/Franziska/MotorImag/Figures/Sensordata_MI/';
if ~exist(DIRFIG); mkdir(DIRFIG); end
%classification performance scores
load('/Users/franziskapellegrini/Dropbox/VitalBCI/mat/scores.mat')
dsRatio = 10;
nsub = 40 ;
DB_noise = db_motorImag_noisechans;
%%
for isub = 2:nsub %subject 1 not available
sub = ['vp' num2str(isub)];
if strcmp(scores{isub}(1),'1') %if subject has a good classification performance
load([DIRIN sub '.mat'])
%convert to microV
eegdata = 0.1 * double(cnt.x)';
%filter before resampling
fs = 1000;
notchb = [48 52];
[bband, aband] = butter(2, notchb/fs*2,'stop');
[bhigh, ahigh] = butter(2, 1/fs*2, 'high');
[blow, alow] = butter(2, 45/fs*2, 'low');
eegdata = filtfilt(bhigh, ahigh, eegdata');
eegdata = filtfilt(bband, aband, eegdata);
eegdata = filtfilt(blow, alow, eegdata)';
%resample
eegdata = eegdata(:,1:dsRatio:end);
fs = 100;
%also enter marker positions
if ~(strcmp(mrk.className{1},'left')) || ~(strcmp(mrk.className{2},'right'))
error(['check classNames in subject ' subs{isub}])
end
eegdata(end+1,:) = zeros(1,size(eegdata,2));
eegdata(end,mrk.pos) = mrk.toe;
%load data into EEG struct
EEG.etc.eeglabvers = '2021.1'; % this tracks which version of EEGLAB is being used, you may ignore it
EEG = pop_importdata('dataformat','array','nbchan',0,'data','eegdata','setname','test','srate',100,'pnts',0,'xmin',0);
EEG = eeg_checkset( EEG );
%add events
EEG = pop_chanevent( EEG, EEG.nbchan,'edge','leading');
%% add chan info
%load template
eloc = readlocs('standard_1005.elc' );
%remove all channels of eloc that are not contained in data
out = [];
out_eloc=[];
in = [];
for ii = 1:length(eloc)
if ~ismember(eloc(ii).labels,cnt.clab)
out = [out,ii];
else
in = [in find(strcmp(eloc(ii).labels,cnt.clab))];
end
end
eloc(out)=[];
%remove channels in data that have not been found in eloc template
out_labs = setdiff(1:EEG.nbchan,in);
EEG = pop_select(EEG, 'nochannel',out_labs);
%bring channels in right order
eloc1 = eloc;
for ii = 1:length(eloc)
name_eloc = eloc(ii).labels;
ind_clab = find(strcmp(eloc(ii).labels,cnt.clab));
eloc1(ind_clab) = eloc(ii);
end
%add chanlocs to EEG structure
EEG = pop_editset(EEG,'chanlocs',eloc1,'setname','setName');
EEG = eeg_checkset(EEG);
%% Preprocessing
eeglabp = fileparts(which('eeglab.m'));
EEG = pop_chanedit(EEG, 'lookup',fullfile(eeglabp, 'plugins','dipfit','standard_BEM','elec','standard_1005.elc'));
chanlocs = EEG(1).chanlocs;
%manual rejection of bad channels
clear chans
chans = fp_matchdbs_motorImag(DB_noise,sub);
if ~isempty(chans)
EEG = pop_select(EEG,'nochannel',chans);
EEG = pop_interp(EEG, chanlocs);
end
%% plot on channel level
pop_spectopo(EEG, 1, [],'EEG' , 'percent', 100, 'freq', 10, 'freqrange',[0 100],'electrodes','on');
outname = [DIRFIG sub '.png'];
print(outname,'-dpng');
close all
%% save preprocessed EEG struct
pop_saveset(EEG,'filename',['prep_' sub],'filepath',DIROUT)
end
end