32template <
typename NumericType>
34 : coefficients ({ NumericType(),
42template <
typename NumericType>
44 NumericType a0, NumericType a1)
50 auto a0inv =
static_cast<NumericType
> (1) / a0;
52 coefficients.add (b0 * a0inv,
57template <
typename NumericType>
59 NumericType a0, NumericType a1, NumericType a2)
65 auto a0inv =
static_cast<NumericType
> (1) / a0;
67 coefficients.add (b0 * a0inv,
74template <
typename NumericType>
76 NumericType a0, NumericType a1, NumericType a2, NumericType a3)
82 auto a0inv =
static_cast<NumericType
> (1) / a0;
84 coefficients.add (b0 * a0inv,
93template <
typename NumericType>
95 NumericType frequency)
97 jassert (sampleRate > 0.0);
98 jassert (frequency > 0 && frequency <=
static_cast<float> (sampleRate * 0.5));
105template <
typename NumericType>
107 NumericType frequency)
109 jassert (sampleRate > 0.0);
110 jassert (frequency > 0 && frequency <=
static_cast<float> (sampleRate * 0.5));
117template <
typename NumericType>
119 NumericType frequency)
121 jassert (sampleRate > 0.0);
122 jassert (frequency > 0 && frequency <=
static_cast<float> (sampleRate * 0.5));
129template <
typename NumericType>
131 NumericType frequency)
133 return makeLowPass (sampleRate, frequency, inverseRootTwo);
136template <
typename NumericType>
138 NumericType frequency,
141 jassert (sampleRate > 0.0);
142 jassert (frequency > 0 && frequency <=
static_cast<float> (sampleRate * 0.5));
146 auto nSquared = n * n;
148 auto c1 = 1 / (1 + invQ * n + nSquared);
151 1, c1 * 2 * (1 - nSquared),
152 c1 * (1 - invQ * n + nSquared));
155template <
typename NumericType>
157 NumericType frequency)
159 return makeHighPass (sampleRate, frequency, inverseRootTwo);
162template <
typename NumericType>
164 NumericType frequency,
167 jassert (sampleRate > 0.0);
168 jassert (frequency > 0 && frequency <=
static_cast<float> (sampleRate * 0.5));
172 auto nSquared = n * n;
174 auto c1 = 1 / (1 + invQ * n + nSquared);
177 1, c1 * 2 * (nSquared - 1),
178 c1 * (1 - invQ * n + nSquared));
181template <
typename NumericType>
183 NumericType frequency)
185 return makeBandPass (sampleRate, frequency, inverseRootTwo);
188template <
typename NumericType>
190 NumericType frequency,
193 jassert (sampleRate > 0.0);
194 jassert (frequency > 0 && frequency <=
static_cast<float> (sampleRate * 0.5));
198 auto nSquared = n * n;
200 auto c1 = 1 / (1 + invQ * n + nSquared);
204 c1 * 2 * (1 - nSquared),
205 c1 * (1 - invQ * n + nSquared));
208template <
typename NumericType>
210 NumericType frequency)
212 return makeNotch (sampleRate, frequency, inverseRootTwo);
215template <
typename NumericType>
217 NumericType frequency,
220 jassert (sampleRate > 0.0);
221 jassert (frequency > 0 && frequency <=
static_cast<float> (sampleRate * 0.5));
225 auto nSquared = n * n;
227 auto c1 = 1 / (1 + n * invQ + nSquared);
228 auto b0 = c1 * (1 + nSquared);
229 auto b1 = 2 * c1 * (1 - nSquared);
231 return *
new Coefficients (b0, b1, b0, 1, b1, c1 * (1 - n * invQ + nSquared));
234template <
typename NumericType>
236 NumericType frequency)
238 return makeAllPass (sampleRate, frequency, inverseRootTwo);
241template <
typename NumericType>
243 NumericType frequency,
246 jassert (sampleRate > 0);
247 jassert (frequency > 0 && frequency <= sampleRate * 0.5);
251 auto nSquared = n * n;
253 auto c1 = 1 / (1 + invQ * n + nSquared);
254 auto b0 = c1 * (1 - n * invQ + nSquared);
255 auto b1 = c1 * 2 * (1 - nSquared);
260template <
typename NumericType>
262 NumericType cutOffFrequency,
264 NumericType gainFactor)
266 jassert (sampleRate > 0.0);
267 jassert (cutOffFrequency > 0.0 && cutOffFrequency <= sampleRate * 0.5);
270 auto A = jmax (
static_cast<NumericType
> (0.0), std::sqrt (gainFactor));
271 auto aminus1 = A - 1;
273 auto omega = (2 *
MathConstants<NumericType>::pi * jmax (cutOffFrequency,
static_cast<NumericType
> (2.0))) /
static_cast<NumericType
> (sampleRate);
274 auto coso = std::cos (omega);
275 auto beta = std::sin (omega) * std::sqrt (A) / Q;
276 auto aminus1TimesCoso = aminus1 * coso;
278 return *
new Coefficients (A * (aplus1 - aminus1TimesCoso + beta),
279 A * 2 * (aminus1 - aplus1 * coso),
280 A * (aplus1 - aminus1TimesCoso - beta),
281 aplus1 + aminus1TimesCoso + beta,
282 -2 * (aminus1 + aplus1 * coso),
283 aplus1 + aminus1TimesCoso - beta);
286template <
typename NumericType>
288 NumericType cutOffFrequency,
290 NumericType gainFactor)
292 jassert (sampleRate > 0);
293 jassert (cutOffFrequency > 0 && cutOffFrequency <=
static_cast<NumericType
> (sampleRate * 0.5));
296 auto A = jmax (
static_cast<NumericType
> (0.0), std::sqrt (gainFactor));
297 auto aminus1 = A - 1;
299 auto omega = (2 *
MathConstants<NumericType>::pi * jmax (cutOffFrequency,
static_cast<NumericType
> (2.0))) /
static_cast<NumericType
> (sampleRate);
300 auto coso = std::cos (omega);
301 auto beta = std::sin (omega) * std::sqrt (A) / Q;
302 auto aminus1TimesCoso = aminus1 * coso;
304 return *
new Coefficients (A * (aplus1 + aminus1TimesCoso + beta),
305 A * -2 * (aminus1 + aplus1 * coso),
306 A * (aplus1 + aminus1TimesCoso - beta),
307 aplus1 - aminus1TimesCoso + beta,
308 2 * (aminus1 - aplus1 * coso),
309 aplus1 - aminus1TimesCoso - beta);
312template <
typename NumericType>
314 NumericType frequency,
316 NumericType gainFactor)
318 jassert (sampleRate > 0);
319 jassert (frequency > 0 && frequency <=
static_cast<NumericType
> (sampleRate * 0.5));
321 jassert (gainFactor > 0);
323 auto A = jmax (
static_cast<NumericType
> (0.0), std::sqrt (gainFactor));
325 auto alpha = std::sin (omega) / (Q * 2);
326 auto c2 = -2 * std::cos (omega);
327 auto alphaTimesA = alpha * A;
328 auto alphaOverA = alpha / A;
336template <
typename NumericType>
339 return (
static_cast<size_t> (coefficients.size()) - 1) / 2;
342template <
typename NumericType>
345 constexpr Complex<double> j (0, 1);
346 const auto order = getFilterOrder();
347 const auto* coefs = coefficients.begin();
349 jassert (frequency >= 0 && frequency <= sampleRate * 0.5);
351 Complex<double> numerator = 0.0, denominator = 0.0, factor = 1.0;
354 for (
size_t n = 0; n <= order; ++n)
356 numerator +=
static_cast<double> (coefs[n]) * factor;
363 for (
size_t n = order + 1; n <= 2 * order; ++n)
365 denominator +=
static_cast<double> (coefs[n]) * factor;
369 return std::abs (numerator / denominator);
372template <
typename NumericType>
374 size_t numSamples,
double sampleRate)
const noexcept
376 constexpr Complex<double> j (0, 1);
377 const auto order = getFilterOrder();
378 const auto* coefs = coefficients.begin();
380 jassert (order >= 0);
382 for (
size_t i = 0; i < numSamples; ++i)
384 jassert (frequencies[i] >= 0 && frequencies[i] <= sampleRate * 0.5);
386 Complex<double> numerator = 0.0, denominator = 0.0, factor = 1.0;
389 for (
size_t n = 0; n <= order; ++n)
391 numerator +=
static_cast<double> (coefs[n]) * factor;
398 for (
size_t n = order + 1; n <= 2 * order; ++n)
400 denominator +=
static_cast<double> (coefs[n]) * factor;
404 magnitudes[i] = std::abs(numerator / denominator);
408template <
typename NumericType>
411 constexpr Complex<double> j (0, 1);
412 const auto order = getFilterOrder();
413 const auto* coefs = coefficients.begin();
415 jassert (frequency >= 0 && frequency <= sampleRate * 0.5);
417 Complex<double> numerator = 0.0, denominator = 0.0, factor = 1.0;
420 for (
size_t n = 0; n <= order; ++n)
422 numerator +=
static_cast<double> (coefs[n]) * factor;
429 for (
size_t n = order + 1; n <= 2 * order; ++n)
431 denominator +=
static_cast<double> (coefs[n]) * factor;
435 return std::arg (numerator / denominator);
438template <
typename NumericType>
440 size_t numSamples,
double sampleRate)
const noexcept
442 jassert (sampleRate > 0);
444 constexpr Complex<double> j (0, 1);
445 const auto order = getFilterOrder();
446 const auto* coefs = coefficients.begin();
447 auto invSampleRate = 1 / sampleRate;
449 jassert (order >= 0);
451 for (
size_t i = 0; i < numSamples; ++i)
453 jassert (frequencies[i] >= 0 && frequencies[i] <= sampleRate * 0.5);
455 Complex<double> numerator = 0.0, denominator = 0.0, factor = 1.0;
458 for (
size_t n = 0; n <= order; ++n)
460 numerator +=
static_cast<double> (coefs[n]) * factor;
467 for (
size_t n = order + 1; n <= 2 * order; ++n)
469 denominator +=
static_cast<double> (coefs[n]) * factor;
473 phases[i] = std::arg (numerator / denominator);
A smart-pointer class which points to a reference-counted object.
Commonly used mathematical constants.
A set of coefficients for use in an Filter object.
static Ptr makeNotch(double sampleRate, NumericType frequency)
Returns the coefficients for a notch filter.
static Ptr makeHighShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Returns the coefficients for a high-pass shelf filter with variable Q and gain.
void getMagnitudeForFrequencyArray(const double *frequencies, double *magnitudes, size_t numSamples, double sampleRate) const noexcept
Returns the magnitude frequency response of the filter for a given frequency array and sample rate.
double getMagnitudeForFrequency(double frequency, double sampleRate) const noexcept
Returns the magnitude frequency response of the filter for a given frequency and sample rate.
static Ptr makeAllPass(double sampleRate, NumericType frequency)
Returns the coefficients for an all-pass filter.
static Ptr makeFirstOrderHighPass(double sampleRate, NumericType frequency)
Returns the coefficients for a first order high-pass filter.
void getPhaseForFrequencyArray(double *frequencies, double *phases, size_t numSamples, double sampleRate) const noexcept
Returns the phase frequency response of the filter for a given frequency array and sample rate.
static Ptr makeFirstOrderAllPass(double sampleRate, NumericType frequency)
Returns the coefficients for a first order all-pass filter.
static Ptr makePeakFilter(double sampleRate, NumericType centreFrequency, NumericType Q, NumericType gainFactor)
Returns the coefficients for a peak filter centred around a given frequency, with a variable Q and ga...
static Ptr makeLowPass(double sampleRate, NumericType frequency)
Returns the coefficients for a low-pass filter.
Coefficients()
Creates a null set of coefficients (which will produce silence).
double getPhaseForFrequency(double frequency, double sampleRate) const noexcept
Returns the phase frequency response of the filter for a given frequency and sample rate.
size_t getFilterOrder() const noexcept
Returns the filter order associated with the coefficients.
static Ptr makeLowShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Returns the coefficients for a low-pass shelf filter with variable Q and gain.
static Ptr makeBandPass(double sampleRate, NumericType frequency)
Returns the coefficients for a band-pass filter.
static Ptr makeHighPass(double sampleRate, NumericType frequency)
Returns the coefficients for a high-pass filter.
static Ptr makeFirstOrderLowPass(double sampleRate, NumericType frequency)
Returns the coefficients for a first order low-pass filter.