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