TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
biosig2eeglab.m
Go to the documentation of this file.
1 % biosig2eeglab() - convert BIOSIG structue to EEGLAB structure
2 %
3 % Usage:
4 % >> OUTEEG = pop_biosig2eeglab(hdr, data, interval);
5 %
6 % Inputs:
7 % hdr - BIOSIG header
8 % data - BIOSIG data array
9 %
10 % Optional input:
11 % interval - BIOSIG does not remove event which are outside of
12 % the data range when importing data range subsets. This
13 % parameter helps fix this problem.
14 %
15 % Outputs:
16 % OUTEEG - EEGLAB data structure
17 %
18 % Author: Arnaud Delorme, SCCN, INC, UCSD, Oct. 29, 2009-
19 %
20 % Note: BIOSIG toolbox must be installed. Download BIOSIG at
21 % http://biosig.sourceforge.net
22 % Contact a.schloegl@ieee.org for troubleshooting using BIOSIG.
23 
24 % Copyright (C) 2003 Arnaud Delorme, SCCN, INC, UCSD, arno@salk.edu
25 %
26 % This program is free software; you can redistribute it and/or modify
27 % it under the terms of the GNU General Public License as published by
28 % the Free Software Foundation; either version 2 of the License, or
29 % (at your option) any later version.
30 %
31 % This program is distributed in the hope that it will be useful,
32 % but WITHOUT ANY WARRANTY; without even the implied warranty of
33 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 % GNU General Public License for more details.
35 %
36 % You should have received a copy of the GNU General Public License
37 % along with this program; if not, write to the Free Software
38 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 
40 function EEG = biosig2eeglab(dat, DAT, interval, channels, importevent);
41 
42 if nargin < 2
43  help biosig2eeglab;
44  return;
45 end;
46 if nargin < 3
47  interval = [];
48 end;
49 if nargin < 4
50  channels = [];
51 end;
52 if nargin < 5
53  importevent = 0;
54 end;
55 
56 % import data
57 % -----------
58 DAT = DAT';
59 EEG = eeg_emptyset;
60 
61 % convert to seconds for sread
62 % ----------------------------
63 EEG.nbchan = size(DAT,1);
64 EEG.srate = dat.SampleRate(1);
65 EEG.data = DAT;
66 clear DAT;
67 % try % why would you do the following??????? JO
68 % EEG.data = EEG.data';
69 % catch,
70 % pack;
71 % EEG.data = EEG.data';
72 % end;
73 EEG.setname = sprintf('%s file', dat.TYPE);
74 EEG.comments = [ 'Original file: ' dat.FileName ];
75 EEG.xmin = 0;
76 nepoch = dat.NRec;
77 EEG.trials = nepoch;
78 EEG.pnts = size(EEG.data,2)/nepoch;
79 
80 if isfield(dat, 'Label') & ~isempty(dat.Label)
81  if isstr(dat.Label)
82  EEG.chanlocs = struct('labels', cellstr(char(dat.Label)));
83  else
84  EEG.chanlocs = struct('labels', dat.Label(1:min(length(dat.Label), size(EEG.data,1))));
85  end;
86  if ~isempty(channels)
87  EEG.chanlocs = EEG.chanlocs(channels);
88  end;
89 end
90 EEG = eeg_checkset(EEG);
91 
92 % extract events % this part I totally revamped to work... JO
93 % --------------
94 EEG.event = [];
95 
96 % startval = mode(EEG.data(end,:)); % my code
97 % for p = 2:size(EEG.data,2)-1
98 % [codeout] = code(EEG.data(end,p));
99 % if EEG.data(end,p) > EEG.data(end,p-1) & EEG.data(end,p) >= EEG.data(end,p+1)
100 % EEG.event(end+1).latency = p;
101 % EEG.event(end).type = bitand(double(EEG.data(end,p)-startval),255);
102 % end;
103 % end;
104 
105 % lastout = mod(EEG.data(end,1),256);newevs = []; % andrey's code 8 bits
106 % codeout = mod(EEG.data(end,2),256);
107 % for p = 2:size(EEG.data,2)-1
108 % nextcode = mod(EEG.data(end,p+1),256);
109 % if codeout > lastout & codeout >= nextcode
110 % newevs = [newevs codeout];
111 % EEG.event(end+1).latency = p;
112 % EEG.event(end).type = codeout;
113 % end;
114 % lastout = codeout;
115 % codeout = nextcode;
116 % end;
117 
118 %lastout = mod(EEG.data(end,1),256*256);newevs = []; % andrey's code 16 bits
119 %codeout = mod(EEG.data(end,2),256*256);
120 %for p = 2:size(EEG.data,2)-1
121 % nextcode = mod(EEG.data(end,p+1),256*256);
122 % if (codeout > lastout) & (codeout >= nextcode)
123 % newevs = [newevs codeout];
124 % EEG.event(end+1).latency = p;
125 % EEG.event(end).type = codeout;
126 % end;
127 % lastout = codeout;
128 % codeout = nextcode;
129 %end;
130 
131 if importevent
132  if isfield(dat, 'BDF')
133  if dat.BDF.Status.Channel <= size(EEG.data,1)
134  EEG.data(dat.BDF.Status.Channel,:) = [];
135  end;
136  EEG.nbchan = size(EEG.data,1);
137  if ~isempty(EEG.chanlocs) && dat.BDF.Status.Channel <= length(EEG.chanlocs)
138  EEG.chanlocs(dat.BDF.Status.Channel,:) = [];
139  end;
140  elseif isempty(dat.EVENT.POS)
141  disp('Extracting events from last EEG channel...');
142  %Modifieded by Andrey (Aug.5,2008) to detect all non-zero codes:
143  if length(unique(EEG.data(end, 1:100))) > 20
144  disp('Warning: event extraction failure, the last channel contains data');
145  elseif length(unique(EEG.data(end, :))) > 5000
146  disp('Warning: event extraction failure, the last channel contains data');
147  else
148  thiscode = 0;
149  for p = 1:size(EEG.data,2)*size(EEG.data,3)-1
150  prevcode = thiscode;
151  thiscode = mod(EEG.data(end,p),256*256); % andrey's code - 16 bits
152  if (thiscode ~= 0) && (thiscode~=prevcode)
153  EEG.event(end+1).latency = p;
154  EEG.event(end).type = thiscode;
155  end;
156  end;
157  EEG.data(end,:) = [];
158  EEG.chanlocs(end) = [];
159  end;
160  % recreate the epoch field if necessary
161  % -------------------------------------
162  if EEG.trials > 1
163  for i = 1:length(EEG.event)
164  EEG.event(i).epoch = ceil(EEG.event(i).latency/EEG.pnts);
165  end;
166  end;
167  EEG = eeg_checkset(EEG, 'eventconsistency');
168  end;
169 
170  if ~isempty(dat.EVENT.POS)
171  if isfield(dat, 'out') % Alois fix for event interval does not work
172  if isfield(dat.out, 'EVENT')
173  dat.EVENT = dat.out.EVENT;
174  end;
175  end;
176  EEG.event = biosig2eeglabevent(dat.EVENT, interval); % Toby's fix
177 
178  % recreate the epoch field if necessary
179  % -------------------------------------
180  if EEG.trials > 1
181  for i = 1:length(EEG.event)
182  EEG.event(i).epoch = ceil(EEG.event(i).latency/EEG.pnts);
183  end;
184  end;
185 
186  EEG = eeg_checkset(EEG, 'eventconsistency');
187  elseif isempty(EEG.event)
188  disp('Warning: no event found. Events might be embeded in a data channel.');
189  disp(' To extract events, use menu File > Import Event Info > From data channel');
190  end;
191 end;
192 
193 % convert data to single if necessary
194 % -----------------------------------
195 EEG = eeg_checkset(EEG,'makeur'); % Make EEG.urevent field
196 EEG = eeg_checkset(EEG);
eeg_checkset
function eeg_checkset(in EEG, in varargin)
biosig2eeglab
function biosig2eeglab(in dat, in DAT, in interval, in channels, in importevent)
biosig2eeglabevent
function biosig2eeglabevent(in EVENT, in interval)