123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /*******************************************************************************
-
- "A Collection of Useful C++ Classes for Digital Signal Processing"
- By Vinnie Falco
-
- Official project location:
- https://github.com/vinniefalco/DSPFilters
-
- See Documentation.cpp for contact information, notes, and bibliography.
-
- --------------------------------------------------------------------------------
-
- License: MIT License (http://www.opensource.org/licenses/mit-license.php)
- Copyright (c) 2009 by Vinnie Falco
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-
- *******************************************************************************/
-
- #include "Common.h"
- #include "RBJ.h"
-
- namespace Dsp
- {
- namespace RBJ
- {
- void LowPass::setup(double sampleRate,
- double cutoffFrequency,
- double q)
- {
- double w0 = 2 * doublePi * cutoffFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn / (2 * q);
- double b0 = (1 - cs) / 2;
- double b1 = 1 - cs;
- double b2 = (1 - cs) / 2;
- double a0 = 1 + AL;
- double a1 = -2 * cs;
- double a2 = 1 - AL;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
-
- void HighPass::setup(double sampleRate,
- double cutoffFrequency,
- double q)
- {
- double w0 = 2 * doublePi * cutoffFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn / (2 * q);
- double b0 = (1 + cs) / 2;
- double b1 = -(1 + cs);
- double b2 = (1 + cs) / 2;
- double a0 = 1 + AL;
- double a1 = -2 * cs;
- double a2 = 1 - AL;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
-
- void BandPass1::setup(double sampleRate,
- double centerFrequency,
- double bandWidth)
- {
- double w0 = 2 * doublePi * centerFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn / (2 * bandWidth);
- double b0 = bandWidth * AL;// sn / 2;
- double b1 = 0;
- double b2 = -bandWidth * AL;//-sn / 2;
- double a0 = 1 + AL;
- double a1 = -2 * cs;
- double a2 = 1 - AL;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
-
- void BandPass2::setup(double sampleRate,
- double centerFrequency,
- double bandWidth)
- {
- double w0 = 2 * doublePi * centerFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn / (2 * bandWidth);
- double b0 = AL;
- double b1 = 0;
- double b2 = -AL;
- double a0 = 1 + AL;
- double a1 = -2 * cs;
- double a2 = 1 - AL;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
-
- void BandStop::setup(double sampleRate,
- double centerFrequency,
- double bandWidth)
- {
- double w0 = 2 * doublePi * centerFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn / (2 * bandWidth);
- double b0 = 1;
- double b1 = -2 * cs;
- double b2 = 1;
- double a0 = 1 + AL;
- double a1 = -2 * cs;
- double a2 = 1 - AL;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
-
- void LowShelf::setup(double sampleRate,
- double cutoffFrequency,
- double gainDb,
- double shelfSlope)
- {
- double A = pow(10, gainDb / 40);
- double w0 = 2 * doublePi * cutoffFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn / 2 * std::sqrt((A + 1 / A) * (1 / shelfSlope - 1) + 2);
- double sq = 2 * sqrt(A) * AL;
- double b0 = A * ((A + 1) - (A - 1) * cs + sq);
- double b1 = 2 * A * ((A - 1) - (A + 1) * cs);
- double b2 = A * ((A + 1) - (A - 1) * cs - sq);
- double a0 = (A + 1) + (A - 1) * cs + sq;
- double a1 = -2 * ((A - 1) + (A + 1) * cs);
- double a2 = (A + 1) + (A - 1) * cs - sq;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
-
- void HighShelf::setup(double sampleRate,
- double cutoffFrequency,
- double gainDb,
- double shelfSlope)
- {
- double A = pow(10, gainDb / 40);
- double w0 = 2 * doublePi * cutoffFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn / 2 * std::sqrt((A + 1 / A) * (1 / shelfSlope - 1) + 2);
- double sq = 2 * sqrt(A) * AL;
- double b0 = A * ((A + 1) + (A - 1) * cs + sq);
- double b1 = -2 * A * ((A - 1) + (A + 1) * cs);
- double b2 = A * ((A + 1) + (A - 1) * cs - sq);
- double a0 = (A + 1) - (A - 1) * cs + sq;
- double a1 = 2 * ((A - 1) - (A + 1) * cs);
- double a2 = (A + 1) - (A - 1) * cs - sq;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
-
- void BandShelf::setup(double sampleRate,
- double centerFrequency,
- double gainDb,
- double bandWidth)
- {
- double A = pow(10, gainDb / 40);
- double w0 = 2 * doublePi * centerFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn * sinh(doubleLn2 / 2 * bandWidth * w0 / sn);
- assert(!Dsp::is_nan (AL));
- double b0 = 1 + AL * A;
- double b1 = -2 * cs;
- double b2 = 1 - AL * A;
- double a0 = 1 + AL / A;
- double a1 = -2 * cs;
- double a2 = 1 - AL / A;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
-
- void AllPass::setup(double sampleRate,
- double phaseFrequency,
- double q)
- {
- double w0 = 2 * doublePi * phaseFrequency / sampleRate;
- double cs = cos(w0);
- double sn = sin(w0);
- double AL = sn / (2 * q);
- double b0 = 1 - AL;
- double b1 = -2 * cs;
- double b2 = 1 + AL;
- double a0 = 1 + AL;
- double a1 = -2 * cs;
- double a2 = 1 - AL;
- setCoefficients(a0, a1, a2, b0, b1, b2);
- }
- } // namespace RBJ
- } // namespace Dsp
|