Browse Source

Added dc_bloacker and smoothing_fiter from Klein Embedded

Vladimir N. Shilov 1 year ago
parent
commit
a74df36d56
4 changed files with 104 additions and 0 deletions
  1. 19 0
      adc/dc_blocker.c
  2. 37 0
      adc/dc_blocker.h
  3. 16 0
      adc/smoothing_filter.c
  4. 32 0
      adc/smoothing_filter.h

+ 19 - 0
adc/dc_blocker.c

@@ -0,0 +1,19 @@
+#include "dc_blocker.h"
+
+void dc_blocker_init(dc_blocker_t *handle, float blocking_factor)
+{
+    handle->blocking_factor = blocking_factor;
+    handle->gain = (1 + blocking_factor) / 2;
+    handle->prev_input = 0;
+    handle->prev_output = 0;
+}
+
+float dc_blocker_process(dc_blocker_t *handle, float current_input)
+{
+    float output = handle->gain * (current_input - handle->prev_input) +
+                   handle->blocking_factor * handle->prev_output;
+
+    handle->prev_input = current_input;
+    handle->prev_output = output;
+    return output;
+}

+ 37 - 0
adc/dc_blocker.h

@@ -0,0 +1,37 @@
+/**
+ * Quick and simple digital filters for smoothing and DC removal
+ * https://kleinembedded.com/quick-and-simple-digital-filters-for-smoothing-and-dc-removal/
+ */
+#ifndef KLEINLIBC_DC_BLOCKER_H
+#define KLEINLIBC_DC_BLOCKER_H
+
+typedef struct
+{
+    float blocking_factor;
+    float gain;
+    float prev_input;
+    float prev_output;
+} dc_blocker_t;
+
+/**
+ * @brief Initialize the DC blocker.
+ * 
+ * @param handle Pointer to an instance of `dc_blocker_t`.
+ * @param blocking_factor A floating point number between 0 and 1 that
+ *  determines how close to DC the cutoff frequency should be. A value close to (but less than)
+ *  1 will result in a cutoff frequency very close to DC but with a slow response.
+ *  Setting the blocking factor lower will result in a faster response but will attenuate more
+ *  low frequency content. A value between 0.9 and 1 is reasonably in most cases.
+ */
+void dc_blocker_init(dc_blocker_t *handle, float blocking_factor);
+
+/**
+ * @brief Process the next input value.
+ * 
+ * @param handle Pointer to an instance of `dc_blocker_t`.
+ * @param current_input The input value to process.
+ * @return float The filtered output value.
+ */
+float dc_blocker_process(dc_blocker_t *handle, float current_input);
+
+#endif

+ 16 - 0
adc/smoothing_filter.c

@@ -0,0 +1,16 @@
+#include "smoothing_filter.h"
+
+void smoothing_filter_init(smoothing_filter_t *handle, float smoothing_factor)
+{
+    handle->smoothing_factor = smoothing_factor;
+    handle->prev_output = 0;
+}
+
+float smoothing_filter_process(smoothing_filter_t *handle, float current_input)
+{
+    float output = current_input * handle->smoothing_factor +
+                   handle->prev_output * (1 - handle->smoothing_factor);
+
+    handle->prev_output = output;
+    return output;
+}

+ 32 - 0
adc/smoothing_filter.h

@@ -0,0 +1,32 @@
+/**
+ * Quick and simple digital filters for smoothing and DC removal
+ * https://kleinembedded.com/quick-and-simple-digital-filters-for-smoothing-and-dc-removal/
+ */
+#ifndef KLEINLIBC_SMOOTHING_FILTER_H
+#define KLEINLIBC_SMOOTHING_FILTER_H
+
+typedef struct
+{
+    float smoothing_factor;
+    float prev_output;
+} smoothing_filter_t;
+
+/**
+ * @brief Initialize smoothing filter.
+ *
+ * @param handle Pointer to an instance of `smoothing_filter_t`.
+ * @param smoothing_factor A floating point number between 0 and 1 that determines how much smoothing to apply.
+ *  Smaller values apply more smoothing.
+ */
+void smoothing_filter_init(smoothing_filter_t *handle, float smoothing_factor);
+
+/**
+ * @brief Process the next input value.
+ *
+ * @param handle Pointer to an instance of `smoothing_filter_t`.
+ * @param current_input The input value to process.
+ * @return float The filtered output value.
+ */
+float smoothing_filter_process(smoothing_filter_t *handle, float current_input);
+
+#endif