mirror of
https://github.com/xdsopl/robot36.git
synced 2025-12-06 07:12:07 +01:00
added complex moving sum and average
This commit is contained in:
parent
16406fd710
commit
1fb772e346
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
21
app/src/main/java/xdsopl/robot36/ComplexMovingAverage.java
Normal file
21
app/src/main/java/xdsopl/robot36/ComplexMovingAverage.java
Normal 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);
|
||||
}
|
||||
}
|
||||
38
app/src/main/java/xdsopl/robot36/ComplexMovingSum.java
Normal file
38
app/src/main/java/xdsopl/robot36/ComplexMovingSum.java
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue