1 /* B.Angr
2  * Dynamic distorted bandpass filter plugin
3  *
4  * Copyright (C) 2021 by Sven Jähnichen
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 3, or (at your option)
9  * 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 Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef BUTTERWORTHFILTER_HPP_
22 #define BUTTERWORTHFILTER_HPP_
23 
24 #include <cmath>
25 #include <array>
26 
27 #define BUTTERWORTH_MAXORDER 16
28 
29 class ButterworthFilter
30 {
31 public:
ButterworthFilter()32 	ButterworthFilter() : ButterworthFilter (0) {}
33 
ButterworthFilter(const int order)34 	ButterworthFilter (const int order) :
35 		order (order),
36 		o2 (order / 2),
37 		f1 (1)
38 	{
39 		coeff0.fill (0);
40 		coeff1.fill (0);
41 		coeff2.fill (0);
42 		clear();
43 	}
44 
process(const float input)45 	float process (const float input)
46 	{
47 		output = input;
48 
49 		for (int i = 0; i < o2; ++i)
50 		{
51 			buffer0[i] = buffer1[i] * coeff1[i] + buffer2[i] * coeff2[i] + output;
52 			output = (buffer0[i] + buffer1[i] * f1 + buffer2[i]) * coeff0[i];
53 			buffer2[i] = buffer1[i];
54 			buffer1[i] = buffer0[i];
55 		}
56 
57 		return output;
58 	}
59 
get() const60 	float get() const {return output;}
61 
clear()62 	void clear()
63 	{
64 		buffer0.fill (0.0f);
65 		buffer1.fill (0.0f);
66 		buffer2.fill (0.0f);
67 	}
68 
69 
70 protected:
71 	int order;
72 	int o2;
73 	std::array <float, BUTTERWORTH_MAXORDER / 2> coeff0;
74 	std::array <float, BUTTERWORTH_MAXORDER / 2> coeff1;
75 	std::array <float, BUTTERWORTH_MAXORDER / 2> coeff2;
76 	float f1;
77 	std::array <float, BUTTERWORTH_MAXORDER / 2> buffer0;
78 	std::array <float, BUTTERWORTH_MAXORDER / 2> buffer1;
79 	std::array <float, BUTTERWORTH_MAXORDER / 2> buffer2;
80 	float output;
81 };
82 
83 #endif /* BUTTERWORTHFILTER_HPP_ */
84