1 /*
2 * GStreamer
3 * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /*
22 * Chebyshev type 1 filter design based on
23 * "The Scientist and Engineer's Guide to DSP", Chapter 20.
24 * http://www.dspguide.com/
25 *
26 * For type 2 and Chebyshev filters in general read
27 * http://en.wikipedia.org/wiki/Chebyshev_filter
28 *
29 */
30
31 /**
32 * SECTION:element-audiocheblimit
33 *
34 * Attenuates all frequencies above the cutoff frequency (low-pass) or all frequencies below the
35 * cutoff frequency (high-pass). The number of poles and the ripple parameter control the rolloff.
36 *
37 * This element has the advantage over the windowed sinc lowpass and highpass filter that it is
38 * much faster and produces almost as good results. It's only disadvantages are the highly
39 * non-linear phase and the slower rolloff compared to a windowed sinc filter with a large kernel.
40 *
41 * For type 1 the ripple parameter specifies how much ripple in dB is allowed in the passband, i.e.
42 * some frequencies in the passband will be amplified by that value. A higher ripple value will allow
43 * a faster rolloff.
44 *
45 * For type 2 the ripple parameter specifies the stopband attenuation. In the stopband the gain will
46 * be at most this value. A lower ripple value will allow a faster rolloff.
47 *
48 * As a special case, a Chebyshev type 1 filter with no ripple is a Butterworth filter.
49 *
50 * <note><para>
51 * Be warned that a too large number of poles can produce noise. The most poles are possible with
52 * a cutoff frequency at a quarter of the sampling rate.
53 * </para></note>
54 *
55 * <refsect2>
56 * <title>Example launch line</title>
57 * |[
58 * gst-launch-1.0 audiotestsrc freq=1500 ! audioconvert ! audiocheblimit mode=low-pass cutoff=1000 poles=4 ! audioconvert ! alsasink
59 * gst-launch-1.0 filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiocheblimit mode=high-pass cutoff=400 ripple=0.2 ! audioconvert ! alsasink
60 * gst-launch-1.0 audiotestsrc wave=white-noise ! audioconvert ! audiocheblimit mode=low-pass cutoff=800 type=2 ! audioconvert ! alsasink
61 * ]|
62 * </refsect2>
63 */
64
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68
69 #include <string.h>
70
71 #include <gst/gst.h>
72 #include <gst/base/gstbasetransform.h>
73 #include <gst/audio/audio.h>
74 #include <gst/audio/gstaudiofilter.h>
75
76 #include <math.h>
77
78 #include "math_compat.h"
79
80 #include "audiocheblimit.h"
81
82 #include "gst/glib-compat-private.h"
83
84 #define GST_CAT_DEFAULT gst_audio_cheb_limit_debug
85 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
86
87 enum
88 {
89 PROP_0,
90 PROP_MODE,
91 PROP_TYPE,
92 PROP_CUTOFF,
93 PROP_RIPPLE,
94 PROP_POLES
95 };
96
97 #define gst_audio_cheb_limit_parent_class parent_class
98 G_DEFINE_TYPE (GstAudioChebLimit,
99 gst_audio_cheb_limit, GST_TYPE_AUDIO_FX_BASE_IIR_FILTER);
100
101 static void gst_audio_cheb_limit_set_property (GObject * object,
102 guint prop_id, const GValue * value, GParamSpec * pspec);
103 static void gst_audio_cheb_limit_get_property (GObject * object,
104 guint prop_id, GValue * value, GParamSpec * pspec);
105 static void gst_audio_cheb_limit_finalize (GObject * object);
106
107 static gboolean gst_audio_cheb_limit_setup (GstAudioFilter * filter,
108 const GstAudioInfo * info);
109
110 enum
111 {
112 MODE_LOW_PASS = 0,
113 MODE_HIGH_PASS
114 };
115
116 #define GST_TYPE_AUDIO_CHEBYSHEV_FREQ_LIMIT_MODE (gst_audio_cheb_limit_mode_get_type ())
117 static GType
gst_audio_cheb_limit_mode_get_type(void)118 gst_audio_cheb_limit_mode_get_type (void)
119 {
120 static GType gtype = 0;
121
122 if (gtype == 0) {
123 static const GEnumValue values[] = {
124 {MODE_LOW_PASS, "Low pass (default)",
125 "low-pass"},
126 {MODE_HIGH_PASS, "High pass",
127 "high-pass"},
128 {0, NULL, NULL}
129 };
130
131 gtype = g_enum_register_static ("GstAudioChebLimitMode", values);
132 }
133 return gtype;
134 }
135
136 /* GObject vmethod implementations */
137
138 static void
gst_audio_cheb_limit_class_init(GstAudioChebLimitClass * klass)139 gst_audio_cheb_limit_class_init (GstAudioChebLimitClass * klass)
140 {
141 GObjectClass *gobject_class = (GObjectClass *) klass;
142 GstElementClass *gstelement_class = (GstElementClass *) klass;
143 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
144
145 GST_DEBUG_CATEGORY_INIT (gst_audio_cheb_limit_debug, "audiocheblimit", 0,
146 "audiocheblimit element");
147
148 gobject_class->set_property = gst_audio_cheb_limit_set_property;
149 gobject_class->get_property = gst_audio_cheb_limit_get_property;
150 gobject_class->finalize = gst_audio_cheb_limit_finalize;
151
152 g_object_class_install_property (gobject_class, PROP_MODE,
153 g_param_spec_enum ("mode", "Mode",
154 "Low pass or high pass mode",
155 GST_TYPE_AUDIO_CHEBYSHEV_FREQ_LIMIT_MODE, MODE_LOW_PASS,
156 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (gobject_class, PROP_TYPE,
158 g_param_spec_int ("type", "Type", "Type of the chebychev filter", 1, 2, 1,
159 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
160
161 /* FIXME: Don't use the complete possible range but restrict the upper boundary
162 * so automatically generated UIs can use a slider without */
163 g_object_class_install_property (gobject_class, PROP_CUTOFF,
164 g_param_spec_float ("cutoff", "Cutoff", "Cut off frequency (Hz)", 0.0,
165 100000.0, 0.0,
166 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
167 g_object_class_install_property (gobject_class, PROP_RIPPLE,
168 g_param_spec_float ("ripple", "Ripple", "Amount of ripple (dB)", 0.0,
169 200.0, 0.25,
170 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
171
172 /* FIXME: What to do about this upper boundary? With a cutoff frequency of
173 * rate/4 32 poles are completely possible, with a cutoff frequency very low
174 * or very high 16 poles already produces only noise */
175 g_object_class_install_property (gobject_class, PROP_POLES,
176 g_param_spec_int ("poles", "Poles",
177 "Number of poles to use, will be rounded up to the next even number",
178 2, 32, 4,
179 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
180
181 gst_element_class_set_static_metadata (gstelement_class,
182 "Low pass & high pass filter",
183 "Filter/Effect/Audio",
184 "Chebyshev low pass and high pass filter",
185 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
186
187 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_cheb_limit_setup);
188 }
189
190 static void
gst_audio_cheb_limit_init(GstAudioChebLimit * filter)191 gst_audio_cheb_limit_init (GstAudioChebLimit * filter)
192 {
193 filter->cutoff = 0.0;
194 filter->mode = MODE_LOW_PASS;
195 filter->type = 1;
196 filter->poles = 4;
197 filter->ripple = 0.25;
198
199 g_mutex_init (&filter->lock);
200 }
201
202 static void
generate_biquad_coefficients(GstAudioChebLimit * filter,gint p,gint rate,gdouble * b0,gdouble * b1,gdouble * b2,gdouble * a1,gdouble * a2)203 generate_biquad_coefficients (GstAudioChebLimit * filter,
204 gint p, gint rate, gdouble * b0, gdouble * b1, gdouble * b2,
205 gdouble * a1, gdouble * a2)
206 {
207 gint np = filter->poles;
208 gdouble ripple = filter->ripple;
209
210 /* pole location in s-plane */
211 gdouble rp, ip;
212
213 /* zero location in s-plane */
214 gdouble iz = 0.0;
215
216 /* transfer function coefficients for the z-plane */
217 gdouble x0, x1, x2, y1, y2;
218 gint type = filter->type;
219
220 /* Calculate pole location for lowpass at frequency 1 */
221 {
222 gdouble angle = (G_PI / 2.0) * (2.0 * p - 1) / np;
223
224 rp = -sin (angle);
225 ip = cos (angle);
226 }
227
228 /* If we allow ripple, move the pole from the unit
229 * circle to an ellipse and keep cutoff at frequency 1 */
230 if (ripple > 0 && type == 1) {
231 gdouble es, vx;
232
233 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
234
235 vx = (1.0 / np) * asinh (1.0 / es);
236 rp = rp * sinh (vx);
237 ip = ip * cosh (vx);
238 } else if (type == 2) {
239 gdouble es, vx;
240
241 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
242 vx = (1.0 / np) * asinh (es);
243 rp = rp * sinh (vx);
244 ip = ip * cosh (vx);
245 }
246
247 /* Calculate inverse of the pole location to convert from
248 * type I to type II */
249 if (type == 2) {
250 gdouble mag2 = rp * rp + ip * ip;
251
252 rp /= mag2;
253 ip /= mag2;
254 }
255
256 /* Calculate zero location for frequency 1 on the
257 * unit circle for type 2 */
258 if (type == 2) {
259 gdouble angle = G_PI / (np * 2.0) + ((p - 1) * G_PI) / (np);
260 gdouble mag2;
261
262 iz = cos (angle);
263 mag2 = iz * iz;
264 iz /= mag2;
265 }
266
267 /* Convert from s-domain to z-domain by
268 * using the bilinear Z-transform, i.e.
269 * substitute s by (2/t)*((z-1)/(z+1))
270 * with t = 2 * tan(0.5).
271 */
272 if (type == 1) {
273 gdouble t, m, d;
274
275 t = 2.0 * tan (0.5);
276 m = rp * rp + ip * ip;
277 d = 4.0 - 4.0 * rp * t + m * t * t;
278
279 x0 = (t * t) / d;
280 x1 = 2.0 * x0;
281 x2 = x0;
282 y1 = (8.0 - 2.0 * m * t * t) / d;
283 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
284 } else {
285 gdouble t, m, d;
286
287 t = 2.0 * tan (0.5);
288 m = rp * rp + ip * ip;
289 d = 4.0 - 4.0 * rp * t + m * t * t;
290
291 x0 = (t * t * iz * iz + 4.0) / d;
292 x1 = (-8.0 + 2.0 * iz * iz * t * t) / d;
293 x2 = x0;
294 y1 = (8.0 - 2.0 * m * t * t) / d;
295 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
296 }
297
298 /* Convert from lowpass at frequency 1 to either lowpass
299 * or highpass.
300 *
301 * For lowpass substitute z^(-1) with:
302 * -1
303 * z - k
304 * ------------
305 * -1
306 * 1 - k * z
307 *
308 * k = sin((1-w)/2) / sin((1+w)/2)
309 *
310 * For highpass substitute z^(-1) with:
311 *
312 * -1
313 * -z - k
314 * ------------
315 * -1
316 * 1 + k * z
317 *
318 * k = -cos((1+w)/2) / cos((1-w)/2)
319 *
320 */
321 {
322 gdouble k, d;
323 gdouble omega = 2.0 * G_PI * (filter->cutoff / rate);
324
325 if (filter->mode == MODE_LOW_PASS)
326 k = sin ((1.0 - omega) / 2.0) / sin ((1.0 + omega) / 2.0);
327 else
328 k = -cos ((omega + 1.0) / 2.0) / cos ((omega - 1.0) / 2.0);
329
330 d = 1.0 + y1 * k - y2 * k * k;
331 *b0 = (x0 + k * (-x1 + k * x2)) / d;
332 *b1 = (x1 + k * k * x1 - 2.0 * k * (x0 + x2)) / d;
333 *b2 = (x0 * k * k - x1 * k + x2) / d;
334 *a1 = (2.0 * k + y1 + y1 * k * k - 2.0 * y2 * k) / d;
335 *a2 = (-k * k - y1 * k + y2) / d;
336
337 if (filter->mode == MODE_HIGH_PASS) {
338 *a1 = -*a1;
339 *b1 = -*b1;
340 }
341 }
342 }
343
344 static void
generate_coefficients(GstAudioChebLimit * filter,const GstAudioInfo * info)345 generate_coefficients (GstAudioChebLimit * filter, const GstAudioInfo * info)
346 {
347 gint rate;
348
349 if (info) {
350 rate = GST_AUDIO_INFO_RATE (info);
351 } else {
352 rate = GST_AUDIO_FILTER_RATE (filter);
353 }
354
355 GST_LOG_OBJECT (filter, "cutoff %f", filter->cutoff);
356
357 if (rate == 0) {
358 gdouble *a = g_new0 (gdouble, 1);
359 gdouble *b = g_new0 (gdouble, 1);
360
361 a[0] = 1.0;
362 b[0] = 1.0;
363 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
364 (filter), a, 1, b, 1);
365
366 GST_LOG_OBJECT (filter, "rate was not set yet");
367 return;
368 }
369
370 if (filter->cutoff >= rate / 2.0) {
371 gdouble *a = g_new0 (gdouble, 1);
372 gdouble *b = g_new0 (gdouble, 1);
373
374 a[0] = 1.0;
375 b[0] = (filter->mode == MODE_LOW_PASS) ? 1.0 : 0.0;
376 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
377 (filter), a, 1, b, 1);
378 GST_LOG_OBJECT (filter, "cutoff was higher than nyquist frequency");
379 return;
380 } else if (filter->cutoff <= 0.0) {
381 gdouble *a = g_new0 (gdouble, 1);
382 gdouble *b = g_new0 (gdouble, 1);
383
384 a[0] = 1.0;
385 b[0] = (filter->mode == MODE_LOW_PASS) ? 0.0 : 1.0;
386 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
387 (filter), a, 1, b, 1);
388 GST_LOG_OBJECT (filter, "cutoff is lower than zero");
389 return;
390 }
391
392 /* Calculate coefficients for the chebyshev filter */
393 {
394 gint np = filter->poles;
395 gdouble *a, *b;
396 gint i, p;
397
398 a = g_new0 (gdouble, np + 3);
399 b = g_new0 (gdouble, np + 3);
400
401 /* Calculate transfer function coefficients */
402 a[2] = 1.0;
403 b[2] = 1.0;
404
405 for (p = 1; p <= np / 2; p++) {
406 gdouble b0, b1, b2, a1, a2;
407 gdouble *ta = g_new0 (gdouble, np + 3);
408 gdouble *tb = g_new0 (gdouble, np + 3);
409
410 generate_biquad_coefficients (filter, p, rate, &b0, &b1, &b2, &a1, &a2);
411
412 memcpy (ta, a, sizeof (gdouble) * (np + 3));
413 memcpy (tb, b, sizeof (gdouble) * (np + 3));
414
415 /* add the new coefficients for the new two poles
416 * to the cascade by multiplication of the transfer
417 * functions */
418 for (i = 2; i < np + 3; i++) {
419 b[i] = b0 * tb[i] + b1 * tb[i - 1] + b2 * tb[i - 2];
420 a[i] = ta[i] - a1 * ta[i - 1] - a2 * ta[i - 2];
421 }
422 g_free (ta);
423 g_free (tb);
424 }
425
426 /* Move coefficients to the beginning of the array to move from
427 * the transfer function's coefficients to the difference
428 * equation's coefficients */
429 for (i = 0; i <= np; i++) {
430 a[i] = a[i + 2];
431 b[i] = b[i + 2];
432 }
433
434 /* Normalize to unity gain at frequency 0 for lowpass
435 * and frequency 0.5 for highpass */
436 {
437 gdouble gain;
438
439 if (filter->mode == MODE_LOW_PASS)
440 gain =
441 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
442 1.0, 0.0);
443 else
444 gain =
445 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
446 -1.0, 0.0);
447
448 for (i = 0; i <= np; i++) {
449 b[i] /= gain;
450 }
451 }
452
453 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
454 (filter), a, np + 1, b, np + 1);
455
456 GST_LOG_OBJECT (filter,
457 "Generated IIR coefficients for the Chebyshev filter");
458 GST_LOG_OBJECT (filter,
459 "mode: %s, type: %d, poles: %d, cutoff: %.2f Hz, ripple: %.2f dB",
460 (filter->mode == MODE_LOW_PASS) ? "low-pass" : "high-pass",
461 filter->type, filter->poles, filter->cutoff, filter->ripple);
462 GST_LOG_OBJECT (filter, "%.2f dB gain @ 0 Hz",
463 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
464 np + 1, 1.0, 0.0)));
465
466 #ifndef GST_DISABLE_GST_DEBUG
467 {
468 gdouble wc = 2.0 * G_PI * (filter->cutoff / rate);
469 gdouble zr = cos (wc), zi = sin (wc);
470
471 GST_LOG_OBJECT (filter, "%.2f dB gain @ %d Hz",
472 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
473 b, np + 1, zr, zi)), (int) filter->cutoff);
474 }
475 #endif
476
477 GST_LOG_OBJECT (filter, "%.2f dB gain @ %d Hz",
478 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
479 np + 1, -1.0, 0.0)), rate);
480 }
481 }
482
483 static void
gst_audio_cheb_limit_finalize(GObject * object)484 gst_audio_cheb_limit_finalize (GObject * object)
485 {
486 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
487
488 g_mutex_clear (&filter->lock);
489
490 G_OBJECT_CLASS (parent_class)->finalize (object);
491 }
492
493 static void
gst_audio_cheb_limit_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)494 gst_audio_cheb_limit_set_property (GObject * object, guint prop_id,
495 const GValue * value, GParamSpec * pspec)
496 {
497 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
498
499 switch (prop_id) {
500 case PROP_MODE:
501 g_mutex_lock (&filter->lock);
502 filter->mode = g_value_get_enum (value);
503 generate_coefficients (filter, NULL);
504 g_mutex_unlock (&filter->lock);
505 break;
506 case PROP_TYPE:
507 g_mutex_lock (&filter->lock);
508 filter->type = g_value_get_int (value);
509 generate_coefficients (filter, NULL);
510 g_mutex_unlock (&filter->lock);
511 break;
512 case PROP_CUTOFF:
513 g_mutex_lock (&filter->lock);
514 filter->cutoff = g_value_get_float (value);
515 generate_coefficients (filter, NULL);
516 g_mutex_unlock (&filter->lock);
517 break;
518 case PROP_RIPPLE:
519 g_mutex_lock (&filter->lock);
520 filter->ripple = g_value_get_float (value);
521 generate_coefficients (filter, NULL);
522 g_mutex_unlock (&filter->lock);
523 break;
524 case PROP_POLES:
525 g_mutex_lock (&filter->lock);
526 filter->poles = GST_ROUND_UP_2 (g_value_get_int (value));
527 generate_coefficients (filter, NULL);
528 g_mutex_unlock (&filter->lock);
529 break;
530 default:
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
532 break;
533 }
534 }
535
536 static void
gst_audio_cheb_limit_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)537 gst_audio_cheb_limit_get_property (GObject * object, guint prop_id,
538 GValue * value, GParamSpec * pspec)
539 {
540 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
541
542 switch (prop_id) {
543 case PROP_MODE:
544 g_value_set_enum (value, filter->mode);
545 break;
546 case PROP_TYPE:
547 g_value_set_int (value, filter->type);
548 break;
549 case PROP_CUTOFF:
550 g_value_set_float (value, filter->cutoff);
551 break;
552 case PROP_RIPPLE:
553 g_value_set_float (value, filter->ripple);
554 break;
555 case PROP_POLES:
556 g_value_set_int (value, filter->poles);
557 break;
558 default:
559 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
560 break;
561 }
562 }
563
564 /* GstAudioFilter vmethod implementations */
565
566 static gboolean
gst_audio_cheb_limit_setup(GstAudioFilter * base,const GstAudioInfo * info)567 gst_audio_cheb_limit_setup (GstAudioFilter * base, const GstAudioInfo * info)
568 {
569 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (base);
570
571 generate_coefficients (filter, info);
572
573 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, info);
574 }
575