TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
pop_biosig.m
Go to the documentation of this file.
1 % pop_biosig() - import data files into EEGLAB using BIOSIG toolbox
2 %
3 % Usage:
4 % >> OUTEEG = pop_biosig; % pop up window
5 % >> OUTEEG = pop_biosig( filename, channels, type);
6 %
7 % Inputs:
8 % filename - [string] file name
9 %
10 % Optional inputs:
11 % 'channels' - [integer array] list of channel indices
12 % 'blockrange' - [min max] integer range of data blocks to import, in seconds.
13 % Entering [0 3] will import the first three blocks of data.
14 % Default is empty -> import all data blocks.
15 % 'importevent' - ['on'|'off'] import events. Default if 'on'.
16 % 'importannot' - ['on'|'off'] import annotations (EDF+ only). Default if 'on'
17 % 'blockepoch' - ['on'|'off'] force importing continuous data. Default is 'on'
18 % 'ref' - [integer] channel index or index(s) for the reference.
19 % Reference channels are not removed from the data,
20 % allowing easy re-referencing. If more than one
21 % channel, data are referenced to the average of the
22 % indexed channels. WARNING! Biosemi Active II data
23 % are recorded reference-free, but LOSE 40 dB of SNR
24 % if no reference is used!. If you do not know which
25 % channel to use, pick one and then re-reference after
26 % the channel locations are read in. {default: none}
27 % 'rmeventchan' - ['on'|'off'] remove event channel after event
28 % extraction. Default is 'on'.
29 %
30 % Outputs:
31 % OUTEEG - EEGLAB data structure
32 %
33 % Author: Arnaud Delorme, SCCN, INC, UCSD, Oct. 29, 2003-
34 %
35 % Note: BIOSIG toolbox must be installed. Download BIOSIG at
36 % http://biosig.sourceforge.net
37 % Contact a.schloegl@ieee.org for troubleshooting using BIOSIG.
38 
39 % Copyright (C) 2003 Arnaud Delorme, SCCN, INC, UCSD, arno@salk.edu
40 %
41 % This program is free software; you can redistribute it and/or modify
42 % it under the terms of the GNU General Public License as published by
43 % the Free Software Foundation; either version 2 of the License, or
44 % (at your option) any later version.
45 %
46 % This program is distributed in the hope that it will be useful,
47 % but WITHOUT ANY WARRANTY; without even the implied warranty of
48 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 % GNU General Public License for more details.
50 %
51 % You should have received a copy of the GNU General Public License
52 % along with this program; if not, write to the Free Software
53 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
54 
55 function [EEG, command, dat] = pop_biosig(filename, varargin);
56 EEG = [];
57 command = '';
58 
59 if nargin < 1
60  % ask user
61  [filename, filepath] = uigetfile('*.*', 'Choose a data file -- pop_biosig()'); %%% this is incorrect in original version!!!!!!!!!!!!!!
62  drawnow;
63 
64  if filename == 0 return; end;
65  filename = [filepath filename];
66 
67  % look if MEG
68  % -----------
69  if length(filepath)>4
70  if strcmpi(filepath(end-3:end-1), '.ds'), filename = filepath(1:end-1); end;
71  end;
72 
73  % open file to get infos
74  % ----------------------
75  disp('Reading data file header...');
76  dat = sopen(filename);
77  if ~isfield(dat, 'NRec')
78  error('Unsuported data format');
79  end;
80 
81  % special BIOSEMI
82  % ---------------
83  if strcmpi(dat.TYPE, 'BDF')
84  disp('We highly recommend that you choose a reference channel IF these are Biosemi data');
85  disp('(e.g., a mastoid or other channel). Otherwise the data will lose 40 dB of SNR!');
86  end;
87  uilist = { { 'style' 'text' 'String' 'Channel list (defaut all):' } ...
88  { 'style' 'edit' 'string' '' } ...
89  { 'style' 'text' 'String' [ 'Data range (in seconds) to read (default all [0 ' int2str(dat.NRec) '])' ] } ...
90  { 'style' 'edit' 'string' '' } ...
91  { 'style' 'text' 'String' 'Extract event' } ...
92  { 'style' 'checkbox' 'string' '' 'value' 1 'enable' 'on' } {} ...
93  { 'style' 'text' 'String' 'Import anotations (EDF+ only)' } ...
94  { 'style' 'checkbox' 'string' '' 'value' 1 'enable' 'on' } {} ...
95  { 'style' 'text' 'String' 'Force importing continuous data' 'value' 1} ...
96  { 'style' 'checkbox' 'string' '' 'value' 0 } {} ...
97  { 'style' 'text' 'String' 'Reference chan(s) indices - required for BIOSEMI' } ...
98  { 'style' 'edit' 'string' '' } };
99  geom = { [3 1] [3 1] [3 0.35 0.5] [3 0.35 0.5] [3 0.35 0.5] [3 1] };
100 
101  result = inputgui( geom, uilist, 'pophelp(''pop_biosig'')', ...
102  'Load data using BIOSIG -- pop_biosig()');
103  if length(result) == 0 return; end;
104 
105  % decode GUI params
106  % -----------------
107  options = {};
108  if ~isempty(result{1}), options = { options{:} 'channels' eval( [ '[' result{1} ']' ] ) }; end;
109  if ~isempty(result{2}), options = { options{:} 'blockrange' eval( [ '[' result{2} ']' ] ) }; end;
110  if length(result) > 2
111  if ~isempty(result{6}), options = { options{:} 'ref' eval( [ '[' result{6} ']' ] ) }; end;
112  if ~result{3}, options = { options{:} 'importevent' 'off' }; end;
113  if ~result{4}, options = { options{:} 'importannot' 'off' }; end;
114  if result{5}, options = { options{:} 'blockepoch' 'off' }; end;
115  end;
116 else
117  options = varargin;
118 end;
119 
120 % decode imput parameters
121 % -----------------------
122 g = finputcheck( options, { 'blockrange' 'integer' [0 Inf] [];
123  'channels' 'integer' [0 Inf] [];
124  'ref' 'integer' [0 Inf] [];
125  'rmeventchan' 'string' { 'on';'off' } 'on';
126  'importevent' 'string' { 'on';'off' } 'on';
127  'importannot' 'string' { 'on';'off' } 'on';
128  'blockepoch' 'string' { 'on';'off' } 'off' }, 'pop_biosig');
129 if isstr(g), error(g); end;
130 
131 % import data
132 % -----------
133 EEG = eeg_emptyset;
134 if ~isempty(g.channels)
135  dat = sopen(filename, 'r', g.channels,'OVERFLOWDETECTION:OFF');
136 else dat = sopen(filename, 'r', 0,'OVERFLOWDETECTION:OFF');
137 end
138 fprintf('Reading data in %s format...\n', dat.TYPE);
139 
140 if ~isempty(g.blockrange)
141  newblockrange = g.blockrange;
142  newblockrange(2) = min(newblockrange(2), dat.NRec);
143  newblockrange = newblockrange*dat.Dur;
144  DAT=sread(dat, newblockrange(2)-newblockrange(1), newblockrange(1));
145 else
146  DAT=sread(dat, Inf);% this isn't transposed in original!!!!!!!!
147  newblockrange = [];
148 end
149 sclose(dat);
150 
151 if strcmpi(g.blockepoch, 'off')
152  dat.NRec = 1;
153 end;
154 
155 if ~isempty(newblockrange)
156  interval(1) = newblockrange(1) * dat.SampleRate(1) + 1;
157  interval(2) = newblockrange(2) * dat.SampleRate(1);
158 else interval = [];
159 end
160 
161 EEG = biosig2eeglab(dat, DAT, interval, g.channels, strcmpi(g.importevent, 'on'));
162 
163 if strcmpi(g.rmeventchan, 'on') & strcmpi(dat.TYPE, 'BDF') & isfield(dat, 'BDF')
164  if size(EEG.data,1) >= dat.BDF.Status.Channel,
165  disp('Removing event channel...');
166  EEG.data(dat.BDF.Status.Channel,:) = [];
167  if ~isempty(EEG.chanlocs) && length(EEG.chanlocs) >= dat.BDF.Status.Channel
168  EEG.chanlocs(dat.BDF.Status.Channel) = [];
169  end;
170  end;
171  EEG.nbchan = size(EEG.data,1);
172 end;
173 
174 % rerefencing
175 % -----------
176 if ~isempty(g.ref)
177  disp('Re-referencing...');
178  EEG = pop_reref(EEG, g.ref);
179 % EEG.data = EEG.data - repmat(mean(EEG.data(g.ref,:),1), [size(EEG.data,1) 1]);
180 % if length(g.ref) == size(EEG.data,1)
181 % EEG.ref = 'averef';
182 % end;
183 % if length(g.ref) == 1
184 % disp([ 'Warning: channel ' int2str(g.ref) ' is now zeroed (but still present in the data)' ]);
185 % else
186 % disp([ 'Warning: data matrix rank has decreased through re-referencing' ]);
187 % end;
188 end;
189 
190 % test if annotation channel is present
191 % -------------------------------------
192 if isfield(dat, 'EDFplus') && strcmpi(g.importannot, 'on')
193  tmpfields = fieldnames(dat.EDFplus);
194  for ind = 1:length(tmpfields)
195  tmpdat = getfield(dat.EDFplus, tmpfields{ind});
196  if length(tmpdat) == EEG.pnts
197  EEG.data(end+1,:) = tmpdat;
198  EEG.nbchan = EEG.nbchan+1;
199  if ~isempty(EEG.chanlocs)
200  EEG.chanlocs(end+1).labels = tmpfields{ind};
201  end;
202  end;
203  end;
204 end;
205 
206 % convert data to single if necessary
207 % -----------------------------------
208 EEG = eeg_checkset(EEG,'makeur'); % Make EEG.urevent field
209 EEG = eeg_checkset(EEG);
210 
211 % history
212 % -------
213 if isempty(options)
214  command = sprintf('EEG = pop_biosig(''%s'');', filename);
215 else
216  command = sprintf('EEG = pop_biosig(''%s'', %s);', filename, vararg2str(options));
217 end;
sclose
function sclose(in HDR)
eeg_emptyset
function eeg_emptyset()
eeg_checkset
function eeg_checkset(in EEG, in varargin)
biosig2eeglab
function biosig2eeglab(in dat, in DAT, in interval, in channels, in importevent)
sopen
function sopen(in arg1, in PERMISSION, in CHAN, in MODE, in arg5, in arg6)
finputcheck
function finputcheck(in vararg, in fieldlist, in callfunc, in mode, in verbose)
pop_biosig
function pop_biosig(in filename, in varargin)
sread
function sread(in HDR, in NoS, in StartPos)