In Arduino, there is often a need to calculate derivatives and integrals of variables or sensor signals. While these operations have simple mathematical definitions, their implementation is not always straightforward due to the following reasons:

  1. Data procured through an Arduino is discretized and suffers from white noise.
  2. These factors easily violate the requirement of continuity for calculus operations.

image

See: Reducing noise in signals

For instance, a highly noise signal will generate an even noisier derivative, and the overall trend in the data can be completely lost. We can reduce the loss of relevant information by applying a filter to suppress the noise in the original data. The filtered signal can then be used to apply the calculus operators.

Embedded filter

To take into account the need for filtering, I devised a simple library to approximate 1-dimensional calculus operations in real time. It uses an alpha-beta filter to smoothen and numerically differentiate a variable. The library can also integrate the variable should one choose to. Note the library relies on polling to provide a continuously changing variable to refresh the calculus operations.

image

Other

For the sake of simplicity and to reduce memory use, the library uses micros() to update the timestep. This step is shared by all instances of alpha-beta filter.

References:

Code

The library can be found at the following repository: basicCalculus