0001 function [COV,sx0] = vb_observation_noise_specification(noise_parm)
0002
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 const = vb_define_verbose;
0035 VERBOSE_LEVEL_NOTICE = const.VERBOSE_LEVEL_NOTICE;
0036
0037
0038 vb_struct2vars(noise_parm,{'noise_model','megfile_baseline','twin_noise',...
0039 'noise_reg','temporal_filter','trial_average'});
0040
0041 if isempty(temporal_filter), temporal_filter = false; end
0042 if isempty(trial_average), trial_average = false; end
0043
0044 megfile = megfile_baseline;
0045
0046
0047 if iscell(megfile),
0048 Nfile = length(megfile);
0049 else
0050 tmp = megfile;
0051 megfile = {tmp};
0052 Nfile = 1;
0053 end;
0054
0055 switch noise_model
0056 case 1,
0057 vb_disp('Noise model: spherical',VERBOSE_LEVEL_NOTICE);
0058 case 2,
0059 vb_disp('Noise model: diagonal',VERBOSE_LEVEL_NOTICE);
0060 case 3,
0061 vb_disp('Noise model: full covariance',VERBOSE_LEVEL_NOTICE);
0062 end
0063
0064 COV = cell(Nfile,1);
0065 sx0 = 0;
0066 Ndata = 0;
0067
0068 for n = 1:Nfile
0069
0070 vb_disp(['Load MEG data for noise estimate [' megfile{n} ']'], ...
0071 VERBOSE_LEVEL_NOTICE);
0072 bexp = vb_load_meg_data(megfile{n});
0073 MEGinfo = vb_load_meg_info(megfile{n});
0074
0075
0076 if trial_average, bexp = mean(bexp,3); end;
0077
0078
0079 Nch = size(bexp,1);
0080 Ntrial = size(bexp,3);
0081 Cov = zeros(Nch,Nch);
0082
0083
0084 Tnoise = twin_noise(1):twin_noise(2);
0085 Nt = length(Tnoise);
0086
0087
0088 Tstart = vb_index_to_time(Tnoise(1), MEGinfo);
0089 Tend = vb_index_to_time(Tnoise(end), MEGinfo);
0090 vb_disp(['Session ' num2str(n) ': ' num2str(Tstart,'%4.1f') ...
0091 ' - ' num2str(Tend,'%4.1f') '[ms]'],VERBOSE_LEVEL_NOTICE);
0092 b = bexp(:,Tnoise,:);
0093
0094
0095 if temporal_filter
0096 for s = 1:Ntrial
0097 b(:,:,s) = vb_temporal_smooth(b(:,:,s));
0098 end
0099 end
0100
0101 B = reshape(b, [Nch, Nt*Ntrial]);
0102 BB = B*B';
0103
0104 switch noise_model
0105 case 1,
0106 Cov = eye(Nch,Nch);
0107 invCov = eye(Nch,Nch);
0108
0109 case 2,
0110
0111 Cov = diag(diag(BB));
0112
0113 Cov = Cov/(trace(Cov)/Nch);
0114 invCov = pinv(Cov);
0115
0116 case 3,
0117
0118 Cov = BB+(noise_reg*trace(BB)/Nch)*eye(Nch,Nch);
0119
0120 Cov = Cov/(trace(Cov)/Nch);
0121 invCov = pinv(Cov);
0122 end
0123 COV{n} = Cov;
0124
0125 Ndata = Ndata + Nch*Ntrial*Nt;
0126 sx0 = sx0 + sum(sum(invCov.*(BB)));;
0127 end
0128 sx0 = sx0/Ndata;