load_neuroscan.m
来自「绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX」· M 代码 · 共 1,009 行 · 第 1/4 页
M
1,009 行
%LOAD_NEUROSCAN will load NeuroScan binary data file to a NeuroScan
% structure. It supports file format AVG (average EEG), EEG (epoched
% EEG), and CNT (continuous EEG). For continuous EEG format, it can
% automatically distinguish whether data is from SynAmps module or is
% from 100 and 330 KHz modules.
%
% load_neuroscan will use file extension to tell whether it is an AVG,
% an EEG, or a CNT format, unless the fileformat argument is provided.
% If it can not find the proper file extension and the fileformat
% argument is not provided, AVG data will be assumed.
%
% Usage: ns = load_neuroscan(filename, [fileformat], [machineformat], ...
% [cnt_start_time], [cnt_end_time])
%
% ns - structure containing NeuroScan header fields SETUP & ELECTLOC,
% and data. For epoched EEG data, a field SWEEP will be appended.
% For continuous EEG data, fields TEEG & EVENT will be appended.
%
% filename - NeuroScan data file name.
%
% fileformat (optional) - Either 'AVG' (default), 'EEG', 'CNT', or 'CNT32'.
% If it is empty, the default value will be used.
%
% machineformat (optional) - Default is little-endian 'ieee-le'.
% If it is empty, the default value will be used.
%
% 'ieee-le' or 'l' - IEEE floating point with little-endian
% byte ordering
% 'ieee-be' or 'b' - IEEE floating point with big-endian
% byte ordering
% 'vaxd' or 'd' - VAX D floating point and VAX ordering
% 'vaxg' or 'g' - VAX G floating point and VAX ordering
% 'cray' or 'c' - Cray floating point with big-endian
% byte ordering
% 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
% byte ordering and 64 bit long data type
% 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
% ordering and 64 bit long data type.
%
% cnt_start_time (optional) - Only for continuous data file, you can
% specify the start time with unit in second that you
% want to extract. The rounded start time will be
% displayed in ns.start_time. By default, it starts
% at 0 second.
%
% cnt_end_time (optional) - Only for continuous data file, you can
% specify the end time with unit in second that you
% want to extract. The rounded start time will be
% displayed in ns.end_time. By default, it ends at
% the end of data.
%
% Notes:
%
% 1. This program assumed that the data were acquired from NeuroScan
% version 3.0 and above.
%
% 2. There are two headers for all NeuroScan data file. General header
% containing information that applies to all channels should be at
% the beginning of a NeuroScan data file. The size of general header
% is 900 bytes. Channel-specific header containing information that
% pertains to particular channel should come after general header.
% The size of channel-specific header is 75 bytes per channel.
%
% 3. Average Neuroscan data is stored as 4-byte floates in vectored
% format for each channel. Each channel has a 5-byte header that
% is no longer used. Thus, after SETUP & ELECTLOC, there is an
% unused 5-byte header followed by SETUP.pnts of 4-byte floating
% point numbers for the first channel; then a 5-byte header for
% channel two followed by SETUP.pnts * size(float) bytes, etc.
% Therefore, the total number of bytes after SETUP & ELECTLOC is:
% SETUP.nchannels * ( 5 + SETUP.pnts * sizeof(float) ). To scale
% a data point to microvolts, multiply by the channel-specific
% calibration factor (i.e., for electrode j: channel[j]->calib)
% and divided by the number of sweeps in the average (i.e.,
% channel[j]->n);
%
% 4. According to CNTTOASC from Neuroscan, continuous Neuroscan data
% type can be distinguished by SETUP.ContinuousType:
%
% Type 0 or 1 means using 100/330KHz module for continuous files.
% Data is stored as 2-byte integer after SETUP hdr and ELECTLOC
% hdr in multiplexed format. Each data scan consists of
% SETUP.nchannels points.
%
% Type 3 means using SynAmps for continuous files. Data is stored
% as 2-byte integer after SETUP hdr and ELECTLOC hdr, and is sent
% in a blocked rather than multiplexed format. The size of block
% in byte is SETUP.ChannelOffset and the size of block in point
% is SETUP.ChannelOffset/2. Since data is recorded continuously,
% block has to be concatenated each other.
%
% To scale a data point to microvolts for channel j, first subtract
% off the amplifier d.c.offset (if any) found in the variable
% (ELECTLOC[j]->baseline). Then multiply by the sensitivity
% (ELECTLOC[j]->sensitivity) times the channel specific scale factor
% (ELECTLOC[j]->calib) devided by 204.8.
%
% After continuous NeuroScan data, there is an Event Table.
% At the beginning of the event table is a TEEG ("Tagged EEG")
% structure that is defined in the sethead.h file. Following
% this structure is the event table property. There are two
% types of event tables - the first with a minimum of event
% information, and the second with additional behavioral
% information. The Teeg variable in the TEEG structure
% indicates the type of event table. The number of events
% in the event table can be calculated by dividing the size
% variable in the TEEG structure by sizeof(EVENT1) or
% sizeof(EVENT2). The EVENT immediately follow the TEEG.
%
% 5. There are SETUP.compsweeps of data in an .EEG file. Each sweep
% of data consists of a sweep header followed by the EEG data.
% After the sweep header, data is stored as 2-byte integers in
% multiplexed format. The scaling of .EEG data to microvolts is
% identical to scaling of .CNT data above.
%
% References:
%
% 1. Header structure can be found on: http://www.neuro.com/files/sethead.h
% 2. Appendix C of Document number 2204 Revision C, from Neuroscan
% 3. CNTTOASC from Neuroscan Customer Support Download Area
% 4. EEG2ASC from Neuroscan Customer Support Download Area
%
% December 8, 2006 (jimmy):
% Neuroscan version 4.3 also has 32-bit acquisition
% for its continuous data files. By default, it collects
% 16-bit integer data with the original SynAmp (SynAmp1).
% With SynAmp2, it will collect 32-bit integer data.
% According to Neuroscan tech support, "this file format
% must be determined prior to reading the file, i.e. there
% is no flag within the file itself to identify a file as
% 32-bit". In order to read 32-bit data format, optional
% input "fileformat" of "load_neuroscan.m" function must
% be specified to 'cnt32'. For example, you can load data
% using: ns = load_neuroscan('myfile.cnt', 'cnt32'); Type
% "help load_neuroscan" for the usage of "fileformat".
% Because now the continuous data file can be very large,
% you can specify cnt_start_time and cnt_end_time. Their
% units are "second". For example, if you only want to
% extract data from 0 second to 1.998 second, you can type:
% ns = load_neuroscan('myfile.cnt', 'cnt', '', 0, 1.998);
% The actual rounded start_time and end_time will be
% displayed in ns.start_time and ns.end_time.
%
% - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
%
function ns = load_neuroscan(filename, varargin)
if ~exist('filename','var'),
error('Usage: ns=load_neuroscan(filename,[fileformat],[machineformat],[cnt_start_time],[cnt_end_time])');
end
if ~exist(filename,'file')
error([filename, ': Can''t open file']);
end
ext = [];
machineformat = '';
start_time = [];
end_time = [];
if nargin > 1, ext = varargin{1}; end;
if nargin > 2, machineformat = lower(varargin{2}); end;
if nargin > 3, start_time = varargin{3}; end;
if nargin > 4, end_time = varargin{4}; end;
if isempty(ext) | ( ~strcmpi(ext,'avg') & ~strcmpi(ext,'eeg') & ...
~strcmpi(ext,'cnt') & ~strcmpi(ext,'cnt32') )
[tmp1 tmp2 ext] = fileparts(filename);
ext = ext(2:end);
end
if isempty(machineformat)
machineformat = 'ieee-le';
end
fid = fopen(filename,'r',machineformat);
% General header containing information that applies to all channels
% should be at the beginning of a NeuroScan data file. The size of
% general header is 900 bytes.
%
if fid < 0,
msg = sprintf('Cannot open file %s.',filename);
error(msg);
else
ns.SETUP = read_SETUP(fid); % general hdr
end
% Channel-specific header containing information that pertains to
% particular channel should come after general header. The size of
% channel-specific header is 75 bytes per channel.
%
for i = 1:ns.SETUP.nchannels % channel-specific hdr
ns.ELECTLOC(i) = read_ELECTLOC(fid);
end
if strcmpi(ext, 'cnt') | strcmpi(ext, 'cnt32') % read continuous data
if ns.SETUP.savemode ~= 3
error('Invalid continuous NeuroScan data');
end
if strcmpi(ext, 'cnt32')
ns.precision = 'int32';
else
ns.precision = 'int16';
end;
ns.start_time = start_time;
ns.end_time = end_time;
[ns.data start_time end_time] = read_cnt(fid, ns);
ns.start_time = start_time;
ns.end_time = end_time;
% After continuous NeuroScan data, there is an Event Table.
% At the beginning of the event table is a TEEG ("Tagged EEG")
% structure that is defined in the sethead.h file. Following
% this structure is the event table property. There are two
% types of event tables - the first with a minimum of event
% information, and the second with additional behavioral
% information. The Teeg variable in the TEEG structure
% indicates the type of event table. The number of events
% in the event table can be calculated by dividing the size
% variable in the TEEG structure by sizeof(EVENT1) or
% sizeof(EVENT2). The EVENT immediately follow the TEEG.
ns.TEEG = read_TEEG(fid, ns.SETUP); % tag for EEG types
if ns.TEEG.Teeg == 1 % event type 1
event_size = 8;
elseif ns.TEEG.Teeg == 2 % event type 2
event_size = 19;
else
error('Invalid Event table for continuous NeuroScan data');
end
for i = 1:floor(ns.TEEG.Size/event_size) % read event table
ns.EVENT(i) = read_EVENT(fid, ns);
end
elseif strcmpi(ext, 'eeg')
% There are SETUP.compsweeps of data in an .EEG file. Each sweep
% of data consists of a sweep header followed by the EEG data.
%
ns.data = zeros(ns.SETUP.nchannels, ns.SETUP.pnts*ns.SETUP.compsweeps);
for i = 1:ns.SETUP.compsweeps % read SWEEP head
ns.SWEEP(i) = read_SWEEP(fid, ns.SETUP);
% After the sweep header, data is stored as 2-byte integers in
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?