Skip to content

Commit 47fef07

Browse files
committed
Update period calculation logic
1 parent 73cb68e commit 47fef07

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

app/src/main/java/com/googleresearch/capturesync/softwaresync/phasealign/PeriodCalculator.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.util.Collections;
55
import java.util.Comparator;
66
import java.util.List;
7+
import java.util.Timer;
8+
import java.util.TimerTask;
9+
import java.util.concurrent.CountDownLatch;
710
import java.util.stream.Collectors;
811

912
public class PeriodCalculator {
@@ -20,9 +23,18 @@ public long getPeriodNs() throws InterruptedException {
2023
// Start recording timestamps
2124
registeredTimestamps = new ArrayList<>();
2225
shouldRegister = true;
23-
Thread.sleep(CALC_DURATION_MS);
24-
// Stop recording timestamps and calculate period
25-
shouldRegister = false;
26+
final CountDownLatch latch = new CountDownLatch(1);
27+
TimerTask task = new TimerTask() {
28+
public void run() {
29+
// Stop recording timestamps and calculate period
30+
shouldRegister = false;
31+
latch.countDown();
32+
}
33+
};
34+
Timer timer = new Timer("Timer");
35+
36+
timer.schedule(task, CALC_DURATION_MS);
37+
latch.await();
2638
return calcPeriodNsClusters(getDiff(registeredTimestamps));
2739
}
2840

@@ -61,14 +73,10 @@ private long calcPeriodNsMedian(ArrayList<Long> numArray) {
6173
}
6274

6375
private long median(ArrayList<Long> numArray) {
64-
numArray.sort(Comparator.naturalOrder());
65-
double median;
66-
if (numArray.size() % 2 == 0)
67-
median = ((double)numArray.get(numArray.size()/2)
68-
+ (double)numArray.get(numArray.size()/2 - 1))/2;
69-
else
70-
median = (double) numArray.get(numArray.size()/2);
71-
return (long)median;
76+
Collections.sort(numArray);
77+
int middle = numArray.size() / 2;
78+
middle = middle > 0 && middle % 2 == 0 ? middle - 1 : middle;
79+
return numArray.get(middle);
7280
}
7381

7482
public void onFrameTimestamp(long timestampNs) {
@@ -77,6 +85,4 @@ public void onFrameTimestamp(long timestampNs) {
7785
registeredTimestamps.add(timestampNs);
7886
}
7987
}
80-
81-
8288
}

0 commit comments

Comments
 (0)