summaryrefslogtreecommitdiff
path: root/cras/src/dsp/biquad.c
blob: 337a43938abb3d9d78cec43f12e1c9a207253198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Copyright (C) 2010 Google Inc. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE.WEBKIT file.
 */

#include <math.h>
#include "biquad.h"

#ifndef max
#define max(a, b)                                                              \
	({                                                                     \
		__typeof__(a) _a = (a);                                        \
		__typeof__(b) _b = (b);                                        \
		_a > _b ? _a : _b;                                             \
	})
#endif

#ifndef min
#define min(a, b)                                                              \
	({                                                                     \
		__typeof__(a) _a = (a);                                        \
		__typeof__(b) _b = (b);                                        \
		_a < _b ? _a : _b;                                             \
	})
#endif

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

static void set_coefficient(struct biquad *bq, double b0, double b1, double b2,
			    double a0, double a1, double a2)
{
	double a0_inv = 1 / a0;
	bq->b0 = b0 * a0_inv;
	bq->b1 = b1 * a0_inv;
	bq->b2 = b2 * a0_inv;
	bq->a1 = a1 * a0_inv;
	bq->a2 = a2 * a0_inv;
}

static void biquad_lowpass(struct biquad *bq, double cutoff, double resonance)
{
	/* Limit cutoff to 0 to 1. */
	cutoff = max(0.0, min(cutoff, 1.0));

	if (cutoff == 1 || cutoff == 0) {
		/* When cutoff is 1, the z-transform is 1.
		 * When cutoff is zero, nothing gets through the filter, so set
		 * coefficients up correctly.
		 */
		set_coefficient(bq, cutoff, 0, 0, 1, 0, 0);
		return;
	}

	/* Compute biquad coefficients for lowpass filter */
	resonance = max(0.0, resonance); /* can't go negative */
	double g = pow(10.0, 0.05 * resonance);
	double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);

	double theta = M_PI * cutoff;
	double sn = 0.5 * d * sin(theta);
	double beta = 0.5 * (1 - sn) / (1 + sn);
	double gamma = (0.5 + beta) * cos(theta);
	double alpha = 0.25 * (0.5 + beta - gamma);

	double b0 = 2 * alpha;
	double b1 = 2 * 2 * alpha;
	double b2 = 2 * alpha;
	double a1 = 2 * -gamma;
	double a2 = 2 * beta;

	set_coefficient(bq, b0, b1, b2, 1, a1, a2);
}

static void biquad_highpass(struct biquad *bq, double cutoff, double resonance)
{
	/* Limit cutoff to 0 to 1. */
	cutoff = max(0.0, min(cutoff, 1.0));

	if (cutoff == 1 || cutoff == 0) {
		/* When cutoff is one, the z-transform is 0. */
		/* When cutoff is zero, we need to be careful because the above
		 * gives a quadratic divided by the same quadratic, with poles
		 * and zeros on the unit circle in the same place. When cutoff
		 * is zero, the z-transform is 1.
		 */
		set_coefficient(bq, 1 - cutoff, 0, 0, 1, 0, 0);
		return;
	}

	/* Compute biquad coefficients for highpass filter */
	resonance = max(0.0, resonance); /* can't go negative */
	double g = pow(10.0, 0.05 * resonance);
	double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);

	double theta = M_PI * cutoff;
	double sn = 0.5 * d * sin(theta);
	double beta = 0.5 * (1 - sn) / (1 + sn);
	double gamma = (0.5 + beta) * cos(theta);
	double alpha = 0.25 * (0.5 + beta + gamma);

	double b0 = 2 * alpha;
	double b1 = 2 * -2 * alpha;
	double b2 = 2 * alpha;
	double a1 = 2 * -gamma;
	double a2 = 2 * beta;

	set_coefficient(bq, b0, b1, b2, 1, a1, a2);
}

static void biquad_bandpass(struct biquad *bq, double frequency, double Q)
{
	/* No negative frequencies allowed. */
	frequency = max(0.0, frequency);

	/* Don't let Q go negative, which causes an unstable filter. */
	Q = max(0.0, Q);

	if (frequency <= 0 || frequency >= 1) {
		/* When the cutoff is zero, the z-transform approaches 0, if Q
		 * > 0. When both Q and cutoff are zero, the z-transform is
		 * pretty much undefined. What should we do in this case?
		 * For now, just make the filter 0. When the cutoff is 1, the
		 * z-transform also approaches 0.
		 */
		set_coefficient(bq, 0, 0, 0, 1, 0, 0);
		return;
	}
	if (Q <= 0) {
		/* When Q = 0, the above formulas have problems. If we
		 * look at the z-transform, we can see that the limit
		 * as Q->0 is 1, so set the filter that way.
		 */
		set_coefficient(bq, 1, 0, 0, 1, 0, 0);
		return;
	}

	double w0 = M_PI * frequency;
	double alpha = sin(w0) / (2 * Q);
	double k = cos(w0);

	double b0 = alpha;
	double b1 = 0;
	double b2 = -alpha;
	double a0 = 1 + alpha;
	double a1 = -2 * k;
	double a2 = 1 - alpha;

	set_coefficient(bq, b0, b1, b2, a0, a1, a2);
}

static void biquad_lowshelf(struct biquad *bq, double frequency, double db_gain)
{
	/* Clip frequencies to between 0 and 1, inclusive. */
	frequency = max(0.0, min(frequency, 1.0));

	double A = pow(10.0, db_gain / 40);

	if (frequency == 1) {
		/* The z-transform is a constant gain. */
		set_coefficient(bq, A * A, 0, 0, 1, 0, 0);
		return;
	}
	if (frequency <= 0) {
		/* When frequency is 0, the z-transform is 1. */
		set_coefficient(bq, 1, 0, 0, 1, 0, 0);
		return;
	}

	double w0 = M_PI * frequency;
	double S = 1; /* filter slope (1 is max value) */
	double alpha = 0.5 * sin(w0) * sqrt((A + 1 / A) * (1 / S - 1) + 2);
	double k = cos(w0);
	double k2 = 2 * sqrt(A) * alpha;
	double a_plus_one = A + 1;
	double a_minus_one = A - 1;

	double b0 = A * (a_plus_one - a_minus_one * k + k2);
	double b1 = 2 * A * (a_minus_one - a_plus_one * k);
	double b2 = A * (a_plus_one - a_minus_one * k - k2);
	double a0 = a_plus_one + a_minus_one * k + k2;
	double a1 = -2 * (a_minus_one + a_plus_one * k);
	double a2 = a_plus_one + a_minus_one * k - k2;

	set_coefficient(bq, b0, b1, b2, a0, a1, a2);
}

static void biquad_highshelf(struct biquad *bq, double frequency,
			     double db_gain)
{
	/* Clip frequencies to between 0 and 1, inclusive. */
	frequency = max(0.0, min(frequency, 1.0));

	double A = pow(10.0, db_gain / 40);

	if (frequency == 1) {
		/* The z-transform is 1. */
		set_coefficient(bq, 1, 0, 0, 1, 0, 0);
		return;
	}
	if (frequency <= 0) {
		/* When frequency = 0, the filter is just a gain, A^2. */
		set_coefficient(bq, A * A, 0, 0, 1, 0, 0);
		return;
	}

	double w0 = M_PI * frequency;
	double S = 1; /* filter slope (1 is max value) */
	double alpha = 0.5 * sin(w0) * sqrt((A + 1 / A) * (1 / S - 1) + 2);
	double k = cos(w0);
	double k2 = 2 * sqrt(A) * alpha;
	double a_plus_one = A + 1;
	double a_minus_one = A - 1;

	double b0 = A * (a_plus_one + a_minus_one * k + k2);
	double b1 = -2 * A * (a_minus_one + a_plus_one * k);
	double b2 = A * (a_plus_one + a_minus_one * k - k2);
	double a0 = a_plus_one - a_minus_one * k + k2;
	double a1 = 2 * (a_minus_one - a_plus_one * k);
	double a2 = a_plus_one - a_minus_one * k - k2;

	set_coefficient(bq, b0, b1, b2, a0, a1, a2);
}

static void biquad_peaking(struct biquad *bq, double frequency, double Q,
			   double db_gain)
{
	/* Clip frequencies to between 0 and 1, inclusive. */
	frequency = max(0.0, min(frequency, 1.0));

	/* Don't let Q go negative, which causes an unstable filter. */
	Q = max(0.0, Q);

	double A = pow(10.0, db_gain / 40);

	if (frequency <= 0 || frequency >= 1) {
		/* When frequency is 0 or 1, the z-transform is 1. */
		set_coefficient(bq, 1, 0, 0, 1, 0, 0);
		return;
	}
	if (Q <= 0) {
		/* When Q = 0, the above formulas have problems. If we
		 * look at the z-transform, we can see that the limit
		 * as Q->0 is A^2, so set the filter that way.
		 */
		set_coefficient(bq, A * A, 0, 0, 1, 0, 0);
		return;
	}

	double w0 = M_PI * frequency;
	double alpha = sin(w0) / (2 * Q);
	double k = cos(w0);

	double b0 = 1 + alpha * A;
	double b1 = -2 * k;
	double b2 = 1 - alpha * A;
	double a0 = 1 + alpha / A;
	double a1 = -2 * k;
	double a2 = 1 - alpha / A;

	set_coefficient(bq, b0, b1, b2, a0, a1, a2);
}

static void biquad_notch(struct biquad *bq, double frequency, double Q)
{
	/* Clip frequencies to between 0 and 1, inclusive. */
	frequency = max(0.0, min(frequency, 1.0));

	/* Don't let Q go negative, which causes an unstable filter. */
	Q = max(0.0, Q);

	if (frequency <= 0 || frequency >= 1) {
		/* When frequency is 0 or 1, the z-transform is 1. */
		set_coefficient(bq, 1, 0, 0, 1, 0, 0);
		return;
	}
	if (Q <= 0) {
		/* When Q = 0, the above formulas have problems. If we
		 * look at the z-transform, we can see that the limit
		 * as Q->0 is 0, so set the filter that way.
		 */
		set_coefficient(bq, 0, 0, 0, 1, 0, 0);
		return;
	}

	double w0 = M_PI * frequency;
	double alpha = sin(w0) / (2 * Q);
	double k = cos(w0);

	double b0 = 1;
	double b1 = -2 * k;
	double b2 = 1;
	double a0 = 1 + alpha;
	double a1 = -2 * k;
	double a2 = 1 - alpha;

	set_coefficient(bq, b0, b1, b2, a0, a1, a2);
}

static void biquad_allpass(struct biquad *bq, double frequency, double Q)
{
	/* Clip frequencies to between 0 and 1, inclusive. */
	frequency = max(0.0, min(frequency, 1.0));

	/* Don't let Q go negative, which causes an unstable filter. */
	Q = max(0.0, Q);

	if (frequency <= 0 || frequency >= 1) {
		/* When frequency is 0 or 1, the z-transform is 1. */
		set_coefficient(bq, 1, 0, 0, 1, 0, 0);
		return;
	}

	if (Q <= 0) {
		/* When Q = 0, the above formulas have problems. If we
		 * look at the z-transform, we can see that the limit
		 * as Q->0 is -1, so set the filter that way.
		 */
		set_coefficient(bq, -1, 0, 0, 1, 0, 0);
		return;
	}

	double w0 = M_PI * frequency;
	double alpha = sin(w0) / (2 * Q);
	double k = cos(w0);

	double b0 = 1 - alpha;
	double b1 = -2 * k;
	double b2 = 1 + alpha;
	double a0 = 1 + alpha;
	double a1 = -2 * k;
	double a2 = 1 - alpha;

	set_coefficient(bq, b0, b1, b2, a0, a1, a2);
}

void biquad_set(struct biquad *bq, enum biquad_type type, double freq, double Q,
		double gain)
{
	/* Default is an identity filter. Also clear history values. */
	set_coefficient(bq, 1, 0, 0, 1, 0, 0);
	bq->x1 = 0;
	bq->x2 = 0;
	bq->y1 = 0;
	bq->y2 = 0;

	switch (type) {
	case BQ_LOWPASS:
		biquad_lowpass(bq, freq, Q);
		break;
	case BQ_HIGHPASS:
		biquad_highpass(bq, freq, Q);
		break;
	case BQ_BANDPASS:
		biquad_bandpass(bq, freq, Q);
		break;
	case BQ_LOWSHELF:
		biquad_lowshelf(bq, freq, gain);
		break;
	case BQ_HIGHSHELF:
		biquad_highshelf(bq, freq, gain);
		break;
	case BQ_PEAKING:
		biquad_peaking(bq, freq, Q, gain);
		break;
	case BQ_NOTCH:
		biquad_notch(bq, freq, Q);
		break;
	case BQ_ALLPASS:
		biquad_allpass(bq, freq, Q);
		break;
	case BQ_NONE:
		break;
	}
}