diff --git a/README.md b/README.md index d8feffb..95c9e72 100644 --- a/README.md +++ b/README.md @@ -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. +### [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) Some constants we need diff --git a/calculus.hh b/calculus.hh new file mode 100644 index 0000000..6aced55 --- /dev/null +++ b/calculus.hh @@ -0,0 +1,42 @@ +/* +Some calculus functions + +Copyright 2019 Ahmet Inan +*/ + +#pragma once + +namespace DSP { + +template +class Integrator +{ + TYPE sum; +public: + constexpr Integrator(TYPE sum = TYPE(0)) : sum(sum) + { + } + TYPE operator()(TYPE input) + { + return sum += input; + } +}; + +template +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; + } +}; + +} +