Skip to content

Commit 3dba3ef

Browse files
committed
Update calcPeriodNsClusters
1 parent 8140fb2 commit 3dba3ef

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.googleresearch.capturesync.softwaresync.phasealign;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class PeriodCalculator {
10+
private final static long CALC_DURATION_MS = 5000;
11+
private volatile boolean shouldRegister;
12+
private ArrayList<Long> registeredTimestamps;
13+
14+
public PeriodCalculator() {
15+
registeredTimestamps = new ArrayList<>();
16+
}
17+
18+
// Blocking call, returns 0 in case of error
19+
public long getPeriodNs() throws InterruptedException {
20+
// Start recording timestamps
21+
registeredTimestamps = new ArrayList<>();
22+
shouldRegister = true;
23+
Thread.sleep(CALC_DURATION_MS);
24+
// Stop recording timestamps and calculate period
25+
shouldRegister = false;
26+
// TODO: switch to clusters!
27+
return calcPeriodNsClusters(getDiff(registeredTimestamps));
28+
}
29+
30+
private ArrayList<Long> getDiff(ArrayList<Long> arrayList) {
31+
Long prev = 0L;
32+
ArrayList<Long> result = new ArrayList<>();
33+
for (Long aLong : arrayList) {
34+
if (prev == 0L) {
35+
prev = aLong;
36+
} else {
37+
result.add(aLong - prev);
38+
prev = aLong;
39+
}
40+
}
41+
return result;
42+
}
43+
44+
private long calcPeriodNsClusters(ArrayList<Long> numArray) {
45+
long initEstimate = Collections.min(numArray);
46+
long nClust = Math.round(1.0 * Collections.max(numArray) / initEstimate);
47+
double weightedSum = 0L;
48+
for (int i = 0; i < nClust; i++) {
49+
int finalI = i;
50+
ArrayList<Long> clust = (ArrayList<Long>)numArray.stream().filter(
51+
x -> (x > (finalI + 0.5)*initEstimate) && (x < (finalI + 1.5)*initEstimate)
52+
).collect(Collectors.toList());
53+
if (clust.size() > 0) {
54+
weightedSum += 1.0 * median(clust) / (i + 1) * clust.size();
55+
}
56+
}
57+
return Math.round(weightedSum / numArray.size());
58+
}
59+
60+
private long calcPeriodNsMedian(ArrayList<Long> numArray) {
61+
return median(numArray);
62+
}
63+
64+
private long median(ArrayList<Long> numArray) {
65+
numArray.sort(Comparator.naturalOrder());
66+
double median;
67+
if (numArray.size() % 2 == 0)
68+
median = ((double)numArray.get(numArray.size()/2)
69+
+ (double)numArray.get(numArray.size()/2 - 1))/2;
70+
else
71+
median = (double) numArray.get(numArray.size()/2);
72+
return (long)median;
73+
}
74+
75+
public void onFrameTimestamp(long timestampNs) {
76+
// Register timestamp
77+
if (shouldRegister) {
78+
registeredTimestamps.add(timestampNs);
79+
}
80+
}
81+
82+
83+
}

0 commit comments

Comments
 (0)