wolffd@0: function [DAT,S]=readbdf(DAT,Records,Mode)
wolffd@0: % [DAT,signal]=readedf(EDF_Struct,Records)
wolffd@0: % Loads selected Records of an EDF File (European Data Format for Biosignals) into MATLAB
wolffd@0: % About EDF
wolffd@0: %
wolffd@0: % Records1 List of Records for Loading
wolffd@0: % Mode 0 default
wolffd@0: % 1 No AutoCalib
wolffd@0: % Mode+2 Concatanated (channels with lower sampling rate if more than 1 record is loaded)
wolffd@0:
wolffd@0: % Version 2.11
wolffd@0: % 03.02.1998
wolffd@0: % Copyright (c) 1997,98 by Alois Schloegl
wolffd@0: % a.schloegl@ieee.org
wolffd@0:
wolffd@0: % This program is free software; you can redistribute it and/or
wolffd@0: % modify it under the terms of the GNU General Public License
wolffd@0: % as published by the Free Software Foundation; either version 2
wolffd@0: % of the License, or (at your option) any later version.
wolffd@0: %
wolffd@0: % This program is distributed in the hope that it will be useful,
wolffd@0: % but WITHOUT ANY WARRANTY; without even the implied warranty of
wolffd@0: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolffd@0: % GNU General Public License for more details.
wolffd@0: %
wolffd@0: % You should have received a copy of the GNU General Public License
wolffd@0: % along with this program; if not, write to the Free Software
wolffd@0: % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
wolffd@0: %
wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0: % This program has been modified from the original version for .EDF files
wolffd@0: % The modifications are to the number of bytes read on line 53 (from 2 to
wolffd@0: % 3) and to the type of data read - line 54 (from int16 to bit24). Finally the name
wolffd@0: % was changed from readedf to readbdf
wolffd@0: % T.S. Lorig Sept 6, 2002
wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0:
wolffd@0: if nargin<3 Mode=0; end;
wolffd@0:
wolffd@0: EDF=DAT.Head;
wolffd@0: RecLen=max(EDF.SPR);
wolffd@0:
wolffd@0: S=NaN*zeros(RecLen,EDF.NS);
wolffd@0: DAT.Record=zeros(length(Records)*RecLen,EDF.NS);
wolffd@0: DAT.Valid=uint8(zeros(1,length(Records)*RecLen));
wolffd@0: DAT.Idx=Records(:)';
wolffd@0:
wolffd@0: for nrec=1:length(Records),
wolffd@0:
wolffd@0: NREC=(DAT.Idx(nrec)-1);
wolffd@0: if NREC<0 fprintf(2,'Warning READEDF: invalid Record Number %i \n',NREC);end;
wolffd@0:
wolffd@0: fseek(EDF.FILE.FID,(EDF.HeadLen+NREC*EDF.AS.spb*3),'bof');
wolffd@0: [s, count]=fread(EDF.FILE.FID,EDF.AS.spb,'bit24');
wolffd@0:
wolffd@0: S(EDF.AS.IDX2)=s;
wolffd@0:
wolffd@0: %%%%% Test on Over- (Under-) Flow
wolffd@0: % V=sum([(S'==EDF.DigMax(:,ones(RecLen,1))) + (S'==EDF.DigMin(:,ones(RecLen,1)))])==0;
wolffd@0: V=sum([(S(:,EDF.Chan_Select)'>=EDF.DigMax(EDF.Chan_Select,ones(RecLen,1))) + ...
wolffd@0: (S(:,EDF.Chan_Select)'<=EDF.DigMin(EDF.Chan_Select,ones(RecLen,1)))])==0;
wolffd@0: EDF.ERROR.DigMinMax_Warning(find(sum([(S'>EDF.DigMax(:,ones(RecLen,1))) + (S'0))=1;
wolffd@0: % invalid=[invalid; find(V==0)+l*k];
wolffd@0:
wolffd@0: if floor(Mode/2)==1
wolffd@0: for k=1:EDF.NS,
wolffd@0: DAT.Record(nrec*EDF.SPR(k)+(1-EDF.SPR(k):0),k)=S(1:EDF.SPR(k),k);
wolffd@0: end;
wolffd@0: else
wolffd@0: DAT.Record(nrec*RecLen+(1-RecLen:0),:)=S;
wolffd@0: end;
wolffd@0:
wolffd@0: DAT.Valid(nrec*RecLen+(1-RecLen:0))=V;
wolffd@0: end;
wolffd@0: if rem(Mode,2)==0 % Autocalib
wolffd@0: DAT.Record=[ones(RecLen*length(Records),1) DAT.Record]*EDF.Calib;
wolffd@0: end;
wolffd@0:
wolffd@0: DAT.Record=DAT.Record';
wolffd@0: return;
wolffd@0: