Fix negative variance

This commit is contained in:
DTTerastar 2023-12-03 08:14:45 -05:00
parent f84b89555c
commit daca669bed
2 changed files with 20 additions and 18 deletions

View File

@ -7,19 +7,19 @@ Checks: >
performance-*,
portability-*,
readability-*,
-bugprone-easily-swappable-parameters,
-google-readability-namespace-comments,
-google-runtime-int,
-google-runtime-references,
-llvmlibc-callee-namespace,
-misc-non-private-member-variables-in-classes,
-readability-named-parameter,
-modernize-macro-to-enum,
-modernize-use-trailing-return-type,
-readability-braces-around-statements,
-readability-magic-numbers
-llvmlibc-callee-namespace
-bugprone-easily-swappable-parameters
-readability-magic-numbers
-readability-identifier-length
-readability-identifier-naming
-modernize-macro-to-enum
-readability-identifier-length,
-readability-identifier-naming,
-readability-magic-numbers,
-readability-named-parameter
CheckOptions:
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
@ -27,7 +27,6 @@ CheckOptions:
- { key: readability-identifier-naming.StructCase, value: CamelCase }
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
- { key: readability-identifier-naming.FunctionCase, value: CamelCase }
- { key: readability-identifier-naming.VariableCase, value: lower_case }
- { key: readability-identifier-naming.PrivateMemberSuffix, value: _ }
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ }
- { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE }
@ -41,3 +40,4 @@ CheckOptions:
- { key: readability-identifier-naming.MemberConstantPrefix, value: k }
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
- { key: readability-identifier-naming.StaticConstantPrefix, value: k }
- { key: readability-identifier-naming.VariableCase, value: CamelCase }

View File

@ -15,16 +15,16 @@ void FilteredDistance::initSpike(float dist) {
readings[i] = dist;
}
total = dist * NUM_READINGS;
totalSquared = dist * dist * NUM_READINGS; // Initialize sum of squared distances
totalSquared = dist * dist * NUM_READINGS; // Initialize sum of squared distances
}
float FilteredDistance::removeSpike(float dist) {
total -= readings[readIndex]; // Subtract the last reading
totalSquared -= readings[readIndex] * readings[readIndex]; // Subtract the square of the last reading
total -= readings[readIndex]; // Subtract the last reading
totalSquared -= readings[readIndex] * readings[readIndex]; // Subtract the square of the last reading
readings[readIndex] = dist; // Read the sensor
total += readings[readIndex]; // Add the reading to the total
totalSquared += readings[readIndex] * readings[readIndex]; // Add the square of the reading
readings[readIndex] = dist; // Read the sensor
total += readings[readIndex]; // Add the reading to the total
totalSquared += readings[readIndex] * readings[readIndex]; // Add the square of the reading
readIndex = (readIndex + 1) % NUM_READINGS; // Advance to the next position in the array
@ -69,7 +69,9 @@ float FilteredDistance::getAlpha(float cutoff, float dT) {
}
const float FilteredDistance::getVariance() const {
float mean = total / static_cast<float>(NUM_READINGS);
float meanOfSquares = totalSquared / static_cast<float>(NUM_READINGS);
return meanOfSquares - (mean * mean); // Variance formula: E(X^2) - (E(X))^2
auto mean = total / static_cast<float>(NUM_READINGS);
auto meanOfSquares = totalSquared / static_cast<float>(NUM_READINGS);
auto variance = meanOfSquares - (mean * mean); // Variance formula: E(X^2) - (E(X))^2
if (variance < 0.0f) return 0.0f;
return variance;
}