1function [EVENT,cc] = adb2event(fn,Fs)
2% ADB2EVENT loads and artifact scoring file of the
3%   artifact database (ADB) of sleep EEG [1]
4%   and converts the data into event information
5%
6%  [s,H] = sload(filename.edf)
7%  H.EVENT = adb2event([H.FILE.Name,'.txt], H.SampleRate);
8%
9% see also: SOPEN, SREAD, SSEEK, STELL, SCLOSE, SWRITE, SEOF
10%
11% Reference(s):
12% [1] Artifact database of sleep EEG. Available online http://www.dpmi.tu-graz.ac.at/ADB/
13% [2] A. Schlögl, P. Anderer, M.-J. Barbanoj, G. Klösch,G. Gruber, J.L. Lorenzo, O. Filz, M. Koivuluoma, I. Rezek, S.J. Roberts,A. Värri, P. Rappelsberger, G. Pfurtscheller, G. Dorffner
14%       Artifact processing of the sleep EEG in the "SIESTA"-project,
15%       Proceedings EMBEC'99, Part II, pp.1644-1645, 4-7. Nov. 1999,Vienna, Austria.
16% [3] A. Schlögl, P. Anderer, S.J. Roberts, M. Pregenzer, G.Pfurtscheller.
17%       Artefact detection in sleep EEG by the use of Kalman filtering.
18%       Proceedings EMBEC'99, Part II, pp.1648-1649, 4-7. Nov. 1999,Vienna, Austria.
19% [4] A. Schlögl, P. Anderer, M.-J. Barbanoj, G. Dorffner, G. Gruber, G. Klösch, J.L. Lorenzo, P. Rappelsberger, G. Pfurtscheller.
20%       Artifacts in the sleep EEG - A database for the evaluation of automated processing methods.
21%       Proceedings of the Third International Congress of the World Federation of Sleep Research Societies (WFSRS). Editors: H. Schulz. P.L. Parmeggiani, and M. Chase. Sleep Research Online 1999:2 (Supplement 1), p. 586.
22%       available online: http://www.sro.org/cftemplate/wfsrscongress/indiv.cfm?ID=19998586
23
24
25% This program is free software; you can redistribute it and/or
26% modify it under the terms of the GNU General Public License
27% as published by the Free Software Foundation; either version 2
28% of the License, or (at your option) any later version.
29%
30% This program is distributed in the hope that it will be useful,
31% but WITHOUT ANY WARRANTY; without even the implied warranty of
32% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33% GNU General Public License for more details.
34%
35% You should have received a copy of the GNU General Public License
36% along with this program; if not, write to the Free Software
37% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
38
39%	(C) 1997-2004 by Alois Schloegl <alois.schloegl@gmail.com>
40%    	This is part of the BIOSIG-toolbox http://biosig.sf.net/
41
42ma = load(fn);
43
44% code from MAK2BIN.M (C) 1998-2004 A. Schlögl
45ERG = zeros(size(ma));
46
47%%%% one artifact %%%%
48for k=0:9,
49        if exist('OCTAVE_VERSION')==5
50                ERG = ERG+(ma==k)*2^k;
51        else
52                ERG(ma==k) = 2^k;
53        end;
54end;
55
56%%%% more than one artifact %%%%
57[i,j] = find(ma>9);
58L='123456789';
59for k=1:length(i),
60        b=int2str(ma(i(k),j(k)));
61        erg=0;
62        for l=1:9,
63                if any(b==L(l)), erg=erg+2^l; end;
64        end;
65        ERG(i(k),j(k)) = erg;
66end;
67
68N   = 0;
69POS = [];
70TYP = [];
71DUR = [];
72CHN = [];
73cc  = zeros(1,10);
74for k = 1:9,
75        for c = 1:7;%size(ERG,2),
76                tmp = [0;~~(bitand(ERG(:,c),2^k));0];
77
78                cc(k+1) = cc(k+1) + sum(tmp);
79                pos = find(diff(tmp)>0);
80                pos2 = find(diff(tmp)<0);
81                n   = length(pos);
82
83                POS = [POS; pos(:)];
84                TYP = [TYP; repmat(k,n,1)];
85                CHN = [CHN; repmat(c,n,1)];
86                DUR = [DUR; pos2(:)-pos(:)];
87                N   = N + n;
88        end;
89end;
90
91EVENT.Fs = 1;
92if nargin>1,
93        EVENT.Fs = Fs;
94end;
95
96[tmp,ix] = sort(POS);
97EVENT.POS = (POS(ix)-1)*EVENT.Fs+1;
98EVENT.TYP = TYP(ix) + hex2dec('0100');
99EVENT.CHN = CHN(ix);
100EVENT.DUR = DUR(ix)*EVENT.Fs;
101EVENT.N   = N;
102