Home > vbmeg > functions > device > eeg > brainamp > in_fopen_brainamp.m

in_fopen_brainamp

PURPOSE ^

IN_FOPEN_BRAINAMP: Open a BrainVision BrainAmp .eeg file.

SYNOPSIS ^

function sFile = in_fopen_brainamp(DataFile)

DESCRIPTION ^

 IN_FOPEN_BRAINAMP: Open a BrainVision BrainAmp .eeg file.

 USAGE:  sFile = in_fopen_brainamp(DataFile)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function sFile = in_fopen_brainamp(DataFile)
0002 % IN_FOPEN_BRAINAMP: Open a BrainVision BrainAmp .eeg file.
0003 %
0004 % USAGE:  sFile = in_fopen_brainamp(DataFile)
0005 
0006 % @=============================================================================
0007 % This software is part of the Brainstorm software:
0008 % http://neuroimage.usc.edu/brainstorm
0009 %
0010 % Copyright (c)2000-2013 Brainstorm by the University of Southern California
0011 % This software is distributed under the terms of the GNU General Public License
0012 % as published by the Free Software Foundation. Further details on the GPL
0013 % license can be found at http://www.gnu.org/copyleft/gpl.html.
0014 %
0015 % FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
0016 % UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
0017 % WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
0018 % MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
0019 % LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
0020 %
0021 % For more information type "brainstorm license" at command prompt.
0022 % =============================================================================@
0023 %
0024 % Authors: Guillaume Dumas, Francois Tadel, 2012-2013
0025 % 2014-12-08 rhayashi Added channelmat.Channel(i).scale : scale factor
0026         
0027 %% ===== GET FILES =====
0028 % Build header and markers files names
0029 % VHDR/AHDR File (header)
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 % VMRK/AMRK File (markers)
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 %% ===== READ HEADER =====
0052 hdr.chnames = {};
0053 hdr.chloc = [];
0054 % Open and read file
0055 fid = fopen(VhdrFile,'r');
0056 curBlock = '';
0057 % Read file line by line
0058 while 1
0059     % Read one line
0060     newLine = fgetl(fid);
0061     if ~ischar(newLine)
0062         break;
0063     end
0064     % Empty lines and comment lines: skip
0065     if isempty(newLine) || ismember(newLine(1), {';', char(10), char(13)})
0066         continue;
0067     end
0068     % Read block names
0069     if (newLine(1) == '[')
0070         curBlock = newLine;
0071         curBlock(ismember(curBlock, '[] ')) = [];
0072         continue;
0073     end
0074     % Skip non-attribution lines
0075     if ~any(newLine == '=')
0076         continue;
0077     end
0078     % Split around the '='
0079     argLine = strtrim(str_split(newLine, '='));
0080     if (length(argLine) ~= 2) || (length(argLine{1}) < 2) || isempty(argLine{2})
0081         continue;
0082     end
0083     % Parameter
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                     % Convert Spherical(degrees) => Spherical(radians) => Cartesian
0093                     TH  = (90 - tmpLoc(2)) ./ 180 * pi;
0094                     PHI = (90 + tmpLoc(3)) ./ 180 * pi;
0095                     [X,Y,Z] = sph2cart(PHI, TH, 1);
0096                     % Assign location
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 % Close file
0108 fclose(fid);
0109 
0110 
0111 %% ===== REBUILD ACQ INFO =====
0112 % BINARY and MULTIPLEXED files
0113 if (strcmpi(hdr.DataFormat, 'BINARY') && strcmpi(hdr.DataOrientation, 'MULTIPLEXED'))
0114     % EEG file: get size
0115     dirInfo = dir(DataFile);
0116     % Get number of samples
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 % ASCII and VECTORIZED files
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 %% ===== CREATE BRAINSTORM SFILE STRUCTURE =====
0139 % Initialize returned file structure
0140 sFile = db_template('sfile');
0141 % Add information read from header
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 % Comment: short filename
0149 [tmp__, sFile.comment, tmp__] = fileparts(DataFile);
0150 % Consider that the sampling rate of the file is the sampling rate of the first signal
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 % No info on bad channels
0156 sFile.channelflag = ones(hdr.NumberOfChannels, 1);
0157 
0158 
0159 %% ===== CREATE EMPTY CHANNEL FILE =====
0160 ChannelMat = db_template('channelmat');
0161 ChannelMat.Comment = 'BrainAmp channels';
0162 ChannelMat.Channel = repmat(db_template('channeldesc'), [1, hdr.NumberOfChannels]);
0163 % For each channel
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 % Return channel structure
0188 sFile.channelmat = ChannelMat;
0189      
0190 
0191 %% ===== READ EVENTS =====
0192 if ~isempty(VmrkFile)
0193     newEvents = in_events_brainamp(sFile, VmrkFile);
0194     % Add each new event
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         % Make sure that the sample indices are round values
0202         newEvents(iNew).samples = round(newEvents(iNew).samples);
0203         newEvents(iNew).times   = newEvents(iNew).samples ./ sFile.prop.sfreq;
0204         % If event does not exist yet: add it at the end of the list
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         % Event exists: merge occurrences
0214         else
0215             % Merge events occurrences
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             % Sort by sample indices
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

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