refactoring

This commit is contained in:
rusefi 2018-09-29 21:15:19 -07:00
parent bd40691e67
commit 1c86e4a2f9
7 changed files with 54 additions and 27 deletions

View File

@ -1,6 +1,11 @@
package com.rusefi; package com.rusefi;
public class CyclicBuffer { import java.util.Arrays;
/**
* Fixed-size array backed buffer
*/
public class CyclicBuffer implements DataBuffer {
private final double values[]; private final double values[];
private int size = 0; private int size = 0;
@ -10,17 +15,6 @@ public class CyclicBuffer {
values = new double[maxSize]; values = new double[maxSize];
} }
public double getStandardDeviation() {
double accumulator = 0;
double avg = average();
for (int i = 0; i < getSize(); i++) {
double v = get(i) - avg;
accumulator += v * v;
}
return Math.sqrt(accumulator);
}
public void add(double value) { public void add(double value) {
if (size < values.length) if (size < values.length)
size++; size++;
@ -30,21 +24,15 @@ public class CyclicBuffer {
pointer = 0; pointer = 0;
} }
public double average() {
return sum() / size;
}
public double sum() {
double result = 0;
for (int i = 0; i < size; i++)
result += values[i];
return result;
}
public int getSize() { public int getSize() {
return size; return size;
} }
@Override
public double[] getValues() {
return Arrays.copyOfRange(values, 0, size);
}
public int getMaxSize() { public int getMaxSize() {
return values.length; return values.length;
} }

View File

@ -0,0 +1,26 @@
package com.rusefi;
public interface DataBuffer {
double[] getValues();
static double sum(double[] values) {
double result = 0;
for (double value : values)
result += value;
return result;
}
static double average(double[] values) {
return sum(values) / values.length;
}
static double getStandardDeviation(double[] values) {
double accumulator = 0;
double avg = average(values);
for (double value : values) {
double v = value - avg;
accumulator += v * v;
}
return Math.sqrt(accumulator);
}
}

View File

@ -0,0 +1,4 @@
package com.rusefi;
public class TimeBasedBuffer {
}

View File

@ -1,6 +1,7 @@
package com.rusefi.core; package com.rusefi.core;
import com.rusefi.CyclicBuffer; import com.rusefi.CyclicBuffer;
import com.rusefi.DataBuffer;
import com.rusefi.SensorConversion; import com.rusefi.SensorConversion;
import com.rusefi.waves.EngineReport; import com.rusefi.waves.EngineReport;
@ -52,7 +53,7 @@ public class SensorStats {
public void onSensorUpdate(double value) { public void onSensorUpdate(double value) {
cb.add(value); cb.add(value);
if (cb.getSize() == cb.getMaxSize()) { if (cb.getSize() == cb.getMaxSize()) {
double stdDev = cb.getStandardDeviation(); double stdDev = DataBuffer.getStandardDeviation(cb.getValues());
SensorCentral.getInstance().setValue(stdDev, destination); SensorCentral.getInstance().setValue(stdDev, destination);
} }
} }

View File

@ -1,6 +1,7 @@
package com.rusefi.test; package com.rusefi.test;
import com.rusefi.CyclicBuffer; import com.rusefi.CyclicBuffer;
import com.rusefi.DataBuffer;
import org.junit.Test; import org.junit.Test;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
@ -13,12 +14,12 @@ public class CyclicBufferTest {
cb.add(1); cb.add(1);
cb.add(3); cb.add(3);
assertEquals(2.0, cb.average()); assertEquals(2.0, DataBuffer.average(cb.getValues()));
cb.add(111); cb.add(111);
cb.add(211); cb.add(211);
assertEquals(108.33333333333333, cb.average()); assertEquals(108.33333333333333, DataBuffer.average(cb.getValues()));
assertEquals(147.1144679039647, cb.getStandardDeviation()); assertEquals(147.1144679039647, DataBuffer.getStandardDeviation(cb.getValues()));
} }
} }

View File

@ -0,0 +1,4 @@
package com.rusefi.test;
public class TimeBasedBufferTest {
}

View File

@ -7,6 +7,9 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
/** /**
* Simple command line utility
*
*
* Date: 3/8/13 * Date: 3/8/13
* (c) Andrey Belomutskiy * (c) Andrey Belomutskiy
*/ */