0001 function sFile = in_fopen_brainamp(DataFile)
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 VhdrFile = [DataFile(1:end-4) '.vhdr'];
0031 if ~file_exist(VhdrFile)
0032 VhdrFile = [DataFile(1:end-4) '.ahdr'];
0033 if ~file_exist(VhdrFile)
0034 error('Could not open VHDR header file.');
0035 else
0036 error(['File is saved in BrainVision V-Amp encrypted format.' 10 ...
0037 'Please, export it to "binary data format" before trying to read it in Brainstorm.']);
0038 end
0039 end
0040
0041 VmrkFile = [DataFile(1:end-4) '.vmrk'];
0042 if ~file_exist(VmrkFile)
0043 VmrkFile = [DataFile(1:end-4) '.amrk'];
0044 if ~file_exist(VmrkFile)
0045 disp('BRAINAMP> Warning: Could not open VMRK markers file.');
0046 VmrkFile = [];
0047 end
0048 end
0049
0050
0051
0052 hdr.chnames = {};
0053 hdr.chloc = [];
0054
0055 fid = fopen(VhdrFile,'r');
0056 curBlock = '';
0057
0058 while 1
0059
0060 newLine = fgetl(fid);
0061 if ~ischar(newLine)
0062 break;
0063 end
0064
0065 if isempty(newLine) || ismember(newLine(1), {';', char(10), char(13)})
0066 continue;
0067 end
0068
0069 if (newLine(1) == '[')
0070 curBlock = newLine;
0071 curBlock(ismember(curBlock, '[] ')) = [];
0072 continue;
0073 end
0074
0075 if ~any(newLine == '=')
0076 continue;
0077 end
0078
0079 argLine = strtrim(str_split(newLine, '='));
0080 if (length(argLine) ~= 2) || (length(argLine{1}) < 2) || isempty(argLine{2})
0081 continue;
0082 end
0083
0084 if strcmpi(argLine{1}(1:2), 'Ch')
0085 iChan = str2num(argLine{1}(3:end));
0086 if ~isempty(iChan)
0087 if strcmpi(curBlock, 'ChannelInfos')
0088 hdr.chnames{iChan} = argLine{2};
0089 elseif strcmpi(curBlock, 'Coordinates')
0090 tmpLoc = str2num(argLine{2});
0091 if (length(tmpLoc) == 3) && (tmpLoc(1) == 1)
0092
0093 TH = (90 - tmpLoc(2)) ./ 180 * pi;
0094 PHI = (90 + tmpLoc(3)) ./ 180 * pi;
0095 [X,Y,Z] = sph2cart(PHI, TH, 1);
0096
0097 hdr.chloc(iChan,1:3) = [-X, -Y, Z+.5] .* .0875;
0098 end
0099 end
0100 end
0101 elseif ismember(argLine{1}, {'NumberOfChannels', 'SamplingInterval', 'DataPoints', 'SegmentDataPoints'})
0102 hdr.(argLine{1}) = str2num(argLine{2});
0103 else
0104 hdr.(file_standardize(argLine{1})) = argLine{2};
0105 end
0106 end
0107
0108 fclose(fid);
0109
0110
0111
0112
0113 if (strcmpi(hdr.DataFormat, 'BINARY') && strcmpi(hdr.DataOrientation, 'MULTIPLEXED'))
0114
0115 dirInfo = dir(DataFile);
0116
0117 switch lower(hdr.BinaryFormat)
0118 case 'int_16';
0119 hdr.bytesize = 2;
0120 hdr.byteformat = 'int16';
0121 case 'int_32';
0122 hdr.bytesize = 4;
0123 hdr.byteformat = 'int32';
0124 case 'ieee_float_32';
0125 hdr.bytesize = 4;
0126 hdr.byteformat = 'float32';
0127 end
0128 hdr.nsamples = dirInfo.bytes ./ (hdr.NumberOfChannels * hdr.bytesize);
0129
0130 elseif (strcmpi(hdr.DataFormat, 'ASCII') && strcmpi(hdr.DataOrientation, 'VECTORIZED'))
0131 hdr.nsamples = hdr.DataPoints;
0132 else
0133 error(['Only reading binary multiplexed or vectorize ASCII data format.' 10 'Please contact us if you would like to read other types of files in Brainstorm.']);
0134 end
0135
0136
0137
0138
0139
0140 sFile = db_template('sfile');
0141
0142 sFile.byteorder = 'l';
0143 sFile.filename = DataFile;
0144 sFile.format = 'EEG-BRAINAMP';
0145 sFile.channelmat = [];
0146 sFile.device = 'BRAINAMP';
0147 sFile.header = hdr;
0148
0149 [tmp__, sFile.comment, tmp__] = fileparts(DataFile);
0150
0151 sFile.prop.sfreq = 1e6 ./ hdr.SamplingInterval;
0152 sFile.prop.samples = [0, hdr.nsamples - 1];
0153 sFile.prop.times = sFile.prop.samples ./ sFile.prop.sfreq;
0154 sFile.prop.nAvg = 1;
0155
0156 sFile.channelflag = ones(hdr.NumberOfChannels, 1);
0157
0158
0159
0160 ChannelMat = db_template('channelmat');
0161 ChannelMat.Comment = 'BrainAmp channels';
0162 ChannelMat.Channel = repmat(db_template('channeldesc'), [1, hdr.NumberOfChannels]);
0163
0164 for i = 1:hdr.NumberOfChannels
0165 if ~isempty(hdr.chnames{i})
0166 read = textscan(hdr.chnames{i}, '%s ', 'Delimiter', ',');
0167 chInfo = read{1};
0168 ChannelMat.Channel(i).Name = chInfo{1};
0169 scale = str2double(chInfo{3});
0170 if isempty(scale) || isnan(scale)
0171 scale = 1.0;
0172 end
0173 ChannelMat.Channel(i).scale = scale;
0174 else
0175 ChannelMat.Channel(i).Name = sprintf('E%d', i);
0176 end
0177 if ~isempty(hdr.chloc)
0178 ChannelMat.Channel(i).Loc = hdr.chloc(i,:)';
0179 else
0180 ChannelMat.Channel(i).Loc = [0; 0; 0];
0181 end
0182 ChannelMat.Channel(i).Type = 'EEG';
0183 ChannelMat.Channel(i).Orient = [];
0184 ChannelMat.Channel(i).Weight = 1;
0185 ChannelMat.Channel(i).Comment = [];
0186 end
0187
0188 sFile.channelmat = ChannelMat;
0189
0190
0191
0192 if ~isempty(VmrkFile)
0193 newEvents = in_events_brainamp(sFile, VmrkFile);
0194
0195 for iNew = 1:length(newEvents)
0196 if ~isempty(sFile.events)
0197 iEvt = find(strcmpi(newEvents(iNew).label, {sFile.events.label}));
0198 else
0199 iEvt = [];
0200 end
0201
0202 newEvents(iNew).samples = round(newEvents(iNew).samples);
0203 newEvents(iNew).times = newEvents(iNew).samples ./ sFile.prop.sfreq;
0204
0205 if isempty(iEvt)
0206 if isempty(sFile.events)
0207 iEvt = 1;
0208 sFile.events = newEvents(iNew);
0209 else
0210 iEvt = length(sFile.events) + 1;
0211 sFile.events(iEvt) = newEvents(iNew);
0212 end
0213
0214 else
0215
0216 sFile.events(iEvt).times = [sFile.events(iEvt).times, newEvents(iNew).times];
0217 sFile.events(iEvt).samples = [sFile.events(iEvt).samples, newEvents(iNew).samples];
0218 sFile.events(iEvt).epochs = [sFile.events(iEvt).epochs, newEvents(iNew).epochs];
0219 sFile.events(iEvt).reactTimes = [sFile.events(iEvt).reactTimes, newEvents(iNew).reactTimes];
0220
0221 if (size(sFile.events(iEvt).samples, 2) > 1)
0222 [tmp__, iSort] = unique(sFile.events(iEvt).samples(1,:));
0223 sFile.events(iEvt).samples = sFile.events(iEvt).samples(:,iSort);
0224 sFile.events(iEvt).times = sFile.events(iEvt).times(:,iSort);
0225 sFile.events(iEvt).epochs = sFile.events(iEvt).epochs(iSort);
0226 if ~isempty(sFile.events(iEvt).reactTimes)
0227 sFile.events(iEvt).reactTimes = sFile.events(iEvt).reactTimes(iSort);
0228 end
0229 end
0230 end
0231 end
0232 end
0233
0234
0235 function [ret] = file_exist(file)
0236
0237 if ~exist('file', 'var')
0238 error('specified file does not exist.');
0239 end
0240
0241 if exist(file, 'file') == 2
0242 ret = true;
0243 else
0244 ret = false;
0245 end