3 * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
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 "audiocheblimit.h"
86 #define GST_CAT_DEFAULT gst_audio_cheb_limit_debug
87 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
89 static const GstElementDetails element_details =
90 GST_ELEMENT_DETAILS ("AudioChebLimit",
91 "Filter/Effect/Audio",
92 "Chebyshev low pass and high pass filter",
93 "Sebastian Dröge <slomo@circular-chaos.org>");
95 /* Filter signals and args */
112 #define ALLOWED_CAPS \
113 "audio/x-raw-float," \
114 " width = (int) { 32, 64 }, " \
115 " endianness = (int) BYTE_ORDER," \
116 " rate = (int) [ 1, MAX ]," \
117 " channels = (int) [ 1, MAX ]"
119 #define DEBUG_INIT(bla) \
120 GST_DEBUG_CATEGORY_INIT (gst_audio_cheb_limit_debug, "audiocheblimit", 0, "audiocheblimit element");
122 GST_BOILERPLATE_FULL (GstAudioChebLimit,
123 gst_audio_cheb_limit, GstAudioFilter, GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
125 static void gst_audio_cheb_limit_set_property (GObject * object,
126 guint prop_id, const GValue * value, GParamSpec * pspec);
127 static void gst_audio_cheb_limit_get_property (GObject * object,
128 guint prop_id, GValue * value, GParamSpec * pspec);
130 static gboolean gst_audio_cheb_limit_setup (GstAudioFilter * filter,
131 GstRingBufferSpec * format);
133 gst_audio_cheb_limit_transform_ip (GstBaseTransform * base, GstBuffer * buf);
134 static gboolean gst_audio_cheb_limit_start (GstBaseTransform * base);
136 static void process_64 (GstAudioChebLimit * filter,
137 gdouble * data, guint num_samples);
138 static void process_32 (GstAudioChebLimit * filter,
139 gfloat * data, guint num_samples);
147 #define GST_TYPE_AUDIO_CHEBYSHEV_FREQ_LIMIT_MODE (gst_audio_cheb_limit_mode_get_type ())
149 gst_audio_cheb_limit_mode_get_type (void)
151 static GType gtype = 0;
154 static const GEnumValue values[] = {
155 {MODE_LOW_PASS, "Low pass (default)",
157 {MODE_HIGH_PASS, "High pass",
162 gtype = g_enum_register_static ("GstAudioChebLimitMode", values);
167 /* GObject vmethod implementations */
170 gst_audio_cheb_limit_base_init (gpointer klass)
172 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
175 gst_element_class_set_details (element_class, &element_details);
177 caps = gst_caps_from_string (ALLOWED_CAPS);
178 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
180 gst_caps_unref (caps);
184 gst_audio_cheb_limit_dispose (GObject * object)
186 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
198 if (filter->channels) {
199 GstAudioChebLimitChannelCtx *ctx;
200 gint i, channels = GST_AUDIO_FILTER (filter)->format.channels;
202 for (i = 0; i < channels; i++) {
203 ctx = &filter->channels[i];
208 g_free (filter->channels);
209 filter->channels = NULL;
212 G_OBJECT_CLASS (parent_class)->dispose (object);
216 gst_audio_cheb_limit_class_init (GstAudioChebLimitClass * klass)
218 GObjectClass *gobject_class;
219 GstBaseTransformClass *trans_class;
220 GstAudioFilterClass *filter_class;
222 gobject_class = (GObjectClass *) klass;
223 trans_class = (GstBaseTransformClass *) klass;
224 filter_class = (GstAudioFilterClass *) klass;
226 gobject_class->set_property = gst_audio_cheb_limit_set_property;
227 gobject_class->get_property = gst_audio_cheb_limit_get_property;
228 gobject_class->dispose = gst_audio_cheb_limit_dispose;
230 g_object_class_install_property (gobject_class, PROP_MODE,
231 g_param_spec_enum ("mode", "Mode",
232 "Low pass or high pass mode",
233 GST_TYPE_AUDIO_CHEBYSHEV_FREQ_LIMIT_MODE, MODE_LOW_PASS,
234 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
235 g_object_class_install_property (gobject_class, PROP_TYPE,
236 g_param_spec_int ("type", "Type", "Type of the chebychev filter", 1, 2, 1,
237 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
239 /* FIXME: Don't use the complete possible range but restrict the upper boundary
240 * so automatically generated UIs can use a slider without */
241 g_object_class_install_property (gobject_class, PROP_CUTOFF,
242 g_param_spec_float ("cutoff", "Cutoff", "Cut off frequency (Hz)", 0.0,
243 100000.0, 0.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
244 g_object_class_install_property (gobject_class, PROP_RIPPLE,
245 g_param_spec_float ("ripple", "Ripple", "Amount of ripple (dB)", 0.0,
246 200.0, 0.25, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
248 /* FIXME: What to do about this upper boundary? With a cutoff frequency of
249 * rate/4 32 poles are completely possible, with a cutoff frequency very low
250 * or very high 16 poles already produces only noise */
251 g_object_class_install_property (gobject_class, PROP_POLES,
252 g_param_spec_int ("poles", "Poles",
253 "Number of poles to use, will be rounded up to the next even number",
254 2, 32, 4, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
256 filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_cheb_limit_setup);
257 trans_class->transform_ip =
258 GST_DEBUG_FUNCPTR (gst_audio_cheb_limit_transform_ip);
259 trans_class->start = GST_DEBUG_FUNCPTR (gst_audio_cheb_limit_start);
263 gst_audio_cheb_limit_init (GstAudioChebLimit * filter,
264 GstAudioChebLimitClass * klass)
266 filter->cutoff = 0.0;
267 filter->mode = MODE_LOW_PASS;
270 filter->ripple = 0.25;
271 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
273 filter->have_coeffs = FALSE;
276 filter->channels = NULL;
280 generate_biquad_coefficients (GstAudioChebLimit * filter,
281 gint p, gdouble * a0, gdouble * a1, gdouble * a2,
282 gdouble * b1, gdouble * b2)
284 gint np = filter->poles;
285 gdouble ripple = filter->ripple;
287 /* pole location in s-plane */
290 /* zero location in s-plane */
291 gdouble rz = 0.0, iz = 0.0;
293 /* transfer function coefficients for the z-plane */
294 gdouble x0, x1, x2, y1, y2;
295 gint type = filter->type;
297 /* Calculate pole location for lowpass at frequency 1 */
299 gdouble angle = (M_PI / 2.0) * (2.0 * p - 1) / np;
305 /* If we allow ripple, move the pole from the unit
306 * circle to an ellipse and keep cutoff at frequency 1 */
307 if (ripple > 0 && type == 1) {
310 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
312 vx = (1.0 / np) * asinh (1.0 / es);
315 } else if (type == 2) {
318 es = sqrt (pow (10.0, ripple / 10.0) - 1.0);
319 vx = (1.0 / np) * asinh (es);
324 /* Calculate inverse of the pole location to convert from
325 * type I to type II */
327 gdouble mag2 = rp * rp + ip * ip;
333 /* Calculate zero location for frequency 1 on the
334 * unit circle for type 2 */
336 gdouble angle = M_PI / (np * 2.0) + ((p - 1) * M_PI) / (np);
341 mag2 = rz * rz + iz * iz;
346 /* Convert from s-domain to z-domain by
347 * using the bilinear Z-transform, i.e.
348 * substitute s by (2/t)*((z-1)/(z+1))
349 * with t = 2 * tan(0.5).
355 m = rp * rp + ip * ip;
356 d = 4.0 - 4.0 * rp * t + m * t * t;
361 y1 = (8.0 - 2.0 * m * t * t) / d;
362 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
367 m = rp * rp + ip * ip;
368 d = 4.0 - 4.0 * rp * t + m * t * t;
370 x0 = (t * t * iz * iz + 4.0) / d;
371 x1 = (-8.0 + 2.0 * iz * iz * t * t) / d;
373 y1 = (8.0 - 2.0 * m * t * t) / d;
374 y2 = (-4.0 - 4.0 * rp * t - m * t * t) / d;
377 /* Convert from lowpass at frequency 1 to either lowpass
380 * For lowpass substitute z^(-1) with:
387 * k = sin((1-w)/2) / sin((1+w)/2)
389 * For highpass substitute z^(-1) with:
397 * k = -cos((1+w)/2) / cos((1-w)/2)
403 2.0 * M_PI * (filter->cutoff / GST_AUDIO_FILTER (filter)->format.rate);
405 if (filter->mode == MODE_LOW_PASS)
406 k = sin ((1.0 - omega) / 2.0) / sin ((1.0 + omega) / 2.0);
408 k = -cos ((omega + 1.0) / 2.0) / cos ((omega - 1.0) / 2.0);
410 d = 1.0 + y1 * k - y2 * k * k;
411 *a0 = (x0 + k * (-x1 + k * x2)) / d;
412 *a1 = (x1 + k * k * x1 - 2.0 * k * (x0 + x2)) / d;
413 *a2 = (x0 * k * k - x1 * k + x2) / d;
414 *b1 = (2.0 * k + y1 + y1 * k * k - 2.0 * y2 * k) / d;
415 *b2 = (-k * k - y1 * k + y2) / d;
417 if (filter->mode == MODE_HIGH_PASS) {
424 /* Evaluate the transfer function that corresponds to the IIR
425 * coefficients at zr + zi*I and return the magnitude */
427 calculate_gain (gdouble * a, gdouble * b, gint num_a, gint num_b, gdouble zr,
430 gdouble sum_ar, sum_ai;
431 gdouble sum_br, sum_bi;
432 gdouble gain_r, gain_i;
441 for (i = num_a; i >= 0; i--) {
445 sum_ar = (sum_r_old * zr - sum_i_old * zi) + a[i];
446 sum_ai = (sum_r_old * zi + sum_i_old * zr) + 0.0;
451 for (i = num_b; i >= 0; i--) {
455 sum_br = (sum_r_old * zr - sum_i_old * zi) - b[i];
456 sum_bi = (sum_r_old * zi + sum_i_old * zr) - 0.0;
462 (sum_ar * sum_br + sum_ai * sum_bi) / (sum_br * sum_br + sum_bi * sum_bi);
464 (sum_ai * sum_br - sum_ar * sum_bi) / (sum_br * sum_br + sum_bi * sum_bi);
466 return (sqrt (gain_r * gain_r + gain_i * gain_i));
470 generate_coefficients (GstAudioChebLimit * filter)
472 gint channels = GST_AUDIO_FILTER (filter)->format.channels;
484 if (filter->channels) {
485 GstAudioChebLimitChannelCtx *ctx;
488 for (i = 0; i < channels; i++) {
489 ctx = &filter->channels[i];
494 g_free (filter->channels);
495 filter->channels = NULL;
498 if (GST_AUDIO_FILTER (filter)->format.rate == 0) {
500 filter->a = g_new0 (gdouble, 1);
503 filter->channels = g_new0 (GstAudioChebLimitChannelCtx, channels);
504 GST_LOG_OBJECT (filter, "rate was not set yet");
508 filter->have_coeffs = TRUE;
510 if (filter->cutoff >= GST_AUDIO_FILTER (filter)->format.rate / 2.0) {
512 filter->a = g_new0 (gdouble, 1);
513 filter->a[0] = (filter->mode == MODE_LOW_PASS) ? 1.0 : 0.0;
515 filter->channels = g_new0 (GstAudioChebLimitChannelCtx, channels);
516 GST_LOG_OBJECT (filter, "cutoff was higher than nyquist frequency");
518 } else if (filter->cutoff <= 0.0) {
520 filter->a = g_new0 (gdouble, 1);
521 filter->a[0] = (filter->mode == MODE_LOW_PASS) ? 0.0 : 1.0;
523 filter->channels = g_new0 (GstAudioChebLimitChannelCtx, channels);
524 GST_LOG_OBJECT (filter, "cutoff is lower than zero");
528 /* Calculate coefficients for the chebyshev filter */
530 gint np = filter->poles;
534 filter->num_a = np + 1;
535 filter->a = a = g_new0 (gdouble, np + 3);
536 filter->num_b = np + 1;
537 filter->b = b = g_new0 (gdouble, np + 3);
539 filter->channels = g_new0 (GstAudioChebLimitChannelCtx, channels);
540 for (i = 0; i < channels; i++) {
541 GstAudioChebLimitChannelCtx *ctx = &filter->channels[i];
543 ctx->x = g_new0 (gdouble, np + 1);
544 ctx->y = g_new0 (gdouble, np + 1);
547 /* Calculate transfer function coefficients */
551 for (p = 1; p <= np / 2; p++) {
552 gdouble a0, a1, a2, b1, b2;
553 gdouble *ta = g_new0 (gdouble, np + 3);
554 gdouble *tb = g_new0 (gdouble, np + 3);
556 generate_biquad_coefficients (filter, p, &a0, &a1, &a2, &b1, &b2);
558 memcpy (ta, a, sizeof (gdouble) * (np + 3));
559 memcpy (tb, b, sizeof (gdouble) * (np + 3));
561 /* add the new coefficients for the new two poles
562 * to the cascade by multiplication of the transfer
564 for (i = 2; i < np + 3; i++) {
565 a[i] = a0 * ta[i] + a1 * ta[i - 1] + a2 * ta[i - 2];
566 b[i] = tb[i] - b1 * tb[i - 1] - b2 * tb[i - 2];
572 /* Move coefficients to the beginning of the array
573 * and multiply the b coefficients with -1 to move from
574 * the transfer function's coefficients to the difference
575 * equation's coefficients */
577 for (i = 0; i <= np; i++) {
582 /* Normalize to unity gain at frequency 0 for lowpass
583 * and frequency 0.5 for highpass */
587 if (filter->mode == MODE_LOW_PASS)
588 gain = calculate_gain (a, b, np, np, 1.0, 0.0);
590 gain = calculate_gain (a, b, np, np, -1.0, 0.0);
592 for (i = 0; i <= np; i++) {
597 GST_LOG_OBJECT (filter,
598 "Generated IIR coefficients for the Chebyshev filter");
599 GST_LOG_OBJECT (filter,
600 "mode: %s, type: %d, poles: %d, cutoff: %.2f Hz, ripple: %.2f dB",
601 (filter->mode == MODE_LOW_PASS) ? "low-pass" : "high-pass",
602 filter->type, filter->poles, filter->cutoff, filter->ripple);
603 GST_LOG_OBJECT (filter, "%.2f dB gain @ 0 Hz",
604 20.0 * log10 (calculate_gain (a, b, np, np, 1.0, 0.0)));
607 2.0 * M_PI * (filter->cutoff /
608 GST_AUDIO_FILTER (filter)->format.rate);
609 gdouble zr = cos (wc), zi = sin (wc);
611 GST_LOG_OBJECT (filter, "%.2f dB gain @ %d Hz",
612 20.0 * log10 (calculate_gain (a, b, np, np, zr, zi)),
613 (int) filter->cutoff);
615 GST_LOG_OBJECT (filter, "%.2f dB gain @ %d Hz",
616 20.0 * log10 (calculate_gain (a, b, np, np, -1.0, 0.0)),
617 GST_AUDIO_FILTER (filter)->format.rate / 2);
622 gst_audio_cheb_limit_set_property (GObject * object, guint prop_id,
623 const GValue * value, GParamSpec * pspec)
625 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
629 GST_BASE_TRANSFORM_LOCK (filter);
630 filter->mode = g_value_get_enum (value);
631 generate_coefficients (filter);
632 GST_BASE_TRANSFORM_UNLOCK (filter);
635 GST_BASE_TRANSFORM_LOCK (filter);
636 filter->type = g_value_get_int (value);
637 generate_coefficients (filter);
638 GST_BASE_TRANSFORM_UNLOCK (filter);
641 GST_BASE_TRANSFORM_LOCK (filter);
642 filter->cutoff = g_value_get_float (value);
643 generate_coefficients (filter);
644 GST_BASE_TRANSFORM_UNLOCK (filter);
647 GST_BASE_TRANSFORM_LOCK (filter);
648 filter->ripple = g_value_get_float (value);
649 generate_coefficients (filter);
650 GST_BASE_TRANSFORM_UNLOCK (filter);
653 GST_BASE_TRANSFORM_LOCK (filter);
654 filter->poles = GST_ROUND_UP_2 (g_value_get_int (value));
655 generate_coefficients (filter);
656 GST_BASE_TRANSFORM_UNLOCK (filter);
659 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
665 gst_audio_cheb_limit_get_property (GObject * object, guint prop_id,
666 GValue * value, GParamSpec * pspec)
668 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (object);
672 g_value_set_enum (value, filter->mode);
675 g_value_set_int (value, filter->type);
678 g_value_set_float (value, filter->cutoff);
681 g_value_set_float (value, filter->ripple);
684 g_value_set_int (value, filter->poles);
687 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
692 /* GstAudioFilter vmethod implementations */
695 gst_audio_cheb_limit_setup (GstAudioFilter * base, GstRingBufferSpec * format)
697 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (base);
700 if (format->width == 32)
701 filter->process = (GstAudioChebLimitProcessFunc)
703 else if (format->width == 64)
704 filter->process = (GstAudioChebLimitProcessFunc)
709 filter->have_coeffs = FALSE;
714 static inline gdouble
715 process (GstAudioChebLimit * filter,
716 GstAudioChebLimitChannelCtx * ctx, gdouble x0)
718 gdouble val = filter->a[0] * x0;
721 for (i = 1, j = ctx->x_pos; i < filter->num_a; i++) {
722 val += filter->a[i] * ctx->x[j];
725 j = filter->num_a - 1;
728 for (i = 1, j = ctx->y_pos; i < filter->num_b; i++) {
729 val += filter->b[i] * ctx->y[j];
732 j = filter->num_b - 1;
737 if (ctx->x_pos > filter->num_a - 1)
739 ctx->x[ctx->x_pos] = x0;
744 if (ctx->y_pos > filter->num_b - 1)
747 ctx->y[ctx->y_pos] = val;
753 #define DEFINE_PROCESS_FUNC(width,ctype) \
755 process_##width (GstAudioChebLimit * filter, \
756 g##ctype * data, guint num_samples) \
758 gint i, j, channels = GST_AUDIO_FILTER (filter)->format.channels; \
761 for (i = 0; i < num_samples / channels; i++) { \
762 for (j = 0; j < channels; j++) { \
763 val = process (filter, &filter->channels[j], *data); \
769 DEFINE_PROCESS_FUNC (32, float);
770 DEFINE_PROCESS_FUNC (64, double);
772 #undef DEFINE_PROCESS_FUNC
774 /* GstBaseTransform vmethod implementations */
776 gst_audio_cheb_limit_transform_ip (GstBaseTransform * base, GstBuffer * buf)
778 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (base);
780 GST_BUFFER_SIZE (buf) / (GST_AUDIO_FILTER (filter)->format.width / 8);
782 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buf)))
783 gst_object_sync_values (G_OBJECT (filter), GST_BUFFER_TIMESTAMP (buf));
785 if (gst_base_transform_is_passthrough (base))
788 if (!filter->have_coeffs)
789 generate_coefficients (filter);
791 filter->process (filter, GST_BUFFER_DATA (buf), num_samples);
798 gst_audio_cheb_limit_start (GstBaseTransform * base)
800 GstAudioChebLimit *filter = GST_AUDIO_CHEB_LIMIT (base);
801 gint channels = GST_AUDIO_FILTER (filter)->format.channels;
802 GstAudioChebLimitChannelCtx *ctx;
805 /* Reset the history of input and output values if
806 * already existing */
807 if (channels && filter->channels) {
808 for (i = 0; i < channels; i++) {
809 ctx = &filter->channels[i];
811 memset (ctx->x, 0, (filter->poles + 1) * sizeof (gdouble));
813 memset (ctx->y, 0, (filter->poles + 1) * sizeof (gdouble));