Skip to content

Commit 2ad592a

Browse files
author
celadon
committed
Improve TapLatencyAnalyzer to detect TAP and TONE peaks separately
Signed-off-by: celadon <celadon@intel.com>
1 parent 7234afa commit 2ad592a

1 file changed

Lines changed: 36 additions & 17 deletions

File tree

apps/OboeTester/app/src/main/java/com/mobileer/oboetester/TapLatencyAnalyser.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
package com.mobileer.oboetester;
1717

1818
import java.util.ArrayList;
19-
19+
import android.util.Log;
2020
/**
2121
* Analyze a recording and extract edges for latency analysis.
2222
*/
2323
public class TapLatencyAnalyser {
2424
public static final int TYPE_TAP = 0;
25+
public static final int TYPE_TONE =1;
2526
float[] mHighPassBuffer;
2627

2728
private float mDroop = 0.995f;
@@ -90,25 +91,43 @@ private TapLatencyEvent[] scanForEdges(float[] peakBuffer, int numSamples) {
9091
float fast = 0.0f;
9192
final float slowCoefficient = 0.01f;
9293
final float fastCoefficient = 0.10f;
93-
float lowThreshold = EDGE_THRESHOLD;
94+
final float EDGE_THRESHOLD = 0.01f; // trigger threshold for tap
95+
final float REARM_FRACTION = 0.3f; // fraction of last peak to rearm
96+
final int MIN_TONE_DELAY = 2000;
97+
float lastPeak = 0.0f;
9498
boolean armed = true;
99+
boolean tapDetected = false;
100+
boolean toneDetected = false;
101+
int lastTapIndex = -1;
102+
95103
int sampleIndex = 0;
104+
96105
for (float level : peakBuffer) {
97-
slow = slow + (level - slow) * slowCoefficient; // low pass filter
98-
fast = fast + (level - fast) * fastCoefficient; // low pass filter
99-
if (armed && (fast > EDGE_THRESHOLD) && (fast > (2.0 * slow))) {
100-
events.add(new TapLatencyEvent(TYPE_TAP, sampleIndex));
101-
armed = false;
102-
// Set a new, lower threshold based on the height of the detected peak.
103-
// This allows us to detect a second, smaller peak, but not to trigger
104-
// on the smaller variations that occur after the initial peak.
105-
lowThreshold = fast * LOW_FRACTION;
106-
}
107-
// Use hysteresis when rearming.
108-
if (fast < lowThreshold) {
109-
armed = true;
110-
}
111-
sampleIndex++;
106+
slow = slow + (level - slow) * slowCoefficient; // low pass filter
107+
fast = fast + (level - fast) * fastCoefficient; // low pass filter
108+
if (armed && !tapDetected && (fast > EDGE_THRESHOLD) && (fast > 2.0f * slow)) {
109+
Log.i("TTL", "TTL Event of TYPE_TAP detected at " + sampleIndex + " fast=" + fast);
110+
events.add(new TapLatencyEvent(TYPE_TAP, sampleIndex));
111+
armed = false;
112+
tapDetected = true;
113+
lastPeak = fast;
114+
lastTapIndex = sampleIndex;
115+
}
116+
117+
if (tapDetected && !toneDetected && !armed && (sampleIndex - lastTapIndex) > MIN_TONE_DELAY) {
118+
if ((fast > EDGE_THRESHOLD) && (fast > 1.5f * slow)) {
119+
Log.i("TTL", "TTL Event of TYPE_TONE detected at " + sampleIndex + " fast=" + fast);
120+
events.add(new TapLatencyEvent(TYPE_TONE, sampleIndex));
121+
toneDetected = true;
122+
}
123+
}
124+
125+
// Rearm only when signal has fallen enough
126+
if (!armed && (fast < lastPeak * REARM_FRACTION)) {
127+
Log.i("TTL", "Signal has settled, rearming now (fast=" + fast + ")");
128+
armed = true;
129+
}
130+
sampleIndex++;
112131
}
113132
return events.toArray(new TapLatencyEvent[0]);
114133
}

0 commit comments

Comments
 (0)