0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 function [DAT,H1]=vb_openbdf(FILENAME)
0043
0044 SLASH='/';
0045 BSLASH=setstr(92);
0046
0047 cname=computer;
0048 if cname(1:2)=='PC' SLASH=BSLASH; end;
0049
0050 fid=fopen(FILENAME,'r','ieee-le');
0051 if fid<0
0052 fprintf(2,['Error LOADEDF: File ' FILENAME ' not found\n']);
0053 return;
0054 end;
0055
0056 EDF.FILE.FID=fid;
0057 EDF.FILE.OPEN = 1;
0058 EDF.FileName = FILENAME;
0059
0060 PPos=min([max(find(FILENAME=='.')) length(FILENAME)+1]);
0061 SPos=max([0 find((FILENAME=='/') | (FILENAME==BSLASH))]);
0062 EDF.FILE.Ext = FILENAME(PPos+1:length(FILENAME));
0063 EDF.FILE.Name = FILENAME(SPos+1:PPos-1);
0064 if SPos==0
0065 EDF.FILE.Path = pwd;
0066 else
0067 EDF.FILE.Path = FILENAME(1:SPos-1);
0068 end;
0069 EDF.FileName = [EDF.FILE.Path SLASH EDF.FILE.Name '.' EDF.FILE.Ext];
0070
0071 H1=setstr(fread(EDF.FILE.FID,256,'char')');
0072 EDF.VERSION=H1(1:8);
0073
0074 EDF.PID = deblank(H1(9:88));
0075 EDF.RID = deblank(H1(89:168));
0076
0077
0078 EDF.T0=[str2num(H1(168+[7 8])) str2num(H1(168+[4 5])) str2num(H1(168+[1 2])) str2num(H1(168+[9 10])) str2num(H1(168+[12 13])) str2num(H1(168+[15 16])) ];
0079
0080
0081 if EDF.VERSION(1)=='0'
0082 if EDF.T0(1) < 91
0083 EDF.T0(1)=2000+EDF.T0(1);
0084 else
0085 EDF.T0(1)=1900+EDF.T0(1);
0086 end;
0087 else ;
0088
0089 end;
0090
0091 EDF.HeadLen = str2num(H1(185:192));
0092
0093 EDF.NRec = str2num(H1(237:244));
0094 EDF.Dur = str2num(H1(245:252));
0095 EDF.NS = str2num(H1(253:256));
0096
0097 EDF.Label = setstr(fread(EDF.FILE.FID,[16,EDF.NS],'char')');
0098 EDF.Transducer = setstr(fread(EDF.FILE.FID,[80,EDF.NS],'char')');
0099 EDF.PhysDim = setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')');
0100
0101 EDF.PhysMin= str2num(setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')'));
0102 EDF.PhysMax= str2num(setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')'));
0103 EDF.DigMin = str2num(setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')'));
0104 EDF.DigMax = str2num(setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')'));
0105
0106
0107 if (length(EDF.DigMin) ~= EDF.NS)
0108 fprintf(2,'Warning OPENEDF: Failing Digital Minimum\n');
0109 EDF.DigMin = -(2^15)*ones(EDF.NS,1);
0110 end
0111 if (length(EDF.DigMax) ~= EDF.NS)
0112 fprintf(2,'Warning OPENEDF: Failing Digital Maximum\n');
0113 EDF.DigMax = (2^15-1)*ones(EDF.NS,1);
0114 end
0115 if (any(EDF.DigMin >= EDF.DigMax))
0116 fprintf(2,'Warning OPENEDF: Digital Minimum larger than Maximum\n');
0117 end
0118
0119 if (length(EDF.PhysMin) ~= EDF.NS)
0120 fprintf(2,'Warning OPENEDF: Failing Physical Minimum\n');
0121 EDF.PhysMin = EDF.DigMin;
0122 end
0123 if (length(EDF.PhysMax) ~= EDF.NS)
0124 fprintf(2,'Warning OPENEDF: Failing Physical Maximum\n');
0125 EDF.PhysMax = EDF.DigMax;
0126 end
0127 if (any(EDF.PhysMin >= EDF.PhysMax))
0128 fprintf(2,'Warning OPENEDF: Physical Minimum larger than Maximum\n');
0129 EDF.PhysMin = EDF.DigMin;
0130 EDF.PhysMax = EDF.DigMax;
0131 end
0132 EDF.PreFilt= setstr(fread(EDF.FILE.FID,[80,EDF.NS],'char')');
0133 tmp = fread(EDF.FILE.FID,[8,EDF.NS],'char')';
0134 EDF.SPR = str2num(setstr(tmp));
0135
0136 fseek(EDF.FILE.FID,32*EDF.NS,0);
0137
0138 EDF.Cal = (EDF.PhysMax-EDF.PhysMin)./ ...
0139 (EDF.DigMax-EDF.DigMin);
0140 EDF.Off = EDF.PhysMin - EDF.Cal .* EDF.DigMin;
0141 tmp = find(EDF.Cal < 0);
0142 EDF.Cal(tmp) = ones(size(tmp));
0143 EDF.Off(tmp) = zeros(size(tmp));
0144
0145 EDF.Calib=[EDF.Off';(diag(EDF.Cal))];
0146
0147
0148
0149 EDF.SampleRate = EDF.SPR / EDF.Dur;
0150
0151 EDF.FILE.POS = ftell(EDF.FILE.FID);
0152 if EDF.NRec == -1
0153 fseek(EDF.FILE.FID, 0, 'eof');
0154 endpos = ftell(EDF.FILE.FID);
0155 EDF.NRec = floor((endpos - EDF.FILE.POS) / (sum(EDF.SPR) * 2));
0156 fseek(EDF.FILE.FID, EDF.FILE.POS, 'bof');
0157 H1(237:244)=sprintf('%-8i',EDF.NRec);
0158 end;
0159
0160 EDF.Chan_Select=(EDF.SPR==max(EDF.SPR));
0161 for k=1:EDF.NS
0162 if EDF.Chan_Select(k)
0163 EDF.ChanTyp(k)='N';
0164 else
0165 EDF.ChanTyp(k)=' ';
0166 end;
0167 if findstr(upper(EDF.Label(k,:)),'ECG')
0168 EDF.ChanTyp(k)='C';
0169 elseif findstr(upper(EDF.Label(k,:)),'EKG')
0170 EDF.ChanTyp(k)='C';
0171 elseif findstr(upper(EDF.Label(k,:)),'EEG')
0172 EDF.ChanTyp(k)='E';
0173 elseif findstr(upper(EDF.Label(k,:)),'EOG')
0174 EDF.ChanTyp(k)='O';
0175 elseif findstr(upper(EDF.Label(k,:)),'EMG')
0176 EDF.ChanTyp(k)='M';
0177 end;
0178 end;
0179
0180 EDF.AS.spb = sum(EDF.SPR);
0181 bi=[0;cumsum(EDF.SPR)];
0182
0183 idx=[];idx2=[];
0184 for k=1:EDF.NS,
0185 idx2=[idx2, (k-1)*max(EDF.SPR)+(1:EDF.SPR(k))];
0186 end;
0187 maxspr=max(EDF.SPR);
0188 idx3=zeros(EDF.NS*maxspr,1);
0189 for k=1:EDF.NS, idx3(maxspr*(k-1)+(1:maxspr))=bi(k)+ceil((1:maxspr)'/maxspr*EDF.SPR(k));end;
0190
0191
0192 EDF.AS.IDX2=idx2;
0193
0194
0195
0196 DAT.Head=EDF;
0197 DAT.MX.ReRef=1;
0198
0199
0200
0201 return;