diff --git a/app/src/main/java/xdsopl/robot36/Complex.java b/app/src/main/java/xdsopl/robot36/Complex.java index db9bb71..b7cc70a 100644 --- a/app/src/main/java/xdsopl/robot36/Complex.java +++ b/app/src/main/java/xdsopl/robot36/Complex.java @@ -16,6 +16,14 @@ public class Complex { this.real = real; this.imag = imag; } + Complex set(float real, float imag) { + this.real = real; + this.imag = imag; + return this; + } + Complex set(float real) { + return set(real, 0); + } float norm() { return real * real + imag * imag; } diff --git a/app/src/main/java/xdsopl/robot36/ComplexMovingAverage.java b/app/src/main/java/xdsopl/robot36/ComplexMovingAverage.java new file mode 100644 index 0000000..02e22f2 --- /dev/null +++ b/app/src/main/java/xdsopl/robot36/ComplexMovingAverage.java @@ -0,0 +1,21 @@ +/* +Complex Moving Average + +Copyright 2024 Ahmet Inan +*/ + +package xdsopl.robot36; + +public class ComplexMovingAverage extends ComplexMovingSum { + public ComplexMovingAverage(int length) { + super(length); + } + + public Complex avg() { + return sum().div(length); + } + + public Complex avg(Complex input) { + return sum(input).div(length); + } +} diff --git a/app/src/main/java/xdsopl/robot36/ComplexMovingSum.java b/app/src/main/java/xdsopl/robot36/ComplexMovingSum.java new file mode 100644 index 0000000..49293e9 --- /dev/null +++ b/app/src/main/java/xdsopl/robot36/ComplexMovingSum.java @@ -0,0 +1,38 @@ +/* +Complex Moving Sum + +Copyright 2024 Ahmet Inan +*/ + +package xdsopl.robot36; + +public class ComplexMovingSum { + public final int length; + private final SimpleMovingSum real; + private final SimpleMovingSum imag; + private final Complex temp; + + ComplexMovingSum(int length) { + this.length = length; + this.real = new SimpleMovingSum(length); + this.imag = new SimpleMovingSum(length); + this.temp = new Complex(); + } + + void add(Complex input) { + real.add(input.real); + imag.add(input.imag); + } + + Complex sum() { + temp.real = real.sum(); + temp.imag = imag.sum(); + return temp; + } + + Complex sum(Complex input) { + temp.real = real.sum(input.real); + temp.imag = imag.sum(input.imag); + return temp; + } +} diff --git a/app/src/main/java/xdsopl/robot36/MainActivity.java b/app/src/main/java/xdsopl/robot36/MainActivity.java index 4e320f9..9f1e786 100644 --- a/app/src/main/java/xdsopl/robot36/MainActivity.java +++ b/app/src/main/java/xdsopl/robot36/MainActivity.java @@ -38,7 +38,7 @@ public class MainActivity extends AppCompatActivity { private AudioRecord audioRecord; private TextView status; private SimpleMovingAverage powerAvg; - private SimpleMovingAverage realSyncAvg, imagSyncAvg; + private ComplexMovingAverage syncAvg; private Phasor osc_1200; private Complex sad; private int tint; @@ -62,11 +62,7 @@ public class MainActivity extends AppCompatActivity { private void processSamples() { for (float v : recordBuffer) { - sad.real = v; - sad.imag = 0; - sad.mul(osc_1200.rotate()); - sad.real = realSyncAvg.avg(sad.real); - sad.imag = imagSyncAvg.avg(sad.imag); + sad = syncAvg.avg(sad.set(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) @@ -87,8 +83,7 @@ public class MainActivity extends AppCompatActivity { powerAvg = new SimpleMovingAverage(powerWindowSamples); double syncPulseSeconds = 0.01; int syncPulseSamples = (int) Math.round(syncPulseSeconds * sampleRate); - realSyncAvg = new SimpleMovingAverage(syncPulseSamples); - imagSyncAvg = new SimpleMovingAverage(syncPulseSamples); + syncAvg = new ComplexMovingAverage(syncPulseSamples); osc_1200 = new Phasor(-1200, sampleRate); sad = new Complex(); }