2 % BDF2BIOSIG_EVENTS converts BDF Status channel into BioSig Event codes.
7 % HDR is the header structure generated by SOPEN, SLOAD or mexSLOAD
8 % from loading a Biosemi (BDF) file.
9 % Specifically, HDR.BDF.ANNONS contains the info of the status channel.
11 % determines how the BDF status channel is converted into
12 % the
event table HDR.EVENT. Currently, the following modes are
14 % 1: epoching information is derived from bit17
15 % only lower 8 bits are supported
16 % 2: suggested decoding
if standardized
event codes (according to
17 % .../biosig/doc/eventcodes.txt) are used
18 % 3: Trigger Input 1-15, raising and falling edges
19 % 4: [default] Trigger Input 1-15, raising edges
20 % 5: Trigger input 1-8, raising and falling edges are considered
21 % 6: Trigger input 1-8, only raising edges are considered
22 % 7: bit-based decoding
23 % 99: not recommended, because it could break some functionality in BioSig
25 % HDR.EVENT contains the generated Event table.
27 % see also: doc/eventcodes.txt, doc/header.txt, SOPEN, SLOAD
36 % Copyright (C) 2007,2008,2009,2011 by Alois Schloegl <a.schloegl@ieee.org>
37 % This is part of the BIOSIG-toolbox http:
39 % This library is free software; you can redistribute it and/or
40 % modify it under the terms of the GNU Library General Public
41 % License as published by the Free Software Foundation; either
42 % Version 2 of the License, or (at your option) any later version.
44 % This library is distributed in the hope that it will be useful,
45 % but WITHOUT ANY WARRANTY; without even the implied warranty of
46 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47 % Library General Public License for more details.
49 % You should have received a copy of the GNU Library General Public
50 % License along with this library; if not, write to the
51 % Free Software Foundation, Inc., 59 Temple Place - Suite 330,
52 % Boston, MA 02111-1307, USA.
55 if nargin<2 || isempty(Mode),
59 if ~isfield(HDR,'BDF') || ~isfield(HDR.BDF,'ANNONS')
65 ix1 = diff(
double([0;bitand(t,2^16)])); % start of epoch
66 ix2 = diff(
double([0;bitand(t,2^16-1)])); % labels
68 % defines mapping of the BDF-status channel to BioSig event codes
69 switch Mode, % determines default decoding
72 % epoching information is derived from bit17
73 % only lower 8 bits are supported
74 POS = [find(ix1>0);find(ix2>0);find(ix1<0);find(ix2<0)];
75 TYP = [repmat(hex2dec('7ffe'),sum(ix1>0),1); bitand(t(ix2>0),255); repmat(hex2dec('fffe'),sum(ix1<0),1); bitor(bitand(t(find(ix2<0)-1),255),2^15)];
78 % suggested decoding if standardized event codes (according to
79 % .../biosig/doc/eventcodes.txt) are used
81 TYP = [bitand(t(ix2>0),2^16-1)];
84 % Trigger Input 1-15, raising and falling edges
85 t = bitand(HDR.BDF.ANNONS,2^16-1);
86 t(~~bitand(HDR.BDF.ANNONS,2^16)) = 0;
87 ix2 = diff(
double([0;bitand(t,2^16-1)])); % labels
89 TYP = [t(ix2>0); t(find(ix2<0)-1)+hex2dec('8000')];
92 % Trigger Input 1-15, raising edges
93 t = bitand(HDR.BDF.ANNONS,2^16-1);
94 t(~~bitand(HDR.BDF.ANNONS,2^16)) = 0;
95 ix2 = diff(
double([0;bitand(t,2^16-1)])); % labels
100 % Trigger input 1-8, raising and falling edges are considered
101 t = bitand(HDR.BDF.ANNONS,255); % only bit1-8 are considered, useful if bit9-16 are open/undefined
102 t(~~bitand(HDR.BDF.ANNONS,2^16)) = 0;
103 ix2 = diff(
double([0;bitand(t,2^16-1)])); % labels
105 TYP = [t(ix2>0); t(find(ix2<0)-1)+hex2dec('8000')];
108 % Trigger input 1-8, only raising edges are considered
109 t = bitand(HDR.BDF.ANNONS,255); % only bit1-8 are considered, useful if bit9-16 are open/undefined
110 t(~~bitand(HDR.BDF.ANNONS,2^16)) = 0;
111 ix2 = diff(
double([0;bitand(t,2^16-1)])); % labels
116 %% bit-based decoding
120 t = bitand(HDR.BDF.ANNONS,2^(k-1));
121 t = t~=t(1); % support of low-active and high-active
122 ix2 = diff(
double([0;t])); % labels
123 POS = [POS; find(ix2>0); find(ix2<0)];
124 TYP = [TYP; repmat(k,sum(ix2>0),1); repmat(k+hex2dec('8000'),sum(ix2<0),1)];
125 HDR.EVENT.CodeDesc{k} = sprintf(
'bit %i',k);
128 %% bit-based decoding with only high-active
132 t = bitand(HDR.BDF.ANNONS,2^(k-1));
133 ix2 = diff(
double([0;t]));
134 POS = [POS; find(ix2>0)];
135 TYP = [TYP; repmat(k,sum(ix2>0),1)];
136 HDR.EVENT.CodeDesc{k} = sprintf(
'bit %i',k);
140 % not recommended, because it could
break some functionality in BioSig
141 POS = [find(ix2>0);find(ix2<0)];
142 TYP = [bitand(t(ix2>0),2^16-1); bitor(bitand(t(find(ix2<0)-1),2^16-1),2^15)];
145 fprintf(HDR.FILE.stderr,
'Warning BDF2BIOSIG_EVENTS: Mode BDF:%d not supported\n', Mode);
149 [HDR.EVENT.POS,ix] = sort(POS);
150 HDR.EVENT.TYP = TYP(ix);
153 %%%% BDF Trigger and status
154 t = bitand(HDR.BDF.ANNONS,hex2dec(
'00ffff'));
155 ix = diff(
double([0;t]));
156 HDR.BDF.Trigger.POS = find(ix);
157 HDR.BDF.Trigger.TYP = t(HDR.BDF.Trigger.POS);
159 t = bitand(bitshift(HDR.BDF.ANNONS,-16),hex2dec(
'00ff'));
160 ix = diff(
double([0;t]));
161 HDR.BDF.Status.POS = find(ix);
162 HDR.BDF.Status.TYP = t(HDR.BDF.Status.POS);
164 %HDR.BDF.ANNONS = []; % not needed anymore, saves memory