55#include " SignalProcessor.h"
66
77/* *
8- * DC Blocking IIR filter:
9- * Leaky differentiator.
8+ * DC Blocking IIR filter, aka Leaky differentiator.
9+ * Ref: https://www.dsprelated.com/freebooks/filters/DC_Blocker.html
1010 */
1111class DcBlockingFilter : public SignalProcessor {
1212private:
13- const float lambda;
1413 float x1, y1;
14+ float R;
1515public:
16- DcBlockingFilter (float lambda = 0.995 ): lambda(lambda ), x1(0 ), y1(0 ) {}
16+ DcBlockingFilter (float R = 0.995 ): R(R ), x1(0 ), y1(0 ) {}
1717
18+ /* *
19+ * Get adaptation time constant in samples.
20+ */
21+ float getTimeConstant (){
22+ return 1 /(1 -R); // approximate
23+ }
24+ /* *
25+ * Set adaptation time constant in samples.
26+ */
27+ void setTimeConstant (float tc){
28+ R = (tc - 1 ) / tc;
29+ }
1830 void reset (){
1931 x1 = y1 = 0 ;
2032 }
2133
2234 /* process a single sample and return the result */
2335 float process (float x){
24- y1 = x - x1 + lambda *y1;
36+ y1 = x - x1 + R *y1;
2537 x1 = x;
2638 return y1;
2739 }
@@ -31,7 +43,7 @@ class DcBlockingFilter : public SignalProcessor {
3143 float y = y1;
3244 while (size--){
3345 x = *input++;
34- y = x - x1 + lambda *y;
46+ y = x - x1 + R *y;
3547 x1 = x;
3648 *output++ = y;
3749 }
@@ -52,8 +64,8 @@ class DcBlockingFilter : public SignalProcessor {
5264 process (in, out, in.getSize ());
5365 }
5466
55- static DcBlockingFilter* create (float lambda =0.995 ){
56- return new DcBlockingFilter (lambda );
67+ static DcBlockingFilter* create (float R =0.995 ){
68+ return new DcBlockingFilter (R );
5769 }
5870
5971 static void destroy (DcBlockingFilter* obj){
@@ -65,14 +77,29 @@ class StereoDcBlockingFilter : public MultiSignalProcessor {
6577private:
6678 DcBlockingFilter left, right;
6779public:
68- StereoDcBlockingFilter (float lambda = 0.995 ): left(lambda), right(lambda) {}
80+ StereoDcBlockingFilter (float R = 0.995 ): left(R), right(R) {}
81+
82+ /* *
83+ * Get adaptation time constant in samples.
84+ */
85+ float getTimeConstant (){
86+ return left.getTimeConstant ();
87+ }
88+ /* *
89+ * Set adaptation time constant in samples.
90+ */
91+ void setTimeConstant (float tc){
92+ left.setTimeConstant (tc);
93+ right.setTimeConstant (tc);
94+ }
95+
6996 void process (AudioBuffer& input, AudioBuffer& output){
7097 left.process (input.getSamples (LEFT_CHANNEL), output.getSamples (LEFT_CHANNEL));
7198 right.process (input.getSamples (RIGHT_CHANNEL), output.getSamples (RIGHT_CHANNEL));
7299 }
73100
74- static StereoDcBlockingFilter* create (float lambda ){
75- return new StereoDcBlockingFilter (lambda );
101+ static StereoDcBlockingFilter* create (float R= 0.995 ){
102+ return new StereoDcBlockingFilter (R );
76103 }
77104
78105 static void destroy (StereoDcBlockingFilter* obj){
0 commit comments