Home > vbmeg > functions > device > eeg > biosemi > vb_util_read_trigger_file.m

vb_util_read_trigger_file

PURPOSE ^

read trigger file and get trigger samples

SYNOPSIS ^

function [trigger_channel_idx, trigger_sample_idx] =vb_util_read_trigger_file(trigger_file, sample_freq)

DESCRIPTION ^

 read trigger file and get trigger samples
 [usage]
   [trigger_cannel_idx, trigger_sample_idx] = ...
     vb_util_read_trigger_file(trigger_file, sample_freq)
 [input]
   trigger_file : <required> trigger file
    sample_freq : <conditionally required> sampling frequency
                : this parameter is necessary when my_type is 'TIME'
 [output]
   trigger_channel_idx : index list of trigger channel (empty data)
    trigger_sample_idx : index of trigger samples
 [note]
   trigger_file must have the struct 'trigger_spec' as follows
   trigger_spec : <<struct>> dataset of trigger
                :  - my_list : list of trigger (time or sample number)
                :  - my_type : 'TIME' or 'SAMPLE'
                :  - my_unit : 'MS' means milli seccond
                :  - idx_type: 'ALL' or 'BEGIN' or 'END' ['BEGIN']
 [history]
   2007-01-11 (Sako) initial version

 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 [trigger_channel_idx, trigger_sample_idx] = ...
0002   vb_util_read_trigger_file(trigger_file, sample_freq)
0003 % read trigger file and get trigger samples
0004 % [usage]
0005 %   [trigger_cannel_idx, trigger_sample_idx] = ...
0006 %     vb_util_read_trigger_file(trigger_file, sample_freq)
0007 % [input]
0008 %   trigger_file : <required> trigger file
0009 %    sample_freq : <conditionally required> sampling frequency
0010 %                : this parameter is necessary when my_type is 'TIME'
0011 % [output]
0012 %   trigger_channel_idx : index list of trigger channel (empty data)
0013 %    trigger_sample_idx : index of trigger samples
0014 % [note]
0015 %   trigger_file must have the struct 'trigger_spec' as follows
0016 %   trigger_spec : <<struct>> dataset of trigger
0017 %                :  - my_list : list of trigger (time or sample number)
0018 %                :  - my_type : 'TIME' or 'SAMPLE'
0019 %                :  - my_unit : 'MS' means milli seccond
0020 %                :  - idx_type: 'ALL' or 'BEGIN' or 'END' ['BEGIN']
0021 % [history]
0022 %   2007-01-11 (Sako) initial version
0023 %
0024 % Copyright (C) 2011, ATR All Rights Reserved.
0025 % License : New BSD License(see VBMEG_LICENSE.txt)
0026 
0027 % --- CHECK ARGUMENT --- %
0028 if ~exist('trigger_file', 'var') trigger_file = []; end;
0029 if ~exist('sample_freq', 'var') sample_freq = []; end;
0030 
0031 [trigger_file, sampling_frequency, trigger_spec] = ...
0032   inner_check_argument(trigger_file, sample_freq);
0033 
0034 
0035 % --- MAIN PROCEDURE --------------------------------------------------------- %
0036 %
0037 trigger_channel_idx = [];
0038 
0039 switch trigger_spec.my_type
0040   case  'SAMPLE'
0041     trigger_sample_idx = ...
0042       inner_set_sample_index(trigger_spec.idx_type, trigger_spec.my_list);
0043         
0044   case  'TIME'
0045     switch trigger_spec.my_unit
0046       case  'MS'
0047         if ~isempty(sampling_frequency)
0048           [msec_time, sample_idx] = vb_util_sample2msectime2sample( ...
0049             [], trigger_spec.my_list, sampling_frequency);
0050           trigger_sample_idx = ...
0051             inner_set_sample_index(trigger_spec.idx_type, sample_idx);
0052         end
0053       otherwise
0054         error('unknown trigger_spec.unit : %s', trigger_spec.my_unit);
0055     end
0056         
0057   otherwise
0058     % as a sample list for now
0059     trigger_sample_idx = ...
0060       inner_set_sample_index(trigger_spec.idx_type, trigger_spec.my_list);
0061 
0062 end % end of switch trigger_spec.my_type
0063 
0064 %
0065 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0066 
0067 % --- INNER FUNCTIONS -------------------------------------------------------- %
0068 %
0069 % --- inner_check_argument()
0070 %
0071 function [trigger_file, sample_freq, trigger_spec] = ...
0072   inner_check_argument(trigger_file, sample_freq)
0073 
0074 if isempty(trigger_file)
0075   error('trigger_file is a required parameter');
0076 end
0077 
0078 if exist(trigger_file, 'file') ~= 2
0079   error('cannot read trigger_file : %s', trigger_file)
0080 end
0081 
0082 % initialize
0083 trigger_spec.my_list = [];
0084 trigger_spec.my_type = [];
0085 trigger_spec.my_unit = [];
0086 trigger_spec.idx_type = [];
0087 
0088 load(trigger_file);
0089 
0090 if ~exist('my_list', 'var')
0091   error('trigger_file ''%s'' does not have trigger list data', trigger_file);
0092 end
0093 
0094 if ~exist('my_type', 'var'), my_type = 'SAMPLE'; end;
0095 if ~exist('my_unit', 'var'), my_unit = 'MS'; end;
0096 if ~exist('idx_type', 'var'), idx_type = 'BEGIN'; end;
0097 
0098 if strcmp(my_type, 'TIME') && isempty(sample_freq)
0099   error('''sample_freq'' is necessary when type is ''TIME''');
0100 end
0101 
0102 trigger_spec.my_list = my_list; end;
0103 trigger_spec.my_type = my_type; end;
0104 trigger_spec.my_unit = my_unit; end;
0105 trigger_spec.idx_type = idx_type; end;
0106 %
0107 % --- end of inner_check_argument()
0108 
0109 % --- inner_set_sample_index()
0110 %
0111 function sample_idx = inner_set_sample_index(index_type, this_index)
0112 
0113 % this_index size must be [1 x N] in this function
0114 this_index = vb_util_arrange_list(this_index, 1);
0115 
0116 switch index_type
0117   case  'ALL'
0118     sample_idx.all_idx = this_index;
0119   case  'BEGIN'
0120     sample_idx.beg_idx = this_index;
0121   case  'END'
0122     sample_idx.end_idx = this_index;
0123   otherwise
0124     error('unknown index type: %s', index_type);
0125 end
0126 %
0127 % --- end of inner_set_sample_index()
0128 %%% END OF FILE %%%

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