Skip to content

Vulintus/Vulintus_Digital_Filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vulintus_Digital_Filter

License: MIT Arduino Compatible

A collection of Arduino-compatible digital filter classes used in Vulintus devices' firmware. This library started as a stripped-down fork of Jon Driscoll's "Filters" library, but now differs somewhat in the calculations based on our own interpretations. Basic examples for Arduino are included. This library is, as always, a work-in-progress.

Disclaimer: Sections of this README file were AI-generated, but checked for accuracy by a human.


Simple Infinite Impulse Response (IIR) Filters

The most basic low- or high-pass filters, "Vulintus_IIR_Filter," are designed as single-pole discretized RC filters. The implementation of these filters is adapted largely from the Wikipedia pages for low-pass and high-pass filters.

For IIR filters, new output values are calculated from the recurrence relations derived from basic RC circuit models:

$$y[i] = (1 - \alpha) \cdot x[i] + \alpha \cdot y[i-1] \qquad \text{(low-pass)}$$

$$y[i] = \alpha \cdot (x[i] - x[i-1]) + \alpha \cdot y[i-1] \qquad \text{(high-pass)}$$

These equations differ because the RC circuit models for low-pass and high-pass filters differ by flipping the position of the capacitor and resistor. For both equations, $x[i]$ and $x[i-1]$ are the current and previous raw signal samples, and $y[i]$ and $y[i-1]$ are the current and previous filter output values. The factor $\alpha$ for both equations is given by:

$$\alpha = e^{-\Delta t/\tau}$$

where $\Delta t$ is the time between samples and $\tau$ is the decay constant ($\tau = RC$). However, calls to the exp() function can be computationally expensive. So long as $\Delta t$ is small compared to $\tau$, we can quickly approximate $\alpha$ as:

$$\alpha = \frac{\tau}{\tau + \Delta t}$$

The error difference between the exponential calculation and the approximation is less than 1% for $\tau/\Delta t > 7$ and less than 0.1% for $\tau/\Delta t > 22$.


Installation

  1. Download the latest release as a .zip file.
  2. In the Arduino IDE, go to Sketch → Include Library → Add .ZIP Library... and select the downloaded file.
  3. Alternatively, extract the archive directly into your Arduino/libraries/ folder and restart the IDE.

Usage

Include the library and instantiate a filter with a cutoff frequency in Hz:

#include <Vulintus_Digital_Filter.h>

const float CUTOFF_FREQ = 1.0;                      // Cutoff frequency, in Hz.
Vulintus_LowPass_Filter  lp_filter(CUTOFF_FREQ);    // Single-pole IIR low-pass filter.
Vulintus_HighPass_Filter hp_filter(CUTOFF_FREQ);    // Single-pole IIR high-pass filter.

Call input() in your sampling loop. When a single argument is passed, the filter will call micros() internally to use as the sampling time.

float filtered = lp_filter.input(raw);

Passing a microsecond clock reading as a second argument allows for filter updates separated in time from signal sampling:

float raw = analogRead(A0);
unsigned long microtime = micros();
delay(5);
float filtered = lp_filter.input(raw, microtime);

The current output is also accessible directly via the output member:

Serial.println(lp_filter.output);

The cutoff frequency can be read or updated at any time:

lp_filter.cutoff_frequency(5.0);             // Set to 5 Hz.
float fc = lp_filter.cutoff_frequency();     // Read current cutoff frequency.

See examples/Vulintus_Digital_Filter_Demo for a complete example that uses the Arduino Serial Plotter to visualize filter output on a square-wave test signal.


License

This library is released under the MIT License.


About

Arduino-compatible digital filter library used in Vulintus devices' firmware.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages