| 378 | } |
| 379 | |
| 380 | void Biquad::setBandpassParams(double frequency, double Q) |
| 381 | { |
| 382 | // No negative frequencies allowed. |
| 383 | frequency = std::max(0.0, frequency); |
| 384 | |
| 385 | // Don't let Q go negative, which causes an unstable filter. |
| 386 | Q = std::max(0.0, Q); |
| 387 | |
| 388 | if (frequency > 0 && frequency < 1) |
| 389 | { |
| 390 | double w0 = static_cast<double>(LAB_PI) * frequency; |
| 391 | if (Q > 0) |
| 392 | { |
| 393 | double alpha = sin(w0) / (2 * Q); |
| 394 | double k = cos(w0); |
| 395 | |
| 396 | double b0 = alpha; |
| 397 | double b1 = 0; |
| 398 | double b2 = -alpha; |
| 399 | double a0 = 1 + alpha; |
| 400 | double a1 = -2 * k; |
| 401 | double a2 = 1 - alpha; |
| 402 | |
| 403 | setNormalizedCoefficients(b0, b1, b2, a0, a1, a2); |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | // When Q = 0, the above formulas have problems. If we look at |
| 408 | // the z-transform, we can see that the limit as Q->0 is 1, so |
| 409 | // set the filter that way. |
| 410 | setNormalizedCoefficients(1, 0, 0, |
| 411 | 1, 0, 0); |
| 412 | } |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | // When the cutoff is zero, the z-transform approaches 0, if Q |
| 417 | // > 0. When both Q and cutoff are zero, the z-transform is |
| 418 | // pretty much undefined. What should we do in this case? |
| 419 | // For now, just make the filter 0. When the cutoff is 1, the |
| 420 | // z-transform also approaches 0. |
| 421 | setNormalizedCoefficients(0, 0, 0, |
| 422 | 1, 0, 0); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | void Biquad::setZeroPolePairs(const complex<double> & zero, const complex<double> & pole) |
| 427 | { |
no outgoing calls
no test coverage detected