Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Setting window size #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions library/src/main/java/com/squareup/seismic/ShakeDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,20 @@ public void setSensitivity(int accelerationThreshold) {
this.accelerationThreshold = accelerationThreshold;
}

/** Sets window size. */
public void setWindowSize(long size) {
queue.setWindowSize(size);
}

/** Queue of samples. Keeps a running average. */
static class SampleQueue {

private static final long DEFAULT_MAX_WINDOW_SIZE = 500000000; // 0.5s
private static final long DEFAULT_MIN_WINDOW_SIZE = DEFAULT_MAX_WINDOW_SIZE >> 1; // 0.25s

/** Window size in ns. Used to compute the average. */
private static final long MAX_WINDOW_SIZE = 500000000; // 0.5s
private static final long MIN_WINDOW_SIZE = MAX_WINDOW_SIZE >> 1; // 0.25s
private long maxWindowSize = DEFAULT_MAX_WINDOW_SIZE;
private long minWindowSize = DEFAULT_MIN_WINDOW_SIZE;

/**
* Ensure the queue size never falls below this size, even if the device
Expand All @@ -138,7 +146,7 @@ static class SampleQueue {
*/
void add(long timestamp, boolean accelerating) {
// Purge samples that proceed window.
purge(timestamp - MAX_WINDOW_SIZE);
purge(timestamp - maxWindowSize);

// Add the sample to the queue.
Sample added = pool.acquire();
Expand Down Expand Up @@ -191,6 +199,15 @@ void purge(long cutoff) {
}
}

/** Changes window size. */
void setWindowSize(long size) {
maxWindowSize = size;
minWindowSize = maxWindowSize >> 1;
if (sampleCount > MIN_QUEUE_SIZE) {
purge(newest.timestamp - maxWindowSize);
}
}

/** Copies the samples into a list, with the oldest entry at index 0. */
List<Sample> asList() {
List<Sample> list = new ArrayList<Sample>();
Expand All @@ -209,7 +226,7 @@ List<Sample> asList() {
boolean isShaking() {
return newest != null
&& oldest != null
&& newest.timestamp - oldest.timestamp >= MIN_WINDOW_SIZE
&& newest.timestamp - oldest.timestamp >= minWindowSize
&& acceleratingCount >= (sampleCount >> 1) + (sampleCount >> 2);
}
}
Expand Down
15 changes: 15 additions & 0 deletions library/src/test/java/com/squareup/seismic/ShakeDetectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@ private void assertContent(ShakeDetector.SampleQueue q, boolean... expected) {
q.clear();
assertThat(q.isShaking()).isFalse();
}

@Test public void testWindowSize() {
ShakeDetector.SampleQueue q = new ShakeDetector.SampleQueue();
q.setWindowSize(4000000000L);
q.add(1000000000L, true);
q.add(2000000000L, true);
q.add(3000000000L, true);
q.add(4000000000L, true);
q.add(5000000000L, true);
q.add(6000000000L, true);
q.add(7000000000L, true);
assertThat(q.asList()).hasSize(5);
q.setWindowSize(1000000000L);
assertThat(q.asList()).hasSize(4); // MIN_QUEUE_SIZE
}
}