Debian Codegraph @

src/biquad.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2025 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include <math.h>
5 #include "biquad.h"
6
7 // Filter type names
8 static const char *filter_type_names[] = {
9 "Peaking",
10 "Low Shelf",
11 "High Shelf",
12 "Lowpass",
13 "Highpass",
14 "Bandpass",
15 "Notch",
16 "Gain",
17 "LP 6dB/oct",
18 "HP 6dB/oct",
19 "LS 6dB/oct",
20 "HS 6dB/oct"
21 };
22
23 const char *biquad_type_name(BiquadFilterType type) {
24 if (type < 0 || type >= BIQUAD_TYPE_COUNT)
25 return "Unknown";
26 return filter_type_names[type];
27 }
28
29 const char **biquad_get_type_names(void) {
30 return filter_type_names;
31 }
32
33 gboolean biquad_type_uses_gain(BiquadFilterType type) {
34 return type == BIQUAD_TYPE_PEAKING ||
35 type == BIQUAD_TYPE_LOW_SHELF ||
36 type == BIQUAD_TYPE_HIGH_SHELF ||
37 type == BIQUAD_TYPE_GAIN ||
38 type == BIQUAD_TYPE_LOW_SHELF_1 ||
39 type == BIQUAD_TYPE_HIGH_SHELF_1;
40 }
41
42 gboolean biquad_type_uses_q(BiquadFilterType type) {
43 // First-order filters and gain don't use Q
44 return type != BIQUAD_TYPE_GAIN &&
45 type != BIQUAD_TYPE_LOWPASS_1 &&
46 type != BIQUAD_TYPE_HIGHPASS_1 &&
47 type != BIQUAD_TYPE_LOW_SHELF_1 &&
48 type != BIQUAD_TYPE_HIGH_SHELF_1;
49 }
50
51 // Calculate biquad coefficients using Audio EQ Cookbook formulas
52 // Reference: https://www.w3.org/TR/audio-eq-cookbook/
53 void biquad_calculate(
54 const struct biquad_params *params,
55 double sample_rate,
56 struct biquad_coeffs *coeffs
57 ) {
58 double w0 = 2.0 * M_PI * params->freq / sample_rate;
59 double cos_w0 = cos(w0);
60 double sin_w0 = sin(w0);
61 double alpha = sin_w0 / (2.0 * params->q);
62
63 // A = sqrt(10^(gain_db/20)) = 10^(gain_db/40)
64 double A = pow(10.0, params->gain_db / 40.0);
65
66 double b0, b1, b2, a0, a1, a2;
67
68 switch (params->type) {
69 case BIQUAD_TYPE_PEAKING:
70 b0 = 1.0 + alpha * A;
71 b1 = -2.0 * cos_w0;
72 b2 = 1.0 - alpha * A;
73 a0 = 1.0 + alpha / A;
74 a1 = -2.0 * cos_w0;
75 a2 = 1.0 - alpha / A;
76 break;
77
78 case BIQUAD_TYPE_LOW_SHELF: {
79 double sqrt_A = sqrt(A);
80 double sqrt_A_alpha = 2.0 * sqrt_A * alpha;
81 b0 = A * ((A + 1.0) - (A - 1.0) * cos_w0 + sqrt_A_alpha);
82 b1 = 2.0 * A * ((A - 1.0) - (A + 1.0) * cos_w0);
83 b2 = A * ((A + 1.0) - (A - 1.0) * cos_w0 - sqrt_A_alpha);
84 a0 = (A + 1.0) + (A - 1.0) * cos_w0 + sqrt_A_alpha;
85 a1 = -2.0 * ((A - 1.0) + (A + 1.0) * cos_w0);
86 a2 = (A + 1.0) + (A - 1.0) * cos_w0 - sqrt_A_alpha;
87 break;
88 }
89
90 case BIQUAD_TYPE_HIGH_SHELF: {
91 double sqrt_A = sqrt(A);
92 double sqrt_A_alpha = 2.0 * sqrt_A * alpha;
93 b0 = A * ((A + 1.0) + (A - 1.0) * cos_w0 + sqrt_A_alpha);
94 b1 = -2.0 * A * ((A - 1.0) + (A + 1.0) * cos_w0);
95 b2 = A * ((A + 1.0) + (A - 1.0) * cos_w0 - sqrt_A_alpha);
96 a0 = (A + 1.0) - (A - 1.0) * cos_w0 + sqrt_A_alpha;
97 a1 = 2.0 * ((A - 1.0) - (A + 1.0) * cos_w0);
98 a2 = (A + 1.0) - (A - 1.0) * cos_w0 - sqrt_A_alpha;
99 break;
100 }
101
102 case BIQUAD_TYPE_LOWPASS:
103 b0 = (1.0 - cos_w0) / 2.0;
104 b1 = 1.0 - cos_w0;
105 b2 = (1.0 - cos_w0) / 2.0;
106 a0 = 1.0 + alpha;
107 a1 = -2.0 * cos_w0;
108 a2 = 1.0 - alpha;
109 break;
110
111 case BIQUAD_TYPE_HIGHPASS:
112 b0 = (1.0 + cos_w0) / 2.0;
113 b1 = -(1.0 + cos_w0);
114 b2 = (1.0 + cos_w0) / 2.0;
115 a0 = 1.0 + alpha;
116 a1 = -2.0 * cos_w0;
117 a2 = 1.0 - alpha;
118 break;
119
120 case BIQUAD_TYPE_BANDPASS:
121 // Constant skirt gain, peak gain = Q
122 b0 = alpha;
123 b1 = 0.0;
124 b2 = -alpha;
125 a0 = 1.0 + alpha;
126 a1 = -2.0 * cos_w0;
127 a2 = 1.0 - alpha;
128 break;
129
130 case BIQUAD_TYPE_NOTCH:
131 b0 = 1.0;
132 b1 = -2.0 * cos_w0;
133 b2 = 1.0;
134 a0 = 1.0 + alpha;
135 a1 = -2.0 * cos_w0;
136 a2 = 1.0 - alpha;
137 break;
138
139 case BIQUAD_TYPE_GAIN:
140 // Pure gain, no frequency dependence
141 // gain_db uses 20*log10 scale (not 40 like peaking A parameter)
142 coeffs->b0 = pow(10.0, params->gain_db / 20.0);
143 coeffs->b1 = 0.0;
144 coeffs->b2 = 0.0;
145 coeffs->a1 = 0.0;
146 coeffs->a2 = 0.0;
147 return;
148
149 case BIQUAD_TYPE_LOWPASS_1: {
150 // First-order lowpass (6 dB/octave)
151 double K = tan(M_PI * params->freq / sample_rate);
152 double norm = 1.0 / (1.0 + K);
153 coeffs->b0 = K * norm;
154 coeffs->b1 = K * norm;
155 coeffs->b2 = 0.0;
156 coeffs->a1 = (K - 1.0) * norm;
157 coeffs->a2 = 0.0;
158 return;
159 }
160
161 case BIQUAD_TYPE_HIGHPASS_1: {
162 // First-order highpass (6 dB/octave)
163 double K = tan(M_PI * params->freq / sample_rate);
164 double norm = 1.0 / (1.0 + K);
165 coeffs->b0 = norm;
166 coeffs->b1 = -norm;
167 coeffs->b2 = 0.0;
168 coeffs->a1 = (K - 1.0) * norm;
169 coeffs->a2 = 0.0;
170 return;
171 }
172
173 case BIQUAD_TYPE_LOW_SHELF_1: {
174 // First-order low shelf
175 // Ensure exact relationship: b0 - b1 = 1 - a1 (Nyquist gain = 1)
176 double K = tan(M_PI * params->freq / sample_rate);
177 double V = pow(10.0, fabs(params->gain_db) / 20.0);
178 if (params->gain_db >= 0) {
179 // Boost: a1 = (K-1)/(1+K)
180 double denom = 1.0 + K;
181 coeffs->a1 = (K - 1.0) / denom;
182 double nyq_diff = 1.0 - coeffs->a1; // Exact: b0 - b1 = nyq_diff
183 double dc_sum = 2.0 * V * K / denom; // b0 + b1
184 coeffs->b0 = (dc_sum + nyq_diff) / 2.0;
185 coeffs->b1 = (dc_sum - nyq_diff) / 2.0;
186 } else {
187 // Cut: a1 = (V*K-1)/(1+V*K)
188 double denom = 1.0 + V * K;
189 coeffs->a1 = (V * K - 1.0) / denom;
190 double nyq_diff = 1.0 - coeffs->a1; // Exact: b0 - b1 = nyq_diff
191 double dc_sum = 2.0 * K / denom; // b0 + b1
192 coeffs->b0 = (dc_sum + nyq_diff) / 2.0;
193 coeffs->b1 = (dc_sum - nyq_diff) / 2.0;
194 }
195 coeffs->b2 = 0.0;
196 coeffs->a2 = 0.0;
197 return;
198 }
199
200 case BIQUAD_TYPE_HIGH_SHELF_1: {
201 // First-order high shelf
202 // Ensure exact relationship: b0 + b1 = 1 + a1 (DC gain = 1)
203 double K = tan(M_PI * params->freq / sample_rate);
204 double V = pow(10.0, fabs(params->gain_db) / 20.0);
205 if (params->gain_db >= 0) {
206 // Boost: a1 = (K-1)/(1+K)
207 double denom = 1.0 + K;
208 coeffs->a1 = (K - 1.0) / denom;
209 double dc_sum = 1.0 + coeffs->a1; // Exact: b0 + b1 = dc_sum
210 double nyq_diff = 2.0 * V / denom; // b0 - b1
211 coeffs->b0 = (dc_sum + nyq_diff) / 2.0;
212 coeffs->b1 = (dc_sum - nyq_diff) / 2.0;
213 } else {
214 // Cut: a1 = (K-V)/(V+K)
215 double denom = V + K;
216 coeffs->a1 = (K - V) / denom;
217 double dc_sum = 1.0 + coeffs->a1; // Exact: b0 + b1 = dc_sum
218 double nyq_diff = 2.0 / denom; // b0 - b1
219 coeffs->b0 = (dc_sum + nyq_diff) / 2.0;
220 coeffs->b1 = (dc_sum - nyq_diff) / 2.0;
221 }
222 coeffs->b2 = 0.0;
223 coeffs->a2 = 0.0;
224 return;
225 }
226
227 default:
228 // Passthrough (unity gain)
229 coeffs->b0 = 1.0;
230 coeffs->b1 = 0.0;
231 coeffs->b2 = 0.0;
232 coeffs->a1 = 0.0;
233 coeffs->a2 = 0.0;
234 return;
235 }
236
237 // Normalize by a0
238 coeffs->b0 = b0 / a0;
239 coeffs->b1 = b1 / a0;
240 coeffs->b2 = b2 / a0;
241 coeffs->a1 = a1 / a0;
242 coeffs->a2 = a2 / a0;
243 }
244
245 void biquad_to_fixed_point(
246 const struct biquad_coeffs *coeffs,
247 long fixed[5]
248 ) {
249 // Hardware expects: [b0, b1, b2, -a1, -a2] * 2^28
250 fixed[0] = lround(coeffs->b0 * BIQUAD_FIXED_POINT_SCALE);
251 fixed[1] = lround(coeffs->b1 * BIQUAD_FIXED_POINT_SCALE);
252 fixed[2] = lround(coeffs->b2 * BIQUAD_FIXED_POINT_SCALE);
253 fixed[3] = lround(-coeffs->a1 * BIQUAD_FIXED_POINT_SCALE);
254 fixed[4] = lround(-coeffs->a2 * BIQUAD_FIXED_POINT_SCALE);
255 }
256
257 // Calculate frequency response magnitude in dB
258 // H(z) = (b0 + b1*z^-1 + b2*z^-2) / (1 + a1*z^-1 + a2*z^-2)
259 // At frequency f: z = e^(j*2*pi*f/fs)
260 double biquad_response_db(
261 const struct biquad_coeffs *coeffs,
262 double freq,
263 double sample_rate
264 ) {
265 double w = 2.0 * M_PI * freq / sample_rate;
266 double cos_w = cos(w);
267 double cos_2w = cos(2.0 * w);
268 double sin_w = sin(w);
269 double sin_2w = sin(2.0 * w);
270
271 // Numerator: b0 + b1*e^(-jw) + b2*e^(-2jw)
272 double num_real = coeffs->b0 + coeffs->b1 * cos_w + coeffs->b2 * cos_2w;
273 double num_imag = -coeffs->b1 * sin_w - coeffs->b2 * sin_2w;
274
275 // Denominator: 1 + a1*e^(-jw) + a2*e^(-2jw)
276 double den_real = 1.0 + coeffs->a1 * cos_w + coeffs->a2 * cos_2w;
277 double den_imag = -coeffs->a1 * sin_w - coeffs->a2 * sin_2w;
278
279 // |H|^2 = |num|^2 / |den|^2
280 double num_mag_sq = num_real * num_real + num_imag * num_imag;
281 double den_mag_sq = den_real * den_real + den_imag * den_imag;
282
283 if (den_mag_sq < 1e-20)
284 return 0.0;
285
286 double mag_sq = num_mag_sq / den_mag_sq;
287
288 if (mag_sq < 1e-20)
289 return -100.0;
290
291 return 10.0 * log10(mag_sq);
292 }
293
294 void biquad_from_fixed_point(
295 const long fixed[5],
296 struct biquad_coeffs *coeffs
297 ) {
298 // Hardware format: [b0, b1, b2, -a1, -a2] * 2^28
299 coeffs->b0 = (double)fixed[0] / BIQUAD_FIXED_POINT_SCALE;
300 coeffs->b1 = (double)fixed[1] / BIQUAD_FIXED_POINT_SCALE;
301 coeffs->b2 = (double)fixed[2] / BIQUAD_FIXED_POINT_SCALE;
302 coeffs->a1 = -(double)fixed[3] / BIQUAD_FIXED_POINT_SCALE;
303 coeffs->a2 = -(double)fixed[4] / BIQUAD_FIXED_POINT_SCALE;
304 }
305
306 // Tolerance for fixed-point round-trip (2^-28 ≈ 3.7e-9, allow some margin)
307 #define COEFF_TOL 1e-6
308
309 // Helper to check if two values are approximately equal
310 static gboolean approx_eq(double a, double b, double tol) {
311 return fabs(a - b) < tol;
312 }
313
314 // Check coefficient equality with fixed-point tolerance
315 static gboolean coeff_eq(double a, double b) {
316 return fabs(a - b) < COEFF_TOL;
317 }
318
319 // Analyze coefficients to determine filter parameters
320 gboolean biquad_analyze(
321 const struct biquad_coeffs *coeffs,
322 double sample_rate,
323 struct biquad_params *params
324 ) {
325 double b0 = coeffs->b0;
326 double b1 = coeffs->b1;
327 double b2 = coeffs->b2;
328 double a1 = coeffs->a1;
329 double a2 = coeffs->a2;
330
331 // Check for pure gain filter (b1, b2, a1, a2 all zero, b0 is gain)
332 if (approx_eq(b1, 0.0, 0.001) && approx_eq(b2, 0.0, 0.001) &&
333 approx_eq(a1, 0.0, 0.001) && approx_eq(a2, 0.0, 0.001) &&
334 b0 > 0.0) {
335 params->type = BIQUAD_TYPE_GAIN;
336 params->freq = 1000.0; // Arbitrary, not used
337 params->q = 0.707; // Arbitrary, not used
338 params->gain_db = 20.0 * log10(b0);
339 params->gain_db = CLAMP(params->gain_db,
340 -GAIN_DB_LIMIT, GAIN_DB_LIMIT);
341 return TRUE;
342 }
343
344 // Check for first-order filters (b2 = 0, a2 = 0)
345 if (approx_eq(b2, 0.0, 0.001) && approx_eq(a2, 0.0, 0.001)) {
346 // First-order filter: H(z) = (b0 + b1*z^-1) / (1 + a1*z^-1)
347 // K = (1 + a1) / (1 - a1) = tan(pi * fc / fs)
348 double K = (1.0 + a1) / (1.0 - a1);
349 if (K < 0.001) K = 0.001;
350 double freq = atan(K) * sample_rate / M_PI;
351 if (freq < 20.0) freq = 20.0;
352 if (freq > 20000.0) freq = 20000.0;
353 params->freq = freq;
354 params->q = 0.707; // Not used for first-order
355
356 // First-order lowpass: b0 = b1
357 if (coeff_eq(b0, b1)) {
358 params->type = BIQUAD_TYPE_LOWPASS_1;
359 params->gain_db = 0.0;
360 return TRUE;
361 }
362
363 // First-order highpass: b0 = -b1
364 if (coeff_eq(b0, -b1)) {
365 params->type = BIQUAD_TYPE_HIGHPASS_1;
366 params->gain_db = 0.0;
367 return TRUE;
368 }
369
370 // Low shelf: b0 - b1 = 1 - a1 (Nyquist gain = 1)
371 if (coeff_eq(b0 - b1, 1.0 - a1)) {
372 params->type = BIQUAD_TYPE_LOW_SHELF_1;
373 double dc_gain = (b0 + b1) / (1.0 + a1);
374 params->gain_db = 20.0 * log10(fabs(dc_gain));
375 if (params->gain_db < -24.0) params->gain_db = -24.0;
376 if (params->gain_db > 24.0) params->gain_db = 24.0;
377
378 // Recover K: boost uses a1 = (K-1)/(K+1), cut uses a1 = (V*K-1)/(V*K+1)
379 double V = pow(10.0, fabs(params->gain_db) / 20.0);
380 double K_term = (1.0 + a1) / (1.0 - a1);
381 double K = (params->gain_db >= 0) ? K_term : K_term / V;
382 if (K > 0)
383 params->freq = atan(K) * sample_rate / M_PI;
384 if (params->freq < 20.0) params->freq = 20.0;
385 if (params->freq > 20000.0) params->freq = 20000.0;
386 return TRUE;
387 }
388
389 // High shelf: b0 + b1 = 1 + a1 (DC gain = 1)
390 if (coeff_eq(b0 + b1, 1.0 + a1)) {
391 params->type = BIQUAD_TYPE_HIGH_SHELF_1;
392 double nyq_gain = (b0 - b1) / (1.0 - a1);
393 params->gain_db = 20.0 * log10(fabs(nyq_gain));
394 if (params->gain_db < -24.0) params->gain_db = -24.0;
395 if (params->gain_db > 24.0) params->gain_db = 24.0;
396
397 // Recover K: boost uses a1 = (K-1)/(K+1), cut uses a1 = (K-V)/(K+V)
398 double V = pow(10.0, fabs(params->gain_db) / 20.0);
399 double K_term = (1.0 + a1) / (1.0 - a1);
400 double K = (params->gain_db >= 0) ? K_term : K_term * V;
401 if (K > 0)
402 params->freq = atan(K) * sample_rate / M_PI;
403 if (params->freq < 20.0) params->freq = 20.0;
404 if (params->freq > 20000.0) params->freq = 20000.0;
405 return TRUE;
406 }
407
408 // Unknown first-order, default to lowpass
409 params->type = BIQUAD_TYPE_LOWPASS_1;
410 params->gain_db = 0.0;
411 return TRUE;
412 }
413
414 // For normalized biquad coefficients, a1_norm = a1/a0, so we need to
415 // recover a0 to get the original cos_w0 = -a1/2.
416 //
417 // For all filter types: a0 = 2/(1+a2)
418 // (derived from a2_norm = (1-x)/(1+x) where x=alpha or alpha/A)
419 double a0 = 2.0 / (1.0 + a2);
420 double cos_w0 = -a1 * a0 / 2.0;
421
422 // Clamp cos_w0 to valid range
423 if (cos_w0 < -1.0) cos_w0 = -1.0;
424 if (cos_w0 > 1.0) cos_w0 = 1.0;
425
426 double w0 = acos(cos_w0);
427 double sin_w0 = sin(w0);
428 double freq = w0 * sample_rate / (2.0 * M_PI);
429
430 // Clamp frequency to valid range
431 if (freq < 20.0) freq = 20.0;
432 if (freq > 20000.0) freq = 20000.0;
433
434 params->freq = freq;
435 params->gain_db = 0.0;
436
437 // Check DC and Nyquist gains early to detect shelf filters
438 // For low shelf: H(DC) = A, H(Nyquist) = 1
439 // For high shelf: H(DC) = 1, H(Nyquist) = A
440 double dc_gain = (b0 + b1 + b2) / (1.0 + a1 + a2);
441 double nyq_gain = (b0 - b1 + b2) / (1.0 - a1 + a2);
442
443 // Low shelf: DC gain significantly different from 1, Nyquist ≈ 1
444 // Exclude highpass (dc_gain ≈ 0) which would otherwise match with extreme cut
445 if (fabs(dc_gain - 1.0) > 0.05 && fabs(dc_gain) > 0.01 &&
446 approx_eq(nyq_gain, 1.0, 0.1)) {
447 params->type = BIQUAD_TYPE_LOW_SHELF;
448 // dc_gain = A² from cookbook, so gain_db = 20*log10(A²) = 40*log10(A)
449 params->gain_db = 20.0 * log10(fabs(dc_gain));
450 params->gain_db = CLAMP(params->gain_db,
451 -GAIN_DB_LIMIT, GAIN_DB_LIMIT);
452
453 // For frequency formula, we need actual A = sqrt(dc_gain)
454 double A = sqrt(fabs(dc_gain));
455
456 // Derive cos_w0 from a1 and A for low shelf
457 // The formula: num/den gives -cos_w0, so negate num
458 double num = -(a1 * (A + 1) + (1 + a2) * (A - 1));
459 double den = a1 * (A - 1) + (1 + a2) * (A + 1);
460 double shelf_cos_w0 = 0.0;
461 if (fabs(den) > 0.001) {
462 shelf_cos_w0 = num / den;
463 if (shelf_cos_w0 < -1.0) shelf_cos_w0 = -1.0;
464 if (shelf_cos_w0 > 1.0) shelf_cos_w0 = 1.0;
465 double shelf_w0 = acos(shelf_cos_w0);
466 params->freq = shelf_w0 * sample_rate / (2.0 * M_PI);
467 }
468
469 // Extract Q from: (1-a2)/(1+a2) = sqrt(A)*sin_w0 / [Q * ((A+1)+(A-1)*cos_w0)]
470 double shelf_sin_w0 = sqrt(1.0 - shelf_cos_w0 * shelf_cos_w0);
471 double x = (A + 1) + (A - 1) * shelf_cos_w0;
472 double y_over_x = (1.0 - a2) / (1.0 + a2);
473 if (fabs(y_over_x * x) > 1e-10) {
474 params->q = sqrt(A) * shelf_sin_w0 / (y_over_x * x);
475 } else {
476 params->q = 0.707;
477 }
478 if (params->q < 0.1) params->q = 0.1;
479 if (params->q > 10.0) params->q = 10.0;
480 return TRUE;
481 }
482
483 // High shelf: Nyquist gain significantly different from 1, DC ≈ 1
484 // Exclude lowpass (nyq_gain ≈ 0) which would otherwise match with extreme cut
485 if (fabs(nyq_gain - 1.0) > 0.05 && fabs(nyq_gain) > 0.01 &&
486 approx_eq(dc_gain, 1.0, 0.1)) {
487 params->type = BIQUAD_TYPE_HIGH_SHELF;
488 // nyq_gain = A² from cookbook, so gain_db = 20*log10(A²) = 40*log10(A)
489 params->gain_db = 20.0 * log10(fabs(nyq_gain));
490 params->gain_db = CLAMP(params->gain_db,
491 -GAIN_DB_LIMIT, GAIN_DB_LIMIT);
492
493 // For frequency formula, we need actual A = sqrt(nyq_gain)
494 double A = sqrt(fabs(nyq_gain));
495
496 // Derive cos_w0 from a1 and A for high shelf
497 double num = a1 * (A + 1) - (1 + a2) * (A - 1);
498 double den = a1 * (A - 1) - (1 + a2) * (A + 1);
499 double shelf_cos_w0 = 0.0;
500 if (fabs(den) > 0.001) {
501 shelf_cos_w0 = num / den;
502 if (shelf_cos_w0 < -1.0) shelf_cos_w0 = -1.0;
503 if (shelf_cos_w0 > 1.0) shelf_cos_w0 = 1.0;
504 double shelf_w0 = acos(shelf_cos_w0);
505 params->freq = shelf_w0 * sample_rate / (2.0 * M_PI);
506 }
507
508 // Extract Q: for high shelf, x = (A+1) - (A-1)*cos_w0
509 double shelf_sin_w0 = sqrt(1.0 - shelf_cos_w0 * shelf_cos_w0);
510 double x = (A + 1) - (A - 1) * shelf_cos_w0;
511 double y_over_x = (1.0 - a2) / (1.0 + a2);
512 if (fabs(y_over_x * x) > 1e-10) {
513 params->q = sqrt(A) * shelf_sin_w0 / (y_over_x * x);
514 } else {
515 params->q = 0.707;
516 }
517 if (params->q < 0.1) params->q = 0.1;
518 if (params->q > 10.0) params->q = 10.0;
519
520 return TRUE;
521 }
522
523 // Bandpass: b1 = 0, b0 = -b2
524 if (coeff_eq(b1, 0.0) && coeff_eq(b0, -b2)) {
525 params->type = BIQUAD_TYPE_BANDPASS;
526 // alpha from normalized a2: a2_n = (1-alpha)/(1+alpha)
527 double alpha = (1.0 - a2) / (1.0 + a2);
528 if (alpha > 1e-10)
529 params->q = sin_w0 / (2.0 * alpha);
530 else
531 params->q = 1.0;
532 if (params->q < 0.1) params->q = 0.1;
533 if (params->q > 10.0) params->q = 10.0;
534 return TRUE;
535 }
536
537 // Peaking: b1 = a1 (both equal -2*cos_w0), but b0 ≠ b2
538 if (coeff_eq(b1, a1) && !coeff_eq(b0, b2)) {
539 params->type = BIQUAD_TYPE_PEAKING;
540 // For peaking: b0 = 1 + alpha*A, b2 = 1 - alpha*A (before norm)
541 // After norm by a0 = 1 + alpha/A:
542 // b0_n = (1 + alpha*A) / (1 + alpha/A)
543 // b2_n = (1 - alpha*A) / (1 + alpha/A)
544 // a2_n = (1 - alpha/A) / (1 + alpha/A)
545 //
546 // From a2: alpha/A = (1 - a2) / (1 + a2)
547 // From b0-b2: alpha*A * 2 / a0 = b0 - b2
548 // so alpha*A = (b0 - b2) * (1 + alpha/A) / 2
549 // = (b0 - b2) * (1 + (1-a2)/(1+a2)) / 2
550 // = (b0 - b2) * (2/(1+a2)) / 2
551 // = (b0 - b2) / (1 + a2)
552
553 // Direct computation without intermediate divisions:
554 // A² = (b0 - b2) / (1 - a2)
555 // alpha² = (b0 - b2) * (1 - a2) / (1 + a2)²
556 double b_diff = b0 - b2;
557 double a_diff = 1.0 - a2;
558
559 double A_sq = b_diff / a_diff;
560 double A = (A_sq > 0 && isfinite(A_sq)) ? sqrt(A_sq) : 1.0;
561
562 double alpha_sq = b_diff * a_diff / ((1.0 + a2) * (1.0 + a2));
563 double alpha = (alpha_sq > 0) ? sqrt(alpha_sq) : 0.001;
564
565 params->gain_db = 40.0 * log10(A);
566 params->gain_db = CLAMP(params->gain_db,
567 -GAIN_DB_LIMIT, GAIN_DB_LIMIT);
568
569 if (alpha > 1e-10)
570 params->q = sin_w0 / (2.0 * alpha);
571 else
572 params->q = 1.0;
573 if (params->q < 0.1) params->q = 0.1;
574 if (params->q > 10.0) params->q = 10.0;
575 return TRUE;
576 }
577
578 // Notch: b0 = b2, b1 = a1
579 if (coeff_eq(b0, b2) && coeff_eq(b1, a1)) {
580 params->type = BIQUAD_TYPE_NOTCH;
581 double alpha = (1.0 - a2) / (1.0 + a2);
582 if (alpha > 1e-10)
583 params->q = sin_w0 / (2.0 * alpha);
584 else
585 params->q = 1.0;
586 if (params->q < 0.1) params->q = 0.1;
587 if (params->q > 10.0) params->q = 10.0;
588 return TRUE;
589 }
590
591 // Lowpass: b0 = b2, b1 = 2*b0
592 if (coeff_eq(b0, b2) && coeff_eq(b1, 2.0 * b0)) {
593 params->type = BIQUAD_TYPE_LOWPASS;
594 double alpha = (1.0 - a2) / (1.0 + a2);
595 if (alpha > 1e-10)
596 params->q = sin_w0 / (2.0 * alpha);
597 else
598 params->q = 0.707;
599 if (params->q < 0.1) params->q = 0.1;
600 if (params->q > 10.0) params->q = 10.0;
601 return TRUE;
602 }
603
604 // Highpass: b0 = b2, b1 = -2*b0
605 if (coeff_eq(b0, b2) && coeff_eq(b1, -2.0 * b0)) {
606 params->type = BIQUAD_TYPE_HIGHPASS;
607 double alpha = (1.0 - a2) / (1.0 + a2);
608 if (alpha > 1e-10)
609 params->q = sin_w0 / (2.0 * alpha);
610 else
611 params->q = 0.707;
612 if (params->q < 0.1) params->q = 0.1;
613 if (params->q > 10.0) params->q = 10.0;
614 return TRUE;
615 }
616
617 // Default to gain with 0dB if we can't identify (equivalent to bypass)
618 params->type = BIQUAD_TYPE_GAIN;
619 params->freq = 1000.0;
620 params->q = 0.707;
621 params->gain_db = 0.0;
622 return TRUE;
623 }