|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Low-pass filter implementation |
| 4 | + * |
| 5 | + * This module provides a low-pass filter implementation for smoothing sensor readings |
| 6 | + * and reducing noise in analog signals. The filter uses an exponential moving average |
| 7 | + * algorithm with configurable response time. |
| 8 | + */ |
| 9 | + |
1 | 10 | #ifndef ROCKETLIB_LOW_PASS_FILTER_H |
2 | 11 | #define ROCKETLIB_LOW_PASS_FILTER_H |
3 | 12 |
|
|
10 | 19 | extern "C" { |
11 | 20 | #endif |
12 | 21 |
|
13 | | -// Inline function to calculate the sample frequency based on the time difference in milliseconds |
| 22 | +/** |
| 23 | + * @brief Calculate the sample frequency based on the time difference in milliseconds |
| 24 | + * |
| 25 | + * @param time_diff_ms Time difference between samples in milliseconds |
| 26 | + * @return double Sample frequency in Hz |
| 27 | + */ |
14 | 28 | static inline double sample_freq(double time_diff_ms) { |
15 | 29 | return 1000.0 / time_diff_ms; |
16 | 30 | } |
17 | 31 |
|
18 | | -// Inline function to calculate the alpha value for the low-pass filter based on the response time |
| 32 | +/** |
| 33 | + * @brief Calculate the alpha value for the low-pass filter based on the response time |
| 34 | + * |
| 35 | + * The alpha value determines the filter's responsiveness. A higher alpha value results |
| 36 | + * in faster response but less filtering, while a lower alpha value provides more filtering |
| 37 | + * but slower response. |
| 38 | + * |
| 39 | + * @param TR Response time constant in milliseconds |
| 40 | + * @param time_diff_ms Time difference between samples in milliseconds |
| 41 | + * @return double Alpha value (0.0 to 1.0) for the low-pass filter |
| 42 | + */ |
19 | 43 | static inline double low_pass_alpha(double TR, double time_diff_ms) { |
20 | 44 | double freq = sample_freq(time_diff_ms); |
21 | 45 | return (freq * TR / 5.0) / (1.0 + freq * TR / 5.0); |
22 | 46 | } |
23 | 47 |
|
24 | | -// Initializes the low-pass filter by calculating and storing the alpha value based on the given |
25 | | -// response time. |
| 48 | +/** |
| 49 | + * @brief Initializes the low-pass filter by calculating and storing the alpha value |
| 50 | + * |
| 51 | + * This function calculates the alpha value based on the given response time and stores |
| 52 | + * it in the provided pointer. The alpha value is used in subsequent filter updates. |
| 53 | + * |
| 54 | + * @param alpha Pointer to store the calculated alpha value |
| 55 | + * @param response_time Response time constant in milliseconds (must be > 0) |
| 56 | + * @return w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if alpha is NULL or |
| 57 | + * response_time is invalid |
| 58 | + */ |
26 | 59 | w_status_t low_pass_filter_init(double *alpha, double response_time); |
27 | 60 |
|
28 | | -// Updates the low-pass filter with a new value and returns the operation status |
| 61 | +/** |
| 62 | + * @brief Updates the low-pass filter with a new value and returns the operation status |
| 63 | + * |
| 64 | + * This function applies the low-pass filter algorithm to the new input value using the |
| 65 | + * provided alpha value. The filtered result is stored in low_pass_value and updated |
| 66 | + * in-place for the next iteration. |
| 67 | + * |
| 68 | + * @param alpha Alpha value (0.0 to 1.0) for the low-pass filter |
| 69 | + * @param new_input_value New input value to filter |
| 70 | + * @param low_pass_value Pointer to the current filtered value (updated in-place) |
| 71 | + * @return w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if low_pass_value is |
| 72 | + * NULL or alpha is out of valid range (0.0 to 1.0) |
| 73 | + */ |
29 | 74 | w_status_t update_low_pass(double alpha, uint16_t new_input_value, double *low_pass_value); |
30 | 75 |
|
31 | 76 | #ifdef __cplusplus |
|
0 commit comments