Beast - Music Synthesizer and Composer  0.11.1+10.g2da35
gslfilter.hh
Go to the documentation of this file.
1  // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
2 #ifndef __GSL_FILTER_H__
3 #define __GSL_FILTER_H__
4 
5 #include <bse/bsemath.hh>
6 
7 
8 /* --- transformations --- */
9 static inline BseComplex bse_trans_s2z (BseComplex s);
10 static inline double bse_trans_freq2s (double w);
11 static inline double bse_trans_zepsilon2ss (double epsilon);
12 
13 
14 /* --- filter roots and poles --- */
15 void gsl_filter_butter_rp (uint iorder,
16  double freq, /* 0..pi */
17  double epsilon,
18  BseComplex *roots, /* [0..iorder-1] */
19  BseComplex *poles);
20 void gsl_filter_tscheb1_rp (uint iorder,
21  double freq, /* 0..pi */
22  double epsilon,
23  BseComplex *roots, /* [0..iorder-1] */
24  BseComplex *poles);
25 void gsl_filter_tscheb2_rp (uint iorder,
26  double c_freq, /* 0..pi */
27  double steepness,
28  double epsilon,
29  BseComplex *roots, /* [0..iorder-1] */
30  BseComplex *poles);
31 
32 
33 /* --- tschebyscheff type II steepness --- */
34 double gsl_filter_tscheb2_steepness_db (uint iorder,
35  double c_freq,
36  double epsilon,
37  double stopband_db);
38 double gsl_filter_tscheb2_steepness (uint iorder,
39  double c_freq,
40  double epsilon,
41  double residue);
42 
43 
44 /* --- lowpass filters --- */
45 void gsl_filter_butter_lp (uint iorder,
46  double freq, /* 0..pi */
47  double epsilon,
48  double *a, /* [0..iorder] */
49  double *b);
50 void gsl_filter_tscheb1_lp (uint iorder,
51  double freq, /* 0..pi */
52  double epsilon,
53  double *a, /* [0..iorder] */
54  double *b);
55 void gsl_filter_tscheb2_lp (uint iorder,
56  double c_freq, /* 0..pi */
57  double steepness,
58  double epsilon,
59  double *a, /* [0..iorder] */
60  double *b);
61 
62 
63 /* --- highpass filters --- */
64 void gsl_filter_butter_hp (uint iorder,
65  double freq, /* 0..pi */
66  double epsilon,
67  double *a, /* [0..iorder] */
68  double *b);
69 void gsl_filter_tscheb1_hp (uint iorder,
70  double freq, /* 0..pi */
71  double epsilon,
72  double *a, /* [0..iorder] */
73  double *b);
74 void gsl_filter_tscheb2_hp (uint iorder,
75  double c_freq, /* 0..pi */
76  double steepness,
77  double epsilon,
78  double *a, /* [0..iorder] */
79  double *b);
80 
81 
82 /* --- bandpass filters --- */
83 void gsl_filter_butter_bp (uint iorder,
84  double freq1, /* 0..pi */
85  double freq2, /* 0..pi */
86  double epsilon,
87  double *a, /* [0..iorder] */
88  double *b);
89 void gsl_filter_tscheb1_bp (uint iorder,
90  double freq1, /* 0..pi */
91  double freq2, /* 0..pi */
92  double epsilon,
93  double *a, /* [0..iorder] */
94  double *b);
95 void gsl_filter_tscheb2_bp (uint iorder,
96  double freq1, /* 0..pi */
97  double freq2, /* 0..pi */
98  double steepness,
99  double epsilon,
100  double *a, /* [0..iorder] */
101  double *b);
102 
103 
104 /* --- bandstop filters --- */
105 void gsl_filter_butter_bs (uint iorder,
106  double freq1, /* 0..pi */
107  double freq2, /* 0..pi */
108  double epsilon,
109  double *a, /* [0..iorder] */
110  double *b);
111 void gsl_filter_tscheb1_bs (uint iorder,
112  double freq1, /* 0..pi */
113  double freq2, /* 0..pi */
114  double epsilon,
115  double *a, /* [0..iorder] */
116  double *b);
117 void gsl_filter_tscheb2_bs (uint iorder,
118  double freq1, /* 0..pi */
119  double freq2, /* 0..pi */
120  double steepness,
121  double epsilon,
122  double *a, /* [0..iorder] */
123  double *b);
124 
125 
126 /* --- FIR Filters --- */
127 void gsl_filter_fir_approx (uint iorder,
128  double *a, /* [0..iorder] */
129  uint n_points,
130  const double *freq,
131  const double *value,
132  gboolean interpolate_db);
133 
134 
135 /* --- IIR Filter Evaluation --- */
136 typedef struct {
137  guint order;
138  gdouble *a; /* [0..order] */
139  gdouble *b; /* [0..order] */
140  gdouble *w; /* [0..2*order] */
141 } GslIIRFilter;
142 void gsl_iir_filter_setup (GslIIRFilter *f,
143  guint order,
144  const gdouble *a,
145  const gdouble *b,
146  gdouble *buffer); /* 4*(order+1) */
147 void gsl_iir_filter_change (GslIIRFilter *f,
148  guint order,
149  const gdouble *a,
150  const gdouble *b,
151  gdouble *buffer); /* 4*(order+1) */
152 void gsl_iir_filter_eval (GslIIRFilter *f,
153  guint n_values,
154  const gfloat *x,
155  gfloat *y);
156 
157 
158 /* --- Biquad Filters --- */
159 typedef enum /*< skip >*/
160 {
161  GSL_BIQUAD_NORMALIZE_PASSBAND,
162  GSL_BIQUAD_NORMALIZE_RESONANCE_GAIN,
163  GSL_BIQUAD_NORMALIZE_PEAK_GAIN
164 } GslBiquadNormalize;
165 
166 typedef enum /*< skip >*/
167 {
168  GSL_BIQUAD_RESONANT_LOWPASS = 1,
169  GSL_BIQUAD_RESONANT_HIGHPASS,
170  GSL_BIQUAD_LOWSHELVE,
171  GSL_BIQUAD_HIGHSHELVE,
172  GSL_BIQUAD_PEAK
173 } GslBiquadType;
174 
175 typedef struct {
176  GslBiquadType type;
177  GslBiquadNormalize normalize; /* high/low pass */
178  gfloat f_fn;
179  gfloat gain;
180  gfloat quality; /* peak/notch */
181  guint dirty : 1; /* post filter_config() changes? */
182  guint approx_values : 1; /* biquad_config_approx_*() called? */
183  /*< private >*/
184  gdouble k, v;
186 
187 typedef struct {
188  gdouble xc0, xc1, xc2;
189  gdouble yc1, yc2; /* yc0==1 */
190  gdouble xd1, xd2, yd1, yd2; /* history */
192 
193 void gsl_biquad_config_init (GslBiquadConfig *c,
194  GslBiquadType type,
195  GslBiquadNormalize normalize);
196 void gsl_biquad_config_setup (GslBiquadConfig *c,
197  gfloat f_fn,
198  gfloat gain,
199  gfloat quality);
200 void gsl_biquad_config_approx_freq (GslBiquadConfig *c,
201  gfloat f_fn);
202 void gsl_biquad_config_approx_gain (GslBiquadConfig *c,
203  gfloat gain);
204 void gsl_biquad_filter_config (GslBiquadFilter *f,
205  GslBiquadConfig *c,
206  gboolean reset_state);
207 void gsl_biquad_filter_eval (GslBiquadFilter *f,
208  guint n_values,
209  const gfloat *x,
210  gfloat *y);
211 
212 
213 /* --- filter scanning -- */
214 gdouble gsl_filter_sine_scan (guint order,
215  const gdouble *a,
216  const gdouble *b,
217  gdouble freq,
218  gdouble mix_freq);
219 
220 
221 /* --- implementations --- */
222 static inline BseComplex
223 bse_trans_s2z (BseComplex s)
224 {
225  /* 1 + (Td/2) * s
226  * z = ----------------
227  * 1 - (Td/2) * s
228  */
229  BseComplex one = { 1, 0 };
230  return bse_complex_div (bse_complex_add (one, s), bse_complex_sub (one, s));
231  /* return bse_complex_div (bse_complex_sub (s, one), bse_complex_add (s, one)); */
232 }
233 static inline double
234 bse_trans_freq2s (double w)
235 {
236  return tan (w / 2.);
237 }
238 static inline double
239 bse_trans_zepsilon2ss (double zepsilon)
240 {
241  double e2 = (1.0 - zepsilon) * (1.0 - zepsilon);
242  /* 1___ _________________
243  * | \ | 1.0
244  * |-----<---- 1 - zepsilon zepsilon = \ | ----------------
245  * |_______\________________ \| 1 + sepsilon^2
246  */
247  return sqrt ((1.0 - e2) / e2);
248 }
249 static inline double
250 bse_trans_freq2z (double w)
251 {
252  return atan (w) * 2.;
253 }
254 
255 
256 
257 #endif /* __GSL_FILTER_H__ */ /* vim:set ts=8 sw=2 sts=2: */
void gsl_filter_butter_hp(uint iorder, double freq, double epsilon, double *a, double *b)
Definition: gslfilter.cc:447
double gsl_filter_tscheb2_steepness_db(uint iorder, double c_freq, double epsilon, double stopband_db)
Definition: gslfilter.cc:298
Definition: gslfilter.hh:187
void gsl_filter_tscheb2_bp(uint iorder, double freq1, double freq2, double steepness, double epsilon, double *a, double *b)
Definition: gslfilter.cc:590
sqrt
Definition: bsemath.hh:37
Definition: gslfilter.hh:136
atan
void gsl_filter_tscheb1_hp(uint iorder, double freq, double epsilon, double *a, double *b)
Definition: gslfilter.cc:470
gdouble gsl_filter_sine_scan(guint order, const gdouble *a, const gdouble *b, gdouble freq, gdouble mix_freq)
Definition: gslfilter.cc:1306
double gsl_filter_tscheb2_steepness(uint iorder, double c_freq, double epsilon, double residue)
Definition: gslfilter.cc:316
void gsl_filter_fir_approx(uint iorder, double *a, uint n_points, const double *freq, const double *value, gboolean interpolate_db)
Definition: gslfilter.cc:847
void gsl_filter_tscheb2_lp(uint iorder, double c_freq, double steepness, double epsilon, double *a, double *b)
Definition: gslfilter.cc:412
void gsl_filter_tscheb2_bs(uint iorder, double freq1, double freq2, double steepness, double epsilon, double *a, double *b)
Definition: gslfilter.cc:696
void gsl_filter_butter_bp(uint iorder, double freq1, double freq2, double epsilon, double *a, double *b)
Definition: gslfilter.cc:521
void gsl_filter_tscheb1_bs(uint iorder, double freq1, double freq2, double epsilon, double *a, double *b)
Definition: gslfilter.cc:661
tan
void gsl_filter_butter_bs(uint iorder, double freq1, double freq2, double epsilon, double *a, double *b)
Definition: gslfilter.cc:627
void gsl_filter_tscheb2_hp(uint iorder, double c_freq, double steepness, double epsilon, double *a, double *b)
Definition: gslfilter.cc:494
void gsl_filter_tscheb1_bp(uint iorder, double freq1, double freq2, double epsilon, double *a, double *b)
Definition: gslfilter.cc:555
void gsl_filter_tscheb1_lp(uint iorder, double freq, double epsilon, double *a, double *b)
Definition: gslfilter.cc:373
void gsl_filter_butter_lp(uint iorder, double freq, double epsilon, double *a, double *b)
Definition: gslfilter.cc:343
Definition: gslfilter.hh:175