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
29 * Transformation from lowpass to bandpass/bandreject:
30 * http://docs.dewresearch.com/DspHelp/html/IDH_LinearSystems_LowpassToBandPassZ.htm
31 * http://docs.dewresearch.com/DspHelp/html/IDH_LinearSystems_LowpassToBandStopZ.htm
36 * SECTION:element-audiochebband
38 * Attenuates all frequencies outside (bandpass) or inside (bandreject) of a frequency
39 * band. The number of poles and the ripple parameter control the rolloff.
41 * This element has the advantage over the windowed sinc bandpass and bandreject 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.
45 * For type 1 the ripple parameter specifies how much ripple in dB is allowed in the passband, i.e.
46 * some frequencies in the passband will be amplified by that value. A higher ripple value will allow
49 * For type 2 the ripple parameter specifies the stopband attenuation. In the stopband the gain will
50 * be at most this value. A lower ripple value will allow a faster rolloff.
52 * As a special case, a Chebyshev type 1 filter with no ripple is a Butterworth filter.
55 * Be warned that a too large number of poles can produce noise. The most poles are possible with
56 * a cutoff frequency at a quarter of the sampling rate.
60 * <title>Example launch line</title>
62 * gst-launch audiotestsrc freq=1500 ! audioconvert ! audiochebband mode=band-pass lower-frequency=1000 upper-frequenc=6000 poles=4 ! audioconvert ! alsasink
63 * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiochebband mode=band-reject lower-frequency=1000 upper-frequency=4000 ripple=0.2 ! audioconvert ! alsasink
64 * gst-launch audiotestsrc wave=white-noise ! audioconvert ! audiochebband mode=band-pass lower-frequency=1000 upper-frequency=4000 type=2 ! audioconvert ! alsasink
74 #include <gst/base/gstbasetransform.h>
75 #include <gst/audio/audio.h>
76 #include <gst/audio/gstaudiofilter.h>
77 #include <gst/controller/gstcontroller.h>
81 #include "math_compat.h"
83 #include "audiochebband.h"
85 #define GST_CAT_DEFAULT gst_audio_cheb_band_debug
86 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
99 #define gst_audio_cheb_band_parent_class parent_class
100 G_DEFINE_TYPE (GstAudioChebBand, gst_audio_cheb_band,
101 GST_TYPE_AUDIO_FX_BASE_IIR_FILTER);
103 static void gst_audio_cheb_band_set_property (GObject * object,
104 guint prop_id, const GValue * value, GParamSpec * pspec);
105 static void gst_audio_cheb_band_get_property (GObject * object,
106 guint prop_id, GValue * value, GParamSpec * pspec);
107 static void gst_audio_cheb_band_finalize (GObject * object);
109 static gboolean gst_audio_cheb_band_setup (GstAudioFilter * filter,
110 GstRingBufferSpec * format);
118 #define GST_TYPE_AUDIO_CHEBYSHEV_FREQ_BAND_MODE (gst_audio_cheb_band_mode_get_type ())
120 gst_audio_cheb_band_mode_get_type (void)
122 static GType gtype = 0;
125 static const GEnumValue values[] = {
126 {MODE_BAND_PASS, "Band pass (default)",
128 {MODE_BAND_REJECT, "Band reject",
133 gtype = g_enum_register_static ("GstAudioChebBandMode", values);
138 /* GObject vmethod implementations */
141 gst_audio_cheb_band_class_init (GstAudioChebBandClass * klass)
143 GObjectClass *gobject_class = (GObjectClass *) klass;
144 GstElementClass *gstelement_class = (GstElementClass *) klass;
145 GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
147 GST_DEBUG_CATEGORY_INIT (gst_audio_cheb_band_debug, "audiochebband", 0,
148 "audiochebband element");
150 gobject_class->set_property = gst_audio_cheb_band_set_property;
151 gobject_class->get_property = gst_audio_cheb_band_get_property;
152 gobject_class->finalize = gst_audio_cheb_band_finalize;
154 g_object_class_install_property (gobject_class, PROP_MODE,
155 g_param_spec_enum ("mode", "Mode",
156 "Low pass or high pass mode", GST_TYPE_AUDIO_CHEBYSHEV_FREQ_BAND_MODE,
158 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_class, PROP_TYPE,
160 g_param_spec_int ("type", "Type", "Type of the chebychev filter", 1, 2, 1,
161 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
163 /* FIXME: Don't use the complete possible range but restrict the upper boundary
164 * so automatically generated UIs can use a slider without */
165 g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
166 g_param_spec_float ("lower-frequency", "Lower frequency",
167 "Start frequency of the band (Hz)", 0.0, 100000.0,
169 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
170 g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
171 g_param_spec_float ("upper-frequency", "Upper frequency",
172 "Stop frequency of the band (Hz)", 0.0, 100000.0, 0.0,
173 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
174 g_object_class_install_property (gobject_class, PROP_RIPPLE,
175 g_param_spec_float ("ripple", "Ripple", "Amount of ripple (dB)", 0.0,
177 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
178 /* FIXME: What to do about this upper boundary? With a frequencies near
179 * rate/4 32 poles are completely possible, with frequencies very low
180 * or very high 16 poles already produces only noise */
181 g_object_class_install_property (gobject_class, PROP_POLES,
182 g_param_spec_int ("poles", "Poles",
183 "Number of poles to use, will be rounded up to the next multiply of four",
185 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
187 gst_element_class_set_details_simple (gstelement_class,
188 "Band pass & band reject filter", "Filter/Effect/Audio",
189 "Chebyshev band pass and band reject filter",
190 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
192 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_cheb_band_setup);
196 gst_audio_cheb_band_init (GstAudioChebBand * filter)
198 filter->lower_frequency = filter->upper_frequency = 0.0;
199 filter->mode = MODE_BAND_PASS;
202 filter->ripple = 0.25;
204 filter->lock = g_mutex_new ();
208 generate_biquad_coefficients (GstAudioChebBand * filter,
209 gint p, gdouble * a0, gdouble * a1, gdouble * a2, gdouble * a3,
210 gdouble * a4, gdouble * b1, gdouble * b2, gdouble * b3, gdouble * b4)
212 gint np = filter->poles / 2;
213 gdouble ripple = filter->ripple;
215 /* pole location in s-plane */
218 /* zero location in s-plane */
221 /* transfer function coefficients for the z-plane */
222 gdouble x0, x1, x2, y1, y2;
223 gint type = filter->type;
225 /* Calculate pole location for lowpass at frequency 1 */
227 gdouble angle = (G_PI / 2.0) * (2.0 * p - 1) / np;
233 /* If we allow ripple, move the pole from the unit
234 * circle to an ellipse and keep cutoff at frequency 1 */
235 if (ripple > 0 && type == 1) {
238 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
240 vx = (1.0 / np) * asinh (1.0 / es);
243 } else if (type == 2) {
246 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
247 vx = (1.0 / np) * asinh (es);
252 /* Calculate inverse of the pole location to move from
253 * type I to type II */
255 gdouble mag2 = rp * rp + ip * ip;
261 /* Calculate zero location for frequency 1 on the
262 * unit circle for type 2 */
264 gdouble angle = G_PI / (np * 2.0) + ((p - 1) * G_PI) / (np);
272 /* Convert from s-domain to z-domain by
273 * using the bilinear Z-transform, i.e.
274 * substitute s by (2/t)*((z-1)/(z+1))
275 * with t = 2 * tan(0.5).
281 m = rp * rp + ip * ip;
282 d = 4.0 - 4.0 * rp * t + m * t * t;
287 y1 = (8.0 - 2.0 * m * t * t) / d;
288 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
293 m = rp * rp + ip * ip;
294 d = 4.0 - 4.0 * rp * t + m * t * t;
296 x0 = (t * t * iz * iz + 4.0) / d;
297 x1 = (-8.0 + 2.0 * iz * iz * t * t) / d;
299 y1 = (8.0 - 2.0 * m * t * t) / d;
300 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
303 /* Convert from lowpass at frequency 1 to either bandpass
306 * For bandpass substitute z^(-1) with:
309 * -z + alpha * z - beta
310 * ----------------------------
312 * beta * z - alpha * z + 1
314 * alpha = (2*a*b)/(1+b)
316 * a = cos((w1 + w0)/2) / cos((w1 - w0)/2)
317 * b = tan(1/2) * cot((w1 - w0)/2)
319 * For bandreject substitute z^(-1) with:
322 * z - alpha * z + beta
323 * ----------------------------
325 * beta * z - alpha * z + 1
327 * alpha = (2*a)/(1+b)
329 * a = cos((w1 + w0)/2) / cos((w1 - w0)/2)
330 * b = tan(1/2) * tan((w1 - w0)/2)
337 2.0 * G_PI * (filter->lower_frequency /
338 GST_AUDIO_FILTER (filter)->format.rate);
340 2.0 * G_PI * (filter->upper_frequency /
341 GST_AUDIO_FILTER (filter)->format.rate);
343 if (filter->mode == MODE_BAND_PASS) {
344 a = cos ((w1 + w0) / 2.0) / cos ((w1 - w0) / 2.0);
345 b = tan (1.0 / 2.0) / tan ((w1 - w0) / 2.0);
347 alpha = (2.0 * a * b) / (1.0 + b);
348 beta = (b - 1.0) / (b + 1.0);
350 d = 1.0 + beta * (y1 - beta * y2);
352 *a0 = (x0 + beta * (-x1 + beta * x2)) / d;
353 *a1 = (alpha * (-2.0 * x0 + x1 + beta * x1 - 2.0 * beta * x2)) / d;
355 (-x1 - beta * beta * x1 + 2.0 * beta * (x0 + x2) +
356 alpha * alpha * (x0 - x1 + x2)) / d;
357 *a3 = (alpha * (x1 + beta * (-2.0 * x0 + x1) - 2.0 * x2)) / d;
358 *a4 = (beta * (beta * x0 - x1) + x2) / d;
359 *b1 = (alpha * (2.0 + y1 + beta * y1 - 2.0 * beta * y2)) / d;
361 (-y1 - beta * beta * y1 - alpha * alpha * (1.0 + y1 - y2) +
362 2.0 * beta * (-1.0 + y2)) / d;
363 *b3 = (alpha * (y1 + beta * (2.0 + y1) - 2.0 * y2)) / d;
364 *b4 = (-beta * beta - beta * y1 + y2) / d;
366 a = cos ((w1 + w0) / 2.0) / cos ((w1 - w0) / 2.0);
367 b = tan (1.0 / 2.0) * tan ((w1 - w0) / 2.0);
369 alpha = (2.0 * a) / (1.0 + b);
370 beta = (1.0 - b) / (1.0 + b);
372 d = -1.0 + beta * (beta * y2 + y1);
374 *a0 = (-x0 - beta * x1 - beta * beta * x2) / d;
375 *a1 = (alpha * (2.0 * x0 + x1 + beta * x1 + 2.0 * beta * x2)) / d;
377 (-x1 - beta * beta * x1 - 2.0 * beta * (x0 + x2) -
378 alpha * alpha * (x0 + x1 + x2)) / d;
379 *a3 = (alpha * (x1 + beta * (2.0 * x0 + x1) + 2.0 * x2)) / d;
380 *a4 = (-beta * beta * x0 - beta * x1 - x2) / d;
381 *b1 = (alpha * (-2.0 + y1 + beta * y1 + 2.0 * beta * y2)) / d;
383 -(y1 + beta * beta * y1 + 2.0 * beta * (-1.0 + y2) +
384 alpha * alpha * (-1.0 + y1 + y2)) / d;
385 *b3 = (alpha * (beta * (-2.0 + y1) + y1 + 2.0 * y2)) / d;
386 *b4 = -(-beta * beta + beta * y1 + y2) / d;
392 generate_coefficients (GstAudioChebBand * filter)
394 if (GST_AUDIO_FILTER (filter)->format.rate == 0) {
395 gdouble *a = g_new0 (gdouble, 1);
398 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
399 (filter), a, 1, NULL, 0);
400 GST_LOG_OBJECT (filter, "rate was not set yet");
404 if (filter->upper_frequency <= filter->lower_frequency) {
405 gdouble *a = g_new0 (gdouble, 1);
407 a[0] = (filter->mode == MODE_BAND_PASS) ? 0.0 : 1.0;
408 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
409 (filter), a, 1, NULL, 0);
411 GST_LOG_OBJECT (filter, "frequency band had no or negative dimension");
415 if (filter->upper_frequency > GST_AUDIO_FILTER (filter)->format.rate / 2) {
416 filter->upper_frequency = GST_AUDIO_FILTER (filter)->format.rate / 2;
417 GST_LOG_OBJECT (filter, "clipped upper frequency to nyquist frequency");
420 if (filter->lower_frequency < 0.0) {
421 filter->lower_frequency = 0.0;
422 GST_LOG_OBJECT (filter, "clipped lower frequency to 0.0");
425 /* Calculate coefficients for the chebyshev filter */
427 gint np = filter->poles;
431 a = g_new0 (gdouble, np + 5);
432 b = g_new0 (gdouble, np + 5);
434 /* Calculate transfer function coefficients */
438 for (p = 1; p <= np / 4; p++) {
439 gdouble a0, a1, a2, a3, a4, b1, b2, b3, b4;
440 gdouble *ta = g_new0 (gdouble, np + 5);
441 gdouble *tb = g_new0 (gdouble, np + 5);
443 generate_biquad_coefficients (filter, p, &a0, &a1, &a2, &a3, &a4, &b1,
446 memcpy (ta, a, sizeof (gdouble) * (np + 5));
447 memcpy (tb, b, sizeof (gdouble) * (np + 5));
449 /* add the new coefficients for the new two poles
450 * to the cascade by multiplication of the transfer
452 for (i = 4; i < np + 5; i++) {
454 a0 * ta[i] + a1 * ta[i - 1] + a2 * ta[i - 2] + a3 * ta[i - 3] +
457 tb[i] - b1 * tb[i - 1] - b2 * tb[i - 2] - b3 * tb[i - 3] -
464 /* Move coefficients to the beginning of the array
465 * and multiply the b coefficients with -1 to move from
466 * the transfer function's coefficients to the difference
467 * equation's coefficients */
469 for (i = 0; i <= np; i++) {
474 /* Normalize to unity gain at frequency 0 and frequency
475 * 0.5 for bandreject and unity gain at band center frequency
477 if (filter->mode == MODE_BAND_REJECT) {
478 /* gain is sqrt(H(0)*H(0.5)) */
481 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
484 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1,
487 gain1 = sqrt (gain1 * gain2);
489 for (i = 0; i <= np; i++) {
493 /* gain is H(wc), wc = center frequency */
496 2.0 * G_PI * (filter->lower_frequency /
497 GST_AUDIO_FILTER (filter)->format.rate);
499 2.0 * G_PI * (filter->upper_frequency /
500 GST_AUDIO_FILTER (filter)->format.rate);
501 gdouble w0 = (w2 + w1) / 2.0;
502 gdouble zr = cos (w0), zi = sin (w0);
504 gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b, np + 1, zr,
507 for (i = 0; i <= np; i++) {
512 gst_audio_fx_base_iir_filter_set_coefficients (GST_AUDIO_FX_BASE_IIR_FILTER
513 (filter), a, np + 1, b, np + 1);
515 GST_LOG_OBJECT (filter,
516 "Generated IIR coefficients for the Chebyshev filter");
517 GST_LOG_OBJECT (filter,
518 "mode: %s, type: %d, poles: %d, lower-frequency: %.2f Hz, upper-frequency: %.2f Hz, ripple: %.2f dB",
519 (filter->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject",
520 filter->type, filter->poles, filter->lower_frequency,
521 filter->upper_frequency, filter->ripple);
523 GST_LOG_OBJECT (filter, "%.2f dB gain @ 0Hz",
524 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
528 2.0 * G_PI * (filter->lower_frequency /
529 GST_AUDIO_FILTER (filter)->format.rate);
531 2.0 * G_PI * (filter->upper_frequency /
532 GST_AUDIO_FILTER (filter)->format.rate);
533 gdouble w0 = (w2 + w1) / 2.0;
538 GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
539 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
540 b, np + 1, zr, zi)), (int) filter->lower_frequency);
543 GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
544 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
546 (int) ((filter->lower_frequency + filter->upper_frequency) / 2.0));
549 GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
550 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1,
551 b, np + 1, zr, zi)), (int) filter->upper_frequency);
553 GST_LOG_OBJECT (filter, "%.2f dB gain @ %dHz",
554 20.0 * log10 (gst_audio_fx_base_iir_filter_calculate_gain (a, np + 1, b,
556 GST_AUDIO_FILTER (filter)->format.rate / 2);
561 gst_audio_cheb_band_finalize (GObject * object)
563 GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
565 g_mutex_free (filter->lock);
568 G_OBJECT_CLASS (parent_class)->finalize (object);
572 gst_audio_cheb_band_set_property (GObject * object, guint prop_id,
573 const GValue * value, GParamSpec * pspec)
575 GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
579 g_mutex_lock (filter->lock);
580 filter->mode = g_value_get_enum (value);
581 generate_coefficients (filter);
582 g_mutex_unlock (filter->lock);
585 g_mutex_lock (filter->lock);
586 filter->type = g_value_get_int (value);
587 generate_coefficients (filter);
588 g_mutex_unlock (filter->lock);
590 case PROP_LOWER_FREQUENCY:
591 g_mutex_lock (filter->lock);
592 filter->lower_frequency = g_value_get_float (value);
593 generate_coefficients (filter);
594 g_mutex_unlock (filter->lock);
596 case PROP_UPPER_FREQUENCY:
597 g_mutex_lock (filter->lock);
598 filter->upper_frequency = g_value_get_float (value);
599 generate_coefficients (filter);
600 g_mutex_unlock (filter->lock);
603 g_mutex_lock (filter->lock);
604 filter->ripple = g_value_get_float (value);
605 generate_coefficients (filter);
606 g_mutex_unlock (filter->lock);
609 g_mutex_lock (filter->lock);
610 filter->poles = GST_ROUND_UP_4 (g_value_get_int (value));
611 generate_coefficients (filter);
612 g_mutex_unlock (filter->lock);
615 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
621 gst_audio_cheb_band_get_property (GObject * object, guint prop_id,
622 GValue * value, GParamSpec * pspec)
624 GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (object);
628 g_value_set_enum (value, filter->mode);
631 g_value_set_int (value, filter->type);
633 case PROP_LOWER_FREQUENCY:
634 g_value_set_float (value, filter->lower_frequency);
636 case PROP_UPPER_FREQUENCY:
637 g_value_set_float (value, filter->upper_frequency);
640 g_value_set_float (value, filter->ripple);
643 g_value_set_int (value, filter->poles);
646 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
651 /* GstAudioFilter vmethod implementations */
654 gst_audio_cheb_band_setup (GstAudioFilter * base, GstRingBufferSpec * format)
656 GstAudioChebBand *filter = GST_AUDIO_CHEB_BAND (base);
658 generate_coefficients (filter);
660 return GST_AUDIO_FILTER_CLASS (parent_class)->setup (base, format);