0001 function [event_st, HDR] = read_bdf_marker_chans(bdfFileName)
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 if ~exist(bdfFileName,'file')
0015     error('Could not locate file: %s', bdfFileName)
0016 end
0017 
0018 
0019 
0020 GDFTYPES=[0 1 2 3 4 5 6 7 16 17 18 255+[1 12 22 24] 511+[1 12 22 24]];
0021 
0022 
0023 GDFTYP_BYTE=zeros(1,512+64);
0024 GDFTYP_BYTE(256+(1:64))=(1:64)/8;
0025 GDFTYP_BYTE(512+(1:64))=(1:64)/8;
0026 GDFTYP_BYTE(1:19)=[1 1 1 2 2 4 4 8 8 4 8 0 0 0 0 0 4 8 16]';
0027 
0028 HDR.FileName = bdfFileName;
0029 HDR.FILE.stdout = 1;
0030 HDR.FILE.stderr = 2;
0031 HDR.FILE.PERMISSION='r';
0032 
0033 
0034 HDR = getfiletype(HDR);
0035 [HDR.FILE.FID]=fopen(HDR.FileName,[HDR.FILE.PERMISSION,'b'],'ieee-le');
0036                 
0037 
0038 
0039 [H1,count]=fread(HDR.FILE.FID,[1,192],'uint8');     
0040 if count<192,
0041     HDR.ErrNo = [64,HDR.ErrNo];
0042     return;
0043 end;
0044 H1(193:256)= fread(HDR.FILE.FID,[1,256-192],'uint8');     
0045 H1 = char(H1);
0046 
0047 H2idx = [16 80 8 8 8 8 8 80 8 32];
0048 
0049 HDR.VERSION=char(H1(1:8));                     
0050 if all(abs(H1(1:8))==[255,abs('BIOSEMI')]),
0051     HDR.VERSION = -1;
0052 end
0053 
0054 HDR.HeadLen = str2double(H1(185:192));           
0055 HDR.reserved1=H1(193:236);              
0056 HDR.NRec    = str2double(H1(237:244));     
0057 HDR.Dur     = str2double(H1(245:252));     
0058 HDR.NS      = str2double(H1(253:256));     
0059 HDR.AS.H1 = H1;                         
0060 
0061 idx1=cumsum([0 H2idx]);
0062 idx2=HDR.NS*idx1;
0063 
0064 h2=zeros(HDR.NS,256);
0065 [H2,count]=fread(HDR.FILE.FID,HDR.NS*256,'uint8');
0066 if count < HDR.NS*256
0067     HDR.ErrNo=[8,HDR.ErrNo];
0068     return;
0069 end;
0070 
0071 
0072 tmp = find((H2<32) | ((H2>126) & (H2~=255) & (H2~=181)& (H2~=230)));
0073 if ~isempty(tmp) 
0074     H2(tmp) = 32;
0075     HDR.ErrNo = [1026,HDR.ErrNo];
0076 end;
0077 
0078 for k=1:length(H2idx);
0079     
0080     h2(:,idx1(k)+1:idx1(k+1))=reshape(H2(idx2(k)+1:idx2(k+1)),H2idx(k),HDR.NS)';
0081 end;
0082 
0083 h2=char(h2);
0084 
0085 HDR.Label      =            h2(:,idx1(1)+1:idx1(2));
0086 HDR.Transducer =    cellstr(h2(:,idx1(2)+1:idx1(3)));
0087 HDR.PhysDim    =            h2(:,idx1(3)+1:idx1(4));
0088 HDR.PhysMin    = str2double(h2(:,idx1(4)+1:idx1(5)))';
0089 HDR.PhysMax    = str2double(h2(:,idx1(5)+1:idx1(6)))';
0090 HDR.DigMin     = str2double(h2(:,idx1(6)+1:idx1(7)))';
0091 HDR.DigMax     = str2double(h2(:,idx1(7)+1:idx1(8)))';
0092 HDR.PreFilt    =            h2(:,idx1(8)+1:idx1(9));
0093 HDR.AS.SPR     = str2double(h2(:,idx1(9)+1:idx1(10)));
0094 
0095 if (HDR.VERSION ~= -1),
0096     HDR.GDFTYP     = 3*ones(1,HDR.NS);    
0097 else
0098     HDR.GDFTYP     = (255+24)*ones(1,HDR.NS);    
0099 end;
0100 
0101 HDR.AS.spb = sum(HDR.AS.SPR);    
0102 HDR.AS.bi  = [0;cumsum(HDR.AS.SPR(:))];
0103 HDR.AS.bpb = sum(ceil(HDR.AS.SPR.*GDFTYP_BYTE(HDR.GDFTYP+1)'));    
0104 
0105 HDR.EVENT.POS = [];
0106 HDR.EVENT.TYP = [];
0107 
0108 tmp = strmatch('Status',HDR.Label);
0109 HDR.BDF.Status.Channel = tmp;
0110 
0111 
0112 status = fseek(HDR.FILE.FID,HDR.HeadLen+HDR.AS.bi(HDR.BDF.Status.Channel)*3,'bof');
0113 
0114 
0115 
0116 [t,c] = fread(HDR.FILE.FID,inf,[int2str(HDR.AS.SPR(HDR.BDF.Status.Channel)*3),'*uint8'],HDR.AS.bpb-HDR.AS.SPR(HDR.BDF.Status.Channel)*3);
0117 
0118 
0119 fclose(HDR.FILE.FID);
0120 
0121 
0122 HDR.BDF.ANNONS = reshape(double(t),3,length(t)/3)'*2.^[0;8;16];
0123 
0124 t = HDR.BDF.ANNONS; 
0125 eventVector = bitand(t, hex2dec('00ffff'));
0126 
0127 
0128 
0129 
0130 
0131 bitOffset = 8;
0132 eventVector = bitshift(eventVector,-bitOffset);
0133 
0134 nbits = 8;
0135 for ibit = 1:nbits
0136     onsetMask = diff([0; ~bitget(eventVector,ibit)]) > 0;
0137     if any(onsetMask)
0138         fprintf('Found %d events on bit %d\n', sum(onsetMask), ibit+bitOffset);
0139         HDR.EVENT.POS = [HDR.EVENT.POS; find(onsetMask)];
0140         HDR.EVENT.TYP = [HDR.EVENT.TYP; repmat(2^(ibit+bitOffset-1),sum(onsetMask),1)];
0141     end
0142 end
0143 
0144 
0145 [HDR.EVENT.POS, idx] = sort(HDR.EVENT.POS, 'ascend');
0146 HDR.EVENT.TYP = HDR.EVENT.TYP(idx);
0147 
0148 
0149 event_st = struct('type',[],'latency',[],'duration',[]);
0150 for iev = 1:length(HDR.EVENT.POS)
0151     event_st(iev).type = HDR.EVENT.TYP(iev);
0152     event_st(iev).latency = HDR.EVENT.POS(iev);
0153 end
0154 return