-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadjustlocs.m
415 lines (381 loc) · 16.9 KB
/
adjustlocs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
% adjustlocs() - read neuroscan polar location file (.asc)
%
% Usage:
% >> chanlocs = adjustlocs( chanlocs );
% >> chanlocs = adjustlocs( chanlocs, 'key1', val1, 'key2', val2, ...);
%
% Inputs:
% chanlocs - EEGLAB channel location data structure. See
% help readlocs()
%
% Optional inputs:
% 'center' - [cell array] one or several electrode names to compute
% center location (if several electrodes are provided, the
% function use their iso-barycenter). Ex: { 'cz' } or
% { 'fz' 'pz' }.
% 'autocenter' - ['on'|'off'] attempt to automatically detect all symetrical
% electrode in the 10-20 system to compute the center.
% Default: 'on'.
% 'rotate' - [cell array] name and planar angle of landmark electrodes
% to use as template planar rotation. Ex: { 'c3', 90 }.
% 'autorotate' - ['on'|'off'] attempt to automatically detect
% electrode in the 10-20 system to compute the average
% planar rotation. Default 'on'.
% 'scale' - [cell array] name and phi angle of electrodes along
% horizontal central line. Ex: { 'c3' 44 }. Scale uniformly
% all direction. Use 'hscale' and 'vscale' for scaling x and
% y axis independently.
% 'hscale' - [cell array] name and phi angle of electrodes along
% honrizontal central line. Ex: { 'c3' 44 }.
% 'vscale' - [cell array] name and phi angle of one electrodes along
% central vertical axis. Ex: { 'fz' 44 }.
% 'autoscale' - ['on'|'off'] automatic scaling with 10-20 system used as
% reference. Default is 'on'.
% 'uniform' - ['on'|'off'] force the scaling to be uniform along the X
% and the Y axis. Default is 'on'.
% 'coordinates' - ['pol'|'sph'|'cart'] use polar coordinates ('pol'), sperical
% coordinates ('sph') or cartesian ('cart'). Default is 'sph'.
% (Note that using polar and spherical coordinates have the
% same effect, except that units are different).
% Default is 'sph'.
%
% Outputs:
% chanlocs - EEGLAB channel location data structure. See
% help readlocs()
%
% Note: operations are performed in the following order, first re-centering
% then planar rotation and finally sperical re-scaling.
%
% Author: Arnaud Delorme, CNL / Salk Institute, 1 Dec 2003
%
% See also: readlocs()
% Copyright (C) 2003 Arnaud Delorme, Salk Institute, [email protected]
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
function chanlocs = adjustlocs( chanlocs, varargin)
if nargin < 1
help adjustlocs;
return;
end
% check input parameters
% ----------------------
g = finputcheck( varargin, { 'hscale' 'cell' [] {};
'vscale' 'cell' [] {};
'scale' 'cell' [] {};
'center' 'cell' [] {};
'rotate' 'cell' [] {};
'autoscale' 'string' { 'on','off' } 'off';
'autocenter' 'string' { 'on','off' } 'on';
'autorotate' 'string' { 'on','off' } 'on';
'uniform' 'string' { 'on','off' } 'on';
'coordinates' 'string' { 'pol','sph','cart' } 'sph' });
if ischar(g), error(g); end
names = { chanlocs.labels };
% auto center
% -----------
if strcmpi(g.autocenter, 'on') && isempty(g.center)
disp('Reading template 10-20 file');
locs1020 = readlocs('eeglab1020.ced');
% scan electrodes for horiz pos
% -----------------------------
tmpnames = lower(names);
tmpnames1020 = lower({ locs1020.labels });
[tmp indelec] = intersect_bc(tmpnames1020, tmpnames);
% remove non-symetrical electrodes
% --------------------------------
if ~isempty(indelec)
if find(indelec == 79), indelec(end+1) = 80; end; % for Cz
ind2remove = [];
for index = 1:length(indelec)
if mod(indelec(index),2)
if ~ismember(indelec(index)+1, indelec)
ind2remove = [ ind2remove index ];
end
else
if ~ismember(indelec(index)-1, indelec)
ind2remove = [ ind2remove index ];
end
end
end
indelec(ind2remove) = [];
if find(indelec == 80), indelec(end) = []; end; % for Cz
g.center = tmpnames1020(indelec);
end
if isempty(g.center)
disp('No electrodes found for auto-centering')
else
disp([ num2str(length(g.center)) ' landmark electrodes found for position auto-centering' ])
end
end
% auto rotate
% -----------
if strcmpi(g.autorotate, 'on') && isempty(g.rotate)
if exist('locs1020') ~= 1
disp('Reading template 10-20 file');
locs1020 = readlocs('eeglab1020.ced');
end
% scan electrodes for horiz pos
% -----------------------------
tmpnames = lower(names);
tmpnames1020 = lower({ locs1020.labels });
tmptheta1020 = { locs1020.theta };
[tmp indelec] = intersect_bc(tmpnames1020(1:end-1), tmpnames); % do not use cz
g.rotate(1:2:2*length(indelec)) = tmpnames1020 (indelec);
g.rotate(2:2:2*length(indelec)+1) = tmptheta1020(indelec);
if isempty(g.rotate)
disp('No electrodes found for auto planar rotation')
else
disp([ num2str(length(g.rotate)/2) ' landmark electrodes found for auto planar rotation' ])
end
end
% auto scale
% ----------
if strcmpi(g.autoscale, 'on') && isempty(g.hscale) && isempty(g.vscale)
if exist('locs1020') ~= 1
disp('Reading template 10-20 file');
locs1020 = readlocs('eeglab1020.ced');
end
if strcmpi(g.uniform, 'off')
% remove all vertical electrodes for horizontal scaling
% -----------------------------------------------------
theta = [ locs1020.theta ];
indxh = find(abs(theta) == 90);
indxv = union_bc(find(theta == 0) , find(theta == 180));
locs1020horiz = locs1020(indxh);
locs1020vert = locs1020(indxv);
% scan electrodes for horiz pos
% -----------------------------
tmpnames = lower(names);
tmpnames1020 = lower({ locs1020horiz.labels });
tmpradius1020 = { locs1020horiz.radius };
[tmp indelec] = intersect_bc(tmpnames1020, tmpnames);
if isempty(indelec)
disp('No electrodes found for horiz. position spherical re-scaling')
else
disp([ num2str(length(indelec)) ' landmark electrodes found for horiz. position spherical re-scaling' ]);
if strcmpi(g.coordinates, 'cart')
g.hscale(2:2:2*length(indelec)+1) = [ locs1020horiz.Y ];
else
g.hscale(2:2:2*length(indelec)+1) = tmpradius1020(indelec);
end
end
% scan electrodes for vert. pos
% -----------------------------
tmpnames1020 = lower({ locs1020vert.labels });
tmpradius1020 = { locs1020vert.radius };
[tmp indelec] = intersect_bc(tmpnames1020, tmpnames);
if isempty(indelec)
disp('No electrodes found for vertical position spherical re-scaling')
else
disp([ num2str(length(indelec)) ' landmark electrodes found for vertical spherical re-scaling' ]);
g.vscale(1:2:2*length(indelec)) = tmpnames1020 (indelec);
if strcmpi(g.coordinates, 'cart')
g.vscale(2:2:2*length(indelec)+1) = [ locs1020vert.X ];
else
g.vscale(2:2:2*length(indelec)+1) = tmpradius1020(indelec);
end
end;
else
% uniform scaling
% ---------------
tmpnames = lower(names);
tmpnames1020 = lower({ locs1020.labels });
tmpradius1020 = { locs1020.radius };
[tmp indelec] = intersect_bc(tmpnames1020, tmpnames);
if isempty(indelec)
disp('No electrodes found for uniform spherical re-scaling')
else
disp([ num2str(length(indelec)) ' landmark electrodes found for uniform spherical re-scaling' ]);
g.scale(1:2:2*length(indelec)) = tmpnames1020 (indelec);
if strcmpi(g.coordinates, 'cart')
tmpabsxyz = mattocell(abs([ locs1020.X ]+j*[ locs1020.Y ]));
g.scale(2:2:2*length(indelec)+1) = tmpabsxyz(indelec);
else
g.scale(2:2:2*length(indelec)+1) = tmpradius1020(indelec);
end
end
end
if strcmpi(g.coordinates, 'sph')
g.coordinates = 'pol'; % use polar coordinates for scaling
end
end
% get X and Y coordinates
% -----------------------
if strcmpi(g.coordinates, 'sph') || strcmpi(g.coordinates, 'pol')
[X Y] = pol2cart( [ chanlocs.theta ]/180*pi, [ chanlocs.radius ]); Z = 1;
if strcmpi(g.coordinates, 'sph')
X = X/0.25*46;
Y = Y/0.25*46;
end
else
X = [ chanlocs.X ];
Y = [ chanlocs.Y ];
Z = [ chanlocs.Z ];
end
% recenter
% --------
if ~isempty(g.center)
for index = 1:length(g.center)
tmpindex = strmatch( lower(g.center{index}), lower(names), 'exact' );
if isempty(tmpindex)
error(['Electrode ''' g.center{index} ''' not found for re-centering']);
end
indexelec(index) = tmpindex;
end
showmsg('Using electrode', 'for re-centering', g.center);
centerx = mean(X(indexelec));
centery = mean(Y(indexelec));
X = X - centerx;
Y = Y - centery;
end
% planar rotation
% ---------------
if ~isempty(g.rotate)
% find electrodes
% ---------------
clear elec;
for index = 1:2:length(g.rotate)
tmpindex = strmatch( lower(g.rotate{index}), lower(names), 'exact' );
if isempty(tmpindex)
error(['Electrode ''' g.rotate{index} ''' not found for left-right scaling']);
end
elec((index+1)/2) = tmpindex;
end
vals = [ g.rotate{2:2:end} ];
% compute average scaling factor
% ------------------------------
[ allangles tmp ] = cart2pol(X(elec), Y(elec));
allangles = allangles/pi*180;
diffangle = allangles - vals;
%diffangle2 = allangles + vals;
%if abs(diffangle1) > abs(diffangle2), diffangle = diffangle2;
%else diffangle = diffangle1;
%end
tmpind = find(diffangle > 180); diffangle(tmpind) = diffangle(tmpind)-360;
tmpind = find(diffangle < -180); diffangle(tmpind) = diffangle(tmpind)+360;
anglerot = mean(diffangle);
tmpcplx = (X+j*Y)*exp(-j*anglerot/180*pi);
X = real(tmpcplx);
Y = imag(tmpcplx);
showmsg('Using electrode', ['for planar rotation (' num2str(anglerot,2) ' degrees)'], g.rotate(1:2:end));
end
% computing scaling factors
% -------------------------
if ~isempty(g.scale)
% find electrodes
% ---------------
clear elec;
for index = 1:2:length(g.scale)
tmpindex = strmatch( lower(g.scale{index}), lower(names), 'exact' );
if isempty(tmpindex)
error(['Electrode ''' g.scale{index} ''' not found for left-right scaling']);
end
elec((index+1)/2) = tmpindex;
end
vals = [ g.scale{2:2:end} ];
% compute average scaling factor
% ------------------------------
nonzero = find(vals > 0);
hscalefact = mean(abs(Y(elec(nonzero))+j*X(elec(nonzero)))./vals(nonzero)); % *46/0.25; %/44/0.25;
vscalefact = hscalefact;
showmsg('Using electrode', ['for uniform spherical re-scaling (x' num2str(1/hscalefact,4) ')'], g.scale(1:2:end));
else
if ~isempty(g.hscale)
% find electrodes
% ---------------
clear elec;
for index = 1:2:length(g.hscale)
tmpindex = strmatch( lower(g.hscale{index}), lower(names), 'exact' );
if isempty(tmpindex)
error(['Electrode ''' g.hscale{index} ''' not found for left-right scaling']);
end
elec((index+1)/2) = tmpindex;
end
vals = [ g.hscale{2:2:end} ];
showmsg('Using electrode', [ 'for left-right spherical re-scaling (x' num2str(1/hscalefact,4) ')'], g.hscale(1:2:end));
% compute average scaling factor
% ------------------------------
hscalefact = mean(abs(Y(elec))./vals); % *46/0.25; %/44/0.25;
if isempty(g.vscale)
vscalefact = hscalefact;
end
end
if ~isempty(g.vscale)
% find electrodes
% ---------------
clear elec;
for index = 1:2:length(g.vscale)
tmpindex = strmatch( lower(g.vscale{index}), lower(names), 'exact' );
if isempty(tmpindex)
error(['Electrode ''' g.vscale{index} ''' not found for rear-front scaling']);
end
elec((index+1)/2) = tmpindex;
end
vals = [ g.vscale{2:2:end} ];
showmsg('Using electrode', ['for rear-front spherical re-scaling (x' num2str(1/vscalefact,4) ')'], g.vscale(1:2:end));
% compute average scaling factor
% ------------------------------
vscalefact = mean(abs(X(elec))./vals); % *46/0.25; %/44/0.25;
if isempty(g.vscale)
hscalefact = vscalefact;
end
end
end
% uniform?
% --------
if strcmpi(g.uniform, 'on') && ( ~isempty(g.vscale) || ~isempty(g.hscale))
disp('uniform scaling: averaging left-right and rear-front scaling factor');
hscalefact = mean([hscalefact vscalefact]);
vscalefact = hscalefact;
end
% scaling data
% ------------
if ~isempty(g.vscale) || ~isempty(g.hscale) || ~isempty(g.scale)
Y = Y/hscalefact;
X = X/vscalefact;
Z = Z/((hscalefact+vscalefact)/2);
end
% updating structure
% ------------------
if strcmpi(g.coordinates, 'sph') || strcmpi(g.coordinates, 'pol')
[phi,theta] = cart2pol(Y, X);
phi = phi/pi*180;
if strcmpi(g.coordinates, 'pol')
theta = theta/0.25*46;
end
% convert to other types of coordinates
% -------------------------------------
labels = names';
chanlocs = struct('labels', names, 'sph_theta_besa', mattocell(theta), ...
'sph_phi_besa', mattocell(phi), 'sph_radius', { chanlocs.sph_radius });
chanlocs = convertlocs( chanlocs, 'sphbesa2all');
else
for index = 1:length(chanlocs)
chanlocs(index).X = X(index);
chanlocs(index).Y = Y(index);
chanlocs(index).Z = Z(index);
end
chanlocs = convertlocs(chanlocs, 'cart2all');
end
function showmsg(begmsg, endmsg, struct);
if length(struct) <= 1
disp([ begmsg ' ''' struct{1} ''' ' endmsg]);
elseif length(struct) <= 2
disp([ begmsg ' ''' struct{1} ''' and ''' struct{2} ''' ' endmsg]);
elseif length(struct) <= 3
disp([ begmsg ' ''' struct{1} ''', ''' struct{2} ''' and ''' struct{3} ''' ' endmsg]);
else
disp([ begmsg ' ''' struct{1} ''', ''' struct{2} ''', ''' struct{3} ''' ... ' endmsg]);
end