-
Notifications
You must be signed in to change notification settings - Fork 3
/
loadavg.m
307 lines (271 loc) · 10.7 KB
/
loadavg.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
% loadavg() - loading eeg average data file from Neuroscan into
% matlab.
% Usage:
% >> [signal, variance, chan_names, ...
% pnts, rate, xmin, xmax] = loadavg( filename, version );
% Input:
% filename - input Neuroscan .avg file
% version - [1 or 2] function version. Default is 2 which scales
% the data properly.
%
% Output:
% signal - output signal
% variance - variance of the signal
% chan_names - array that represent the name of the electrodes
% pnts - number of data points
% srate - sampling rate
% xmin - ERP onset time
% xmax - ERP final time
%
% Example:
% % load data into the array named 'signal'
% [signal]=loadavg( 'test.avg' );
% % plot the signal for the first electrode
% plot( signal(1,:) );
%
% Author: Arnaud Delorme (v1), Yang Zhang (v2)
% Copyright (C) 2001 Arnaud Delorme, Salk Institute, [email protected]
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
% Average binary file format
% Average data is stored as 4-byte floats in vectored format for each
% channel. Each channel has a 5-byte header that is no longer used. Thus,
% after the main file header, there is an unused 5-byte header followed by
% erp.pnts of 4-byte floating point numbers for the first channel; then a
% 5-byte header for channel two followed by erp.pnts*sizeof(float) bytes,
% etc. Therefore, the total number of bytes after the main header is:
% erp.nchannels * (5 + erp.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 divide by the number of sweeps in the
% average (i.e., channel[j]->n).
function [signal, variance, chan_names, pnts, rate, xmin, xmax]=loadavg( FILENAME,version)
if nargin<1
help loadavg
return;
end
if nargin < 2 || version == 2
disp('Reading using method 2');
[signal, variance, chan_names, pnts, rate, xmin, xmax]=loadavg_bcl( FILENAME );
signal = signal';
return;
end
disp('Reading using method 1');
%if isempty(find(FILENAME=='.')) FILENAME=[FILENAME '.eeg']; end
BOOL='int16';
ULONG='int32';
FLOAT='float32';
fid=fopen(FILENAME,'r','ieee-le');
if fid<0
fprintf(2,['Error LOADEEG: File ' FILENAME ' not found\n']);
return;
end
S_nsweeps_offset_total = 362;
S_nsweeps_offset_accepted = 364;
S_pnts_offset = 368;
S_nchans_offset = 370;
S_variance_offset = 375;
S_rate_offset = 376;
S_xmin_offset = 505;
S_xmax_offset = 509;
packed_sizeof_SETUP = 900;
% read general part of the erp header and set variables
% -----------------------------------------------------
%erp = fread(fid, 362, 'uchar'); % skip the firsts 368 bytes
%nsweeps = fread(fid, 1, 'ushort'); % number of sweeps
%erp = fread(fid, 4, 'uchar'); % skip 4 bytes
%pnts= fread(fid, 1, 'ushort'); % number of point per waveform
%chan= fread(fid, 1, 'ushort'); % number of channels
%erp = fread(fid, 4, 'uchar'); % skip 4 bytes
%rate= fread(fid, 1, 'ushort'); % sample rate (Hz)
%erp = fread(fid, 127, 'uchar'); % skip 125 bytes
%xmin= fread(fid, 1, 'float32'); % in s
%xmax= fread(fid, 1, 'float32'); % in s
%erp = fread(fid, 387, 'uchar'); % skip 387 bytes
% read # of channels, # of samples, variance flag, and real time bounds
% ---------------------------------------------------------------------
fseek(fid, S_nsweeps_offset_accepted, 'bof'); nsweeps = fread(fid, 1, 'ushort');
if nsweeps == 0
fseek(fid, S_nsweeps_offset_total, 'bof'); nsweeps = fread(fid, 1, 'ushort');
end
fseek(fid, S_pnts_offset, 'bof'); pnts = fread(fid, 1, 'ushort');
fseek(fid, S_nchans_offset, 'bof'); chan = fread(fid, 1, 'ushort');
fseek(fid, S_variance_offset, 'bof'); variance_flag = fread(fid, 1, 'uchar');
fseek(fid, S_rate_offset, 'bof'); rate = fread(fid, 1, 'ushort');
fseek(fid, S_xmin_offset, 'bof'); xmin = fread(fid, 1, 'float32');
fseek(fid, S_xmax_offset, 'bof'); xmax = fread(fid, 1, 'float32');
fseek(fid, packed_sizeof_SETUP, 'bof');
fprintf('number of channels : %d\n', chan);
fprintf('number of points per trial : %d\n', pnts);
fprintf('sampling rate (Hz) : %f\n', rate);
fprintf('xmin (s) : %f\n', xmin);
fprintf('xmax (s) : %f\n', xmax);
fprintf('number of trials (s) : %d\n', nsweeps);
if nsweeps == 0, nsweeps = 1; end
% read electrode configuration
% ----------------------------
fprintf('Electrode configuration\n');
for elec = 1:chan
channel_label_tmp = fread(fid, 10, 'uchar');
chan_names(elec,:) = channel_label_tmp';
for index = 2:9 if chan_names(elec,index) == 0 chan_names(elec,index)=' '; end; end
erp = fread(fid, 47-10, 'uchar');
baseline(elec) = fread(fid, 1, 'ushort');
erp = fread(fid, 10, 'uchar');
sensitivity(elec) = fread(fid, 1, 'float32');
erp = fread(fid, 8, 'uchar');
calib(elec) = fread(fid, 1, 'float32');
fprintf('%s: baseline: %d\tsensitivity: %f\tcalibration: %f\n', ...
char(chan_names(elec,1:4)), baseline(elec), sensitivity(elec), calib(elec));
factor(elec) = calib(elec) * sensitivity(elec) / 204.8;
end
xsize = chan * pnts;
buf_size = chan * pnts ; % size in shorts
count_selected = 1;
fprintf('Reserving array (can take some time)\n');
signal = zeros( chan, pnts*nsweeps);
fprintf('Array reserved, scanning file\n');
signal = zeros(pnts, chan);
variance = zeros(pnts, chan);
for elec = 1:chan
% skip sweeps header and read data
% --------------------------------
fseek(fid, 5, 'cof');
signal(:, elec) = fread(fid, pnts, 'float32') * factor(elec) / nsweeps;
end
if variance_flag
for elec = 1:chan
variance(:, elec) = fread(fid, pnts, 'float32');
end
else
variance = 'novariance';
end;
signal = signal';
variance = variance';
fclose(fid);
return;
function [signal, variance, chan_names, pnts, rate, xmin, xmax]=loadavg_bcl(FILENAME,chanNameList)
%writed by allen zhang based on EEGLAB's loadavg.m
%2009-11-25
% NENU,CHANGCHUN,CHINA
% update by Yang Zhang
%2011-2-28
% NENU,Changchun, CHINA
% revised by Yang Zhang
% 2011-4-6
%using automatic routine to identifiy the type of the avg file to get the true nsweeps parameter
if ~exist('chanNameList','var')
chanNameList={'all'};
end
try
fid=fopen(FILENAME,'r','ieee-le');
% read general part of the erp header and set variables
% -----------------------------------------------------
fseek(fid, 362, 'cof');% skip the firsts 362 from BOF (368 bytes in orginal comment?)
% disp(ftell(fid));% 360 bytes
% hdr.nsweeps = fread(fid, 1, 'ushort');
% disp(ftell(fid));% 362
% hdr.compsweeps = fread(fid, 1, 'ushort');% the exact sweep numbers for eeg and single subject avg file| in grand avg file it represents the number of subjects
% hdr.acceptcnt = fread(fid, 1, 'ushort');% number of accepted sweeps also the exact sweep numbers for grand avg file
% hdr.rejectcnt = fread(fid, 1, 'ushort');%number of rejected sweeps
% disp(ftell(fid));% 368
compsweeps = fread(fid, 1, 'ushort');% the exact sweeps numbers for eeg file| in Grand avg file it represented number of subjects
acceptcnt = fread(fid, 1, 'ushort');% number of accepted sweeps
rejectcnt = fread(fid, 1, 'ushort');%number of rejected sweeps
% determine the type of avg file and choose the right value for nsweeps
if (rejectcnt+acceptcnt)~=compsweeps
disp('It''s a grand average file!!!');
disp(['Subject number = ',num2str(compsweeps),'; sweeps = ',num2str(acceptcnt)]);
nsweeps=compsweeps;
else
disp('It''s a single subject average file!!!');
disp(['nsweeps = ',num2str(compsweeps),'; Accepted sweeps = ',num2str(acceptcnt),'; Rejected sweeps = ',num2str(rejectcnt)]);
nsweeps=acceptcnt;
end
pnts=fread(fid, 1, 'ushort'); % number of point per waveform
chan=fread(fid, 1, 'ushort'); % number of channels
fseek(fid, 3, 'cof'); % skip 3 bytes
variance_flag=fread(fid, 1, 'uchar');
rate=fread(fid, 1, 'ushort'); % sample rate (Hz)
fseek(fid, 127, 'cof'); % skip 127 bytes
xmin=fread(fid, 1, 'float32'); % in s
xmax=fread(fid, 1, 'float32'); % in s
fseek(fid, 387, 'cof'); % skip 387 bytes
% read electrode configuration
% ----------------------------
for elec = 1:chan
channel_label_tmp = fread(fid, 10, 'uchar');
electrodes(elec).tmp=channel_label_tmp;
chan_names(elec,:) = channel_label_tmp'; %#ok<*AGROW>
for index = 2:9
if chan_names(elec,index) == 0
chan_names(elec,index)=' ';
end
end
fseek(fid, 61, 'cof');%skip 61 bytes
electrodes(elec).calib= fread(fid, 1, 'float32');
% erp = fread(fid, 47-10, 'uchar');
% baseline(elec) = fread(fid, 1, 'ushort');
% erp = fread(fid, 10, 'uchar');
% sensitivity(elec) = fread(fid, 1, 'float32');
% erp = fread(fid, 8, 'uchar');
% electrodes(elec).calib= fread(fid, 1, 'float32');
%
% fprintf('%s: baseline: %d\tsensitivity: %f\tcalibration: %f\n', ...
% char(chan_names(elec,1:4)), baseline(elec), sensitivity(elec), electrodes(elec).calib);
end
signal = zeros(pnts, chan);
variance = zeros(pnts, chan);
for elec = 1:chan
% To scale a data point to
% microvolts, multiply by the channel-specific calibration factor (i.e., for electrode j:
% channel[j]->calib) and divide by the number of sweeps in the average (i.e.,
% channel[j]->n);
% skip sweeps header and read data
% --------------------------------
fseek(fid, 5, 'cof');
signal(:, elec) =fread(fid, pnts, 'float32')*electrodes(elec).calib/nsweeps;
end
if variance_flag
for elec = 1:chan
variance(:, elec) = fread(fid, pnts, 'float32')*electrodes(elec).calib/nsweeps;% not sure
end
else
variance = 'novariance';
end
%%
if ~strcmpi(chanNameList{1},'all')
chanIDX=ones(1,chan);
for ichanList=1:numel(chanNameList)
for elec=1:chan
if strcmpi(chanNameList{ichanList},char(chan_names(elec,1:numel(chanNameList{ichanList}))))
chanIDX(elec)=0;
break;
end
end
end
signal=signal(:,~chanIDX);
if variance_flag
variance=variance(:,~chanIDX);
end
chan_names=chan_names(~chanIDX,:);
end
% signal = signal';
% variance = variance';
fclose(fid);
catch errorLOAD
disp(FILENAME);
rethrow(errorLOAD);
end
return;