Home > functions > device > meg > vb_megfile_make_trial_data.m

vb_megfile_make_trial_data

PURPOSE ^

Make trial data files of MEG after extracting trials from continuous raw data

SYNOPSIS ^

function [meg_data, ch_info] = vb_megfile_make_trial_data(meg_file, proc_spec)

DESCRIPTION ^

 Make trial data files of MEG after extracting trials from continuous raw data
 [usage]
   [meg_data, ch_info] = vb_megfile_make_trial_data(meg_file, proc_spec)

 [input]
    meg_file : <required> MEG-MAT file as base (with path)
   proc_spec : <required> <<struct>> struct defined process specifications
             :  fields are as follows:
      .root_dir  : <optional> data directory for new meg file
                 : if not specified, directory of meg_file is used
        .trigger : <required> list of sample of trigger [1 x Ntrial]
  .pretrigger_ms : <required> pretrigger [msec]
 .posttrigger_ms : <required> posttrigger [msec]
    .sample_freq : <requied> sampling frequency [Hz]
       .new_file : <optional> new MEG file (without path)
                 : this will be made at 'root_dir'
                 : if this is not specified, data is not stored.
    .new_bin_dir : <optional> external data directory where chopped data 
                 :  will be stored.
                 :  This is relative path from '.new_file'
                 :  if this is not specified, data is stored internally
                 :  (bexp etc.)

 [output]
   meg_data : chopped data [Nchannel x Nsample x Ntrial]
    ch_info : <<struct>> channel information of loaded data
            :  .Active [Nchannel x 1]
            :  .Name   [Nchannel x 1]
            :  .ID     [Nchannel x 1]
            :  .Type   [Nchannel x 1]

 [note]
   @see vb_msrmnt_make_trial_data.m

 [history]
   2009-07-24 (Sako) initial version
   2009-09-01 (Sako) substituted vb_load_measurement_info for vb_load_meg_info
   2011-06-02 (Sako) modified according to the new data format

 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 [meg_data, ch_info] = vb_megfile_make_trial_data(meg_file, proc_spec)
0002 % Make trial data files of MEG after extracting trials from continuous raw data
0003 % [usage]
0004 %   [meg_data, ch_info] = vb_megfile_make_trial_data(meg_file, proc_spec)
0005 %
0006 % [input]
0007 %    meg_file : <required> MEG-MAT file as base (with path)
0008 %   proc_spec : <required> <<struct>> struct defined process specifications
0009 %             :  fields are as follows:
0010 %      .root_dir  : <optional> data directory for new meg file
0011 %                 : if not specified, directory of meg_file is used
0012 %        .trigger : <required> list of sample of trigger [1 x Ntrial]
0013 %  .pretrigger_ms : <required> pretrigger [msec]
0014 % .posttrigger_ms : <required> posttrigger [msec]
0015 %    .sample_freq : <requied> sampling frequency [Hz]
0016 %       .new_file : <optional> new MEG file (without path)
0017 %                 : this will be made at 'root_dir'
0018 %                 : if this is not specified, data is not stored.
0019 %    .new_bin_dir : <optional> external data directory where chopped data
0020 %                 :  will be stored.
0021 %                 :  This is relative path from '.new_file'
0022 %                 :  if this is not specified, data is stored internally
0023 %                 :  (bexp etc.)
0024 %
0025 % [output]
0026 %   meg_data : chopped data [Nchannel x Nsample x Ntrial]
0027 %    ch_info : <<struct>> channel information of loaded data
0028 %            :  .Active [Nchannel x 1]
0029 %            :  .Name   [Nchannel x 1]
0030 %            :  .ID     [Nchannel x 1]
0031 %            :  .Type   [Nchannel x 1]
0032 %
0033 % [note]
0034 %   @see vb_msrmnt_make_trial_data.m
0035 %
0036 % [history]
0037 %   2009-07-24 (Sako) initial version
0038 %   2009-09-01 (Sako) substituted vb_load_measurement_info for vb_load_meg_info
0039 %   2011-06-02 (Sako) modified according to the new data format
0040 %
0041 % Copyright (C) 2011, ATR All Rights Reserved.
0042 % License : New BSD License(see VBMEG_LICENSE.txt)
0043 
0044 % --- CHECK ARGUMENTS --- %
0045 if ~exist('meg_file', 'var'), meg_file = ''; end
0046 if ~exist('proc_spec', 'var'), proc_spec = []; end
0047 [meg_file, proc_spec] = inner_check_arguments(meg_file, proc_spec);
0048 
0049 % --- MAIN PROCEDURE --------------------------------------------------------- %
0050 %
0051 meg_data = [];
0052 ch_info = [];
0053 
0054 fs0  = proc_spec.sample_freq;
0055 
0056 meg_info = vb_load_measurement_info(meg_file);
0057 fs  = vb_info_get_sampling_frequency(meg_info);
0058 
0059 % Recalculate trial time index according to 'Pre/Posttrigger'
0060 % if Sample Frequency is different from raw data
0061 % Convert trigger time index for fs [Hz]
0062 frate = (fs/fs0);
0063 
0064 % --- initial trig is loaded from trig_file
0065 trigger = round( proc_spec.trigger * frate );
0066 
0067 % Sample number for Pre/Post period in data file
0068 pretrigger  = ceil(proc_spec.pretrigger_ms  * (fs/1000));
0069 posttrigger = ceil(proc_spec.posttrigger_ms * (fs/1000));
0070 
0071 std_sample_list = (-pretrigger:posttrigger)';
0072 n_sample = length(std_sample_list);
0073 n_trial  = length(trigger);
0074 
0075 trig_info.Trigger = trigger;
0076 trig_info.Pretrigger = pretrigger;
0077 trig_info.Posttrigger = posttrigger;
0078 [tied_sample] = vb_loadspec_make_trial_sample(trig_info);
0079 % --- tied_sample is the array of from and to [Ntrial x 2]
0080 
0081 ix_trial = zeros(n_sample, n_trial);
0082 for i_trial = 1:n_trial
0083   ix_trial(:,i_trial) = tied_sample(i_trial,1):tied_sample(i_trial,2);
0084 end
0085 
0086 % ---
0087 % --- If new binary directory is given, store chopped data as the new
0088 % --- binary data externally.
0089 % --- If it is not given, store chopped data internally (bexp)
0090 % ---
0091 if isempty(proc_spec.new_file)
0092   new_bin_dir = '';
0093   save_externally = false;
0094 else
0095   
0096   if ~isfield(proc_spec, 'new_bin_dir') || isempty(proc_spec.new_bin_dir)
0097     new_bin_dir = '';
0098     save_externally = false;
0099 
0100   else
0101     save_externally = true;
0102 
0103     fpath = vb_get_file_parts(proc_spec.new_file);
0104     new_bin_dir = [fpath '/' proc_spec.new_bin_dir];
0105     if exist(new_bin_dir, 'dir') ~= 7
0106       vb_mkdir(new_bin_dir);
0107     end
0108     precision = vb_meginfo_get_precision(meg_info);
0109   end
0110 end
0111 
0112 if save_externally
0113 
0114   ch_labels = vb_megfile_get_channel_label_whole(meg_file);
0115   ch_len    = size(ch_labels, 1);
0116 
0117   fprintf('\n...now on chopping data ...\n');
0118   LINE_LEN = 30;
0119   
0120   for i_ch = 1:ch_len
0121     load_spec.ChannelName = {ch_labels{i_ch}};
0122     load_spec.ChannelSwitch = true;
0123     load_spec.ChannelType = 'ALL';
0124     load_spec.ActiveChannel = 0;
0125     load_spec.Trigger = trigger;
0126     load_spec.Pretrigger = pretrigger;
0127     load_spec.Posttrigger = posttrigger;
0128     chopped_data = vb_load_meg_data(meg_file, load_spec);
0129 
0130     % --- re-stored filtered data
0131     new_file = sprintf('%s/%s.ch.meg.dat', new_bin_dir, ch_labels{i_ch});
0132     fid = fopen(new_file, 'wb');
0133     if fid == -1
0134       warining('(%s) cannot open file : %s\n', mfilename, new_file);
0135       continue;
0136     end
0137     fwrite(fid, chopped_data, precision);
0138     fclose(fid);
0139     
0140     fprintf('.');
0141     if ~rem(i_ch, LINE_LEN), fprintf('\n'); end
0142   end
0143   
0144   % --- set empty for internal data buffers
0145   bexp     = [];
0146   bexp_ext = [];
0147   refmg    = [];
0148 
0149 else
0150   % save MEG data as internal variable
0151 
0152   % --- (1)
0153   % --- load for every 'MEG' channels
0154   ch_labels = vb_megfile_get_channel_label_meg(meg_file);
0155   ch_id     = vb_meginfo_get_channel_index_meg(meg_info);
0156   ch_len    = size(ch_labels, 1);
0157 
0158   fprintf('\n...now on processing MEG data ...\n');
0159   
0160   bexp = zeros(ch_len, n_sample, n_trial);
0161 
0162   for i_ch = 1:ch_len
0163     load_spec.ChannelName = {ch_labels{i_ch}};
0164     load_spec.ChannelSwitch = true;
0165     load_spec.ChannelType = 'MEG';
0166     load_spec.ActiveChannel = 0;
0167     
0168     load_spec.Trigger = trigger;
0169     load_spec.Pretrigger = pretrigger;
0170     load_spec.Posttrigger = posttrigger;
0171     bexp(i_ch,:,:) = vb_load_meg_data(meg_file, load_spec);
0172     
0173     fprintf('.');
0174   end
0175   
0176   % --- (2)
0177   % --- load for every 'EXT' channels
0178   ch_labels = vb_megfile_get_channel_label_extra(meg_file);
0179   ch_id     = vb_meginfo_get_channel_index_extra(meg_info);
0180   ch_len    = size(ch_labels, 1);
0181 
0182   fprintf('\n...now on processing EXT data ...\n');
0183 
0184   bexp_ext = zeros(ch_len, n_sample, n_trial);
0185 
0186   for i_ch = 1:ch_len
0187     load_spec.ChannelName = {ch_labels{i_ch}};
0188     load_spec.ChannelSwitch = true;
0189     load_spec.ChannelType = 'EXT';
0190     load_spec.ActiveChannel = 0;
0191     load_spec.Trigger = trigger;
0192     load_spec.Pretrigger = pretrigger;
0193     load_spec.Posttrigger = posttrigger;
0194     bexp_ext(i_ch,:,:) = vb_load_meg_data(meg_file, load_spec);
0195 
0196     fprintf('.');
0197   end
0198 
0199   % --- (3)
0200   % --- load for every 'REF' channels
0201   ch_labels = vb_megfile_get_channel_label_refmg(meg_file);
0202   ch_id     = vb_meginfo_get_channel_index_refmg(meg_info);
0203   ch_len    = size(ch_labels, 1);
0204 
0205   fprintf('\n...now on processing REF data ...\n');
0206 
0207   refmg = zeros(ch_len, n_sample, n_trial);
0208 
0209   for i_ch = 1:ch_len
0210     load_spec.ChannelName = {ch_labels{i_ch}};
0211     load_spec.ChannelSwitch = true;
0212     load_spec.ChannelType = 'REF';
0213     load_spec.ActiveChannel = 0;
0214     load_spec.Trigger = trigger;
0215     load_spec.Pretrigger = pretrigger;
0216     load_spec.Posttrigger = posttrigger;
0217     refmg(i_ch,:,:) = vb_load_meg_data(meg_file, load_spec);
0218 
0219     fprintf('.');
0220   end
0221 end
0222 fprintf('\n');
0223 
0224 
0225 % --- POSTERIORI PROCESS
0226 
0227 % ----- make new meg.mat file
0228 meg = load(meg_file);
0229 new_meginfo = meg.MEGinfo;
0230 meg.ParentInfo = meg.MEGinfo;
0231 
0232 if ~isempty(proc_spec.new_file)
0233   % --- update fields
0234   if save_externally
0235     new_meginfo.saveman.data_dir = proc_spec.new_bin_dir;
0236   else
0237     new_meginfo.saveman = [];
0238   end
0239 
0240   for i_trial = 1:n_trial
0241     new_meginfo.Trial(i_trial).number = i_trial;
0242     new_meginfo.Trial(i_trial).sample = ix_trial(:,i_trial);
0243 
0244     new_meginfo.Trial(i_trial).Active = 1;
0245 %     Trial.number = i_trial;
0246 %     Trial.sample = ix_trial(:,i_trial);
0247 %
0248 %     Trial.Active = 1;
0249 %     new_meginfo.Trial(i_trial) = Trial;
0250   end
0251   new_meginfo.Trial = new_meginfo.Trial';
0252 
0253   new_meginfo.ActiveTrial = ones(n_trial,1);
0254   new_meginfo = vb_meginfo_set_acqtype(new_meginfo, 'Evoked_Raw');
0255 
0256   new_meginfo.Nsample = n_sample;
0257   new_meginfo.Nrepeat = n_trial;
0258   new_meginfo.Pretrigger = pretrigger;
0259     
0260   meg.MEGinfo = new_meginfo;
0261 
0262   meg.bexp = bexp;
0263   meg.bexp_ext = bexp_ext;
0264   meg.refmg = refmg;
0265 
0266   fprintf('\n');
0267   fprintf('save: %s\n',proc_spec.new_file);
0268 
0269   cmd_save = sprintf('save %s -struct meg', proc_spec.new_file);
0270   eval(cmd_save);
0271 end
0272 return;
0273 %
0274 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0275 
0276 % --- INNER FUNCTIONS -------------------------------------------------------- %
0277 %
0278 % --- inner_check_arguments()
0279 %
0280 function [meg_file, proc_spec] = inner_check_arguments(meg_file, proc_spec)
0281 func_ = mfilename;
0282 
0283 % --- check meg_file
0284 if isempty(meg_file)
0285   error('(%s) meg_file is a required parameter', func_);
0286 end
0287 
0288 if exist(meg_file, 'file') ~= 2
0289   error('(%s) cannot find meg_file : %s', func_, meg_file);
0290 end
0291 
0292 % --- check proc_spec ---
0293 %     .root_dir
0294 %     .trigger
0295 %     .pretrigger_ms
0296 %     .posttrigger_ms
0297 %     .sample_freq
0298 %     .new_file
0299 %     .new_bin_dir
0300 % -----------------------
0301 if isempty(proc_spec)
0302   error('(%s) proc_spec is a required parameter', func_);
0303 end
0304 
0305 % --- required fields
0306 
0307 % ----- proc_spec.trigger
0308 if ~isfield(proc_spec, 'trigger') || isempty(proc_spec.trigger)
0309   error('(%s) trigger is a required field of proc_spec', func_);
0310 end
0311 
0312 % ----- proc_spec.pretrigger_ms
0313 if ~isfield(proc_spec, 'pretrigger_ms') || isempty(proc_spec.pretrigger_ms)
0314   error('(%s) pretrigger_ms is a required field of proc_spec', func_);
0315 end
0316 
0317 % ----- proc_spec.posttrigger_ms
0318 if ~isfield(proc_spec, 'posttrigger_ms') || isempty(proc_spec.posttrigger_ms)
0319   error('(%s) posttrigger_ms is a required field of proc_spec', func_);
0320 end
0321 
0322 % ----- proc_spec.sample_freq
0323 if ~isfield(proc_spec, 'sample_freq') || isempty(proc_spec.sample_freq)
0324   error('(%s) sample_freq is a required field of proc_spec', func_);
0325 end
0326 
0327 
0328 % --- optional fields
0329 
0330 % ----- get path of meg_file as a default path
0331 [fpath] = vb_get_file_parts(meg_file);
0332 
0333 % ----- proc_spec.root_dir
0334 if ~isfield(proc_spec, 'root_dir') || isempty(proc_spec.root_dir)
0335     proc_spec.root_dir = fpath;
0336 end
0337 
0338 % ----- proc_spec.new_file
0339 if ~isfield(proc_spec, 'new_file')
0340   proc_spec.new_file = '';
0341 end
0342 
0343 % ----- proc_spec.new_bin_dir
0344 if ~isfield(proc_spec, 'new_bin_dir')
0345   proc_spec.new_bin_dir = '';
0346 end
0347 return;
0348 %
0349 % --- end of inner_check_arguments()
0350 %
0351 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0352 
0353 % --- END OF FILE --- %

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