0001 function tfrGND=sets2TFR(set_fnames,time_window_length,time_step,use_bins,foi)
0002
0003
0004 n_subs=length(set_fnames);
0005 T=time_window_length;
0006
0007
0008
0009
0010
0011
0012 for s=1:n_subs,
0013 if s==1,
0014 global EEG;
0015
0016 EEG=pop_loadset(set_fnames{1});
0017 n_bins=length(EEG.bindesc);
0018 if isempty(use_bins),
0019 use_bins=1:n_bins;
0020 n_use_bins=n_bins;
0021 elseif max(use_bins)>n_bins,
0022 error('This file only has %d bins, but you requested importing bin %d.\n', ...
0023 n_bins,max(use_bins));
0024 else
0025 n_use_bins=length(use_bins);
0026 end
0027
0028 [n_chans, n_tpts, n_trials]=size(EEG.data);
0029 srate=EEG.srate;
0030 orig_times=EEG.times;
0031 orig_chanlocs=EEG.chanlocs;
0032 orig_bindesc=EEG.bindesc;
0033
0034
0035 tfrGND.bindesc=cell(1,n_use_bins);
0036 for b=1:n_use_bins,
0037 tfrGND.bindesc{b}=EEG.bindesc{use_bins(b)};
0038 end
0039 tfrGND.set_fnames=set_fnames;
0040
0041 subXbin.ftrip=cell(n_subs,n_use_bins);
0042
0043 remove_artifact_ics();
0044
0045
0046 dataPP=eeglab2fieldtrip(EEG,'preprocessing','none');
0047
0048 cfg_pre=[];
0049
0050
0051 cfg_pre.channel={'all','-A2'};
0052 cfg_pre.demean='yes';
0053 cfg_pre.baselinewindow ='all';
0054 cfg_pre.trials = 'all';
0055
0056 tpt0_id=find_tpt(EEG.times,0);
0057
0058
0059
0060
0061 for a=1:n_trials,
0062 dataPP.cfg.trl(a,:)=[1 n_tpts tpt0_id a];
0063
0064
0065 dataPP.time{a}=round(dataPP.time{a}*1000)/1000;
0066 end
0067
0068 dataPP=ft_preprocessing(cfg_pre,dataPP);
0069 else
0070 EEG=pop_loadset(set_fnames{s});
0071 n_bins2=length(EEG.bindesc);
0072 [n_chans2, n_tpts2, n_trials2]=size(EEG.data);
0073
0074
0075 EEG.times=round(EEG.times);
0076
0077
0078
0079
0080
0081 if EEG.srate~=srate,
0082 error('Sampling rate of file %s does not equal that of %s.\n',set_fnames{s},set_fnames{1});
0083 end
0084 if n_bins2~=n_bins,
0085 error('File %s has a different number of bins than %s.\n',set_fnames{s},set_fnames{1});
0086 end
0087 if ~isequal(EEG.bindesc,orig_bindesc)
0088 error('File %s has a different bin descriptors than %s.\n',set_fnames{s},set_fnames{1});
0089 end
0090 if n_chans2~=n_chans,
0091 error('File %s has a different number of channels (i.e., electrodes) than %s.\n',set_fnames{s},set_fnames{1});
0092 end
0093 if ~isequal(EEG.chanlocs,orig_chanlocs),
0094 error('File %s has different channel location information than %s.\n',set_fnames{s},set_fnames{1});
0095 end
0096 if n_tpts2~=n_tpts,
0097 error('File %s has a different number of time points than %s.\n',set_fnames{s},set_fnames{1});
0098 end
0099 if ~isequal(EEG.times,orig_times),
0100 error('File %s has a different time point values than %s.\n',set_fnames{s},set_fnames{1});
0101 end
0102
0103 remove_artifact_ics();
0104
0105 dataPP=eeglab2fieldtrip(EEG,'preprocessing','none');
0106
0107 tpt0_id=find_tpt(EEG.times,0);
0108
0109
0110
0111 for a=1:n_trials2,
0112 dataPP.cfg.trl(a,:)=[1 n_tpts tpt0_id a];
0113
0114
0115 dataPP.time{a}=round(dataPP.time{a}*1000)/1000;
0116 end
0117
0118 dataPP=ft_preprocessing(cfg_pre,dataPP);
0119 end
0120
0121
0122 in_bin=bin_membership(EEG);
0123
0124
0125
0126 cfg = [];
0127 cfg.output = 'pow';
0128 cfg.channel = 'all';
0129 cfg.method = 'mtmconvol';
0130
0131
0132
0133 cfg.taper = 'hanning';
0134
0135
0136
0137 freq_step=1;
0138 if isempty(foi),
0139 cfg.foi = 0:freq_step:srate/2;
0140 else
0141 cfg.foi = foi(1):freq_step:foi(end);
0142 end
0143
0144 cfg.pad = ceil((n_tpts/srate)*freq_step)/freq_step;
0145
0146
0147
0148
0149 cfg.t_ftimwin = ones(length(cfg.foi),1).*T;
0150
0151
0152 start_time=dataPP.time{1}(1)+T/2;
0153 start_time=ceil(start_time*srate)/srate;
0154
0155
0156 end_time=dataPP.time{1}(end)-T/2;
0157 end_time=floor(end_time*srate)/srate;
0158 cfg.toi = start_time:time_step:end_time;
0159
0160
0161 for b=1:n_use_bins,
0162 cfg.trials=find(in_bin(:,use_bins(b)));
0163 subXbin.ftrip{s,b}=ft_freqanalysis(cfg, dataPP);
0164 end
0165
0166 end
0167
0168
0169 for bin=1:n_use_bins,
0170 cfg=[];
0171 cfg.keepindividual = 'yes';
0172 cmnd=['tfrGND.ftrip{bin}=ft_freqgrandaverage(cfg'];
0173 for s=1:n_subs,
0174 cmnd=[cmnd ',subXbin.ftrip{' int2str(s) ',' int2str(bin) '}'];
0175 end
0176 cmnd=[cmnd ');'];
0177 warning('off','all');
0178 eval(cmnd);
0179 warning('on','all');
0180 tfrGND.ftrip{bin}.elec=dataPP.elec;
0181 end
0182
0183
0184
0185 for bin=1:n_use_bins,
0186 tfrGND.gnd_powspctrm{bin}=squeeze(mean(tfrGND.ftrip{bin}.powspctrm));
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196 end
0197 tfrGND.gnd_dimord='chan_freq_time';
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 function in_bin=bin_membership(EEG)
0219
0220
0221 n_bin=length(EEG.bindesc);
0222 n_epoch=length(EEG.epoch);
0223
0224 in_bin=zeros(n_epoch,n_bin);
0225
0226 for a=1:n_bin,
0227 bin_name=['bin' int2str(a)];
0228 for ep=1:n_epoch,
0229 if ismember(bin_name,EEG.epoch(ep).eventtype)
0230 in_bin(ep,a)=1;
0231 end
0232 end
0233 end
0234
0235