IIR filter

typedef struct uz_IIR_Filter_t uz_IIR_Filter_t
enum uz_IIR_Filter_selection

enum for readable configuring for type of 1st order filter

Values:

enumerator LowPass_first_order
enumerator HighPass_first_order
struct uz_IIR_Filter_config

Configuration struct for a 1st order filter. Low- and HighPass filter is possible.

Public Members

enum uz_IIR_Filter_selection selection

Filter selection

float cutoff_frequency_Hz

cutoff frequency in Hz of the filter

float sample_frequency_Hz

sample frequency in Hz of the signal

uz_IIR_Filter_t *uz_signals_IIR_Filter_init(struct uz_IIR_Filter_config config)

Initialization of the filter 1st order object.

Parameters:
  • config – uz_Filter_1st_config configuration struct

Returns:

uz_IIR_Filter_t* pointer instance

float uz_signals_IIR_Filter_sample(uz_IIR_Filter_t *self, float input)

Function to filter an input signal with either an LowPass or HighPass filter.

Parameters:
  • self – pointer instance of uz_IIR_Filter_t

  • input – signal, which will be filtered

Returns:

float filtered signal

float uz_signals_IIR_Filter_reverse_sample(uz_IIR_Filter_t *self, float input)

Function to reverse the filter of the input signal with either an LowPass or HighPass filter.

Parameters:
  • self – pointer instance of uz_IIR_Filter_t

  • input – filtered signal

Returns:

float unfiltered signal

Example

Listing 94 Example to init and sample the filter
1#include "uz_signals.h"
2int main(void) {
3   struct uz_IIR_Filter_config config = {.selection = LowPass_first_order, .cutoff_frequency_Hz = 200.0f, .sample_frequency_Hz = 20000.0f};
4   uz_IIR_Filter_t* test_instance = uz_signals_IIR_Filter_init(config);
5   float unfiltered_signal = 20.0f;
6   float filtered_signal = 10.0f;
7   filtered_signal = uz_signals_IIR_Filter_sample(test_instance, unfiltered_signal);
8   unfiltered_signal = uz_signals_IIR_Filter_reverse_sample(test_instance, filtered_signal);
9}

Description

Implemented is either a simple IIR-lowpass or highpass filter of the first order. The reverse functions are intended, for when a filter has been previously applied to a signal and should be removed again.

For the lowpass filter [[1]][[2]]:

\[\alpha = \frac{dt}{dt + RC}\]
\[y[i] = y[i-1] + \alpha \cdot (x[i] - y[i-1])\]

And for the lowpass-reverse filter:

\[y[i] = \frac{x[i] -x[i-1]}{\alpha} + x[i-1]\]

For the highpass filter [[3]]:

\[\alpha = \frac{RC}{dt + RC}\]
\[y[i] = \alpha \cdot (y[i-1] + x[i] - x[i-1])\]

And for the highpass-reverse filter:

\[y[i] = \frac{x[i]}{\alpha} + y[i-1] - x[i-1]\]

Sources