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

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