-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcxcorrsync.m
77 lines (49 loc) · 1.62 KB
/
mcxcorrsync.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
function dout = mcxcorrsync(d,syncper)
%
% Syncronize streams by cross correlation
%
% d: mocap structure
% syncper: start and end time (in seconds) of sync movement
%
% dout: new mocap struct where all markers are aligned to the first marker
%
%
%todo: option for calculating timeder before sync
%todo: optional reference marker (currently only using first marker)
dout = d;
dout.data = NaN(size(dout.data,1),size(dout.data,2));
syncper = d.freq*syncper; %converting syncper to frame numbers
if ~strcmp(d.type,'norm data')
%todo: option for not calculating norm before sync
d2 = mcnorm(d);
dout.data(:,1) = d.data(:,1);
else
d2 = d;
dout.data(:,1:3) = d.data(:,1:3);
end
%creating NaN matrix (ensures that empty frames becomes NaN after aligning)
for i = 2:d2.nMarkers
%find lag
[r,lags] = xcorr(d2.data(syncper(1):syncper(2),1),d2.data(syncper(1):syncper(2),i));
[~,l ] = max(r);
%%%keeping the plotting code for now for debugging%%%
%subplot(4,5,i)
%plot(lags,r)
%hold all
%scatter(lags(l),r(l))
%hold off
%title(['ab lag: ' num2str(lags(l)) ' samples. corr: ' num2str(r(l)) ''])
%timeshift by estimated lag
if strcmp(d.type,'norm data')
dout.data(trimtorange(d2.nFrames,lags(l)),i) = d.data(trimtorange(d2.nFrames,-lags(l)),i);
else
xyz = i*3-[2 1 0];
dout.data(trimtorange(d2.nFrames,lags(l)),xyz) = d.data(trimtorange(d2.nFrames,-lags(l)),xyz);
end
end
end
function datarange = trimtorange(nFrames,lag)
datarange = (1:nFrames)+lag;
datarange(datarange < 1) = [];
datarange(datarange > nFrames) = [];
end