Browse Source

DSP: Added Butterworth methods to the FilterDesign class

tags/2021-05-28
jules 7 years ago
parent
commit
4797b29b27
2 changed files with 106 additions and 9 deletions
  1. +74
    -4
      modules/juce_dsp/filter_design/juce_FilterDesign.cpp
  2. +32
    -5
      modules/juce_dsp/filter_design/juce_FilterDesign.h

+ 74
- 4
modules/juce_dsp/filter_design/juce_FilterDesign.cpp View File

@@ -510,7 +510,7 @@ Array<IIR::Coefficients<FloatType>>
g.add ((1.0 - p[i + r]) / (1.0 - z[i]));
}
Array<IIR::Coefficients<FloatType>> theCascadedCoefficients;
Array<IIR::Coefficients<FloatType>> cascadedCoefficients;
if (r == 1)
{
@@ -518,7 +518,7 @@ Array<IIR::Coefficients<FloatType>>
auto b1 = b0;
auto a1 = static_cast<FloatType> (-std::real (p[0]));
theCascadedCoefficients.add (IIR::Coefficients<FloatType> (b0, b1, 1.f, a1));
cascadedCoefficients.add ({ b0, b1, 1.0f, a1 });
}
for (int i = 0; i < L; ++i)
@@ -532,10 +532,80 @@ Array<IIR::Coefficients<FloatType>>
auto a1 = static_cast<FloatType> (std::real (-p[i+r] - std::conj (p[i + r])));
auto a2 = static_cast<FloatType> (std::real ( p[i+r] * std::conj (p[i + r])));
theCascadedCoefficients.add (IIR::Coefficients<FloatType> (b0, b1, b2, 1, a1, a2));
cascadedCoefficients.add ({ b0, b1, b2, 1, a1, a2 });
}
return theCascadedCoefficients;
return cascadedCoefficients;
}
template <typename FloatType>
Array<IIR::Coefficients<FloatType>>
FilterDesign<FloatType>::designIIRLowpassHighOrderButterworthMethod (FloatType frequency,
double sampleRate, int order)
{
jassert (sampleRate > 0);
jassert (frequency > 0 && frequency <= sampleRate * 0.5);
jassert (order > 0);
Array<IIR::Coefficients<FloatType>> arrayFilters;
if (order % 2 == 1)
{
arrayFilters.add (*IIR::Coefficients<FloatType>::makeFirstOrderLowPass (sampleRate, frequency));
for (auto i = 0; i < order / 2; ++i)
{
auto Q = 1.0 / (2.0 * std::cos ((i + 1.0) * MathConstants<double>::pi / order));
arrayFilters.add (*IIR::Coefficients<FloatType>::makeLowPass (sampleRate, frequency,
static_cast<FloatType> (Q)));
}
}
else
{
for (auto i = 0; i < order / 2; ++i)
{
auto Q = 1.0 / (2.0 * std::cos ((2.0 * i + 1.0) * MathConstants<double>::pi / (order * 2.0)));
arrayFilters.add (*IIR::Coefficients<FloatType>::makeLowPass (sampleRate, frequency,
static_cast<FloatType> (Q)));
}
}
return arrayFilters;
}
template <typename FloatType>
Array<IIR::Coefficients<FloatType>>
FilterDesign<FloatType>::designIIRHighpassHighOrderButterworthMethod (FloatType frequency,
double sampleRate, int order)
{
jassert (sampleRate > 0);
jassert (frequency > 0 && frequency <= sampleRate * 0.5);
jassert (order > 0);
Array<IIR::Coefficients<FloatType>> arrayFilters;
if (order % 2 == 1)
{
arrayFilters.add (*IIR::Coefficients<FloatType>::makeFirstOrderHighPass (sampleRate, frequency));
for (auto i = 0; i < order / 2; ++i)
{
auto Q = 1.0 / (2.0 * std::cos ((i + 1.0) * MathConstants<double>::pi / order));
arrayFilters.add (*IIR::Coefficients<FloatType>::makeHighPass (sampleRate, frequency,
static_cast<FloatType> (Q)));
}
}
else
{
for (auto i = 0; i < order / 2; ++i)
{
auto Q = 1.0 / (2.0 * std::cos ((2.0 * i + 1.0) * MathConstants<double>::pi / (order * 2.0)));
arrayFilters.add (*IIR::Coefficients<FloatType>::makeHighPass (sampleRate, frequency,
static_cast<FloatType> (Q)));
}
}
return arrayFilters;
}
template <typename FloatType>


+ 32
- 5
modules/juce_dsp/filter_design/juce_FilterDesign.h View File

@@ -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<IIRCoefficients> 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<IIRCoefficients> 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


Loading…
Cancel
Save