Home > functions > device > vb_msrmnt_make_trial_data.m

vb_msrmnt_make_trial_data

PURPOSE ^

Make trial data files by chopping continuous data

SYNOPSIS ^

function [data, ch_info] = vb_msrmnt_make_trial_data(proc_spec)

DESCRIPTION ^

 Make trial data files by chopping continuous data
 [usage]
   [data, ch_info] = vb_msrmnt_make_trial_data(proc_spec)
 [input]
   proc_spec : <required> <<struct>>
             :  struct defined process specifications which has fields 
             :  as follows:
             :
     .root_dir  : <optional> data directory of base meg/eeg file
                :  and trig_file ['.']
     .trig_file : <conditionally required> <<file>>
                :  trigger file name without path
                :  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.
     .new_file  : <optional> new MEG/EEG filename
                :  ['<root_dir>/<base file>_epoch_yymmdd-HHMMSS'
                :   .(meg/eeg).mat]
   .new_bin_dir : <optional> external data directory where chopped data will
                :  be stored. *ONLY FOR MEG*
                :  This is relative path from '.new_file'
                :  if this is not specified, data is stored internally
                :  (bexp etc.)
     .proc_parm : <optional> <<struct>>
                :  .Pretrigger_ms  : Pretrigger period   [msec]
                :  .Posttrigger_ms : Posttrigger period  [msec]
                :  .sample_freq    : sampling frequency [Hz]
                :  .data_file      : based [meg/eeg] file (no path)
                :  .trigger        : list of trigger samples
                :                  : [1 x Ntrial]
                :    if these are specified, 
                :    they overwrite the setting values of trig_file
 [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

 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(proc_spec)
0002 % Make trial data files by chopping continuous data
0003 % [usage]
0004 %   [data, ch_info] = vb_msrmnt_make_trial_data(proc_spec)
0005 % [input]
0006 %   proc_spec : <required> <<struct>>
0007 %             :  struct defined process specifications which has fields
0008 %             :  as follows:
0009 %             :
0010 %     .root_dir  : <optional> data directory of base meg/eeg file
0011 %                :  and trig_file ['.']
0012 %     .trig_file : <conditionally required> <<file>>
0013 %                :  trigger file name without path
0014 %                :  this file is expected to have data as follows:
0015 %                :   .trig [1 x Ntrial]
0016 %                :   .parm.data_file (without path) : base file
0017 %                :   .parm.Pretrigger_ms  : pretrigger [msec]
0018 %                :   .parm.Posttrigger_ms : posttrigger [msec]
0019 %                :   .parm.fsamp : sampling frequency [Hz]
0020 %                :  if this is invalid and .proc_parm field is not
0021 %                :  specified, it is error.
0022 %     .new_file  : <optional> new MEG/EEG filename
0023 %                :  ['<root_dir>/<base file>_epoch_yymmdd-HHMMSS'
0024 %                :   .(meg/eeg).mat]
0025 %   .new_bin_dir : <optional> external data directory where chopped data will
0026 %                :  be stored. *ONLY FOR MEG*
0027 %                :  This is relative path from '.new_file'
0028 %                :  if this is not specified, data is stored internally
0029 %                :  (bexp etc.)
0030 %     .proc_parm : <optional> <<struct>>
0031 %                :  .Pretrigger_ms  : Pretrigger period   [msec]
0032 %                :  .Posttrigger_ms : Posttrigger period  [msec]
0033 %                :  .sample_freq    : sampling frequency [Hz]
0034 %                :  .data_file      : based [meg/eeg] file (no path)
0035 %                :  .trigger        : list of trigger samples
0036 %                :                  : [1 x Ntrial]
0037 %                :    if these are specified,
0038 %                :    they overwrite the setting values of trig_file
0039 % [output]
0040 %           : these are the same as the return values of vb_load_meg_data
0041 %      data : chopped data [Nchannel x Nsample x Ntrial]
0042 %   ch_info : channel information struct
0043 %           :  .Active
0044 %           :  .Name
0045 %           :  .ID
0046 %           :  .Type
0047 % [note]
0048 %
0049 % [history]
0050 %   2009-07-24 (Sako) initial version
0051 %   2009-09-01 (Sako) substituted vb_load_measurement_info for vb_load_meg_info
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 [proc_spec] = inner_check_arguments(proc_spec);
0059 
0060 % --- MAIN PROCEDURE --------------------------------------------------------- %
0061 %
0062 result = [];
0063 
0064 % default values
0065 [base_file_def, trigger_def, pretrigger_def, posttrigger_def, ...
0066   sample_freq_def] = ...
0067     inner_read_trigger_file(proc_spec.root_dir, proc_spec.trig_file);
0068 
0069 % substitute values
0070 [base_file_sub, trigger_sub, pretrigger_sub, posttrigger_sub, ...
0071   sample_freq_sub] = ...
0072     inner_read_proc_parm(proc_spec.root_dir, proc_spec.proc_parm);
0073 
0074 
0075 if ~isempty(base_file_sub)
0076   base_file = base_file_sub;
0077 else
0078   base_file = base_file_def;
0079 end
0080 
0081 if ~isempty(trigger_sub)
0082   trigger = trigger_sub;
0083 else
0084   trigger = trigger_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(base_file);
0108 Measurement = vb_info_get_measurement(info);
0109 
0110 % --- MEG
0111 if strcmp(Measurement, 'MEG')
0112   if ~isempty(proc_spec.new_bin_dir)
0113     new_spec.trigger = trigger;
0114     new_spec.pretrigger_ms = pretrigger_ms;
0115     new_spec.posttrigger_ms = posttrigger_ms;
0116     new_spec.sample_freq = sample_freq;
0117     new_spec.new_file = proc_spec.new_file;
0118     new_spec.new_bin_dir = proc_spec.new_bin_dir;
0119     [data, ch_info] = vb_megfile_make_trial_data(base_file, new_spec);
0120     return;
0121   end
0122 end
0123 
0124 % --- make load_spec
0125 
0126 % sample frequency for base_file
0127 info = vb_load_measurement_info(base_file);
0128 fs   = vb_info_get_sampling_frequency(info);
0129 
0130 % Convert trigger time index for fs [Hz]
0131 load_spec.Trigger = round(trigger * (fs/sample_freq));
0132 
0133 % convert trigger data from msec to sample
0134 load_spec.Pretrigger  = ceil(pretrigger_ms  *(fs/1000));
0135 load_spec.Posttrigger = ceil(posttrigger_ms *(fs/1000));
0136 
0137 load_spec.ChannelType = 'ALL';
0138 load_spec.ActiveChannel = false;
0139 new_file = proc_spec.new_file;
0140 
0141 [data, ch_info] = vb_load_meg_data(base_file, load_spec, new_file);
0142 return;
0143 %
0144 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0145 
0146 % --- INNER FUNCTIONS -------------------------------------------------------- %
0147 %
0148 % --- inner_check_arguments()
0149 %
0150 function [proc_spec] = inner_check_arguments(proc_spec)
0151 func_ = mfilename;
0152 
0153 if isempty(proc_spec)
0154   error('(%s) proc_spec is a required parameter', func_);
0155 end
0156 
0157 % --- proc_spec
0158 %  .root_dir
0159 %  .trig_file
0160 %  .new_file
0161 %  .proc_parm
0162 
0163 if ~isfield(proc_spec, 'root_dir') || isempty(proc_spec.root_dir)
0164   proc_spec.root_dir = '.';
0165 end
0166 
0167 if ~isfield(proc_spec, 'trig_file')
0168   proc_spec.trig_file = '';
0169 end
0170 
0171 if ~isfield(proc_spec, 'new_file')
0172   proc_spec.new_file = '';
0173 end
0174 
0175 if ~isfield(proc_spec, 'new_bin_dir')
0176   proc_spec.new_bin_dir = '';
0177 end
0178 
0179 if ~isfield(proc_spec, 'proc_parm')
0180   proc_spec.proc_parm = [];
0181 end
0182 
0183 if isempty(proc_spec.trig_file) && isempty(proc_spec.proc_parm)
0184   error('(%s) either trig_file or proc_parm is required', func_);
0185 end
0186 return;
0187 %
0188 % --- end of inner_check_arguments()
0189 
0190 
0191 % --- inner_read_trigger_file()
0192 %
0193 function [base_file, trigger, pretrigger, posttrigger, sample_freq] = ...
0194   inner_read_trigger_file(root_dir, trig_file)
0195 
0196 func_ = mfilename;
0197 
0198 base_file = '';
0199 trigger = [];
0200 pretrigger = [];
0201 posttrigger = [];
0202 sample_freq = 1024;
0203 
0204 if isempty(trig_file)
0205   return;
0206 end
0207 
0208 
0209 trig_file = [root_dir '/' trig_file];
0210 
0211 if exist(trig_file, 'file') ~= 2
0212   error('(%s) cannot find trig_file : %s', func_, trig_file);
0213 end
0214 
0215 trig = load(trig_file);
0216 if isfield(trig, 'trig') && ~isempty(trig.trig)
0217   trigger = trig.trig;
0218 end
0219 
0220 if isfield(trig, 'parm') && ~isempty(trig.parm)
0221   if isfield(trig.parm, 'data_file') && ~isempty(trig.parm.data_file)
0222     base_file = [root_dir '/' trig.parm.data_file];
0223   end
0224 
0225   if isfield(trig.parm, 'Pretrigger_ms')
0226     pretrigger = trig.parm.Pretrigger_ms;
0227   end
0228 
0229   if isfield(trig.parm, 'Posttrigger_ms')
0230     posttrigger = trig.parm.Posttrigger_ms;
0231   end
0232   
0233   if isfield(trig.parm, 'fsamp')
0234     sample_freq = trig.parm.fsamp;
0235   end
0236 end
0237 return;
0238 %
0239 % --- end of inner_read_trigger_file()
0240 
0241 
0242 % --- inner_read_proc_parm()
0243 %
0244 function [base_file, trigger, pretrigger, posttrigger, sample_freq] = ...
0245   inner_read_proc_parm(root_dir, parm)
0246 
0247 base_file = '';
0248 trigger = [];
0249 pretrigger = [];
0250 posttrigger = [];
0251 sample_freq = [];
0252 
0253 if isempty(parm)
0254   return;
0255 end
0256 
0257 if isfield(parm, 'trigger') && ~isempty(parm.trigger)
0258   trigger = parm.trigger;
0259 end
0260 
0261 if isfield(parm, 'data_file')
0262   base_file = [root_dir '/' parm.data_file];
0263 end
0264 
0265 if isfield(parm, 'Pretrigger_ms')
0266   pretrigger = parm.Pretrigger_ms;
0267 end
0268 
0269 if isfield(parm, 'Posttrigger_ms')
0270   posttrigger = parm.Posttrigger_ms;
0271 end
0272 
0273 if isfield(parm, 'sample_freq')
0274   sample_freq = parm.sample_freq;
0275 end
0276 return;
0277 %
0278 % --- end of inner_read_proc_parm()
0279 %
0280 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0281 
0282 %%% END OF FILE %%%

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005