diff --git a/app/src/main/java/xdsopl/robot36/Delay.java b/app/src/main/java/xdsopl/robot36/Delay.java new file mode 100644 index 0000000..7aec0a0 --- /dev/null +++ b/app/src/main/java/xdsopl/robot36/Delay.java @@ -0,0 +1,27 @@ +/* +Digital delay line + +Copyright 2024 Ahmet Inan +*/ + +package xdsopl.robot36; + +public class Delay { + public final int length; + private final float[] buf; + private int pos; + + Delay(int length) { + this.length = length; + this.buf = new float[length]; + this.pos = 0; + } + + float push(float input) { + float tmp = buf[pos]; + buf[pos] = input; + if (++pos >= length) + pos = 0; + return tmp; + } +} diff --git a/app/src/main/java/xdsopl/robot36/MainActivity.java b/app/src/main/java/xdsopl/robot36/MainActivity.java index 3a4042d..b2d7293 100644 --- a/app/src/main/java/xdsopl/robot36/MainActivity.java +++ b/app/src/main/java/xdsopl/robot36/MainActivity.java @@ -37,6 +37,7 @@ public class MainActivity extends AppCompatActivity { private float[] recordBuffer; private AudioRecord audioRecord; private TextView status; + private Delay powerDelay; private SimpleMovingAverage powerAvg; private ComplexMovingAverage syncAvg; private Phasor osc_1200; @@ -62,7 +63,7 @@ public class MainActivity extends AppCompatActivity { private void processSamples() { for (float v : recordBuffer) { - sad = syncAvg.avg(sad.set(v).mul(osc_1200.rotate())); + sad = syncAvg.avg(sad.set(powerDelay.push(v)).mul(osc_1200.rotate())); float level = sad.norm() / powerAvg.avg(v * v); int x = Math.min((int) (scopeWidth * level), scopeWidth); for (int i = 0; i < x; ++i) @@ -79,8 +80,9 @@ public class MainActivity extends AppCompatActivity { private void initTools(int sampleRate) { double powerWindowSeconds = 0.5; - int powerWindowSamples = (int) Math.round(powerWindowSeconds * sampleRate); + int powerWindowSamples = (int) Math.round(powerWindowSeconds * sampleRate) | 1; powerAvg = new SimpleMovingAverage(powerWindowSamples); + powerDelay = new Delay((powerWindowSamples - 1) / 2); double syncPulseSeconds = 0.009; int syncPulseSamples = (int) Math.round(syncPulseSeconds * sampleRate); syncAvg = new ComplexMovingAverage(syncPulseSamples);