Home > vbmeg > functions > device > eeg > vb_job_eeg_brainamp.m

vb_job_eeg_brainamp

PURPOSE ^

make EEG-MAT file from BrainAmp files.

SYNOPSIS ^

function result_code = vb_job_eeg_brainamp(read_spec, verbose_swt)

DESCRIPTION ^

 make EEG-MAT file from BrainAmp files.

 [usage]
   result_code = vb_job_eeg_brainamp(read_spec, verbose_swt)

 [input]
   read_spec  : <required> <<struct>> specification to read BrainAmp files.
     .bamp_file    : <required> <<file>> brainamp file(.eeg) with valid path
                                         see [note] below.
     .pos_file     : <optional> <<file>> POS-MAT file with valid path
     .device       : <optional> ['BRAINAMP'] device name
     .output_dir   : <optional> output directory for EEG-MAT files ['.']
     .eeg_file     : <optional> ['<date>.eeg.mat'] EEG-MAT file name
     .bin_data_dir : <optional> currently not used.
     .eeginfo_version : <optional> [] version of EEGinfo - unused yet

   verbose_swt     : <optional> <<boolean>> [false]
                   :   switch to output verbose message
 [output]
   result_code : <integer>
               :  1) no problem
 [note]
   See also
     vb_job_meg

   * brain amp data consists of three files(.eeg/.vhdr/.vmrk).
     This function assumes that these files exist in a same directory
     with the same name.

 [history]
   2014-08-19 rhayashi 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:

SOURCE CODE ^

0001 function result_code = vb_job_eeg_brainamp(read_spec, verbose_swt)
0002 % make EEG-MAT file from BrainAmp files.
0003 %
0004 % [usage]
0005 %   result_code = vb_job_eeg_brainamp(read_spec, verbose_swt)
0006 %
0007 % [input]
0008 %   read_spec  : <required> <<struct>> specification to read BrainAmp files.
0009 %     .bamp_file    : <required> <<file>> brainamp file(.eeg) with valid path
0010 %                                         see [note] below.
0011 %     .pos_file     : <optional> <<file>> POS-MAT file with valid path
0012 %     .device       : <optional> ['BRAINAMP'] device name
0013 %     .output_dir   : <optional> output directory for EEG-MAT files ['.']
0014 %     .eeg_file     : <optional> ['<date>.eeg.mat'] EEG-MAT file name
0015 %     .bin_data_dir : <optional> currently not used.
0016 %     .eeginfo_version : <optional> [] version of EEGinfo - unused yet
0017 %
0018 %   verbose_swt     : <optional> <<boolean>> [false]
0019 %                   :   switch to output verbose message
0020 % [output]
0021 %   result_code : <integer>
0022 %               :  1) no problem
0023 % [note]
0024 %   See also
0025 %     vb_job_meg
0026 %
0027 %   * brain amp data consists of three files(.eeg/.vhdr/.vmrk).
0028 %     This function assumes that these files exist in a same directory
0029 %     with the same name.
0030 %
0031 % [history]
0032 %   2014-08-19 rhayashi initial version
0033 %
0034 % Copyright (C) 2011, ATR All Rights Reserved.
0035 % License : New BSD License(see VBMEG_LICENSE.txt)
0036 tic;
0037 
0038 
0039 %
0040 % --- Previous check
0041 %
0042 
0043 % constants
0044 vb_define_device;
0045 func_ = mfilename;
0046 result_code = 1; % no problem
0047 
0048 if (nargin < 1 || 2 < nargin)
0049     error('(%s)Please check the usage of the function', func_);
0050 end
0051 if ~isfield(read_spec, 'bamp_file') || isempty(read_spec.bamp_file)
0052     error('(%s)cannot find bamp_file', func_);
0053 end
0054 if ~isfield(read_spec, 'pos_file')
0055     read_spec.pos_file = '';
0056 end
0057 
0058 if ~isfield(read_spec, 'device') || ...
0059    isempty(read_spec.device)
0060     read_spec.device = 'BRAINAMP';
0061 end
0062 
0063 %
0064 % --- Main Procedure
0065 %
0066 if exist(read_spec.bamp_file, 'file') ~= 2
0067     error('%s)cannot find bamp_file: %s', func_, read_spec.bamp_file);
0068 end
0069 
0070 % Input/Output file
0071 data_file   = read_spec.bamp_file;
0072 pos_file    = read_spec.pos_file;
0073 output_file = fullfile(read_spec.output_dir, read_spec.eeg_file);
0074 
0075 [output_dir, f, e]  = vb_get_file_parts(output_file);
0076 if exist(output_dir, 'dir') ~= 7
0077     vb_mkdir(output_dir);
0078 end
0079 
0080 % read header
0081 sFile     = in_fopen_brainamp(data_file);
0082 
0083 % get information
0084 ch_list       = {sFile.channelmat.Channel.Name}';
0085 Nch_all       = size(ch_list, 1); % This will contain the number of extra channel.
0086 sample_freq   = sFile.prop.sfreq;
0087 Nsamples      = sFile.header.nsamples;
0088 
0089 % read data
0090 sfid = fopen(sFile.filename, 'r', sFile.byteorder);
0091 if sfid == -1, return; end
0092 data2d = in_fread_brainamp(sFile, sfid); % ch x sample
0093 fclose(sfid);
0094 
0095 
0096 % Add marker(trigger) as channel data
0097 for k=2:length(sFile.events)
0098     trig_start_list = sFile.events(k).samples;
0099     Ntrig           = length(trig_start_list);
0100     if Ntrig
0101         data2d = [data2d; zeros(1, Nsamples)];
0102         for j=1:Ntrig
0103             % trigger ON = 1
0104             data2d(end, trig_start_list(j)) = 1;
0105         end
0106         Nch_all = Nch_all + 1;
0107         ch_list{end+1} = ['EXT', num2str(k-1)];
0108     end
0109 end
0110 
0111 %
0112 % --- read sensor location & make
0113 %
0114 pos_to_data_ix   = [];  %  pos to data2d index
0115 pos_valid_ix     = [];
0116 
0117 if isempty(pos_file)
0118     % No POS-MAT file specified.
0119     pos_info.coord_type = '';
0120     pos_info.name       = {sFile.channelmat.Channel.Name}';
0121 else
0122     pos_info = load(pos_file);
0123 end
0124 
0125 % search and create relationships.
0126 % channel of measurement data vs pos(pos_ix, :)
0127 for k=1:length(pos_info.name)
0128     channel = pos_info.name{k};
0129     ix = strmatch(channel, ch_list, 'exact');
0130     if isempty(ix)
0131         warning(['channel : ' channel '  not found in the measurement file.']);
0132     else
0133         pos_to_data_ix = [pos_to_data_ix; ix];
0134         pos_valid_ix = [pos_valid_ix; k];
0135     end
0136 end
0137 
0138 % find unmatched channel and treat them as extra channels.
0139 ext_data_ix = setdiff([1:Nch_all]', pos_to_data_ix);
0140 ext_ch_name = [];
0141 if ~isempty(ext_data_ix)
0142     for k=1:length(ext_data_ix)
0143         ext_ch_name{k, 1} = ch_list{ext_data_ix(k)};
0144     end
0145 end
0146 ext_data = data2d(ext_data_ix, :);
0147 
0148 
0149 %
0150 % --- Formatting for VBMEG
0151 %
0152 if isfield(pos_info, 'pos')
0153     Coord = pos_info.pos(pos_valid_ix, :);
0154 else
0155     Coord = [];
0156 end
0157 
0158 % Data info
0159 s.Measurement = 'EEG';
0160 s.EEGinfo.Measurement   = 'EEG';
0161 s.EEGinfo.Device        = 'BRAINAMP';
0162 s.EEGinfo.Nsample       = Nsamples;
0163 s.EEGinfo.Nrepeat       = 1;
0164 s.EEGinfo.ActiveTrial   = 1;
0165 s.EEGinfo               = vb_eeginfo_set_pre_trigger(s.EEGinfo, 0);
0166 s.EEGinfo.SampleFrequency = sample_freq;
0167 
0168 % Data
0169 s.EEGinfo.Trial.number = 1;
0170 s.EEGinfo.Trial.sample = [1:Nsamples];
0171 s.EEGinfo.Trial.Active = true;
0172 
0173 % Channel info
0174 s.EEGinfo.Nchannel      = length(pos_valid_ix);
0175 s.EEGinfo.CoordType     = pos_info.coord_type;
0176 s.EEGinfo.Coord         = Coord;
0177 s.EEGinfo.ChannelID     = [1:s.EEGinfo.Nchannel]';
0178 s.EEGinfo.ChannelName   = pos_info.name(pos_valid_ix);
0179 s.EEGinfo.ActiveChannel = ones(s.EEGinfo.Nchannel, 1);
0180 
0181 s.EEGinfo.ChannelInfo.Active = s.EEGinfo.ActiveChannel;
0182 s.EEGinfo.ChannelInfo.Name   = s.EEGinfo.ChannelName;
0183 s.EEGinfo.ChannelInfo.Type   = ones(s.EEGinfo.Nchannel, 1);
0184 s.EEGinfo.ChannelInfo.ID     = s.EEGinfo.ChannelID;
0185 s.EEGinfo.ChannelInfo.PhysicalUnit = repmat({'V'}, [s.EEGinfo.Nchannel, 1]);
0186 
0187 Next_ch  = length(ext_data_ix);
0188 if Next_ch
0189     s.EEGinfo.ExtraChannelInfo.Channel_active = ones(Next_ch, 1);
0190     s.EEGinfo.ExtraChannelInfo.Channel_name   = ext_ch_name;
0191     s.EEGinfo.ExtraChannelInfo.Channel_type   = ones(Next_ch, 1);
0192     s.EEGinfo.ExtraChannelInfo.Channel_id     = [s.EEGinfo.Nchannel+1:s.EEGinfo.Nchannel+Next_ch]';
0193 end
0194 
0195 % File
0196 s.EEGinfo.File.BaseFile  = data_file;
0197 s.EEGinfo.File.OutputDir = read_spec.output_dir;
0198 s.EEGinfo.File.EEGFile   = read_spec.eeg_file;
0199 s.EEGinfo.File.DataDir   = '';
0200 
0201 % MRI_ID, Vcenter, Vradius, (device_info)TransInfo
0202 s.EEGinfo = vb_info_add_posfile_info(s.EEGinfo, pos_file);
0203 
0204 % EEG Data
0205 s.eeg_data = data2d([pos_to_data_ix;ext_data_ix] , :);
0206 s.EEGinfo.DataType = repmat({'float32'}, [Nch_all, 1]);
0207 
0208 
0209 fprintf('--- now making EEG-MAT file(%s) ...\n', output_file);
0210 
0211 % Save EEG-MAT
0212 vb_save_struct(output_file, s);
0213 
0214 fprintf(' done!\n');
0215 fprintf('--- (%s) %.1f [sec]\n', func_, toc);

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