1 //          Copyright Jean Pierre Cimalando 2018.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 #pragma once
7 #include <cmath>
8 
9 struct AmpFollower
10 {
11     double p_ = 0;
12     double mem_ = 0;
13     void release(double t); // t = fs * release time
14     double process(double x);
15     void clear();
16     double last_output() const;
17 };
18 
release(double t)19 inline void AmpFollower::release(double t)
20 {
21     p_ = std::exp(-1.0 / t);
22 }
23 
process(double x)24 inline double AmpFollower::process(double x)
25 {
26     double y;
27     double ax = std::fabs(x);
28     double p = p_;
29     if (ax > mem_)
30         y = ax;
31     else
32         y = p * mem_ + (1.0 - p) * ax;
33     mem_ = y;
34     return y;
35 }
36 
clear()37 inline void AmpFollower::clear()
38 {
39     mem_ = 0;
40 }
41 
last_output()42 inline double AmpFollower::last_output() const
43 {
44     return mem_;
45 }
46