Fix negative variance
This commit is contained in:
parent
f84b89555c
commit
daca669bed
18
.clang-tidy
18
.clang-tidy
|
@ -7,19 +7,19 @@ Checks: >
|
||||||
performance-*,
|
performance-*,
|
||||||
portability-*,
|
portability-*,
|
||||||
readability-*,
|
readability-*,
|
||||||
|
-bugprone-easily-swappable-parameters,
|
||||||
-google-readability-namespace-comments,
|
-google-readability-namespace-comments,
|
||||||
-google-runtime-int,
|
-google-runtime-int,
|
||||||
-google-runtime-references,
|
-google-runtime-references,
|
||||||
|
-llvmlibc-callee-namespace,
|
||||||
-misc-non-private-member-variables-in-classes,
|
-misc-non-private-member-variables-in-classes,
|
||||||
-readability-named-parameter,
|
-modernize-macro-to-enum,
|
||||||
|
-modernize-use-trailing-return-type,
|
||||||
-readability-braces-around-statements,
|
-readability-braces-around-statements,
|
||||||
-readability-magic-numbers
|
-readability-identifier-length,
|
||||||
-llvmlibc-callee-namespace
|
-readability-identifier-naming,
|
||||||
-bugprone-easily-swappable-parameters
|
-readability-magic-numbers,
|
||||||
-readability-magic-numbers
|
-readability-named-parameter
|
||||||
-readability-identifier-length
|
|
||||||
-readability-identifier-naming
|
|
||||||
-modernize-macro-to-enum
|
|
||||||
|
|
||||||
CheckOptions:
|
CheckOptions:
|
||||||
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
|
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
|
||||||
|
@ -27,7 +27,6 @@ CheckOptions:
|
||||||
- { key: readability-identifier-naming.StructCase, value: CamelCase }
|
- { key: readability-identifier-naming.StructCase, value: CamelCase }
|
||||||
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
|
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
|
||||||
- { key: readability-identifier-naming.FunctionCase, 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.PrivateMemberSuffix, value: _ }
|
||||||
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ }
|
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ }
|
||||||
- { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE }
|
- { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE }
|
||||||
|
@ -41,3 +40,4 @@ CheckOptions:
|
||||||
- { key: readability-identifier-naming.MemberConstantPrefix, value: k }
|
- { key: readability-identifier-naming.MemberConstantPrefix, value: k }
|
||||||
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
|
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
|
||||||
- { key: readability-identifier-naming.StaticConstantPrefix, value: k }
|
- { key: readability-identifier-naming.StaticConstantPrefix, value: k }
|
||||||
|
- { key: readability-identifier-naming.VariableCase, value: CamelCase }
|
||||||
|
|
|
@ -15,16 +15,16 @@ void FilteredDistance::initSpike(float dist) {
|
||||||
readings[i] = dist;
|
readings[i] = dist;
|
||||||
}
|
}
|
||||||
total = dist * NUM_READINGS;
|
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) {
|
float FilteredDistance::removeSpike(float dist) {
|
||||||
total -= readings[readIndex]; // Subtract the last reading
|
total -= readings[readIndex]; // Subtract the last reading
|
||||||
totalSquared -= readings[readIndex] * readings[readIndex]; // Subtract the square of the last reading
|
totalSquared -= readings[readIndex] * readings[readIndex]; // Subtract the square of the last reading
|
||||||
|
|
||||||
readings[readIndex] = dist; // Read the sensor
|
readings[readIndex] = dist; // Read the sensor
|
||||||
total += readings[readIndex]; // Add the reading to the total
|
total += readings[readIndex]; // Add the reading to the total
|
||||||
totalSquared += readings[readIndex] * readings[readIndex]; // Add the square of the reading
|
totalSquared += readings[readIndex] * readings[readIndex]; // Add the square of the reading
|
||||||
|
|
||||||
readIndex = (readIndex + 1) % NUM_READINGS; // Advance to the next position in the array
|
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 {
|
const float FilteredDistance::getVariance() const {
|
||||||
float mean = total / static_cast<float>(NUM_READINGS);
|
auto mean = total / static_cast<float>(NUM_READINGS);
|
||||||
float meanOfSquares = totalSquared / static_cast<float>(NUM_READINGS);
|
auto meanOfSquares = totalSquared / static_cast<float>(NUM_READINGS);
|
||||||
return meanOfSquares - (mean * mean); // Variance formula: E(X^2) - (E(X))^2
|
auto variance = meanOfSquares - (mean * mean); // Variance formula: E(X^2) - (E(X))^2
|
||||||
|
if (variance < 0.0f) return 0.0f;
|
||||||
|
return variance;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue