Commit 2a7a249b authored by Andero Uusberg's avatar Andero Uusberg

commented the R file

parent c41ee8b5
......@@ -334,6 +334,85 @@ while do.build == 1;
do.build = 0;
end
%% BUILD ANALYSIS STRUCTURE %%
while do.buildERSP == 1;
epochlist = dir([epochdir,'*.set']);
% RESEARCHER INPUT
d.photoType = {'emo' 'ntr'};
d.phase = {'indiv' 'social'};
d.condition = {'low' 'high' 'same' 'none'};
minEpoch = 8; % minimum number of epochs that needs to be retained per design ceafter artifact rejection before the subject is excluded
ersp = struct('times', {[]}, 'chanlocs', readlocs([scriptDir,'locfile.ced']), 'data', {{}}, 'subs', {{}}); % p is a custom structure we'll build to be used later as input to visualizaton functions
ersp.frqs = {'theta' [3 7]; 'alpha' [8 12]; 'beta' [15 25]};
ersp.params = {'cycles',[1 4],'freqs', [3 30], 'baseline',[-1250 -250],'trialbase','full','basenorm','off','freqscale','linear','scale','abs','timesout',length(epochsegment{2}(1):0.0039063:epochsegment{2}(2))/8,'padratio',1,'plotphase','off','plotersp','on','plotitc','on','verbose','off'};
% INITIALIZE VARIABLES
s = 0; bkg = {}; % s counts subjects; bkg is the meta-data matrix
for fld = fields(d)'; p.labels.(fld{:}) = d.(fld{:}); end % take factor and level labels from the d structure and add to the p
for i = 1:length(epochlist); % loop through files
EEG = []; ALLEEG = []; data = []; n = []; % initiate/clear various variables
EEG = pop_loadset('filename', epochlist(i).name, 'filepath', epochdir); % load file
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET);
s = s + 1; % advance the subjects counter
bkg = getBkg(EEG, d, bkg, minEpoch, readlocs([scriptDir,'locfile.ced'])); % call a function to build meta-data matrix
if bkg{end,end} == 0 % if any of the design cells has less than required trials...
s = s - 1; % ... drop the subject from the p structure
continue
end
epoch = epochList(EEG); % get information about each epoch in correct format
p.epochFields = fields(epoch)';
EEG = pop_interp(EEG, readlocs([scriptDir,'locfile.ced']), 'spherical'); % interpolate missing channels - if you threw out a channel the system will try to fill in the data
[ALLEEG EEG CURRENTSET] = eeg_store(ALLEEG, EEG, CURRENTSET);
% COMPUTE ERSP
for c = 1:length(EEG.chanlocs)-4
figure; [a,a,a,times,freqs,a,a,tfdata] = newtimef(EEG.data(c,:,:), EEG.pnts, [EEG.xmin EEG.xmax]*1000, EEG.srate, ersp.params{:});
power = tfdata.*conj(tfdata); % convert to power values
power = power./repmat(mean(power,2),[1 length(times) 1 1]); % full-trial normalization; gain model divide by each trial mean value (based on Grandchamp & Delorme, 2011)
baseline = ersp.params{strmatch('baseline', ersp.params(1:2:end))*2};
[a lti] = min(abs(times-baseline(1))); [a uti] = min(abs(times-baseline(2)));
power = 10*log10(power./repmat(mean(mean(power(:,lti:uti,:,:),2),3),[1 length(times) size(power,3) 1]));% actual baseline correction on mean level + transform to log
% power = power-repmat(mean(power(lfi:ufi,lbi:ubi,:),2),[1 length(times) 1]); % actual baseline correction on trial
close(gcf);
% AVERAGE ERSP FOR EACH DESIGN CELL
for photoType = 1:2 % loop through levels of photoType factor
t.a = strcmp(d.photoType{photoType},{epoch.photoType}); % find all trials for a given level (all level labels were converted to strings by epochList, so we match using strcmp)
for phase = 1:2 % loop through 2. factor levels
t.b = strcmp(d.phase{phase},{epoch.phase});
for condition = 1:4
t.c = strcmp(d.condition{condition},{epoch.condition});
inds = find(t.a.*t.b.*t.c); % find trials belonging to one design cell...
for f = ersp.frqs(:,1)' % keskmistab kohe sagedused kokku
fval = ersp.frqs{strmatch(f{:},ersp.frqs(:,1),'exact'),2};
if fval(1) < freqs(1); fval(1) = freqs(1); end; if fval(2) > freqs(end); fval(2) = freqs(end); end;
[a lfi] = min(abs(freqs-fval(1))); [a ufi] = min(abs(freqs-fval(2)));
ersp.(f{:}).times = times;
ersp.(f{:}).subs{s} = EEG.subject; % record subject code to p structure
end
ersp.(f{:}).data{photoType,phase,condition}(c,:,s) = squeeze(mean(mean(power(lfi:ufi,:,inds),1),3)); % average trials in the selected cell
end
end
end
end
end
save([epochdir date '-ERSP.mat'], 'p'); % save the p structure to disk
do.buildERSP = 0;
end
%% VISUALIZE AND EXPORT %%
while do.analyze == 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment