added complex moving sum and average

This commit is contained in:
Ahmet Inan 2024-04-15 06:43:28 +02:00
parent 16406fd710
commit 1fb772e346
4 changed files with 70 additions and 8 deletions

View file

@ -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;
}

View file

@ -0,0 +1,21 @@
/*
Complex Moving Average
Copyright 2024 Ahmet Inan <xdsopl@gmail.com>
*/
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);
}
}

View file

@ -0,0 +1,38 @@
/*
Complex Moving Sum
Copyright 2024 Ahmet Inan <xdsopl@gmail.com>
*/
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;
}
}

View file

@ -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();
}