-
Notifications
You must be signed in to change notification settings - Fork 97
/
ea_reconstruct_coords.m
71 lines (57 loc) · 2.09 KB
/
ea_reconstruct_coords.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
function [coords,goodz]=ea_reconstruct_coords(trajectory,trajvector,options)
[cimat,reldist]=ea_sample_cuboid(trajectory,options);
vizz=0;
switch options.subj.postopModality
case 'MRI'
headtemp=ea_load_nii(fullfile(ea_getearoot,'templates',filesep,'electrode_contacts','mr','template.nii'));
case 'CT'
headtemp=ea_load_nii(fullfile(ea_getearoot,'templates',filesep,'electrode_contacts','ct','template.nii'));
end
htemp=headtemp.img;
temp=ea_nanmean(htemp(:,:,:,:),4);
% calculate cross-correlation series for all x and y values within the cuboid volume.
disp('Calculating cross-correlation series for all x and y values within the cuboid volume.');
cnt=1;
corrs=zeros(size(cimat,1)*size(cimat,2),299);
for xx=1:size(cimat,1)
for yy=1:size(cimat,2)
[corrs(cnt,:),lags]=ea_xcorr(squeeze(cimat(xx,yy,:)),squeeze(temp(xx,yy,:)));
cnt=cnt+1;
end
end
corrs=ea_nanmean(corrs);
[maxv,maxi]=max(corrs);
maxi=lags(maxi);
if vizz
figure
subplot(2,1,1);
icimat=cat(3,cimat,zeros(31,31,maxi));
imagesc(squeeze(icimat(15,:,:)));
colormap(gray)
subplot(2,1,2);
itemp=cat(3,zeros(31,31,maxi),temp);
imagesc(squeeze(itemp(15,:,:)));
colormap(gray)
end
try
goodz=maxi+min(trajectory(:,3))+reldist; % -1 because lag-series starts with 0 and not one. +reldist because templates start ~reldist voxels before electrode point.
ea_showdis(['Maximal value was: ',num2str(maxv),'.'],options.verbose);
catch
ea_showdis(['Probably algorithm stopped to early. No guess of electrode heights possible.'],options.verbose);
goodz=min(trajectory(:,3));
maxi=1;
end
if isempty(goodz)
ea_showdis(['Probably algorithm stopped to early. No guess of electrode heights possible.'],options.verbose);
goodz=min(trajectory(:,3));
maxi=1;
end
% error on maxi empty:
if isempty(maxi)
ea_error('Reconstruction failed. Please adjust mask-size and entry-point parameters and re-run reconstruction.');
end
startpt=trajectory(end,:);
ntrajvector=trajvector/norm(trajvector);
for coo=1:4
coords(coo,:)=startpt-ntrajvector*maxi -ntrajvector*((coo-1)*reldist);
end