TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
openeep.m
Go to the documentation of this file.
1 function HDR=openeep(HDR,arg2,arg3,arg4,arg5,arg6)
2 % OPENEEP opens EEProbe files (but does not read the data).
3 % However, it is recommended to use SOPEN instead of OPENEEP.
4 %
5 % see also: SLOAD, SOPEN, SREAD, SCLOSE, SEOF, STELL, SSEEK.
6 
7 % $Id: openeep.m 2205 2009-10-27 12:18:15Z schloegl $
8 % Copyright (c) 2007 by Alois Schloegl <a.schloegl@ieee.org>
9 % This is part of the BIOSIG-toolbox http://biosig.sf.net/
10 
11 % This program is free software; you can redistribute it and/or
12 % modify it under the terms of the GNU General Public License
13 % as published by the Free Software Foundation; either version 2
14 % of the License, or (at your option) any later version.
15 %
16 % This program is distributed in the hope that it will be useful,
17 % but WITHOUT ANY WARRANTY; without even the implied warranty of
18 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 % GNU General Public License for more details.
20 %
21 % You should have received a copy of the GNU General Public License
22 % along with this program; if not, write to the Free Software
23 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 
25 if strcmp(HDR.TYPE,'EEProbe-CNT'),
26 
27  HDR.FILE.FID = fopen(HDR.FileName,'rb');
28  H = openiff(HDR.FILE.FID);
29 
30  if isfield(H,'RIFF');
31  HDR.RIFF = H.RIFF;
32  HDR.FILE.OPEN = 1;
33  HDR.Label = {};
34  HDR.PhysDim = {};
35  HDR.SPR = inf;
36  HDR.NRec = 1;
37  HDR.FILE.POS = 0;
38  if ~isfield(HDR.RIFF,'CNT');
39  HDR.TYPE = 'unknown';
40  elseif ~isfield(HDR.RIFF.CNT,'eeph') | ~isfield(HDR.RIFF.CNT,'LIST');
41  HDR.TYPE = 'unknown';
42  else
43  s = char(HDR.RIFF.CNT.eeph);
44  field = '';
45  while ~isempty(s)
46  [line,s] = strtok(s,[10,13]);
47  if strncmp(line,'[Sampling Rate]',15);
48  field = 'SampleRate';
49  elseif strncmp(line,'[Samples]',9);
50  field = 'SPR';
51  elseif strncmp(line,'[Channels]',10);
52  field = 'NS';
53  elseif strncmp(line,'[Basic Channel Data]',20);
54  k = 0;
55  while (k<HDR.NS),
56  [line,s] = strtok(s,[10,13]);
57  if ~strncmp(line,';',1);
58  k = k+1;
59  [num,status,sa]=str2double(line);
60  HDR.Label{k} = sa{1};
61  HDR.PhysDim{k,1} = sa{4};
62  HDR.Cal(k) = num(2)*num(3);
63  end;
64  end;
65  elseif strncmp(line,'[History]',9);
66  [t,s] = strtok(s,[10,13]);
67  while ~strncmp(t,'EOH',3)
68  [t,s] = strtok(s,[10,13]);
69  end;
70  elseif strncmp(line,';',1);
71  elseif strncmp(line,'[',1);
72  field = '';
73  elseif ~isempty(field);
74  [num,status,sa] = str2double(line);
75  if ~status,
76  HDR = setfield(HDR,field,num);
77  field = '';
78  end;
79  end;
80  end;
81 
82  % decode data block
83  HDR.SPR = H.RIFF.CNT.LIST.raw3.ep(1);
84  HDR.NRec = length(H.RIFF.CNT.LIST.raw3.ep)-2;
85 
86 if 0, %try
87  %%% ### FIXME ### %%%
88  %%% decoding of data block
89  %%% this is work in progress.
90  HDR.EEP.epoch_length = H.RIFF.CNT.LIST.raw3.ep(1);
91  HDR.EEP.epoch_start = [H.RIFF.CNT.LIST.raw3.ep(2:end),length(H.RIFF.CNT.LIST.raw3.data)];
92  n = [HDR.EEP.epoch_length,length(H.RIFF.CNT.LIST.raw3.chan)];
93 %dec2bin(double(HDR.RIFF.CNT.LIST.raw3.data(HDR.EEP.epoch_start(1:end-1)+1)),8)
94  %n=n([2,1]);
95  accu = zeros(1,HDR.NS);
96  for k = 1:HDR.NRec,
97  ix1 = HDR.EEP.epoch_start(k)+1;
98  bytes = double(H.RIFF.CNT.LIST.raw3.data(HDR.EEP.epoch_start(k)+1:HDR.EEP.epoch_start(k+1)));
99  ix1 = 1;
100  for ch= 1:HDR.NS,
101  ix1 = ceil(ix1);
102  byte1 = H.RIFF.CNT.LIST.raw3.data(HDR.EEP.epoch_start(k)+ix1);
103  meth = double(bitshift(byte1,-4));
104 
105  if any(meth==[1:3])
106  nbits = bitand(bytes(1),15);
107  nexcbits = bitshift(bytes(2),4);
108  if ~nexcbits, nexcbits = 16; end;
109  y = repmat(NaN,HDR.SPR,1);
110  y(1) = bitshift(bitand(bytes(2),15)*256+bytes(3),4)+bitshift(bytes(4),-4);
111  bitpos = 28;
112  ix1 = ix1 + 3.5;
113  for k1 = 2:HDR.SPR,
114  %ix1 = bitpos/8;
115  ix2 = ix1 + nbits/8;
116  i1 = ceil(ix1);
117  r1 = (ix1-i1+1)*8;
118  i2 = ceil(ix2);
119  r2 = (ix2-i2+1)*8;
120 %[r1,i1,r2,i2],
121  a = bitand(bytes(i1),2^r1-1);
122  a = bitshift(a,16) + bitshift(bytes(i1+1),8) + bytes(i1+2);
123  a = bitshift(a,r2-16-r1);
124  ix1 = ix2;
125  if a>2^(nbits-1)
126  a=a-2^nbits;
127  elseif a==2^(nbits-1)
128  ix2 = ix1 + nexcbits/8;
129  i1 = ceil(ix1);
130  r1 = (ix1-i1+1)*8;
131  i2 = ceil(ix2);
132  r2 = (ix2-i2+1)*8;
133  a = bitand(bytes(i1),2^r1-1);
134  a = bitshift(a,16) + bitshift(bytes(i1+1),8) + bytes(i1+2);
135  a = bitshift(a,r2-16-r1);
136  ix1 = ix2;
137  if a>2^(nexcbits-1)
138  a=a-2^nexcbits;
139  end;
140  end;
141  y(k1)=a;
142  end;
143  %fprintf(HDR.FILE.stderr,'Warning SOPEN(EEProbe): decompression method %i not implented\n',meth);
144  elseif any(meth==[9:11])
145  nbits = bitshift(bitand(bytes(1),15),2) + bitshift(bytes(2),-6);
146  nexcbits = bitand(bytes(2),63);
147  y = repmat(NaN,HDR.SPR,1);
148  y(1) = bytes(3:6)*(256.^[3,2,1,0]');
149  bitpos = 48;
150  ix1 = ix1 + 6;
151  for k1 = 2:HDR.SPR,
152  %ix1 = bitpos/8;
153  ix2 = ix1 + nbits/8;
154  i1 = ceil(ix1);
155  r1 = (ix1-i1+1)*8;
156  i2 = ceil(ix2);
157  r2 = (ix2-i2+1)*8;
158 %[r1,i1,r2,i2],
159  a = bitand(bytes(i1),2^r1-1);
160  a = bitshift(a,32) + bitshift(bytes(i1+1),24) + bitshift(bytes(i1+2),16) + bitshift(bytes(i1+3),8) + bytes(i1+4);
161  a = bitshift(a,r2-32-r1);
162  ix1 = ix2;
163  if a>2^(nbits-1)
164  a=a-2^nbits;
165  elseif a==2^(nbits-1)
166  ix2 = ix1 + nexcbits/8;
167  i1 = ceil(ix1);
168  r1 = (ix1-i1+1)*8;
169  i2 = ceil(ix2);
170  r2 = (ix2-i2+1)*8;
171  a = bitand(bytes(i1),2^r1-1);
172  a = bitshift(a,16) + bitshift(bytes(i1+1),8) + bytes(i1+2);
173  a = bitshift(a,r2-16-r1);
174  ix1 = ix2;
175  if a>2^(nexcbits-1)
176  a=a-2^nexcbits;
177  end;
178  end;
179  y(k1)=a;
180  end;
181  fprintf(HDR.FILE.stderr,'Warning SOPEN(EEProbe): decompression method %i not implemented\n',meth);
182  error('####');
183  elseif meth==0,
184  y = bytes(ix1+(1:2:HDR.SPR*2));
185  y = y - 256*(y>127);
186  y = 256*y + bytes(ix1+(2:2:HDR.SPR*2));
187  ix1 = ix1 + 1 + 2 * HDR.SPR;
188  elseif meth==8,
189  y = bytes(ix1+(1:HDR.SPR*4));
190  y(1:4:end) = y(1:4:end) - 256*(y(1:4:end)>127);
191  y = reshape(y,[4,length(y)/4])'*(256.^[3;2;1;0]);
192  ix1 = ix1 + 1 + 4 * HDR.SPR;
193  else
194  y = repmat(NaN,HDR.SPR,1);
195  %fprintf(HDR.FILE.stderr,'ERROR SOPEN(EEProbe): decompression method %i not supported\n',double(meth));
196  error(sprintf('EEProbe: decompression method %d not supported\n',double(meth)));
197  end;
198 
199  if any(meth==[1,9]);
200  y = reshape(y,[HDR.SPR,1]);
201  y(1,:) = accu(ch)+y(1,:);
202  y = cumsum(y,1);
203  accu(ch) = y(end,:);
204  elseif any(meth==[2,10]);
205  y = reshape(y,[HDR.SPR,1]);
206  y(2,:) = accu(ch)+y(1,:);
207  y(2:end,:) = cumsum(y(2:end,:),1);
208  accu(ch) = y(end,:);
209  elseif any(meth==[3,11]);
210  y = cumsum(reshape(y,[HDR.SPR,1]),2);
211  elseif any(meth==[0,8]);
212  %y = reshape(y,n([2,1]))';
213  y = reshape(y,[HDR.SPR,1]);
214  end;
215  HDR.data(HDR.SPR*(k-1)+1:HDR.SPR*k,H.RIFF.CNT.LIST.raw3.chan(ch)+1) = y;
216  end;
217  end;
218  HDR.Calib = sparse(2:HDR.NS+1,1:HDR.NS,HDR.Cal);
219  HDR.TYPE = 'native';
220 else %catch
221  HDR.Calib = sparse(2:HDR.NS+1,1:HDR.NS,1); % because SREAD uses READ_EEP_CNT.MEX
222 end;
223  end
224  HDR.SPR = HDR.SPR*HDR.NRec;
225  HDR.NRec = 1;
226  end
227 end;
228 
229  % read event file, if applicable
230  fid = 0;
231  if strcmp(HDR.TYPE,'EEProbe-TRG'),
232  fid = fopen(HDR.FileName,'rt');
233  elseif strcmp(HDR.TYPE,'EEProbe-CNT')
234  fid = fopen(fullfile(HDR.FILE.Path,[HDR.FILE.Name,'.trg']),'rt');
235  end;
236  if fid>0,
237  tmp = str2double(fgetl(fid));
238  if ~isnan(tmp(1))
239  HDR.EVENT.SampleRate = 1/tmp(1);
240  N = 0;
241  while ~feof(fid),
242  tmp = fscanf(fid, '%f %d %s', 3);
243  if ~isempty(tmp)
244  N = N + 1;
245  HDR.EVENT.POS(N,1) = round(tmp(1)*HDR.EVENT.SampleRate);
246  HDR.EVENT.TYP(N,1) = 0;
247  %HDR.EVENT.DUR(N,1) = 0;
248  %HDR.EVENT.CHN(N,1) = 0;
249 
250  HDR.EVENT.TeegType{N,1} = char(tmp(3:end));
251  HDR.EVENT.TYP(N,1) = str2double(HDR.EVENT.TeegType{N,1}); % numeric
252  end
253  end;
254  HDR.EVENT.TYP(isnan(HDR.EVENT.TYP))=0;
255  HDR.TRIG = HDR.EVENT.POS(HDR.EVENT.TYP>0);
256  % HDR.EVENT.POS = HDR.EVENT.POS(HDR.EVENT.TYP>0);
257  % HDR.EVENT.TYP = HDR.EVENT.TYP(HDR.EVENT.TYP>0);
258  % HDR.EVENT = rmfield(HDR.EVENT,'TeegType');
259  end;
260  fclose(fid);
261  end;
262 
263  if strcmp(HDR.TYPE,'EEProbe-AVR'),
264  % it appears to be a EEProbe file with an averaged ERP
265  try
266  tmp = read_eep_avr(HDR.FileName);
267  catch
268  fprintf(HDR.FILE.stderr,'ERROR SOPEN (EEProbe): Cannot open EEProbe-file, because read_eep_avr.mex not installed. \n');
269  fprintf(HDR.FILE.stderr,'ERROR SOPEN (EEProbe): see http://www.smi.auc.dk/~roberto/eeprobe/\n');
270  return;
271  end
272 
273  % convert the header information to BIOSIG standards
274  HDR.FILE.FID = 1; % ?
275  HDR.FILE.POS = 0;
276  HDR.NS = tmp.nchan; % number of channels
277  HDR.SampleRate = tmp.rate; % sampling rate
278  HDR.NRec = 1; % it is an averaged ERP, therefore one record
279  HDR.SPR = tmp.npnt; % total number of samples in the file
280  HDR.Dur = tmp.npnt/tmp.rate; % total duration in seconds
281  HDR.Calib = [zeros(1,HDR.NS) ; eye(HDR.NS, HDR.NS)]; % is this correct?
282  HDR.Label = char(tmp.label);
283  HDR.PhysDim = 'uV';
284  HDR.FLAG.UCAL = 1;
285  HDR.FILE.POS = 0;
286  HDR.AS.endpos = HDR.SPR;
287  HDR.Label = tmp.label;
288  HDR.TriggerOffset = 0;
289 
290  HDR.EEP.data = tmp.data';
291  end;
openeep
function openeep(in HDR, in arg2, in arg3, in arg4, in arg5, in arg6)
str2double
function str2double(in s, in cdelim, in rdelim, in ddelim)
openiff
function openiff(in fid, in LEN)