1 /**
2 * Copyright 2017 Cut Through Recordings
3 * License: MIT License
4 * Author(s): Ethan Reker
5 */
6 module ddsp.filter.highpass;
7 
8 import ddsp.filter.biquad;
9 import std.math;
10 
11 /// First order highpass filter
12 class HighpassO1 : BiQuad
13 {
14 public:
15     override void calcCoefficients() nothrow @nogc
16     {
17         _thetac = 2 * PI * _frequency / _sampleRate;
18         _gamma = cos(_thetac) / (1 + sin(_thetac));
19         _a0 = (1 + _gamma) / 2;
20         _a1 = -_a0;
21         _a2 = 0.0;
22         _b1 = -_gamma;
23         _b2 = 0.0;
24     }
25 
26 private:
27     float _thetac;
28     float _gamma;
29 }
30 
31 /// Second order highpass filter
32 class HighpassO2 : BiQuad
33 {
34 public:
35     void setQualityFactor(float Q) nothrow @nogc
36     { 
37         if(Q != _q)
38         {
39             _q = Q;
40             calcCoefficients();
41         }
42     }
43 
44     override void calcCoefficients() nothrow @nogc
45     {
46         _thetac = 2 * PI * _frequency / _sampleRate;
47         _d0 = 1 / _q;
48         _beta = 0.5 * (1 - (_d0 / 2) * sin(_thetac)) / (1 + (_d0 / 2) * sin(_thetac));
49         _gamma = (0.5 + _beta) * cos(_thetac);
50         _a1 = -(0.5 + _beta + _gamma);
51         _a0 = (-_a1) / 2.0;
52         _a2 = _a0;
53         _b1 = -2.0 * _gamma;
54         _b2 = 2.0 * _beta;
55     }
56     
57 private:
58     float _thetac;
59     float _q = 0.707f;
60     float _beta;
61     float _gamma;
62 }
63 
64 /// Second order butterworth highpass filter
65 class ButterworthHP : BiQuad
66 {
67 public:
68     this() nothrow @nogc
69     {
70         super();
71     }
72     override void calcCoefficients() nothrow @nogc
73     {
74         _C = tan(PI * _frequency / _sampleRate);
75         _a0 = 1.0f / (1.0f + sqrt(2.0f) * _C + (_C * _C));
76         _a1 = -2.0f * _a0;
77         _a2 = _a0;
78         _b1 = 2.0f * _a0 * (_C * _C - 1.0f);
79         _b2 = _a0 * (1.0f - sqrt(2.0f) * _C + _C * _C);
80     }
81 
82 private:
83     float _C;
84 }
85 
86 /// Second order LinkwitzRiley highpass filter
87 class LinkwitzRileyHP : BiQuad
88 {
89 public:
90     override void calcCoefficients() nothrow @nogc
91     {
92         _theta = PI * _frequency / _sampleRate;
93         _omega = PI * _frequency;
94         _kappa = _omega / tan(_theta);
95         _delta = _kappa * _kappa + _omega * _omega + 2 * _kappa * _omega;
96         _a0 = (_kappa * _kappa) / _delta;
97         _a1 = -2 * _a0;
98         _a2 = _a0;
99         _b1 = (-2 * _kappa * _kappa + 2 * _omega * _omega) / _delta;
100         _b2 = (-2 * _kappa * _omega + _kappa * _kappa + _omega * _omega) / _delta;
101     }
102 private:
103     float _theta;
104     float _omega;
105     float _kappa;
106     float _delta;
107 }