comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/openbdf.m @ 0:e9a9cd732c1e tip

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