% function [signal,Fs]=loadebs(FILENAME,CHAN)
% signal=loadebs(FILENAME,CHANNEL)
% Loads one CHANNEL of an EBS File (European Biosignals format) into MATLAB
% About EBS
%
% This Matlab script is Under Construction and Testing
% If anybody is interested in this Converter and/or would like to programm it, contact me.
% Version 0.30
% 03.02.1998
% Copyright (c) 1998 Alois Schloegl
% a.schloegl@ieee.org
% 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.
%
fid=fopen(FILENAME,'r');
if fid<0
fprintf(2,['Error LOADEBS: File ' FILENAME ' not found\n']);
return;
end;
fprintf(2,['Warning LOADEBS: Possible Conflict with little vs. big endian Format\n']);
%%%%% (1) Fixed Header (32 bytes) %%%%%
IDCODE=fread(fid,8,'char'); %
if setstr(IDCODE(1:3)')~='EBS'
fprintf(2,'Error LOADEBS: %s not an EBS-File',FILENAME);
if IDCODE(4:8)~=hex2dec('940a131a0d')
fprintf(2,'Warning LOADEBS: %s may be corrupted',FILENAME);
end; end;
EncodingId=fread(fid,1,'int32'); %
NoChan=fread(fid,1,'uint32'); % Number of Channels
NoS=fread(fid,2,'uint32'); % Number of Samples
if NoS(1)==0 NoS=Nos(2)
else fprintf(2,'Error LOADEBS: FILE too large);
end;
LenData=fread(fid,1,'int64'); % Data Length
%%%%% (2) LOAD Variable Header %%%%%
tag=fread(fid,1,'int32'); % Tag field
while tag~=0
l =fread(fid,1,'int32'); % length of value field
val=setstr(fread(fid,4*l,'char')'); % depends on Tag field
if tag==hex2dec('00000002'), %IGNORE
elseif tag==hex2dec('00000004') PATIENT_NAME=val;
elseif tag==hex2dec('00000006') PATIENT_ID=val;
elseif tag==hex2dec('00000008') PATIENT_BIRTHDAY=val;
elseif tag==hex2dec('0000000a') PATIENT_SEX=val;
elseif tag==hex2dec('0000000c') SHORT_DESCRIPTION=val;
elseif tag==hex2dec('0000000e') DESCRIPTION=val;
elseif tag==hex2dec('00000010') SAMPLE_RATE=str2num(val);
elseif tag==hex2dec('00000012') INSTITUTION=val;
elseif tag==hex2dec('00000014') PROCESSING_HISTORY=val;
elseif tag==hex2dec('00000016') LOCATION_DIAGRAM=val;
elseif tag==hex2dec('00000001') PREFERRED_INTEGER_RANGE=vec2matx(vec2matx(val,2*NoChan),2));
elseif tag==hex2dec('00000003') UNITS=val;
elseif tag==hex2dec('00000005') CHANNEL_DESCRIPTION=val;
elseif tag==hex2dec('00000007') CHANNEL_GROUPS=val;
elseif tag==hex2dec('00000009') EVENTS=val;
elseif tag==hex2dec('0000000b') RECORDING_TIME=val;
elseif tag==hex2dec('0000000d') CHANNEL_LOCATIONS=val;
elseif tag==hex2dec('0000000f') FILTERS=val;
end;
tag=fread(fid,1,'int32'); % Tag field
end;
%%%%% (3) Encoded Signal Data (4*d bytes) %%%%%
%--------------------- Under Construction -----------------------
% If You modify some part of this programm, because You use
% Encoding scheme not implemented till now, send Your code
% to me
if EncodingId==hex2dec('00000000') % TIB_16
fprintf(2,'Error LOADEBS: Not able to load %x Encoding\n',EncodingId);
elseif EncodingId==hex2dec('00000001') % CIB_16
fprintf(2,'Error LOADEBS: Not able to load %x Encoding\n',EncodingId);
elseif EncodingId==hex2dec('00000002') % TIL_16
[signal,NoS]=fread(fid,[NoChan,Inf],'int16');
signal=signal';
elseif EncodingId==hex2dec('00000003') % CIL_16
[signal,count]=fread(fid,[NoS,NoChan],'int16');
elseif EncodingId==hex2dec('00000010') % TI_16D
fprintf(2,'Error LOADEBS: Not able to load %x Encoding\n',EncodingId);
elseif EncodingId==hex2dec('00000011') % CI_16D
fprintf(2,'Error LOADEBS: Not able to load %x Encoding\n',EncodingId);
end;
%%%%% (4) Optional Second Variable Header %%%%%
fclose(fid);