TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
famosopen.m
Go to the documentation of this file.
1 function [HDR]=famosopen(arg1,arg3,arg4,arg5,arg6)
2 % FAMOSOPEN opens FAMOS file
3 % However, it is recommended to use SOPEN instead .
4 % For loading whole data files, use SLOAD.
5 %
6 % see also: SOPEN, SREAD, SSEEK, STELL, SCLOSE, SWRITE, SEOF
7 
8 % HDR=famosopen(HDR);
9 
10 % $Id: famosopen.m 2702 2011-06-08 11:59:38Z schloegl $
11 % Copyright (c) 2007 by Alois Schloegl <a.schloegl@ieee.org>
12 % This is part of the BIOSIG-toolbox http://biosig.sf.net/
13 
14 % This program is free software; you can redistribute it and/or
15 % modify it under the terms of the GNU General Public License
16 % as published by the Free Software Foundation; either version 2
17 % of the License, or (at your option) any later version.
18 %
19 % This program is distributed in the hope that it will be useful,
20 % but WITHOUT ANY WARRANTY; without even the implied warranty of
21 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 % GNU General Public License for more details.
23 %
24 % You should have received a copy of the GNU General Public License
25 % along with this program; if not, write to the Free Software
26 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 
28 if isstruct(arg1),
29  HDR=arg1;
30 else
31  FILENAME=arg1;
32  HDR.FileName = FILENAME;
33  [pfad,file,FileExt] = fileparts(HDR.FileName);
34  HDR.FILE.Name = file;
35  HDR.FILE.Path = pfad;
36  HDR.FILE.Ext = FileExt(2:length(FileExt));
37  HDR.FILE.PERMISSION = 'r';
38  HDR.FILE.stdout = 1;
39  HDR.FILE.stderr = 2;
40 end;
41 
42 
43 if any(HDR.FILE.PERMISSION=='r'),
44  %%%%% READ HEADER
45  HDR.FILE.FID = fopen(HDR.FileName,'r');
46  if HDR.FILE.FID<0,
47  fprintf('Error SOPEN: could not open file %s\n',HDR.FileName);
48  return;
49  end;
50  POS = 0;
51  tline = [];
52  CHAN = 0;
53  HDR.AS.SampleRate =[];
54  while ~feof(HDR.FILE.FID),
55  %POS = ftell(HDR.FILE.FID);
56  [t,count] = fread(HDR.FILE.FID,[1,1024],'*char');
57  tline = [tline,t];
58 
59  while length(tline)>512
60  % extract key
61  ix = find(tline=='|'); ix1=ix(1);
62  tline = tline(ix1+1:end);
63  POS = POS + ix1;
64 
65  % extract key, version, length
66  ix = find(tline==','); ix = ix(1:3);
67 
68  [n,v,s] = str2double(tline(1:ix(3)),',');
69  keylen = n(3);
70  if ix(3)+1+keylen <= length(tline),
71  param = tline(ix(3)+1:ix(3)+1+keylen);
72  else
73  param = tline(ix(1)+1:35);
74  end;
75  tline = tline(ix(3)+1:end);
76  POS = POS + ix(3);
77 
78  fprintf(1,'SOPEN(FAMOS) %i: process "%s %s"\n',POS,s{1},param);
79  if isempty(s),
80  elseif strcmp(s{1},'CF') & (n(2)==2)
81  if ~all(n(2:3)==[1,1])
82  fprintf(HDR.FILE.stdout,'Warning SOPEN(FAMOS): unknown/unsupported(?) version\n');
83  end;
84 
85  elseif strcmp(s{1},'CK') & (n(2)==1)
86  if ~all(n(2:3)==[3,1])
87  fprintf(HDR.FILE.stdout,'Warning SOPEN(FAMOS): file %s was not closed, it is perhaps corrupted\n',HDR.FILE.Name);
88  end;
89 
90  elseif 0, strcmp(s{1},'NO') & (n(2)==1)
91  %[n1,v1,s1] = str2double(param,',')
92 
93  elseif 0, strcmp(s{1},'CT') & (n(2)==1)
94  %[n1,v1,s1] = str2double(param,',')
95 
96  elseif 0, strcmp(s{1},'CB') & (n(2)==1)
97  %[n1,v1,s1] = str2double(param,',')
98 
99  elseif 0, strcmp(s{1},'CI') & (n(2)==1)
100  %[n1,v1,s1] = str2double(param,',')
101 
102  elseif 0, strcmp(s{1},'CG') & (n(2)==1)
103  %[n1,v1,s1] = str2double(param,',')
104 
105  elseif 0, strcmp(s{1},'CD') & (n(2)==1)
106  %[n1,v1,s1] = str2double(param,',')
107 
108  elseif 0, strcmp(s{1},'CC') & (n(2)==1)
109  [n1,v1,s1] = str2double(param,',');
110 % CHAN = CHAN+1;
111 
112  elseif 0, strcmp(s{1},'NT') & (n(2)==1)
113  %[n1,v1,s1] = str2double(param,',')
114 
115  elseif 0, strcmp(s{1},'CZ') & (n(2)==1)
116  %[n1,v1,s1] = str2double(param,',')
117 
118  elseif 0, strcmp(s{1},'CC') & (n(2)==1)
119  %[n1,v1,s1] = str2double(param,',')
120 
121  elseif strcmp(s{1},'CP') & (n(2)==1)
122  [n1,v1,s1] = str2double(param,',');
123  CHAN = n1(1);
124  switch n1(3),
125  case 1, typ=2; % uint8
126  case 2, typ=1; % int8
127  case {3,9,11}, typ=4; % uint16
128  case 4, typ=3; % int16
129  case 5, typ=6; % uint32
130  case 6, typ=5; % int32
131  case 7, typ=16; % float
132  case 8, typ=17; % double
133  case 10,typ=0; % double
134  case 13,typ=511+48; % double
135  otherwise,
136  fprintf(1,'SOPEN(FAMOS): typ not supported (yet)\n');
137  return;
138  end;
139  HDR.GDFTYP(CHAN)=typ;
140  if n1(5),
141  fprintf(1,'SOPEN(FAMOS): mask %i not supported!\n',n1(5));
142  end;
143  if n1(5:8)~=[0,0,1,0],
144  fprintf(1,'SOPEN(FAMOS): mask %i.%i.%i.%i not supported (yet)\n',n1(5:8));
145  end;
146 
147  elseif strcmp(s{1},'Cb') & (n(2)==1)
148  [n1,v1,s1] = str2double(param,',');
149  ch = n1(3);
150  sz = n1(2); % bytes per sample
151  HDR.AS.start(ch) = n1(5);
152  HDR.AS.bytes(ch) = n1(6);
153 
154  elseif 0, strcmp(s{1},'CG') & (n(2)==1)
155  [n1,v1,s1] = str2double(param,',');
156 
157  elseif strcmp(s{1},'CD') & (n(2)==1)
158  [n1,v1,s1] = str2double(param,',');
159  HDR.AS.SampleRate(length(HDR.AS.SampleRate)+1) = 1/n1(1);
160  HDR.AS.PhysDimDeltaT = s1{4};
161 
162  elseif strcmp(s{1},'CR') & (n(2)==1)
163  [n1,v1,s1] = str2double(param,',');
164  if length(s1)<6,
165  HDR.PhysDim{CHAN} = ' ';
166  else
167  HDR.PhysDim{CHAN} = s1{6};
168  end;
169  HDR.LeadIdCode(CHAN) = 0;
170  %HDR.Cal(CHAN) = 1;
171 
172  elseif strcmp(s{1},'CN') & (n(2)==1)
173  [n1,v1,s1] = str2double(param,',')
174  HDR.Label{CHAN} = s1{5};
175 
176  elseif strcmp(s{1},'CS') & (n(2)==1)
177  ix1 = find(tline==','); ix1=min(ix1);
178  HDR.FAMOS.LEN = n(3)- ix1;
179  HDR.FAMOS.POS = POS + ix1;
180 
181  % read data
182  for ch = 1:length(HDR.Label),
183  fseek(HDR.FILE.FID,HDR.FAMOS.POS+HDR.AS.start(ch),'bof');
184  [datatyp,limits,datatypes,numbits,GDFTYP]=gdfdatatype(HDR.GDFTYP(ch));
185  [d,c] = fread(HDR.FILE.FID,[HDR.AS.bytes(ch)*8/numbits,1],datatyp);
186 
187  HDR.data{ch} = d;
188  end;
189 
190  POS = HDR.FAMOS.POS+HDR.FAMOS.LEN+1;
191  fseek(HDR.FILE.FID,POS,'bof');
192  [tline,count] = fread(HDR.FILE.FID,[1,1024],'*char');
193 
194  %idx = s(4);
195  else
196  fprintf(1,'SOPEN(FAMOS) %i: key "%s %s" not supported (yet)\n',POS,s{1},param);
197  end;
198 
199 % tline = tline2;
200  end;
201  end;
202  HDR.NS = CHAN;
203  HDR.FILE.OPEN=1;
204  HDR.FILE.POS =0;
205 end;
gdfdatatype
function gdfdatatype(in GDFTYP)
str2double
function str2double(in s, in cdelim, in rdelim, in ddelim)
famosopen
function famosopen(in arg1, in arg3, in arg4, in arg5, in arg6)