added integrator and differentiator

This commit is contained in:
Ahmet Inan 2019-02-28 21:40:53 +01:00
commit ca7a7ad56f
2 changed files with 49 additions and 0 deletions

View file

@ -89,6 +89,13 @@ Implemented are the following [trigger functions](https://en.wikipedia.org/wiki/
The [simple moving average](https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average) gives us the mean of the last N data points. The [simple moving average](https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average) gives us the mean of the last N data points.
### [calculus.hh](calculus.hh)
Some [calculus](https://en.wikipedia.org/wiki/Calculus) functions:
* [Integrator](https://en.wikipedia.org/wiki/Integrator)
* [Differentiator](https://en.wikipedia.org/wiki/Differentiator)
### [const.hh](const.hh) ### [const.hh](const.hh)
Some constants we need Some constants we need

42
calculus.hh Normal file
View file

@ -0,0 +1,42 @@
/*
Some calculus functions
Copyright 2019 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
namespace DSP {
template <typename TYPE>
class Integrator
{
TYPE sum;
public:
constexpr Integrator(TYPE sum = TYPE(0)) : sum(sum)
{
}
TYPE operator()(TYPE input)
{
return sum += input;
}
};
template <typename TYPE>
class Differentiator
{
TYPE prev;
public:
constexpr Differentiator(TYPE prev = TYPE(0)) : prev(prev)
{
}
TYPE operator()(TYPE input)
{
TYPE diff = input - prev;
prev = input;
return diff;
}
};
}