0001 function [Zact ,Jinfo, bayes_parm, vb_parm, MEGinfo] ...
0002 = vbmeg_feature_calc_z(proj_root, curr_parm);
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 if ~isempty(proj_root)
0053 filterfile = fullfile(proj_root, curr_parm.filterfile);
0054 else
0055 filterfile = curr_parm.filterfile;
0056 end
0057
0058
0059 load(filterfile, 'VBfilt','Jinfo','bayes_parm','vb_parm');
0060
0061
0062
0063
0064 [bayes_parm, ix_area, trial_average, tsubsamp, overlap_mode, verbose] = ...
0065 check_arg(bayes_parm, curr_parm);
0066
0067 if ~isempty(proj_root);
0068 bayes_parm_abs = vb_parm_absolute_path(proj_root, bayes_parm);
0069 else
0070 bayes_parm_abs = bayes_parm;
0071 end
0072
0073
0074
0075 [B, Ntrial, Nch, Tsample, Twindow] = ...
0076 vb_megdata_preparation(bayes_parm_abs);
0077
0078 MEGinfo = vb_load_measurement_info(bayes_parm_abs.megfile{1});
0079
0080
0081 Fs = MEGinfo.SampleFrequency;
0082
0083 if ~isfield(curr_parm,'freq_range'),
0084 freq_range = [5 9; 11 16; 21 26]
0085 else
0086 freq_range = curr_parm.freq_range;
0087 end;
0088 if ~isfield(curr_parm,'fft_window'),
0089 fft_window = 0;
0090 else
0091 fft_window = curr_parm.fft_window;
0092 end
0093
0094
0095 Nsession = length(B);
0096 fprintf('Number of session: %d\n', Nsession);
0097
0098
0099 Nwindow = size(Twindow,1);
0100 Tindex = cell(Nwindow,1);
0101
0102 for j=1:Nwindow
0103 Tindex{j} = Twindow(j,1):Twindow(j,2);
0104 end
0105
0106
0107 NFFT = 0;
0108 for j=1:Nwindow
0109 Nt = length(Tindex{j});
0110 nfft = 2^nextpow2(Nt);
0111 if NFFT < nfft, NFFT=nfft; end;
0112 end
0113
0114
0115 df=Fs/NFFT;
0116 freq=0:df:df*(NFFT/2);
0117
0118 if max(freq_range(:)) > Fs/2,
0119 error('max freq is larger than Nyquist freq')
0120 end
0121
0122
0123 [Nband ,D] = size(freq_range);
0124 if D ~=2, error('freq_range should be [Nband x 2]'); end;
0125
0126
0127 flist = zeros(Nband ,2);
0128 for n=1:Nband*2
0129 [tmp, ix] = min(abs(freq - freq_range(n)));
0130 flist(n) = ix;
0131 end
0132
0133 fxlist = [];
0134 ixlist = [];
0135 nxlist = [];
0136 Nlast = 0;
0137
0138
0139 for n=1:Nband
0140 Nlist = flist(n,2) - flist(n,1) + 1;
0141 Nstart = Nlast + 1;
0142 Nlast = Nlast + Nlist;
0143
0144 fxlist = [fxlist, flist(n,1):flist(n,2)];
0145 ixlist = [ixlist, Nstart:Nlast];
0146 nxlist = [nxlist, repmat(n, [1, Nlist])];
0147
0148
0149 end
0150
0151
0152 Nall = length(ixlist);
0153 Wfreq = sparse( ixlist , nxlist , 1 , Nall, Nband) ;
0154
0155
0156
0157
0158
0159 [Njact,Nch,Nw,Ntask] = size(VBfilt.KW);
0160
0161 if Nw==1 && Ntask==1,
0162 KW = VBfilt.KW(:,:,1,1) ;
0163 else
0164 fprintf('(Nwindow=%d ,Ntask=%d)\n',Nw,Ntask)
0165 fprintf('Take average of VB filters\n')
0166 KW = squeeze(mean( mean( VBfilt.KW,3) ,4));
0167 end
0168
0169 fprintf('Active vertex number = %d\n',Njact)
0170 fprintf('Active sensor number = %d\n',Nch)
0171 fprintf('Number of time window= %d\n',Nwindow)
0172 fprintf('Number of bandpass components= %d\n',Nband)
0173 fprintf('Number of FFT points= %d\n',NFFT)
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 VBfreq.KW = KW;
0192 VBfreq.fxlist = fxlist;
0193 VBfreq.Wfreq = Wfreq;
0194 VBfreq.NFFT = NFFT;
0195 VBfreq.Twindow = Twindow;
0196 VBfreq.freq_range = freq_range;
0197
0198 Jinfo.VBfreq = VBfreq;
0199
0200 Jinfo.Nwindow = Nwindow;
0201 Jinfo.SampleFreq = Fs;
0202
0203
0204 Tstart = bayes_parm.twin_meg(1);
0205 Tend = bayes_parm.twin_meg(2);
0206 if isempty(tsubsamp)
0207 Jinfo.Tsample = Tstart:Tend;
0208 else
0209 Jinfo.Tsample = tsubsamp + Tstart - 1;
0210 end
0211
0212
0213
0214 err = 0;
0215 nall = 0;
0216 Zact = zeros( Njact, Nwindow*Nband, sum(Ntrial));
0217
0218 for n=1:Nsession
0219 Ntry = Ntrial(n);
0220
0221 if Ntry == 0, continue; end;
0222
0223 Bs = B{n};
0224
0225 for m=1:Ntry
0226 nall = nall + 1;
0227 for j=1:Nwindow
0228
0229 Tid = Tindex{j};
0230 Nt = length(Tid);
0231
0232 if Nt == 0, continue; end;
0233
0234
0235 Bt = Bs(:,Tid,m);
0236
0237 if fft_window==1,
0238 Bt = vb_repmultiply(Bt, hanning(Nt)');
0239 end
0240
0241 Bf = fft( Bt ,NFFT, 2);
0242
0243
0244 Zf = abs( KW * Bf(:,fxlist) ).^2 ;
0245
0246
0247 Zact(:, (1:Nband) + (j-1)*Nband, nall) = Zf * Wfreq;
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258 end
0259 end;
0260 end
0261
0262 if trial_average == ON
0263 Zact = mean(Zact,3);
0264 end
0265
0266 Jinfo.Ntrial = Ntrial;
0267 Jinfo.Nsession = Nsession;
0268
0269 if verbose==1,
0270 fprintf('\n')
0271 end
0272
0273
0274
0275
0276
0277
0278
0279
0280 return
0281
0282
0283 function [bayes_parm,ix_area,trial_average,tsubsamp,overlap_mode,verbose]= ...
0284 check_arg(bayes_parm,curr_parm)
0285
0286 if isfield(curr_parm,'basisfile'),
0287 bayes_parm.basisfile = curr_parm.basisfile;
0288 end;
0289 if isfield(curr_parm,'megfile'),
0290 bayes_parm.megfile = curr_parm.megfile ;
0291 end;
0292 if isfield(curr_parm,'twin_meg'),
0293 bayes_parm.twin_meg = curr_parm.twin_meg ;
0294 end;
0295 if isfield(curr_parm,'Tperiod'),
0296 bayes_parm.Tperiod = curr_parm.Tperiod ;
0297 end;
0298 if isfield(curr_parm,'Tnext'),
0299 bayes_parm.Tnext = curr_parm.Tnext ;
0300 end;
0301
0302 if ~isfield(curr_parm,'trial_average'),
0303 trial_average = ON;
0304 else
0305 trial_average = curr_parm.trial_average;
0306 end;
0307
0308 bayes_parm.trial_average = trial_average;
0309
0310 if ~isfield(curr_parm,'ix_area'),
0311 ix_area = [];
0312 else
0313 ix_area = curr_parm.ix_area;
0314 end;
0315 if ~isfield(curr_parm,'tsubsamp'),
0316 tsubsamp = [];
0317 else
0318 tsubsamp = curr_parm.tsubsamp;
0319 end;
0320 if ~isfield(curr_parm,'overlap_mode'),
0321 overlap_mode = 0;
0322 else
0323 overlap_mode = curr_parm.overlap_mode;
0324 end;
0325 if ~isfield(curr_parm,'verbose'),
0326 verbose = 1;
0327 else
0328 verbose = curr_parm.verbose;
0329 end;