Skip to content

Commit 927fc25

Browse files
committed
Butterworth filter
1 parent 1ec998a commit 927fc25

File tree

3 files changed

+467
-11
lines changed

3 files changed

+467
-11
lines changed

modules/yup_dsp/designers/yup_FilterDesigner.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,12 @@ int FilterDesigner<CoeffType>::designButterworth (
419419
// Lowpass and Highpass filters
420420
for (int s = 0; s < numStages; ++s)
421421
{
422-
const CoeffType d = static_cast<CoeffType> (2.0) * std::sin (((static_cast<CoeffType> (2 * (s + 1) - 1)) * MathConstants<CoeffType>::pi) / (static_cast<CoeffType> (2 * order)));
422+
const CoeffType d = static_cast<CoeffType> (2.0) *
423+
std::sin (((static_cast<CoeffType> (2 * (s + 1) - 1)) * MathConstants<CoeffType>::pi) / (static_cast<CoeffType> (2 * order)));
423424

424-
const CoeffType beta = static_cast<CoeffType> (0.5) * ((static_cast<CoeffType> (1.0) - (d / static_cast<CoeffType> (2.0)) * std::sin (omega)) /
425-
(static_cast<CoeffType> (1.0) + (d / static_cast<CoeffType> (2.0)) * std::sin (omega)));
425+
const CoeffType beta = static_cast<CoeffType> (0.5) *
426+
((static_cast<CoeffType> (1.0) - (d / static_cast<CoeffType> (2.0)) * std::sin (omega)) /
427+
(static_cast<CoeffType> (1.0) + (d / static_cast<CoeffType> (2.0)) * std::sin (omega)));
426428

427429
const CoeffType gamma = (static_cast<CoeffType> (0.5) + beta) * std::cos (omega);
428430

@@ -512,6 +514,31 @@ int FilterDesigner<CoeffType>::designButterworth (
512514
coefficients.push_back (coeffs);
513515
}
514516
}
517+
else if (filterMode.test (FilterMode::allpass))
518+
{
519+
// Allpass filters - use same structure as lowpass but with different coefficients
520+
for (int s = 0; s < numStages; ++s)
521+
{
522+
const CoeffType d = static_cast<CoeffType> (2.0) * std::sin (((static_cast<CoeffType> (2 * (s + 1) - 1)) * MathConstants<CoeffType>::pi) / (static_cast<CoeffType> (2 * order)));
523+
524+
const CoeffType beta = static_cast<CoeffType> (0.5) * ((static_cast<CoeffType> (1.0) - (d / static_cast<CoeffType> (2.0)) * std::sin (omega)) /
525+
(static_cast<CoeffType> (1.0) + (d / static_cast<CoeffType> (2.0)) * std::sin (omega)));
526+
527+
const CoeffType gamma = (static_cast<CoeffType> (0.5) + beta) * std::cos (omega);
528+
529+
BiquadCoefficients<CoeffType> coeffs;
530+
// For allpass: numerator = reversed denominator
531+
coeffs.a0 = static_cast<CoeffType> (1.0);
532+
coeffs.a1 = static_cast<CoeffType> (-2.0) * gamma;
533+
coeffs.a2 = static_cast<CoeffType> (2.0) * beta;
534+
coeffs.b0 = static_cast<CoeffType> (2.0) * beta;
535+
coeffs.b1 = static_cast<CoeffType> (-2.0) * gamma;
536+
coeffs.b2 = static_cast<CoeffType> (1.0);
537+
538+
coeffs.normalize();
539+
coefficients.push_back (coeffs);
540+
}
541+
}
515542

516543
return static_cast<int> (coefficients.size());
517544
}

modules/yup_dsp/filters/yup_ButterworthFilter.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ButterworthFilter : public BiquadCascade<SampleType, CoeffType>
5555

5656
//==============================================================================
5757
/** Maximum supported filter order */
58-
static constexpr int maxOrder = 32;
58+
static constexpr int maxOrder = 16;
5959

6060
public:
6161
//==============================================================================
@@ -70,7 +70,7 @@ class ButterworthFilter : public BiquadCascade<SampleType, CoeffType>
7070
ButterworthFilter (FilterModeType mode, int filterOrder, CoeffType freq)
7171
: ButterworthFilter()
7272
{
73-
setParameters (mode, filterOrder, freq, static_cast<CoeffType> (0.0), static_cast<CoeffType> (0.0), 44100.0);
73+
setParameters (mode, filterOrder, freq, static_cast<CoeffType> (0.0), 44100.0);
7474
}
7575

7676
//==============================================================================
@@ -91,13 +91,13 @@ class ButterworthFilter : public BiquadCascade<SampleType, CoeffType>
9191
{
9292
mode = resolveFilterMode (mode, getSupportedModes());
9393

94-
jassert (filterOrder >= 1 && filterOrder <= maxOrder);
94+
jassert (filterOrder >= 2 && filterOrder <= maxOrder);
9595
jassert (freq > static_cast<CoeffType> (0.0));
96-
if (mode.test (FilterMode::bandpass) || mode.test (FilterMode::bandstop))
97-
jassert (freq2 > freq && freq2 > static_cast<CoeffType> (0.0));
9896

99-
// Ensure order is valid (1 or power of 2)
100-
filterOrder = filterOrder == 1 ? filterOrder : jlimit (2, maxOrder, nextPowerOfTwo (filterOrder));
97+
if ((mode.test (FilterMode::bandpass) || mode.test (FilterMode::bandstop)) && freq2 < freq)
98+
std::swap (freq, freq2);
99+
100+
filterOrder = jlimit (2, maxOrder, nextEven (filterOrder));
101101

102102
if (filterMode != mode
103103
|| order != filterOrder
@@ -138,7 +138,7 @@ class ButterworthFilter : public BiquadCascade<SampleType, CoeffType>
138138
*/
139139
void setOrder (int filterOrder) noexcept
140140
{
141-
filterOrder = filterOrder == 1 ? filterOrder : jlimit (2, maxOrder, nextPowerOfTwo (filterOrder));
141+
filterOrder = jlimit (2, maxOrder, nextEven (filterOrder));
142142

143143
if (order != filterOrder)
144144
{

0 commit comments

Comments
 (0)