-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eric Bergman-Terrell
committed
Aug 23, 2022
1 parent
c45f38a
commit c52eba8
Showing
5 changed files
with
165 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
app/src/androidTest/java/com/ericbt/ebtcompass/DataSmootherTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
EBT Compass | ||
(C) Copyright 2022, Eric Bergman-Terrell | ||
This file is part of EBT Compass. | ||
EBT Compass is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
EBT Compass is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with EBT Compass. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ericbt.ebtcompass; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
|
||
import com.ericbt.ebtcompass.utils.DataSmoother; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class DataSmootherTests { | ||
@Test | ||
public void singleValue() { | ||
final DataSmoother dataSmoother = new DataSmoother(10); | ||
|
||
final Float value = 234.123f; | ||
|
||
Float average = dataSmoother.add(value); | ||
|
||
assertEquals(average, value); | ||
} | ||
|
||
@Test | ||
public void twoValues() { | ||
final DataSmoother dataSmoother = new DataSmoother(2); | ||
|
||
final Float value1 = 1.0f; | ||
final Float value2 = 2.0f; | ||
|
||
dataSmoother.add(value1); | ||
Float average = dataSmoother.add(value2); | ||
|
||
assertTrue(average == (value1 + value2) / 2.0f); | ||
} | ||
|
||
@Test | ||
public void fourValues() { | ||
final DataSmoother dataSmoother = new DataSmoother(2); | ||
|
||
final Float value1 = 1234.23452f; | ||
final Float value2 = 221234.12f; | ||
final Float value3 = 343.2342342f; | ||
final Float value4 = 41234.567567f; | ||
|
||
dataSmoother.add(value1); | ||
dataSmoother.add(value2); | ||
dataSmoother.add(value3); | ||
|
||
final Float average = dataSmoother.add(value4); | ||
|
||
assertTrue(average == (value3 + value4) / 2.0f); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/main/java/com/ericbt/ebtcompass/utils/DataSmoother.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
EBT Compass | ||
(C) Copyright 2022, Eric Bergman-Terrell | ||
This file is part of EBT Compass. | ||
EBT Compass is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
EBT Compass is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with EBT Compass. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ericbt.ebtcompass.utils; | ||
|
||
public class DataSmoother { | ||
private Float[] data; | ||
private int index = 0; | ||
|
||
public DataSmoother(int maxValues) { | ||
data = new Float[maxValues]; | ||
|
||
for (int i = 0; i < data.length; i++) { | ||
data[i] = Float.NaN; | ||
} | ||
} | ||
|
||
public float add(float value) { | ||
data[index] = value; | ||
|
||
index = (index + 1) % data.length; | ||
|
||
return average(); | ||
} | ||
|
||
private float average() { | ||
int n = 0; | ||
float sum = 0.0f; | ||
|
||
for (int i = 0; i < data.length; i++) { | ||
if (!data[i].isNaN()) { | ||
n++; | ||
sum += data[i]; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return sum / (float) n; | ||
} | ||
} |