3 * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Chebyshev type 1 filter design based on
23 * "The Scientist and Engineer's Guide to DSP", Chapter 20.
24 * http://www.dspguide.com/
26 * For type 2 and Chebyshev filters in general read
27 * http://en.wikipedia.org/wiki/Chebyshev_filter
32 * SECTION:element-audiocheblimit
33 * @short_description: Chebyshev low pass and high pass filter
37 * Attenuates all frequencies above the cutoff frequency (low-pass) or all frequencies below the
38 * cutoff frequency (high-pass). The number of poles and the ripple parameter control the rolloff.
41 * This element has the advantage over the windowed sinc lowpass and highpass filter that it is
42 * much faster and produces almost as good results. It's only disadvantages are the highly
43 * non-linear phase and the slower rolloff compared to a windowed sinc filter with a large kernel.
46 * For type 1 the ripple parameter specifies how much ripple in dB is allowed in the passband, i.e.
47 * some frequencies in the passband will be amplified by that value. A higher ripple value will allow
51 * For type 2 the ripple parameter specifies the stopband attenuation. In the stopband the gain will
52 * be at most this value. A lower ripple value will allow a faster rolloff.
55 * As a special case, a Chebyshev type 1 filter with no ripple is a Butterworth filter.
58 * Be warned that a too large number of poles can produce noise. The most poles are possible with
59 * a cutoff frequency at a quarter of the sampling rate.
61 * <title>Example launch line</title>
64 * gst-launch audiotestsrc freq=1500 ! audioconvert ! audiocheblimit mode=low-pass cutoff=1000 poles=4 ! audioconvert ! alsasink
65 * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiocheblimit mode=high-pass cutoff=400 ripple=0.2 ! audioconvert ! alsasink
66 * gst-launch audiotestsrc wave=white-noise ! audioconvert ! audiocheblimit mode=low-pass cutoff=800 type=2 ! audioconvert ! alsasink
77 #include <gst/base/gstbasetransform.h>
78 #include <gst/audio/audio.h>
79 #include <gst/audio/gstaudiofilter.h>
80 #include <gst/controller/gstcontroller.h>
84 #include "math_compat.h"
86 #include "audiocheblimit.h"
88 #define GST_CAT_DEFAULT gst_audio_cheb_limit_debug
89 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
101 #define DEBUG_INIT(bla) \
102 GST_DEBUG_CATEGORY_INIT (gst_audio_cheb_limit_debug, "audiocheblimit", 0, "audiocheblimit element");
104 GST_BOILERPLATE_FULL (GstAudioChebLimit,
105 gst_audio_cheb_limit, GstAudioFXBaseIIRFilter,
106 GST_TYPE_AUDIO_FX_BASE_IIR_FILTER, DEBUG_INIT);
108 static void gst_audio_cheb_limit_set_property (GObject * object,
109 guint prop_id, const GValue * value, GParamSpec * pspec);
110 static void gst_audio_cheb_limit_get_property (GObject * object,
111 guint prop_id, GValue * value, GParamSpec * pspec);
113 static gboolean gst_audio_cheb_limit_setup (GstAudioFilter * filter,
114 GstRingBufferSpec * format);
122 #define GST_TYPE_AUDIO_CHEBYSHEV_FREQ_LIMIT_MODE (gst_audio_cheb_limit_mode_get_type ())
124 gst_audio_cheb_limit_mode_get_type (void)
126 static GType gtype = 0;
129 static const GEnumValue values[] = {
130 {MODE_LOW_PASS, "Low pass (default)",
132 {MODE_HIGH_PASS, "High pass",
137 gtype = g_enum_register_static ("GstAudioChebLimitMode", values);
142 /* GObject vmethod implementations */
145 gst_audio_cheb_limit_base_init (gpointer klass)
147 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
149 gst_element_class_set_details_simple (element_class,
150 "Low pass & high pass filter",
151 "Filter/Effect/Audio",
152 "Chebyshev low pass and high pass filter",
153 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
157 gst_audio_cheb_limit_class_init (GstAudioChebLimitClass * klass)
159 GObjectClass *gobject_class = (GObjectClass *) klass;
160 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
162 gobject_class->set_property = gst_audio_cheb_limit_set_property;
163 gobject_class->get_property = gst_audio_cheb_limit_get_property;
165 g_object_class_install_property (gobject_class, PROP_MODE,
166 g_param_spec_enum ("mode", "Mode",
167 "Low pass or high pass mode",
168 GST_TYPE_AUDIO_CHEBYSHEV_FREQ_LIMIT_MODE, MODE_LOW_PASS,
169 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
170 g_object_class_install_property (gobject_class, PROP_TYPE,
171 g_param_spec_int ("type", "Type", "Type of the chebychev filter", 1, 2, 1,
172 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
174 /* FIXME: Don't use the complete possible range but restrict the upper boundary
175 * so automatically generated UIs can use a slider without */
176 g_object_class_install_property (gobject_class, PROP_CUTOFF,
177 g_param_spec_float ("cutoff", "Cutoff", "Cut off frequency (Hz)", 0.0,
179 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (gobject_class, PROP_RIPPLE,
181 g_param_spec_float ("ripple", "Ripple", "Amount of ripple (dB)", 0.0,
183 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
185 /* FIXME: What to do about this upper boundary? With a cutoff frequency of
186 * rate/4 32 poles are completely possible, with a cutoff frequency very low
187 * or very high 16 poles already produces only noise */
188 g_object_class_install_property (gobject_class, PROP_POLES,
189 g_param_spec_int ("poles", "Poles",
190 "Number of poles to use, will be rounded up to the next even number",
192 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
194 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_cheb_limit_setup);
198 gst_audio_cheb_limit_init (GstAudioChebLimit * filter,
199 GstAudioChebLimitClass * klass)
201 filter->cutoff = 0.0;
202 filter->mode = MODE_LOW_PASS;
205 filter->ripple = 0.25;
209 generate_biquad_coefficients (GstAudioChebLimit * filter,
210 gint p, gdouble * a0, gdouble * a1, gdouble * a2,
211 gdouble * b1, gdouble * b2)
213 gint np = filter->poles;
214 gdouble ripple = filter->ripple;
216 /* pole location in s-plane */
219 /* zero location in s-plane */
220 gdouble rz = 0.0, iz = 0.0;
222 /* transfer function coefficients for the z-plane */
223 gdouble x0, x1, x2, y1, y2;
224 gint type = filter->type;
226 /* Calculate pole location for lowpass at frequency 1 */
228 gdouble angle = (M_PI / 2.0) * (2.0 * p - 1) / np;
234 /* If we allow ripple, move the pole from the unit
235 * circle to an ellipse and keep cutoff at frequency 1 */
236 if (ripple > 0 && type == 1) {
239 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
241 vx = (1.0 / np) * asinh (1.0 / es);
244 } else if (type == 2) {
247 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
248 vx = (1.0 / np) * asinh (es);
253 /* Calculate inverse of the pole location to convert from
254 * type I to type II */
256 gdouble mag2 = rp * rp + ip * ip;
262 /* Calculate zero location for frequency 1 on the
263 * unit circle for type 2 */
265 gdouble angle = M_PI / (np * 2.0) + ((p - 1) * M_PI) / (np);
270 mag2 = rz * rz + iz * iz;
275 /* Convert from s-domain to z-domain by
276 * using the bilinear Z-transform, i.e.
277 * substitute s by (2/t)*((z-1)/(z+1))
278 * with t = 2 * tan(0.5).
284 m = rp * rp + ip * ip;
285 d = 4.0 - 4.0 * rp * t + m * t * t;
290 y1 = (8.0 - 2.0 * m * t * t) / d;
291 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
296 m = rp * rp + ip * ip;
297 d = 4.0 - 4.0 * rp * t + m * t * t;
299 x0 = (t * t * iz * iz + 4.0) / d;
300 x1 = (-8.0 + 2.0 * iz * iz * t * t) / d;
302 y1 = (8.0 - 2.0 * m * t * t) / d;
303 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
306 /* Convert from lowpass at frequency 1 to either lowpass
309 * For lowpass substitute z^(-1) with:
316 * k = sin((1-w)/2) / sin((1+w)/2)
318 * For highpass substitute z^(-1) with:
326 * k = -cos((1+w)/2) / cos((1-w)/2)
332 2.0 * M_PI * (filter->cutoff / GST_AUDIO_FILTER (filter)->format.rate);
334 if (filter->mode == MODE_LOW_PASS)
335 k = sin ((1.0 - omega) / 2.0) / sin ((1.0 + omega) / 2.0);
337 k = -cos ((omega + 1.0) / 2.0) / cos ((omega - 1.0) / 2.0);
339 d = 1.0 + y1 * k - y2 * k * k;
340 *a0 = (x0 + k * (-x1 + k * x2)) / d;
341 *a1 = (x1 + k * k * x1 - 2.0 * k * (x0 + x2)) / d;
342 *a2 = (x0 * k * k - x1 * k + x2) / d;
343 *b1 = (2.0 * k + y1 + y1 * k * k - 2.0 * y2 * k) / d;
344 *b2 = (-k * k - y1 * k + y2) / d;
346 if (filter->mode == MODE_HIGH_PASS) {
354 generate_coefficients (GstAudioChebLimit * filter)
356 if (GST_AUDIO_FILTER (filter)->format.rate == 0) {
357 gdouble *a = g_new0 (gdouble, 1);
360 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
361 (filter), a, 1, NULL, 0);
363 GST_LOG_OBJECT (filter, "rate was not set yet");
367 if (filter->cutoff >= GST_AUDIO_FILTER (filter)->format.rate / 2.0) {
368 gdouble *a = g_new0 (gdouble, 1);
370 a[0] = (filter->mode == MODE_LOW_PASS) ? 1.0 : 0.0;
371 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
372 (filter), a, 1, NULL, 0);
373 GST_LOG_OBJECT (filter, "cutoff was higher than nyquist frequency");
375 } else if (filter->cutoff <= 0.0) {
376 gdouble *a = g_new0 (gdouble, 1);
378 a[0] = (filter->mode == MODE_LOW_PASS) ? 0.0 : 1.0;
379 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
380 (filter), a, 1, NULL, 0);
381 GST_LOG_OBJECT (filter, "cutoff is lower than zero");
385 /* Calculate coefficients for the chebyshev filter */
387 gint np = filter->poles;
391 a = g_new0 (gdouble, np + 3);
392 b = g_new0 (gdouble, np + 3);
394 /* Calculate transfer function coefficients */
398 for (p = 1; p <= np / 2; p++) {
399 gdouble a0, a1, a2, b1, b2;
400 gdouble *ta = g_new0 (gdouble, np + 3);
401 gdouble *tb = g_new0 (gdouble, np + 3);
403 generate_biquad_coefficients (filter, p, &a0, &a1, &a2, &b1, &b2);
405 memcpy (ta, a, sizeof (gdouble) * (np + 3));
406 memcpy (tb, b, sizeof (gdouble) * (np + 3));
408 /* add the new coefficients for the new two poles
409 * to the cascade by multiplication of the transfer
411 for (i = 2; i < np + 3; i++) {
412 a[i] = a0 * ta[i] + a1 * ta[i - 1] + a2 * ta[i - 2];
413 b[i] = tb[i] - b1 * tb[i - 1] - b2 * tb[i - 2];
419 /* Move coefficients to the beginning of the array
420 * and multiply the b coefficients with -1 to move from
421 * the transfer function's coefficients to the difference
422 * equation's coefficients */
424 for (i = 0; i <= np; i++) {
429 /* Normalize to unity gain at frequency 0 for lowpass
430 * and frequency 0.5 for highpass */
434 if (filter->mode == MODE_LOW_PASS)
436 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
440 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
443 for (i = 0; i <= np; i++) {
448 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
449 (filter), a, np + 1, b, np + 1);
451 GST_LOG_OBJECT (filter,
452 "Generated IIR coefficients for the Chebyshev filter");
453 GST_LOG_OBJECT (filter,
454 "mode: %s, type: %d, poles: %d, cutoff: %.2f Hz, ripple: %.2f dB",
455 (filter->mode == MODE_LOW_PASS) ? "low-pass" : "high-pass",
456 filter->type, filter->poles, filter->cutoff, filter->ripple);
457 GST_LOG_OBJECT (filter, "%.2f dB gain @ 0 Hz",
458 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
461 #ifndef GST_DISABLE_GST_DEBUG
464 2.0 * M_PI * (filter->cutoff /
465 GST_AUDIO_FILTER (filter)->format.rate);
466 gdouble zr = cos (wc), zi = sin (wc);
468 GST_LOG_OBJECT (filter, "%.2f dB gain @ %d Hz",
469 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
470 b, np + 1, zr, zi)), (int) filter->cutoff);
474 GST_LOG_OBJECT (filter, "%.2f dB gain @ %d Hz",
475 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
477 GST_AUDIO_FILTER (filter)->format.rate / 2);
482 gst_audio_cheb_limit_set_property (GObject * object, guint prop_id,
483 const GValue * value, GParamSpec * pspec)
485 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
489 GST_OBJECT_LOCK (filter);
490 filter->mode = g_value_get_enum (value);
491 generate_coefficients (filter);
492 GST_OBJECT_UNLOCK (filter);
495 GST_OBJECT_LOCK (filter);
496 filter->type = g_value_get_int (value);
497 generate_coefficients (filter);
498 GST_OBJECT_UNLOCK (filter);
501 GST_OBJECT_LOCK (filter);
502 filter->cutoff = g_value_get_float (value);
503 generate_coefficients (filter);
504 GST_OBJECT_UNLOCK (filter);
507 GST_OBJECT_LOCK (filter);
508 filter->ripple = g_value_get_float (value);
509 generate_coefficients (filter);
510 GST_OBJECT_UNLOCK (filter);
513 GST_OBJECT_LOCK (filter);
514 filter->poles = GST_ROUND_UP_2 (g_value_get_int (value));
515 generate_coefficients (filter);
516 GST_OBJECT_UNLOCK (filter);
519 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
525 gst_audio_cheb_limit_get_property (GObject * object, guint prop_id,
526 GValue * value, GParamSpec * pspec)
528 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
532 g_value_set_enum (value, filter->mode);
535 g_value_set_int (value, filter->type);
538 g_value_set_float (value, filter->cutoff);
541 g_value_set_float (value, filter->ripple);
544 g_value_set_int (value, filter->poles);
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
552 /* GstAudioFilter vmethod implementations */
555 gst_audio_cheb_limit_setup (GstAudioFilter * base, GstRingBufferSpec * format)
557 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (base);
559 generate_coefficients (filter);
561 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format);