-
Notifications
You must be signed in to change notification settings - Fork 0
/
alignMtxDir.m
72 lines (58 loc) · 2.03 KB
/
alignMtxDir.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
function [centeredDir, centeredMtx, prefDir, pvalue] = ...
alignMtxDir(mtx, tgtTidx, cardinalDir, prefDirOption, prefDir)
%[centeredDir, centeredMtx] = alignMtx(mtx, tgtTidx, cardinalDir)
% returns a matrix which 2nd dimension is rotated based on its activity
% during tgtTidx in the 1st dimension
%
% INPUTS:
% mtx: [time x directions (x channels)]
% tgtTidx: time index to compute preferred direction
% OUTPUTS:
% centeredDir: ??
% prefDir: direction in [deg]
if nargin < 4 || isempty(prefDirOption)
prefDirOption = 1;
end
centralBin = round(0.5*length(cardinalDir));
centralDir = cardinalDir(centralBin);
nTimes = size(mtx,1);
assert(length(tgtTidx) == length(intersect(1:nTimes, tgtTidx)));
nDir = size(mtx,2);
assert(nDir == length(cardinalDir));
nElements = size(mtx);
dims = length(nElements);
if dims>=3
mtx = reshape(mtx, [nTimes nDir prod(nElements(3:end))]);
end
centeredDir = 180/pi*circ_dist(pi/180*cardinalDir, pi/180*centralDir);
% tgtTimes = intersect(find(kerneltlags>0.03), find(kerneltlags<0.25));
centeredMtx = zeros(size(mtx));
pvalue = nan(1,size(mtx,3));
if nargin < 5
prefDir = nan(1,size(mtx,3));
end
for idata = 1:size(mtx,3)
resp = mean(mtx(tgtTidx,:,idata),1);
if nargin<5
switch prefDirOption
case 0
[~,prefBin] = max(resp);
prefDir(idata) = cardinalDir(prefBin);
case 1
[fitPars, fitErr] ...
= fitoriWrapped(cardinalDir, resp,...
[], [nan nan 0 min(resp) nan],'',20, []);
prefDir(idata) = fitPars(1);
case 2
prefDir(idata) = 180/pi*circ_mean(cardinalDir'*pi/180, resp');
end
end
[~,prefBin] = min(abs(prefDir(idata) - cardinalDir));%NG??
centeredMtx(:,:,idata) = circshift(mtx(:,:,idata), centralBin - prefBin, 2);
if nargout>=4
pvalue(idata) = dirDotProdTest(cardinalDir'*pi/180, resp');
end
end
if dims>=3
centeredMtx = reshape(centeredMtx, [nTimes nDir nElements(3:end)]);
end