annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/openbdf.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [DAT,H1]=openbdf(FILENAME)
Daniel@0 2 % EDF=openedf(FILENAME)
Daniel@0 3 % Opens an EDF File (European Data Format for Biosignals) in MATLAB (R)
Daniel@0 4 % <A HREF="http://www.medfac.leidenuniv.nl/neurology/knf/kemp/edf.htm">About EDF</A>
Daniel@0 5
Daniel@0 6 % Copyright (C) 1997-1998 by Alois Schloegl
Daniel@0 7 % a.schloegl@ieee.org
Daniel@0 8 % Ver 2.20 18.Aug.1998
Daniel@0 9 % Ver 2.21 10.Oct.1998
Daniel@0 10 % Ver 2.30 5.Nov.1998
Daniel@0 11 %
Daniel@0 12 % For use under Octave define the following function
Daniel@0 13 % function s=upper(s); s=toupper(s); end;
Daniel@0 14
Daniel@0 15 % V2.12 Warning for missing Header information
Daniel@0 16 % V2.20 EDF.AS.* changed
Daniel@0 17 % V2.30 EDF.T0 made Y2K compatible until Year 2090
Daniel@0 18
Daniel@0 19 % This program is free software; you can redistribute it and/or
Daniel@0 20 % modify it under the terms of the GNU General Public License
Daniel@0 21 % as published by the Free Software Foundation; either version 2
Daniel@0 22 % of the License, or (at your option) any later version.
Daniel@0 23 %
Daniel@0 24 % This program is distributed in the hope that it will be useful,
Daniel@0 25 % but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 26 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 27 % GNU General Public License for more details.
Daniel@0 28 %
Daniel@0 29 % You should have received a copy of the GNU General Public License
Daniel@0 30 % along with this program; if not, write to the Free Software
Daniel@0 31 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Daniel@0 32 % Name changed Sept 6,2002 T.S. Lorig
Daniel@0 33
Daniel@0 34 SLASH='/'; % defines Seperator for Subdirectories
Daniel@0 35 BSLASH=setstr(92);
Daniel@0 36
Daniel@0 37 cname=computer;
Daniel@0 38 if cname(1:2)=='PC' SLASH=BSLASH; end;
Daniel@0 39
Daniel@0 40 fid=fopen(FILENAME,'r','ieee-le');
Daniel@0 41 if fid<0
Daniel@0 42 % fprintf(2,['Error LOADEDF: File ' FILENAME ' not found\n']);
Daniel@0 43 % Warning desactivated in MIRtoolbox
Daniel@0 44 return;
Daniel@0 45 end;
Daniel@0 46
Daniel@0 47 EDF.FILE.FID=fid;
Daniel@0 48 EDF.FILE.OPEN = 1;
Daniel@0 49 EDF.FileName = FILENAME;
Daniel@0 50
Daniel@0 51 PPos=min([max(find(FILENAME=='.')) length(FILENAME)+1]);
Daniel@0 52 SPos=max([0 find((FILENAME=='/') | (FILENAME==BSLASH))]);
Daniel@0 53 EDF.FILE.Ext = FILENAME(PPos+1:length(FILENAME));
Daniel@0 54 EDF.FILE.Name = FILENAME(SPos+1:PPos-1);
Daniel@0 55 if SPos==0
Daniel@0 56 EDF.FILE.Path = pwd;
Daniel@0 57 else
Daniel@0 58 EDF.FILE.Path = FILENAME(1:SPos-1);
Daniel@0 59 end;
Daniel@0 60 EDF.FileName = [EDF.FILE.Path SLASH EDF.FILE.Name '.' EDF.FILE.Ext];
Daniel@0 61
Daniel@0 62 H1=setstr(fread(EDF.FILE.FID,256,'char')'); %
Daniel@0 63 EDF.VERSION=H1(1:8); % 8 Byte Versionsnummer
Daniel@0 64 %if 0 fprintf(2,'LOADEDF: WARNING Version EDF Format %i',ver); end;
Daniel@0 65 EDF.PID = deblank(H1(9:88)); % 80 Byte local patient identification
Daniel@0 66 EDF.RID = deblank(H1(89:168)); % 80 Byte local recording identification
Daniel@0 67 %EDF.H.StartDate = H1(169:176); % 8 Byte
Daniel@0 68 %EDF.H.StartTime = H1(177:184); % 8 Byte
Daniel@0 69 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])) ];
Daniel@0 70
Daniel@0 71 % Y2K compatibility until year 2090
Daniel@0 72 if EDF.VERSION(1)=='0'
Daniel@0 73 if EDF.T0(1) < 91
Daniel@0 74 EDF.T0(1)=2000+EDF.T0(1);
Daniel@0 75 else
Daniel@0 76 EDF.T0(1)=1900+EDF.T0(1);
Daniel@0 77 end;
Daniel@0 78 else ;
Daniel@0 79 % in a future version, this is hopefully not needed
Daniel@0 80 end;
Daniel@0 81
Daniel@0 82 EDF.HeadLen = str2num(H1(185:192)); % 8 Byte Length of Header
Daniel@0 83 % reserved = H1(193:236); % 44 Byte
Daniel@0 84 EDF.NRec = str2num(H1(237:244)); % 8 Byte # of data records
Daniel@0 85 EDF.Dur = str2num(H1(245:252)); % 8 Byte # duration of data record in sec
Daniel@0 86 EDF.NS = str2num(H1(253:256)); % 8 Byte # of signals
Daniel@0 87
Daniel@0 88 EDF.Label = setstr(fread(EDF.FILE.FID,[16,EDF.NS],'char')');
Daniel@0 89 EDF.Transducer = setstr(fread(EDF.FILE.FID,[80,EDF.NS],'char')');
Daniel@0 90 EDF.PhysDim = setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')');
Daniel@0 91
Daniel@0 92 EDF.PhysMin= str2num(setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')'));
Daniel@0 93 EDF.PhysMax= str2num(setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')'));
Daniel@0 94 EDF.DigMin = str2num(setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')')); %
Daniel@0 95 EDF.DigMax = str2num(setstr(fread(EDF.FILE.FID,[8,EDF.NS],'char')')); %
Daniel@0 96
Daniel@0 97 % check validity of DigMin and DigMax
Daniel@0 98 if (length(EDF.DigMin) ~= EDF.NS)
Daniel@0 99 fprintf(2,'Warning OPENEDF: Failing Digital Minimum\n');
Daniel@0 100 EDF.DigMin = -(2^15)*ones(EDF.NS,1);
Daniel@0 101 end
Daniel@0 102 if (length(EDF.DigMax) ~= EDF.NS)
Daniel@0 103 fprintf(2,'Warning OPENEDF: Failing Digital Maximum\n');
Daniel@0 104 EDF.DigMax = (2^15-1)*ones(EDF.NS,1);
Daniel@0 105 end
Daniel@0 106 if (any(EDF.DigMin >= EDF.DigMax))
Daniel@0 107 fprintf(2,'Warning OPENEDF: Digital Minimum larger than Maximum\n');
Daniel@0 108 end
Daniel@0 109 % check validity of PhysMin and PhysMax
Daniel@0 110 if (length(EDF.PhysMin) ~= EDF.NS)
Daniel@0 111 fprintf(2,'Warning OPENEDF: Failing Physical Minimum\n');
Daniel@0 112 EDF.PhysMin = EDF.DigMin;
Daniel@0 113 end
Daniel@0 114 if (length(EDF.PhysMax) ~= EDF.NS)
Daniel@0 115 fprintf(2,'Warning OPENEDF: Failing Physical Maximum\n');
Daniel@0 116 EDF.PhysMax = EDF.DigMax;
Daniel@0 117 end
Daniel@0 118 if (any(EDF.PhysMin >= EDF.PhysMax))
Daniel@0 119 fprintf(2,'Warning OPENEDF: Physical Minimum larger than Maximum\n');
Daniel@0 120 EDF.PhysMin = EDF.DigMin;
Daniel@0 121 EDF.PhysMax = EDF.DigMax;
Daniel@0 122 end
Daniel@0 123 EDF.PreFilt= setstr(fread(EDF.FILE.FID,[80,EDF.NS],'char')'); %
Daniel@0 124 tmp = fread(EDF.FILE.FID,[8,EDF.NS],'char')'; % samples per data record
Daniel@0 125 EDF.SPR = str2num(setstr(tmp)); % samples per data record
Daniel@0 126
Daniel@0 127 fseek(EDF.FILE.FID,32*EDF.NS,0);
Daniel@0 128
Daniel@0 129 EDF.Cal = (EDF.PhysMax-EDF.PhysMin)./ ...
Daniel@0 130 (EDF.DigMax-EDF.DigMin);
Daniel@0 131 EDF.Off = EDF.PhysMin - EDF.Cal .* EDF.DigMin;
Daniel@0 132 tmp = find(EDF.Cal < 0);
Daniel@0 133 EDF.Cal(tmp) = ones(size(tmp));
Daniel@0 134 EDF.Off(tmp) = zeros(size(tmp));
Daniel@0 135
Daniel@0 136 EDF.Calib=[EDF.Off';(diag(EDF.Cal))];
Daniel@0 137 %EDF.Calib=sparse(diag([1; EDF.Cal]));
Daniel@0 138 %EDF.Calib(1,2:EDF.NS+1)=EDF.Off';
Daniel@0 139
Daniel@0 140 EDF.SampleRate = EDF.SPR / EDF.Dur;
Daniel@0 141
Daniel@0 142 EDF.FILE.POS = ftell(EDF.FILE.FID);
Daniel@0 143 if EDF.NRec == -1 % unknown record size, determine correct NRec
Daniel@0 144 fseek(EDF.FILE.FID, 0, 'eof');
Daniel@0 145 endpos = ftell(EDF.FILE.FID);
Daniel@0 146 EDF.NRec = floor((endpos - EDF.FILE.POS) / (sum(EDF.SPR) * 2));
Daniel@0 147 fseek(EDF.FILE.FID, EDF.FILE.POS, 'bof');
Daniel@0 148 H1(237:244)=sprintf('%-8i',EDF.NRec); % write number of records
Daniel@0 149 end;
Daniel@0 150
Daniel@0 151 EDF.Chan_Select=(EDF.SPR==max(EDF.SPR));
Daniel@0 152 for k=1:EDF.NS
Daniel@0 153 if EDF.Chan_Select(k)
Daniel@0 154 EDF.ChanTyp(k)='N';
Daniel@0 155 else
Daniel@0 156 EDF.ChanTyp(k)=' ';
Daniel@0 157 end;
Daniel@0 158 if findstr(upper(EDF.Label(k,:)),'ECG')
Daniel@0 159 EDF.ChanTyp(k)='C';
Daniel@0 160 elseif findstr(upper(EDF.Label(k,:)),'EKG')
Daniel@0 161 EDF.ChanTyp(k)='C';
Daniel@0 162 elseif findstr(upper(EDF.Label(k,:)),'EEG')
Daniel@0 163 EDF.ChanTyp(k)='E';
Daniel@0 164 elseif findstr(upper(EDF.Label(k,:)),'EOG')
Daniel@0 165 EDF.ChanTyp(k)='O';
Daniel@0 166 elseif findstr(upper(EDF.Label(k,:)),'EMG')
Daniel@0 167 EDF.ChanTyp(k)='M';
Daniel@0 168 end;
Daniel@0 169 end;
Daniel@0 170
Daniel@0 171 EDF.AS.spb = sum(EDF.SPR); % Samples per Block
Daniel@0 172 bi=[0;cumsum(EDF.SPR)];
Daniel@0 173
Daniel@0 174 idx=[];idx2=[];
Daniel@0 175 for k=1:EDF.NS,
Daniel@0 176 idx2=[idx2, (k-1)*max(EDF.SPR)+(1:EDF.SPR(k))];
Daniel@0 177 end;
Daniel@0 178 maxspr=max(EDF.SPR);
Daniel@0 179 idx3=zeros(EDF.NS*maxspr,1);
Daniel@0 180 for k=1:EDF.NS, idx3(maxspr*(k-1)+(1:maxspr))=bi(k)+ceil((1:maxspr)'/maxspr*EDF.SPR(k));end;
Daniel@0 181
Daniel@0 182 %EDF.AS.bi=bi;
Daniel@0 183 EDF.AS.IDX2=idx2;
Daniel@0 184 %EDF.AS.IDX3=idx3;
Daniel@0 185
Daniel@0 186
Daniel@0 187 DAT.Head=EDF;
Daniel@0 188 DAT.MX.ReRef=1;
Daniel@0 189
Daniel@0 190 %DAT.MX=feval('loadxcm',EDF);
Daniel@0 191
Daniel@0 192 return;