0001 function EEG = physio_filt_scanner_artifact(EEG,defs)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 try TR = defs.TR; catch error('no TR defined in defs\n'); end
0025 try channel = defs.physio_filt_scanner_artifact.channel; catch
0026 error('no target channel defined in defs.physio...artifact.channel\n');
0027 end
0028 if iscell(channel), channel = channel{1}; end
0029
0030 eeglab('initpaths');
0031
0032 cidx = strmatch(channel,{EEG.chanlocs(:).labels});
0033 if isempty(cidx)
0034 error('target channel %s can not be found within EEG.chanlocs\n',...
0035 channel);
0036 elseif length(cidx) > 1
0037 warning('many channels match target channel %s, using first one\n',...
0038 channel);
0039 cidx = cidx(1);
0040 end
0041
0042
0043 fprintf(1,'creating TR event channel\n');
0044 tr_idxs = 1:TR*EEG.srate:EEG.pnts;
0045 ntr = length(tr_idxs);
0046 tr_events = [ones(ntr,1) (tr_idxs/EEG.srate)'];
0047 EEGe = pop_importevent(EEG,'append','no','event',tr_events,...
0048 'fields',{'type','latency'},'timeunit',1);
0049
0050
0051 fprintf(1,'extracting epochs\n');
0052 EEGe = pop_epoch(EEGe,{},[0 TR],'epochinfo','yes');
0053
0054
0055 fprintf(1,'calculating mean TR waveform for %s channel, ',channel);
0056 W = [];
0057 for it=1:EEGe.trials
0058 W = [W; EEGe.data(cidx,:,it)];
0059 end
0060 mW = mean(W);
0061
0062
0063 dM = zeros(EEG.pnts,EEGe.trials);
0064 fprintf(1,'dM, ');
0065 for it=1:EEGe.trials
0066 start = (EEGe.pnts*(it-1)+1);
0067 stop = EEGe.pnts*it;
0068 dM(start:stop,it) = mW';
0069 end
0070
0071
0072 if stop < size(dM,1)
0073 if (size(dM,1) - (stop - 1)) > length(mW)
0074
0075 error('there are less trials than there should be\n');
0076 else
0077
0078 start = stop + 1;
0079 stop = size(dM,1);
0080 dM(start:stop,end+1) = mW(1:stop-start+1);
0081 end
0082 end
0083 dM(:,end+1) = 1;
0084
0085 fprintf(1,'regressing, ');
0086 [b,bint,r,rint,stats] = regress(EEG.data(cidx,:)',dM);
0087 EEG.data(cidx,:) = EEG.data(cidx,:) - r';