refactoring
This commit is contained in:
parent
bd40691e67
commit
1c86e4a2f9
|
@ -1,6 +1,11 @@
|
|||
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 int size = 0;
|
||||
|
@ -10,17 +15,6 @@ public class CyclicBuffer {
|
|||
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) {
|
||||
if (size < values.length)
|
||||
size++;
|
||||
|
@ -30,21 +24,15 @@ public class CyclicBuffer {
|
|||
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() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double[] getValues() {
|
||||
return Arrays.copyOfRange(values, 0, size);
|
||||
}
|
||||
|
||||
public int getMaxSize() {
|
||||
return values.length;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.rusefi;
|
||||
|
||||
public class TimeBasedBuffer {
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.rusefi.core;
|
||||
|
||||
import com.rusefi.CyclicBuffer;
|
||||
import com.rusefi.DataBuffer;
|
||||
import com.rusefi.SensorConversion;
|
||||
import com.rusefi.waves.EngineReport;
|
||||
|
||||
|
@ -52,7 +53,7 @@ public class SensorStats {
|
|||
public void onSensorUpdate(double value) {
|
||||
cb.add(value);
|
||||
if (cb.getSize() == cb.getMaxSize()) {
|
||||
double stdDev = cb.getStandardDeviation();
|
||||
double stdDev = DataBuffer.getStandardDeviation(cb.getValues());
|
||||
SensorCentral.getInstance().setValue(stdDev, destination);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.rusefi.test;
|
||||
|
||||
import com.rusefi.CyclicBuffer;
|
||||
import com.rusefi.DataBuffer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
@ -13,12 +14,12 @@ public class CyclicBufferTest {
|
|||
cb.add(1);
|
||||
cb.add(3);
|
||||
|
||||
assertEquals(2.0, cb.average());
|
||||
assertEquals(2.0, DataBuffer.average(cb.getValues()));
|
||||
|
||||
cb.add(111);
|
||||
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()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.rusefi.test;
|
||||
|
||||
public class TimeBasedBufferTest {
|
||||
}
|
|
@ -7,6 +7,9 @@ import java.io.FileOutputStream;
|
|||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Simple command line utility
|
||||
*
|
||||
*
|
||||
* Date: 3/8/13
|
||||
* (c) Andrey Belomutskiy
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue