diff --git a/modules/juce_dsp/filter_design/juce_FilterDesign.cpp b/modules/juce_dsp/filter_design/juce_FilterDesign.cpp index 9b8139e71a..d72f7dbf8d 100644 --- a/modules/juce_dsp/filter_design/juce_FilterDesign.cpp +++ b/modules/juce_dsp/filter_design/juce_FilterDesign.cpp @@ -510,7 +510,7 @@ Array> g.add ((1.0 - p[i + r]) / (1.0 - z[i])); } - Array> theCascadedCoefficients; + Array> cascadedCoefficients; if (r == 1) { @@ -518,7 +518,7 @@ Array> auto b1 = b0; auto a1 = static_cast (-std::real (p[0])); - theCascadedCoefficients.add (IIR::Coefficients (b0, b1, 1.f, a1)); + cascadedCoefficients.add ({ b0, b1, 1.0f, a1 }); } for (int i = 0; i < L; ++i) @@ -532,10 +532,80 @@ Array> auto a1 = static_cast (std::real (-p[i+r] - std::conj (p[i + r]))); auto a2 = static_cast (std::real ( p[i+r] * std::conj (p[i + r]))); - theCascadedCoefficients.add (IIR::Coefficients (b0, b1, b2, 1, a1, a2)); + cascadedCoefficients.add ({ b0, b1, b2, 1, a1, a2 }); } - return theCascadedCoefficients; + return cascadedCoefficients; +} + +template +Array> + FilterDesign::designIIRLowpassHighOrderButterworthMethod (FloatType frequency, + double sampleRate, int order) +{ + jassert (sampleRate > 0); + jassert (frequency > 0 && frequency <= sampleRate * 0.5); + jassert (order > 0); + + Array> arrayFilters; + + if (order % 2 == 1) + { + arrayFilters.add (*IIR::Coefficients::makeFirstOrderLowPass (sampleRate, frequency)); + + for (auto i = 0; i < order / 2; ++i) + { + auto Q = 1.0 / (2.0 * std::cos ((i + 1.0) * MathConstants::pi / order)); + arrayFilters.add (*IIR::Coefficients::makeLowPass (sampleRate, frequency, + static_cast (Q))); + } + } + else + { + for (auto i = 0; i < order / 2; ++i) + { + auto Q = 1.0 / (2.0 * std::cos ((2.0 * i + 1.0) * MathConstants::pi / (order * 2.0))); + arrayFilters.add (*IIR::Coefficients::makeLowPass (sampleRate, frequency, + static_cast (Q))); + } + } + + return arrayFilters; +} + +template +Array> + FilterDesign::designIIRHighpassHighOrderButterworthMethod (FloatType frequency, + double sampleRate, int order) +{ + jassert (sampleRate > 0); + jassert (frequency > 0 && frequency <= sampleRate * 0.5); + jassert (order > 0); + + Array> arrayFilters; + + if (order % 2 == 1) + { + arrayFilters.add (*IIR::Coefficients::makeFirstOrderHighPass (sampleRate, frequency)); + + for (auto i = 0; i < order / 2; ++i) + { + auto Q = 1.0 / (2.0 * std::cos ((i + 1.0) * MathConstants::pi / order)); + arrayFilters.add (*IIR::Coefficients::makeHighPass (sampleRate, frequency, + static_cast (Q))); + } + } + else + { + for (auto i = 0; i < order / 2; ++i) + { + auto Q = 1.0 / (2.0 * std::cos ((2.0 * i + 1.0) * MathConstants::pi / (order * 2.0))); + arrayFilters.add (*IIR::Coefficients::makeHighPass (sampleRate, frequency, + static_cast (Q))); + } + } + + return arrayFilters; } template diff --git a/modules/juce_dsp/filter_design/juce_FilterDesign.h b/modules/juce_dsp/filter_design/juce_FilterDesign.h old mode 100644 new mode 100755 index 5ab6aa9d36..92489e123c --- a/modules/juce_dsp/filter_design/juce_FilterDesign.h +++ b/modules/juce_dsp/filter_design/juce_FilterDesign.h @@ -31,7 +31,7 @@ namespace dsp /** This class provides a set of functions which generates FIR::Coefficients - and IIR::Coefficients, of high-order lowpass filters. They can be used + and IIR::Coefficients, of high-order low-pass filters. They can be used for processing directly audio as an equalizer, in resampling algorithms etc. see FIRFilter::Coefficients, FIRFilter, WindowingFunction, IIRFilter::Coefficients, IIRFilter @@ -142,7 +142,7 @@ struct FilterDesign //============================================================================== /** This method returns an array of IIR::Coefficients, made to be used in - cascaded IIRFilters, providing a minimum phase lowpass filter without any + cascaded IIRFilters, providing a minimum phase low-pass filter without any ripple in the pass band and in the stop band. The algorithms are based on "Lecture Notes on Elliptic Filter Design" by @@ -161,8 +161,35 @@ struct FilterDesign FloatType passbandAttenuationdB, FloatType stopbandAttenuationdB); + //============================================================================== + /** This method returns an array of IIR::Coefficients, made to be used in + cascaded IIRFilters, providing a minimum phase low-pass filter without any + ripple in the pass band and in the stop band. + + @param frequency the cutoff frequency of the low-pass filter + @param sampleRate the sample rate being used in the filter design + @param order the order of the resulting IIR filter, providing + an attenuation of -6 dB times order / octave + */ + + static Array designIIRLowpassHighOrderButterworthMethod (FloatType frequency, double sampleRate, + int order); + + /** This method returns an array of IIR::Coefficients, made to be used in + cascaded IIRFilters, providing a minimum phase high-pass filter without any + ripple in the pass band and in the stop band. + + @param frequency the cutoff frequency of the high-pass filter + @param sampleRate the sample rate being used in the filter design + @param order the order of the resulting IIR filter, providing + an attenuation of -6 dB times order / octave + */ + + static Array designIIRHighpassHighOrderButterworthMethod (FloatType frequency, double sampleRate, + int order); + /** This method returns an array of IIR::Coefficients, made to be used in - cascaded IIRFilters, providing a minimum phase lowpass filter without any + cascaded IIRFilters, providing a minimum phase low-pass filter without any ripple in the stop band only. The algorithms are based on "Lecture Notes on Elliptic Filter Design" by @@ -181,7 +208,7 @@ struct FilterDesign FloatType stopbandAttenuationdB); /** This method returns an array of IIR::Coefficients, made to be used in - cascaded IIRFilters, providing a minimum phase lowpass filter without any + cascaded IIRFilters, providing a minimum phase low-pass filter without any ripple in the pass band only. The algorithms are based on "Lecture Notes on Elliptic Filter Design" by @@ -200,7 +227,7 @@ struct FilterDesign FloatType stopbandAttenuationdB); /** This method returns an array of IIR::Coefficients, made to be used in - cascaded IIR::Filters, providing a minimum phase lowpass filter with ripples + cascaded IIR::Filters, providing a minimum phase low-pass filter with ripples in both the pass band and in the stop band. The algorithms are based on "Lecture Notes on Elliptic Filter Design" by