Debian Codegraph @

src/pt2_rcfilters.c

raw symbols SCIP
1 #include <math.h>
2 #include "pt2_header.h"
3 #include "pt2_rcfilters.h"
4
5 #define SMALL_NUMBER (1E-4)
6
7 // 1-pole 6dB/oct RC low-pass filter (Direct Form II)
8 void setupOnePoleFilter(double audioRate, double cutOff, onePoleFilter_t *f)
9 {
10 // this implementation should be OK as long as cutoff is below audioRate/4
11
12 if (cutOff >= audioRate/2.0)
13 cutOff = (audioRate/2.0) - SMALL_NUMBER;
14
15 const double b1 = exp((-2.0 * PI) * cutOff / audioRate);
16 const double a0 = 1.0 - b1;
17
18 f->b1 = (float)b1;
19 f->a0 = (float)a0;
20 }
21
22 void clearOnePoleFilterState(onePoleFilter_t *f)
23 {
24 f->tmpL = f->tmpR = 0.0f;
25 }
26
27 void onePoleLPFilter(onePoleFilter_t *f, const float in, float *out)
28 {
29 f->tmpL = (in * f->a0) + (f->tmpL * f->b1);
30 *out = f->tmpL;
31 }
32
33 void onePoleLPFilterStereo(onePoleFilter_t *f, const float *in, float *out)
34 {
35 // left channel
36 f->tmpL = (in[0] * f->a0) + (f->tmpL * f->b1);
37 out[0] = f->tmpL;
38
39 // right channel
40 f->tmpR = (in[1] * f->a0) + (f->tmpR * f->b1);
41 out[1] = f->tmpR;
42 }
43
44 void onePoleHPFilter(onePoleFilter_t *f, const float in, float *out)
45 {
46 f->tmpL = (in * f->a0) + (f->tmpL * f->b1);
47 *out = in - f->tmpL;
48 }
49
50 void onePoleHPFilterStereo(onePoleFilter_t *f, const float *in, float *out)
51 {
52 // left channel
53 f->tmpL = (in[0] * f->a0) + (f->tmpL * f->b1);
54 out[0] = in[0] - f->tmpL;
55
56 // right channel
57 f->tmpR = (in[1] * f->a0) + (f->tmpR * f->b1);
58 out[1] = in[1] - f->tmpR;
59 }
60
61 /* 2-pole RC low-pass filter with Q factor, based on:
62 ** https://www.musicdsp.org/en/latest/Filters/38-lp-and-hp-filter.html
63 */
64
65 void setupTwoPoleFilter(double audioRate, double cutOff, double qFactor, twoPoleFilter_t *f)
66 {
67 if (cutOff >= audioRate/2.0)
68 cutOff = (audioRate/2.0) - SMALL_NUMBER;
69
70 const double a = 1.0 / tan((PI * cutOff) / audioRate);
71 const double b = 1.0 / qFactor;
72
73 const double a1 = 1.0 / (1.0 + b * a + a * a);
74 const double a2 = (2.0 * a1);
75 const double b1 = 2.0 * (1.0 - a*a) * a1;
76 const double b2 = (1.0 - b * a + a * a) * a1;
77
78 f->a1 = (float)a1;
79 f->a2 = (float)a2;
80 f->b1 = (float)b1;
81 f->b2 = (float)b2;
82 }
83
84 void clearTwoPoleFilterState(twoPoleFilter_t *f)
85 {
86 f->tmpL[0] = f->tmpL[1] = f->tmpL[2] = f->tmpL[3] = 0.0f;
87 f->tmpR[0] = f->tmpR[1] = f->tmpR[2] = f->tmpR[3] = 0.0f;
88 }
89
90 void twoPoleLPFilter(twoPoleFilter_t *f, const float in, float *out)
91 {
92 const float LOut = (in * f->a1) + (f->tmpL[0] * f->a2) + (f->tmpL[1] * f->a1) - (f->tmpL[2] * f->b1) - (f->tmpL[3] * f->b2);
93
94 // shift states
95 f->tmpL[1] = f->tmpL[0];
96 f->tmpL[0] = in;
97 f->tmpL[3] = f->tmpL[2];
98 f->tmpL[2] = LOut;
99
100 // set output
101 *out = LOut;
102 }
103
104 void twoPoleLPFilterStereo(twoPoleFilter_t *f, const float *in, float *out)
105 {
106 const float LOut = (in[0] * f->a1) + (f->tmpL[0] * f->a2) + (f->tmpL[1] * f->a1) - (f->tmpL[2] * f->b1) - (f->tmpL[3] * f->b2);
107 const float ROut = (in[1] * f->a1) + (f->tmpR[0] * f->a2) + (f->tmpR[1] * f->a1) - (f->tmpR[2] * f->b1) - (f->tmpR[3] * f->b2);
108
109 // shift states
110
111 f->tmpL[1] = f->tmpL[0];
112 f->tmpL[0] = in[0];
113 f->tmpL[3] = f->tmpL[2];
114 f->tmpL[2] = LOut;
115
116 f->tmpR[1] = f->tmpR[0];
117 f->tmpR[0] = in[1];
118 f->tmpR[3] = f->tmpR[2];
119 f->tmpR[2] = ROut;
120
121 // set outputs
122
123 out[0] = LOut;
124 out[1] = ROut;
125 }