diff --git a/include/low_pass_filter.h b/include/low_pass_filter.h index 2f0c9aa..0edca69 100644 --- a/include/low_pass_filter.h +++ b/include/low_pass_filter.h @@ -16,10 +16,11 @@ static inline double low_pass_alpha(double TR, double time_diff_ms) { return (freq * TR / 5.0) / (1.0 + freq * TR / 5.0); } -// Initializes the low-pass filter by calculating and storing the alpha value based on the given response time. +// Initializes the low-pass filter by calculating and storing the alpha value based on the given +// response time. w_status_t low_pass_filter_init(double *alpha, double response_time); // Updates the low-pass filter with a new value and returns the operation status -w_status_t update_low_pass(double *alpha, uint16_t new_input_value, double *low_pass_value); +w_status_t update_low_pass(double alpha, uint16_t new_input_value, double *low_pass_value); #endif /* ROCKETLIB_LOW_PASS_FILTER_H */ \ No newline at end of file diff --git a/unit_tests/low_pass_filter_test.c b/unit_tests/low_pass_filter_test.c index 7d24e17..1091ceb 100644 --- a/unit_tests/low_pass_filter_test.c +++ b/unit_tests/low_pass_filter_test.c @@ -34,32 +34,32 @@ void test_update_low_pass() { for (int i = 1; i <= iterations; ++i) { // Store the previous low_pass_value before updating double previous_low_pass = low_pass_value; - // Calculate the expected value based on the low-pass filter formula double expected = (alpha * new_input_value) + ((1.0 - alpha) * previous_low_pass); - // Update the low-pass filter with the fixed input value - result = update_low_pass(&alpha, new_input_value, &low_pass_value); + result = update_low_pass(alpha, new_input_value, &low_pass_value); w_assert(result == W_SUCCESS); - // Print the current iteration and values printf( "Iteration %2d: Low-Pass Value = %.4f | Expected ~ %.4f\n", i, low_pass_value, expected ); - // Verify the correctness of the low-pass update w_assert(fabs(low_pass_value - expected) < EPSILON); } printf("\nAll iterations completed successfully.\n\n"); - // Test with NULL alpha - result = update_low_pass(NULL, new_input_value, &low_pass_value); + // Test with alpha out of range + result = update_low_pass(-0.1, new_input_value, &low_pass_value); + w_assert(result == W_INVALID_PARAM); + printf("Invalid alpha (negative) test passed.\n"); + + result = update_low_pass(1.1, new_input_value, &low_pass_value); w_assert(result == W_INVALID_PARAM); - printf("Null alpha test passed.\n"); + printf("Invalid alpha (greater than 1) test passed.\n"); // Test with NULL low_pass_value - result = update_low_pass(&alpha, new_input_value, NULL); + result = update_low_pass(alpha, new_input_value, NULL); w_assert(result == W_INVALID_PARAM); printf("Null low_pass_value test passed.\n"); }