1 /**
2  *  BiQuad filter
3  *
4  *  Copyright (C) 2006-2018 Teru Kamogashira
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
_FV3_(biquad)21 class _FV3_(biquad)
22 {
23  public:
24   _FV3_(biquad)();
25   void printconfig();
26   void mute();
27   _fv3_float_t get_A1(){return a1;}
28   _fv3_float_t get_A2(){return a2;}
29   _fv3_float_t get_B0(){return b0;}
30   _fv3_float_t get_B1(){return b1;}
31   _fv3_float_t get_B2(){return b2;}
32   void set_A1(_fv3_float_t v){a1=v;}
33   void set_A2(_fv3_float_t v){a2=v;}
34   void set_B0(_fv3_float_t v){b0=v;}
35   void set_B1(_fv3_float_t v){b1=v;}
36   void set_B2(_fv3_float_t v){b2=v;}
37 
38   void setCoefficients(_fv3_float_t _b0, _fv3_float_t _b1, _fv3_float_t _b2, _fv3_float_t _a1, _fv3_float_t _a2);
39 
40   void setAPF_RBJ(_fv3_float_t fc, _fv3_float_t bw, _fv3_float_t fs, unsigned mode);
41   void setLPF_RBJ(_fv3_float_t fc, _fv3_float_t bw, _fv3_float_t fs, unsigned mode);
42   void setHPF_RBJ(_fv3_float_t fc, _fv3_float_t bw, _fv3_float_t fs, unsigned mode);
43   void setBSF_RBJ(_fv3_float_t fc, _fv3_float_t bw, _fv3_float_t fs, unsigned mode);
44   void setBPF_RBJ(_fv3_float_t fc, _fv3_float_t bw, _fv3_float_t fs, unsigned mode);
45   void setBPFP_RBJ(_fv3_float_t fc, _fv3_float_t bw, _fv3_float_t fs, unsigned mode);
46   void setPeakEQ_RBJ(_fv3_float_t fc, _fv3_float_t gain, _fv3_float_t bw, _fv3_float_t fs);
47   void setLSF_RBJ(_fv3_float_t fc, _fv3_float_t gain, _fv3_float_t slope, _fv3_float_t fs);
48   void setHSF_RBJ(_fv3_float_t fc, _fv3_float_t gain, _fv3_float_t slope, _fv3_float_t fs);
49 
50   inline _fv3_float_t process(_fv3_float_t input)
51   {
52     return this->processd1(input);
53   }
54   inline _fv3_float_t operator()(_fv3_float_t input){return this->process(input);}
55 
56   // Direct form I
57   inline _fv3_float_t processd1(_fv3_float_t input)
58   {
59     _fv3_float_t i0 = input;
60     input *= b0;
61     input += b1 * i1 + b2 * i2;
62     input -= a1 * o1 + a2 * o2 ;
63     UNDENORMAL(input);
64     i2 = i1; i1 = i0;
65     o2 = o1; o1 = input;
66     return input;
67   }
68 
69   // Direct form II
70   inline _fv3_float_t processd2(_fv3_float_t input)
71   {
72     input -= a1 * t1 + a2 * t2 ;
73     t0 = input; input *= b0;
74     input += b1 * t1 + b2 * t2;
75     UNDENORMAL(input);
76     t2 = t1; t1 = t0;
77     return input;
78   }
79 
80  private:
81   _FV3_(biquad)(const _FV3_(biquad)& x);
82   _FV3_(biquad)& operator=(const _FV3_(biquad)& x);
83   _fv3_float_t calcAlpha(_fv3_float_t fc, _fv3_float_t bw, _fv3_float_t fs, unsigned mode);
84 
85   _fv3_float_t a1, a2, b0, b1, b2;
86   _fv3_float_t i1, i2, o1, o2, t0, t1, t2;
87 };
88 
_FV3_(iir_lr4)89 class _FV3_(iir_lr4)
90 {
91  public:
92   void setLPF(_fv3_float_t fc, _fv3_float_t fs)
93   { iir1.setLPF_RBJ(fc,FV3_BIQUAD_RBJ_Q_BUTTERWORTH,fs,FV3_BIQUAD_RBJ_Q);
94     iir2.setLPF_RBJ(fc,FV3_BIQUAD_RBJ_Q_BUTTERWORTH,fs,FV3_BIQUAD_RBJ_Q); }
95   void setHPF(_fv3_float_t fc, _fv3_float_t fs)
96   { iir1.setHPF_RBJ(fc,FV3_BIQUAD_RBJ_Q_BUTTERWORTH,fs,FV3_BIQUAD_RBJ_Q);
97     iir2.setHPF_RBJ(fc,FV3_BIQUAD_RBJ_Q_BUTTERWORTH,fs,FV3_BIQUAD_RBJ_Q); }
98 
99   inline _fv3_float_t process(_fv3_float_t input){ return this->processd1(input); }
100   inline _fv3_float_t operator()(_fv3_float_t input){ return this->process(input); }
101   inline _fv3_float_t processd1(_fv3_float_t input){ return iir2(iir1(input)); }
102   inline _fv3_float_t processd2(_fv3_float_t input){ return iir2.processd2(iir1.processd2(input)); }
103 
104  private:
105   _FV3_(iir_lr4)(const _FV3_(iir_lr4)& x );
106   _FV3_(iir_lr4)& operator=(const _FV3_(iir_lr4)& x);
107   _FV3_(biquad) iir1, iir2;
108 };
109