0001 function result_code = vb_job_eeg_brainamp(read_spec, verbose_swt)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 tic;
0037
0038
0039
0040
0041
0042
0043
0044 vb_define_device;
0045 func_ = mfilename;
0046 result_code = 1;
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
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
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
0081 sFile = in_fopen_brainamp(data_file);
0082
0083
0084 ch_list = {sFile.channelmat.Channel.Name}';
0085 Nch_all = size(ch_list, 1);
0086 sample_freq = sFile.prop.sfreq;
0087 Nsamples = sFile.header.nsamples;
0088
0089
0090 sfid = fopen(sFile.filename, 'r', sFile.byteorder);
0091 if sfid == -1, return; end
0092 data2d = in_fread_brainamp(sFile, sfid);
0093 fclose(sfid);
0094
0095
0096
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
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
0113
0114 pos_to_data_ix = [];
0115 pos_valid_ix = [];
0116
0117 if isempty(pos_file)
0118
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
0126
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
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
0151
0152 if isfield(pos_info, 'pos')
0153 Coord = pos_info.pos(pos_valid_ix, :);
0154 else
0155 Coord = [];
0156 end
0157
0158
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
0169 s.EEGinfo.Trial.number = 1;
0170 s.EEGinfo.Trial.sample = [1:Nsamples];
0171 s.EEGinfo.Trial.Active = true;
0172
0173
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
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
0202 s.EEGinfo = vb_info_add_posfile_info(s.EEGinfo, pos_file);
0203
0204
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
0212 vb_save_struct(output_file, s);
0213
0214 fprintf(' done!\n');
0215 fprintf('--- (%s) %.1f [sec]\n', func_, toc);