-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntools_elec_calc_mesogrid.m
133 lines (104 loc) · 3.86 KB
/
ntools_elec_calc_mesogrid.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
function elec = ntools_elec_calc_mesogrid(elec,subjectpath)
% calculate grid 64-128 for PMT model 2110-128-021
mg = strncmpi('MG',elec(:,5),1);
if any(mg)
elec_regular_grid = elec(~mg,:);
elec = elec(mg,:);
name = regexpi(elec(:,1),'[A-Za-z]*[^\d*]','match');
name = unique([name{:}]','stable');
elec_mg = cell(length(name),5); l = 0;
for i=1:length(name)
tf = strncmpi(name{i},elec(:,1),length(name{i}));
% G01~G64
elec_pos_mg = cell2mat(elec(tf,2:4));
% determine the hemisphere that grid locates
if elec_pos_mg(:,1)>0
sph = 'rh';
elseif elec_pos_mg(:,1)<0
sph = 'lh';
else
error(['Grid looks across the hemisphere ', name{i}]);
end
%%
% interpolate to 15*15
[r,c] = meshgrid(1:8,1:8);
[rr,cc] = meshgrid(1:0.5:8,1:0.5:8);
X = interp2(r,c,reshape(elec_pos_mg(:,1),8,8)',rr,cc,'spline');
Y = interp2(r,c,reshape(elec_pos_mg(:,2),8,8)',rr,cc,'spline');
Z = interp2(r,c,reshape(elec_pos_mg(:,3),8,8)',rr,cc,'spline');
% find G65~G128
ind = false(15,15);
ind(1,[2,4,6]) = 1; % g65~g67
ind(2,1:6) = 1; % g68~g73
ind(3,[2,4,6]) = 1; % g74~g76
ind(4,1:9) = 1; % g77~g85
ind(5,2:2:12) = 1; % g86~g91
ind(6,1:14) = 1; % g92~g105
ind(7,2:2:12) = 1; % g106~g111
ind(9,8:2:14) = 1; % g112~g115
ind(10,7:14) = 1; % g116~g123
ind(11,8:2:14) = 1; % g124~g127
ind(12,8) = 1; % g128
xx = X'; xx = xx(:);
yy = Y'; yy = yy(:);
zz = Z'; zz = zz(:);
ind = ind'; ind = ind(:);
xx = xx(ind); yy = yy(ind); zz = zz(ind);
% plot3(xx,yy,zz,'ro'); axis tight; axis equal;
surf = fs_read_surf(fullfile(subjectpath,'surf', [sph '.pial']));
if ~isfield(surf,'coords')
surf.coords = surf.vertices;
end
kk = dsearchn(surf.coords,[xx,yy,zz]);
elec_pos_mg = [elec_pos_mg;surf.coords(kk,:)];
for j = 1:size(elec_pos_mg,1)
elec_mg(l+j,1) = cellstr(sprintf('%s%.3d',char(name{i}),j));
elec_mg(l+j,2:4) = num2cell(elec_pos_mg(j,:));
if j<=64
elec_mg(l+j,5) = {'G'};
else
elec_mg(l+j,5) = {'EG'};
end
end
l = j;
end
elec = [elec_mg;elec_regular_grid];
end
%% map EG to the surface
eg = strncmpi('EG',elec(:,5),1);
if any(eg)
elec_others = elec(~eg,:);
elec = elec(eg,:);
name = regexpi(elec(:,1),'[A-Za-z]*[^\d*]','match');
name = unique([name{:}]','stable');
elec_eg = cell(length(name),5); l = 0;
for i=1:length(name)
tf = strncmpi(name{i},elec(:,1),length(name{i}));
elec_pos_eg = cell2mat(elec(tf,2:4));
% determine the hemisphere that grid locates
if elec_pos_eg(:,1)>0
sph = 'rh';
elseif elec_pos_eg(:,1)<0
sph = 'lh';
else
error(['Grid looks across the hemisphere ', name{i}]);
end
surf = fs_read_surf(fullfile(subjectpath,'surf', [sph '.pial']));
if ~isfield(surf,'coords')
surf.coords = surf.vertices;
end
kk = dsearchn(surf.coords,[elec_pos_eg(:,1),elec_pos_eg(:,2),elec_pos_eg(:,3)]);
elec_pos_eg = surf.coords(kk,:);
for j = 1:size(elec_pos_eg,1)
if any(mg)
elec_eg(l+j,1) = cellstr(sprintf('%s%.3d',char(name{i}),j+64)); % EG index: 065-128
else
elec_eg(l+j,1) = cellstr(sprintf('%s%.3d',char(name{i}),j)); % EG index: 001-128
end
elec_eg(l+j,2:4) = num2cell(elec_pos_eg(j,:));
elec_eg(l+j,5) = {'EG'};
end
l = j;
end
elec = [elec_others; elec_eg];
end