Line data Source code
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 :
10 : #ifndef ROCKETLIB_LOW_PASS_FILTER_H
11 : #define ROCKETLIB_LOW_PASS_FILTER_H
12 :
13 : #include <stdbool.h>
14 : #include <stdint.h>
15 :
16 : #include "common.h"
17 :
18 : #ifdef __cplusplus
19 : extern "C" {
20 : #endif
21 :
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 : */
28 : static inline double sample_freq(double time_diff_ms) {
29 1 : return 1000.0 / time_diff_ms;
30 : }
31 :
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 : */
43 : static inline double low_pass_alpha(double TR, double time_diff_ms) {
44 : double freq = sample_freq(time_diff_ms);
45 1 : return (freq * TR / 5.0) / (1.0 + freq * TR / 5.0);
46 : }
47 :
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 : */
59 : w_status_t low_pass_filter_init(double *alpha, double response_time);
60 :
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 : */
74 : w_status_t update_low_pass(double alpha, uint16_t new_input_value, double *low_pass_value);
75 :
76 : #ifdef __cplusplus
77 : }
78 : #endif
79 :
80 : #endif /* ROCKETLIB_LOW_PASS_FILTER_H */
|