Home > vbmeg > functions > device > vb_msrmnt_make_trial_data.m

vb_msrmnt_make_trial_data

PURPOSE ^

Make trial data files by chopping continuous MEG or EEG data

SYNOPSIS ^

function [data, ch_info] = vb_msrmnt_make_trial_data(meeg_file, proc_spec, new_meeg_file)

DESCRIPTION ^

 Make trial data files by chopping continuous MEG or EEG data
 [usage]
   [data, ch_info] = vb_msrmnt_make_trial_data(meeg_file, proc_spec, new_meeg_file)
 [input]
    meeg_file : <required> <<file>>
                Continuous format MEG data file(.meg.mat) or
                Continuous format EEG data file(.eeg.mat)
    proc_spec : <required> <<struct>>
              :  struct defined process specifications which has fields 
              :  as follows:
              :
      .trig_file : <conditionally required> <<file>>
                 :  trigger file(.trig.mat)
                 :  this file is expected to have data as follows:
                 :   .trig [1 x Ntrial]
                 :   .parm.data_file (without path) : base file
                 :   .parm.Pretrigger_ms  : pretrigger [msec]
                 :   .parm.Posttrigger_ms : posttrigger [msec]
                 :   .parm.fsamp : sampling frequency [Hz]
                 :  if this is invalid and .proc_parm field is not
                 :  specified, it is error.
      .internal  : <optional>
                   = true  : single file format(MEG/EEG). [default]
                   = false : multiple file format(MAT+binary dir)(only MEG).
      .proc_parm : <optional> <<struct>>
                 :  .Pretrigger_ms  : Pretrigger period   [msec]
                 :  .Posttrigger_ms : Posttrigger period  [msec]
                 :  .sample_freq    : sampling frequency [Hz]
                 :  .trigger        : list of trigger samples
                 :                  : [1 x Ntrial]
                 :    if these are specified, 
                 :    they overwrite the setting values of trig_file
   new_meeg_file : <required> <<file>>
                   Trial format MEG data file(.meg.mat) or
                   Trial format EEG data file(.eeg.mat)
 [output]
           : these are the same as the return values of vb_load_meg_data
      data : chopped data [Nchannel x Nsample x Ntrial]
   ch_info : channel information struct
           :  .Active
           :  .Name
           :  .ID
           :  .Type
 [note]

 [history]
   2009-07-24 (Sako) initial version
   2009-09-01 (Sako) substituted vb_load_measurement_info for vb_load_meg_info
   2016-11-28 (rhayashi) Change specification(meeg_file, new_meeg_file)
   2018-08-22 (Takeda) Modified so that cond_id is inherited 

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [data, ch_info] = vb_msrmnt_make_trial_data(meeg_file, proc_spec, new_meeg_file)
0002 % Make trial data files by chopping continuous MEG or EEG data
0003 % [usage]
0004 %   [data, ch_info] = vb_msrmnt_make_trial_data(meeg_file, proc_spec, new_meeg_file)
0005 % [input]
0006 %    meeg_file : <required> <<file>>
0007 %                Continuous format MEG data file(.meg.mat) or
0008 %                Continuous format EEG data file(.eeg.mat)
0009 %    proc_spec : <required> <<struct>>
0010 %              :  struct defined process specifications which has fields
0011 %              :  as follows:
0012 %              :
0013 %      .trig_file : <conditionally required> <<file>>
0014 %                 :  trigger file(.trig.mat)
0015 %                 :  this file is expected to have data as follows:
0016 %                 :   .trig [1 x Ntrial]
0017 %                 :   .parm.data_file (without path) : base file
0018 %                 :   .parm.Pretrigger_ms  : pretrigger [msec]
0019 %                 :   .parm.Posttrigger_ms : posttrigger [msec]
0020 %                 :   .parm.fsamp : sampling frequency [Hz]
0021 %                 :  if this is invalid and .proc_parm field is not
0022 %                 :  specified, it is error.
0023 %      .internal  : <optional>
0024 %                   = true  : single file format(MEG/EEG). [default]
0025 %                   = false : multiple file format(MAT+binary dir)(only MEG).
0026 %      .proc_parm : <optional> <<struct>>
0027 %                 :  .Pretrigger_ms  : Pretrigger period   [msec]
0028 %                 :  .Posttrigger_ms : Posttrigger period  [msec]
0029 %                 :  .sample_freq    : sampling frequency [Hz]
0030 %                 :  .trigger        : list of trigger samples
0031 %                 :                  : [1 x Ntrial]
0032 %                 :    if these are specified,
0033 %                 :    they overwrite the setting values of trig_file
0034 %   new_meeg_file : <required> <<file>>
0035 %                   Trial format MEG data file(.meg.mat) or
0036 %                   Trial format EEG data file(.eeg.mat)
0037 % [output]
0038 %           : these are the same as the return values of vb_load_meg_data
0039 %      data : chopped data [Nchannel x Nsample x Ntrial]
0040 %   ch_info : channel information struct
0041 %           :  .Active
0042 %           :  .Name
0043 %           :  .ID
0044 %           :  .Type
0045 % [note]
0046 %
0047 % [history]
0048 %   2009-07-24 (Sako) initial version
0049 %   2009-09-01 (Sako) substituted vb_load_measurement_info for vb_load_meg_info
0050 %   2016-11-28 (rhayashi) Change specification(meeg_file, new_meeg_file)
0051 %   2018-08-22 (Takeda) Modified so that cond_id is inherited
0052 %
0053 % Copyright (C) 2011, ATR All Rights Reserved.
0054 % License : New BSD License(see VBMEG_LICENSE.txt)
0055 
0056 % --- CHECK ARGUMENTS --- %
0057 if ~exist('proc_spec', 'var'), proc_spec = []; end
0058 if ~exist('meeg_file', 'var'), meeg_file = ''; end
0059 if ~exist('new_meeg_file', 'var'), new_meeg_file = ''; end
0060 
0061 [meeg_file, proc_spec, new_meeg_file] = ...
0062     inner_check_arguments(meeg_file, proc_spec, new_meeg_file);
0063 
0064 % --- MAIN PROCEDURE --------------------------------------------------------- %
0065 %
0066 
0067 % default values
0068 [base_file_def, trigger_def, cond_id_def, pretrigger_def, posttrigger_def, ...
0069   sample_freq_def] = inner_read_trigger_file(proc_spec.trig_file);
0070 
0071 % substitute values
0072 [base_file_sub, trigger_sub, cond_id_sub, pretrigger_sub, posttrigger_sub, ...
0073   sample_freq_sub] = inner_read_proc_parm(meeg_file, proc_spec.proc_parm);
0074 
0075 if ~isempty(trigger_sub)
0076   trigger = trigger_sub;
0077 else
0078   trigger = trigger_def;
0079 end
0080 
0081 if ~isempty(cond_id_sub)
0082   cond_id = cond_id_sub;
0083 else
0084   cond_id = cond_id_def;
0085 end
0086 
0087 if ~isempty(pretrigger_sub)
0088   pretrigger_ms = pretrigger_sub;
0089 else
0090   pretrigger_ms = pretrigger_def;
0091 end
0092 
0093 if ~isempty(posttrigger_sub)
0094   posttrigger_ms = posttrigger_sub;
0095 else
0096   posttrigger_ms = posttrigger_def;
0097 end
0098 
0099 % sample frequency for triger file
0100 if ~isempty(sample_freq_sub)
0101   sample_freq = sample_freq_sub;
0102 else
0103   sample_freq = sample_freq_def;
0104 end
0105 
0106 
0107 info = vb_load_measurement_info(meeg_file);
0108 Measurement = vb_info_get_measurement(info);
0109 
0110 % --- MEG
0111 if strcmp(Measurement, 'MEG')
0112     [f_, p_, e_] = vb_get_file_parts(new_meeg_file);
0113     new_spec.trigger = trigger;
0114     new_spec.cond_id = cond_id;
0115     new_spec.pretrigger_ms = pretrigger_ms;
0116     new_spec.posttrigger_ms = posttrigger_ms;
0117     new_spec.sample_freq = sample_freq;
0118     new_spec.root_dir = f_;
0119     new_spec.new_file = [p_, e_];
0120     new_spec.new_bin_dir = proc_spec.new_bin_dir;
0121     [data, ch_info] = vb_megfile_make_trial_data(meeg_file, new_spec);
0122 else
0123     % EEG case
0124     
0125     % --- make load_spec
0126 
0127     % sample frequency for base_file
0128     info = vb_load_measurement_info(meeg_file);
0129     fs   = vb_info_get_sampling_frequency(info);
0130 
0131     % Convert trigger time index for fs [Hz]
0132     load_spec.Trigger = round(trigger * (fs/sample_freq));
0133 
0134     % convert trigger data from msec to sample
0135     load_spec.Pretrigger  = ceil(pretrigger_ms  *(fs/1000));
0136     load_spec.Posttrigger = ceil(posttrigger_ms *(fs/1000));
0137 
0138     load_spec.ChannelType = 'ALL';
0139     load_spec.ActiveChannel = false;
0140 
0141     [data, ch_info] = vb_load_meg_data(meeg_file, load_spec, new_meeg_file);
0142     
0143     % Add cond_id to EEGinfo
0144     load(new_meeg_file, 'EEGinfo');
0145     EEGinfo.cond_id = cond_id;
0146     vb_save(new_meeg_file, 'EEGinfo');
0147 end
0148 %
0149 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0150 
0151 % --- INNER FUNCTIONS -------------------------------------------------------- %
0152 %
0153 % --- inner_check_arguments()
0154 %
0155 function [meeg_file, proc_spec, new_meeg_file] = ...
0156     inner_check_arguments(meeg_file, proc_spec, new_meeg_file)
0157 func_ = mfilename;
0158 
0159 if isempty(meeg_file)
0160   error('(%s) meeg_file is a required parameter', func_);
0161 end
0162 if exist(meeg_file, 'file') ~= 2
0163   error('(%s) Specified meeg_file not found.', func_);
0164 end    
0165 if isempty(strfind(meeg_file, '.meg.mat')) && ...
0166    isempty(strfind(meeg_file, '.eeg.mat'))
0167   error('(%s) meeg_file is not MEG(.meg.mat) or EEG(.eeg.mat) file.', func_);
0168 end
0169 
0170 if isempty(proc_spec)
0171   error('(%s) proc_spec is a required parameter', func_);
0172 end
0173 
0174 if isempty(new_meeg_file)
0175   error('(%s) new_meeg_file is a required parameter', func_);
0176 end
0177 
0178 % --- proc_spec
0179 %  .trig_file
0180 %  .internal
0181 %  .proc_parm
0182 
0183 if ~isfield(proc_spec, 'trig_file')
0184   proc_spec.trig_file = '';
0185 end
0186 
0187 if ~isfield(proc_spec, 'internal')
0188   proc_spec.internal = true;
0189 end
0190 
0191 if proc_spec.internal == false
0192   [p_, f_, e_] = vb_get_file_parts(new_meeg_file);
0193   new_bin_dir = f_(1:end-4); % f_ : -4 means '.eeg' or '.meg'
0194   proc_spec.new_bin_dir = ['./' new_bin_dir];
0195 else
0196   proc_spec.new_bin_dir = '';
0197 end
0198 
0199 if ~isfield(proc_spec, 'proc_parm')
0200   proc_spec.proc_parm = [];
0201 end
0202 
0203 if isempty(proc_spec.trig_file) && isempty(proc_spec.proc_parm)
0204   error('(%s) either trig_file or proc_parm is required', func_);
0205 end
0206 return;
0207 %
0208 % --- end of inner_check_arguments()
0209 
0210 
0211 % --- inner_read_trigger_file()
0212 %
0213 function [base_file, trigger, cond_id, pretrigger, posttrigger, sample_freq] = ...
0214   inner_read_trigger_file(trig_file)
0215 
0216 func_ = mfilename;
0217 
0218 base_file = '';
0219 trigger = [];
0220 cond_id = [];
0221 pretrigger = [];
0222 posttrigger = [];
0223 sample_freq = 1024;
0224 
0225 if isempty(trig_file)
0226   return;
0227 end
0228 
0229 if exist(trig_file, 'file') ~= 2
0230   error('(%s) cannot find trig_file : %s', func_, trig_file);
0231 end
0232 
0233 trig = load(trig_file);
0234 if isfield(trig, 'trig') && ~isempty(trig.trig)
0235   trigger = trig.trig;
0236 end
0237 if isfield(trig, 'cond_id') && ~isempty(trig.cond_id)
0238   cond_id = trig.cond_id;
0239 end
0240 
0241 if isfield(trig, 'parm') && ~isempty(trig.parm)
0242   if isfield(trig.parm, 'data_file') && ~isempty(trig.parm.data_file)
0243     base_file = trig.parm.data_file;
0244   end
0245 
0246   if isfield(trig.parm, 'Pretrigger_ms')
0247     pretrigger = trig.parm.Pretrigger_ms;
0248   end
0249 
0250   if isfield(trig.parm, 'Posttrigger_ms')
0251     posttrigger = trig.parm.Posttrigger_ms;
0252   end
0253   
0254   if isfield(trig.parm, 'fsamp')
0255     sample_freq = trig.parm.fsamp;
0256   end
0257 end
0258 return;
0259 %
0260 % --- end of inner_read_trigger_file()
0261 
0262 
0263 % --- inner_read_proc_parm()
0264 %
0265 function [base_file, trigger, cond_id, pretrigger, posttrigger, sample_freq] = ...
0266   inner_read_proc_parm(meeg_file, parm)
0267 
0268 base_file = '';
0269 trigger = [];
0270 cond_id = [];
0271 pretrigger = [];
0272 posttrigger = [];
0273 sample_freq = [];
0274 
0275 if isempty(parm)
0276   return;
0277 end
0278 
0279 if isfield(parm, 'trigger') && ~isempty(parm.trigger)
0280   trigger = parm.trigger;
0281 end
0282 if isfield(parm, 'cond_id') && ~isempty(parm.cond_id)
0283   cond_id = parm.cond_id;
0284 end
0285 
0286 [p_, f_, e_] = vb_get_file_parts(meeg_file);
0287 base_file = [f_, e_];
0288 
0289 if isfield(parm, 'Pretrigger_ms')
0290   pretrigger = parm.Pretrigger_ms;
0291 end
0292 
0293 if isfield(parm, 'Posttrigger_ms')
0294   posttrigger = parm.Posttrigger_ms;
0295 end
0296 
0297 if isfield(parm, 'sample_freq')
0298   sample_freq = parm.sample_freq;
0299 end
0300 return;
0301 %
0302 % --- end of inner_read_proc_parm()
0303 %
0304 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0305 
0306 %%% END OF FILE %%%

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005