Home > vbmeg > functions > device > eeg > biosemi > vb_openbdf.m

vb_openbdf

PURPOSE ^

vb_openbdf() - Opens an BDF File (European Data Format for Biosignals) in MATLAB (R)

SYNOPSIS ^

function [DAT,H1]=vb_openbdf(FILENAME)

DESCRIPTION ^

 vb_openbdf() - Opens an BDF File (European Data Format for Biosignals) in MATLAB (R)
 
 Usage:
   >> EDF=openedf(FILENAME)

 Note: About EDF -> www.biosemi.com/faq/file_format.htm

 Author: Alois Schloegl, 5.Nov.1998

 See also: readedf()

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % vb_openbdf() - Opens an BDF File (European Data Format for Biosignals) in MATLAB (R)
0002 %
0003 % Usage:
0004 %   >> EDF=openedf(FILENAME)
0005 %
0006 % Note: About EDF -> www.biosemi.com/faq/file_format.htm
0007 %
0008 % Author: Alois Schloegl, 5.Nov.1998
0009 %
0010 % See also: readedf()
0011 
0012 %    Copyright (C) 1997-1998 by Alois Schloegl
0013 %    a.schloegl@ieee.org
0014 %    Ver 2.20     18.Aug.1998
0015 %    Ver 2.21     10.Oct.1998
0016 %    Ver 2.30      5.Nov.1998
0017 %
0018 %    For use under Octave define the following function
0019 % function s=upper(s); s=toupper(s); end;
0020 
0021 % V2.12    Warning for missing Header information
0022 % V2.20    EDF.AS.* changed
0023 % V2.30    EDF.T0 made Y2K compatible until Year 2090
0024 
0025 % This program is free software; you can redistribute it and/or
0026 % modify it under the terms of the GNU General Public License
0027 % as published by the Free Software Foundation; either version 2
0028 % of the  License, or (at your option) any later version.
0029 %
0030 % This program is distributed in the hope that it will be useful,
0031 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0032 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0033 % GNU General Public License for more details.
0034 %
0035 % You should have received a copy of the GNU General Public License
0036 % along with this program; if not, write to the Free Software
0037 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
0038 
0039 % Name changed for bdf files Sept 6,2002  T.S. Lorig
0040 % Header updated for EEGLAB format (update web link too) - Arnaud Delorme 14 Oct 2002
0041 
0042 function [DAT,H1]=vb_openbdf(FILENAME)
0043     
0044 SLASH='/';        % defines Seperator for Subdirectories
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);                     % 8 Byte  Versionsnummer
0073 %if 0 fprintf(2,'LOADEDF: WARNING  Version EDF Format %i',ver); end;
0074 EDF.PID = deblank(H1(9:88));                  % 80 Byte local patient identification
0075 EDF.RID = deblank(H1(89:168));                % 80 Byte local recording identification
0076 %EDF.H.StartDate = H1(169:176);         % 8 Byte
0077 %EDF.H.StartTime = H1(177:184);         % 8 Byte
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 % Y2K compatibility until year 2090
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         % in a future version, this is hopefully not needed
0089 end;
0090 
0091 EDF.HeadLen = str2num(H1(185:192));  % 8 Byte  Length of Header
0092 % reserved = H1(193:236);             % 44 Byte
0093 EDF.NRec = str2num(H1(237:244));     % 8 Byte  # of data records
0094 EDF.Dur = str2num(H1(245:252));      % 8 Byte  # duration of data record in sec
0095 EDF.NS = str2num(H1(253:256));       % 8 Byte  # of signals
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 % check validity of DigMin and DigMax
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 % check validity of PhysMin and PhysMax
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')';    %    samples per data record
0134 EDF.SPR = str2num(setstr(tmp));    %    samples per data record
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 %EDF.Calib=sparse(diag([1; EDF.Cal]));
0147 %EDF.Calib(1,2:EDF.NS+1)=EDF.Off';
0148 
0149 EDF.SampleRate = EDF.SPR / EDF.Dur;
0150 
0151 EDF.FILE.POS = ftell(EDF.FILE.FID);
0152 if EDF.NRec == -1   % unknown record size, determine correct NRec
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);      % write number of records
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);    % Samples per Block
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 %EDF.AS.bi=bi;
0192 EDF.AS.IDX2=idx2;
0193 %EDF.AS.IDX3=idx3;
0194 
0195 
0196 DAT.Head=EDF;
0197 DAT.MX.ReRef=1;
0198 
0199 %DAT.MX=feval('loadxcm',EDF);
0200 
0201 return;

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005