-
Notifications
You must be signed in to change notification settings - Fork 1
/
align_events_robust.m
149 lines (135 loc) · 3.85 KB
/
align_events_robust.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
function [x_aligned, y_aligned, missing_x, missing_y] = align_events_robust(x,y)
% align two timeseries of irregular, discrete events based on inter-event
% timings, allowing for missing events in x, y or a combination of both
% intended use-case is aligning EEG triggers to a log file where the
% trigger cable was a bit shite and sometimes shorted and sometimes just
% didn't transmit a pulse at all.
% So there is a monotonic function taking index in x to index in y, but not
% nccessarily linear.
% inputs
% x,y are both timeseries with each element representing sample number of a
% discrete event. length(x) doesn't have to equal length(y)
% outputs
% x_aligned and y_aligned are subsets of x and y where a matching was found
% between events following optimisation.
% indoces of missing events (wrt
% original timeseries) are returned in missing_x and missing_y
if size(x,2)>size(x,1)
x=x';
end
if size(y,2)>size(y,1)
y=y';
end
diffx = diff(x);
diffy = diff(y);
figure(99)
subplot(4,1,1)
plot([x x],[0 1])
xlim([min([x;y]) max([x;y])])
subplot(4,1,2)
plot([y y],[0 1])
xlim([min([x;y]) max([x;y])])
% traverse the matrix
done=0; step=0;i=1; j=1;
while ~done
if i==size(diffx,1) || j==size(diffy,1)
done=1
print('finished traverse')
break
end
step=step+1;
path(step,1)=i;
path(step,2)=j;
pathcost(step)=diffx(i)-diffy(j);
% find best next step
costs(1) = diffx(i+1)+diffx(i)-diffy(j); % skip element in x
costs(2) = diffx(i)-diffy(j)-diffy(j+1); % skip element in y
costs(3) = diffx(i+1)-diffy(j+1); % continue diagonally
[min_cost, direction] = min(abs(costs));
switch(direction)
case 1
i=i+1;
case 2
j=j+1;
case 3
i=i+1; j=j+1;
end
end
end %fn
% function [x_aligned, y_aligned, missing_x, missing_y] = align_events_robust(x,y)
% % align two timeseries of irregular, discrete events based on inter-event
% % timings, allowing for missing events in x, y or a combination of both
%
% % intended use-case is aligning EEG triggers to a log file where the
% % trigger cable was a bit shite and sometimes shorted and sometimes just
% % didn't transmit a pulse at all.
% % So there is a monotonic function taking index in x to index in y, but not
% % nccessarily linear.
%
% % inputs
% % x,y are both timeseries with each element representing sample number of a
% % discrete event. length(x) doesn't have to equal length(y)
%
% % outputs
% % x_aligned and y_aligned are subsets of x and y where a matching was found
% % between events following optimisation.
% % indoces of missing events (wrt
% % original timeseries) are returned in missing_x and missing_y
%
% if size(x,2)>size(x,1)
% x=x';
% end
% if size(y,2)>size(y,1)
% y=y';
% end
%
% xdiffs = diff(x);
% ydiffs = diff(y);
% figure(99)
% subplot(4,1,1)
% plot([x x],[0 1])
% xlim([min([x;y]) max([x;y])])
% subplot(4,1,2)
% plot([y y],[0 1])
% xlim([min([x;y]) max([x;y])])
%
%
%
% for i= 1:length(xdiffs)
% dists(:,i) = ydiffs - xdiffs(i);
% end
%
% % traverse the matrix
% done=0; step=0;i=1; j=1;
% while ~done
%
% if i==size(dists,1) || j==size(dists ,2)
% done=1
% print('finished traverse')
% break
% end
%
% step=step+1;
% path(step,1)=i;
% path(step,2)=j;
% pathcost(step)=dists(i,j);
%
%
%
% % find best next step
% costs(1) = dists(i+1,j); % increment i only
% costs(2) = dists(i,j+1); % increment j only
% costs(3) = dists(i+1,j+1); % increment i and j
% [min_cost, direction] = min(abs(costs));
% switch(direction)
% case 1
% i=i+1;
% case 2
% j=j+1;
% case 3
% i=i+1; j=j+1;
% end
%
% end
%
% end %fn