Branch data Line data Source code
1 : : #include <stddef.h>
2 : : #include <stdint.h>
3 : :
4 : : #include "common.h"
5 : : #include "low_pass_filter.h"
6 : :
7 : : /**
8 : : * @brief Initializes the low-pass filter by calculating and storing the alpha value
9 : : *
10 : : * This function calculates the alpha value based on the given response time and stores
11 : : * it in the provided pointer. The alpha value is used in subsequent filter updates.
12 : : *
13 : : * @param alpha Pointer to store the calculated alpha value
14 : : * @param response_time Response time constant in milliseconds (must be > 0)
15 : : * @return w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if alpha is NULL or
16 : : * response_time is invalid
17 : : */
18 : 4 : w_status_t low_pass_filter_init(double *alpha, double response_time) {
19 [ + + + + ]: 4 : if (alpha == NULL || response_time <= 0) {
20 : : return W_INVALID_PARAM; // Return specific error for invalid parameters
21 : : }
22 : : // Calculate and store the alpha value using the low_pass_alpha function
23 : 1 : *alpha = low_pass_alpha(response_time, response_time);
24 : 1 : return W_SUCCESS; // Return success after initialization
25 : : }
26 : :
27 : : /**
28 : : * @brief Updates the low-pass filter with a new value and returns the operation status
29 : : *
30 : : * This function applies the low-pass filter algorithm to the new input value using the
31 : : * provided alpha value. The filtered result is stored in low_pass_value and updated
32 : : * in-place for the next iteration.
33 : : *
34 : : * @param alpha Alpha value (0.0 to 1.0) for the low-pass filter
35 : : * @param new_input_value New input value to filter
36 : : * @param low_pass_value Pointer to the current filtered value (updated in-place)
37 : : * @return w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if low_pass_value is
38 : : * NULL or alpha is out of valid range (0.0 to 1.0)
39 : : */
40 : 53 : w_status_t update_low_pass(double alpha, uint16_t new_input_value, double *low_pass_value) {
41 : : // Check if low_pass_value pointer is NULL
42 [ + + ]: 53 : if (low_pass_value == NULL) {
43 : : return W_INVALID_PARAM; // Return specific error for invalid parameters
44 : : }
45 : :
46 : : // Ensure alpha is within the valid range
47 [ + + + + ]: 52 : if (alpha <= 0.0 || alpha >= 1.0) {
48 : : return W_INVALID_PARAM;
49 : : }
50 : :
51 : : // Low pass filter formula: y[n] = alpha * x[n] + (1 - alpha) * y[n-1]
52 : 50 : *low_pass_value = (alpha * new_input_value) + ((1.0 - alpha) * (*low_pass_value));
53 : 50 : return W_SUCCESS; // Return success after successful update
54 : : }
|