DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::FilterDesign Class Reference

Digital sections from analog ones, and the cascades of an equalizer. More...

#include <FilterDesign.h>

Classes

class  Band
 The frequencies a design is fitted over at one sample rate. More...
 

Static Public Member Functions

static BiquadCoeffs bilinear (const AnalogSection &section, double sampleRate, double exactHz) noexcept
 The bilinear transform of a section, with one frequency mapped exactly.
 
static bool bilinearPolynomial (const double *analogNum, const double *analogDen, int order, double sampleRate, double exactHz, double *digitalNum, double *digitalDen) noexcept
 The bilinear transform of a whole analog network, polynomial to polynomial, with no factoring.
 
static BiquadCoeffs digital (const AnalogSection &section, const Band &band) noexcept
 The biquad whose magnitude follows an analog section up to Nyquist.
 
static BiquadCoeffs digital (const AnalogSection &section, double sampleRate) noexcept
 digital() for one design: builds the Band of the rate on the spot.
 
static int digital (const AnalogCascade &cascade, const Band &band, BiquadCoeffs *out, int capacity) noexcept
 Every section of a cascade through digital().
 
static int digital (const AnalogCascade &cascade, double sampleRate, BiquadCoeffs *out, int capacity) noexcept
 Every section of a cascade through digital(), for one design.
 
static double magnitudeDb (const BiquadCoeffs *sections, int n, double hz, double sampleRate) noexcept
 Magnitude in decibels of n digital sections in series at a frequency.
 
static AnalogCascade highCut (double hz, double slopeDbPerOctave, double q=kButterworthQ) noexcept
 High cut (low-pass) of any slope from 0 to 96 dB per octave.
 
static AnalogCascade lowCut (double hz, double slopeDbPerOctave, double q=kButterworthQ) noexcept
 Low cut (high-pass) of any slope from 0 to 96 dB per octave: highCut() mirrored about the corner.
 
static AnalogCascade lowShelf (double hz, double gainDb, double order, double q=kButterworthQ) noexcept
 Low shelf of any order from 1, whole or not: gainDb below the corner, half of it (in decibels) at the corner, 6 dB per octave of transition per order.
 
static AnalogCascade highShelf (double hz, double gainDb, double order, double q=kButterworthQ) noexcept
 High shelf of any order: lowShelf() mirrored about the corner.
 
static AnalogCascade bell (double hz, double q, double gainDb, double order) noexcept
 Bell of any order: the higher the order, the flatter the top and the steeper the sides (6 dB per octave each per order).
 
static AnalogCascade bandPass (double hz, double q, double order) noexcept
 Butterworth band-pass: 0 dB at the centre, -3 dB at the two edges hz / q apart, 6 dB per octave on each side per order. One biquad per order (a part of an order takes one more: between whole orders the band is that of highCut() at that slope, moved to the centre).
 
static AnalogCascade bandStop (double hz, double q, double order) noexcept
 Butterworth band-stop: a null at the centre, -3 dB at the two edges hz / q apart, and a floor that widens with the order. One biquad per order (a part of an order takes one more).
 
static AnalogCascade allPass (double hz, double q, double order) noexcept
 All-pass of any order: the magnitude stays at 1 and the phase turns 180 degrees per order around the frequency.
 
static AnalogCascade tilt (double pivotHz, double slopeDbPerOctave) noexcept
 Tilt: the same slope across the whole band, 0 dB at the pivot.
 
static double tiltSlopeWithin (double pivotHz, double slopeDbPerOctave, double ceilingDb) noexcept
 The slope a tilt can have at a pivot without lifting the end that rises (20 kHz for a positive slope, 20 Hz for a negative one) by more than a ceiling: the slope asked for, or the one that lands that end on the ceiling. The end that falls is not limited.
 
static AnalogCascade ellipticHighCut (double hz, int order=12, double rippleDb=0.05, double stopDb=100.0) noexcept
 Elliptic high cut: flat to the corner within the ripple, then a wall down to the stop level.
 
static AnalogCascade ellipticLowCut (double hz, int order=12, double rippleDb=0.05, double stopDb=100.0) noexcept
 Elliptic low cut: ellipticHighCut() mirrored about the corner.
 
static double ellipticStopRatio (int order=12, double rippleDb=0.05, double stopDb=100.0) noexcept
 Where the stop band of an elliptic cut starts, as a ratio to its corner (above 1; the same for the high cut and, inverted, for the low cut).
 

Static Public Attributes

static constexpr int kFitSteps = 2
 Most joint steps a fit takes.
 
static constexpr double kButterworthQ = 0.7071067811865476
 The Q that leaves a cut maximally flat.
 
static constexpr double kMaxCutSlope = 96.0
 Steepest cut, in decibels per octave.
 
static constexpr int kMaxOrder = AnalogCascade::kMaxSections
 Highest order of bells, shelves and band filters.
 

Detailed Description

Digital sections from analog ones, and the cascades of an equalizer.

See the file notes for the method. The figures (worst case and mean distance to the analog magnitude, and the cost of a design) are in the changelog entry of this header and are asserted by its tests.

Definition at line 284 of file FilterDesign.h.

Member Function Documentation

◆ allPass()

static AnalogCascade dspark::FilterDesign::allPass ( double  hz,
double  q,
double  order 
)
inlinestaticnoexcept

All-pass of any order: the magnitude stays at 1 and the phase turns 180 degrees per order around the frequency.

The poles are those of the cut of the same order (q acts on the sharpest pair, from order 2 up). The order is any from 1, whole or not: the phase at the frequency moves 90 degrees per order all the way.

Definition at line 681 of file FilterDesign.h.

◆ bandPass()

static AnalogCascade dspark::FilterDesign::bandPass ( double  hz,
double  q,
double  order 
)
inlinestaticnoexcept

Butterworth band-pass: 0 dB at the centre, -3 dB at the two edges hz / q apart, 6 dB per octave on each side per order. One biquad per order (a part of an order takes one more: between whole orders the band is that of highCut() at that slope, moved to the centre).

Definition at line 658 of file FilterDesign.h.

◆ bandStop()

static AnalogCascade dspark::FilterDesign::bandStop ( double  hz,
double  q,
double  order 
)
inlinestaticnoexcept

Butterworth band-stop: a null at the centre, -3 dB at the two edges hz / q apart, and a floor that widens with the order. One biquad per order (a part of an order takes one more).

Definition at line 668 of file FilterDesign.h.

◆ bell()

static AnalogCascade dspark::FilterDesign::bell ( double  hz,
double  q,
double  gainDb,
double  order 
)
inlinestaticnoexcept

Bell of any order: the higher the order, the flatter the top and the steeper the sides (6 dB per octave each per order).

Order 1 is AnalogSection::peak(). The gain is gainDb at the centre and half of it (in decibels) at the two edges hz / q apart, whatever the order, whole or not; a cut is the exact inverse of the boost. One biquad per order (a part of an order takes one more).

Definition at line 631 of file FilterDesign.h.

◆ bilinear()

static BiquadCoeffs dspark::FilterDesign::bilinear ( const AnalogSection &  section,
double  sampleRate,
double  exactHz 
)
inlinestaticnoexcept

The bilinear transform of a section, with one frequency mapped exactly.

Parameters
sectionAnalog section.
sampleRateSample rate in Hz.
exactHzFrequency that keeps its place (from 0.49 of the rate up, none does and the plain transform is used).

Definition at line 347 of file FilterDesign.h.

◆ bilinearPolynomial()

static bool dspark::FilterDesign::bilinearPolynomial ( const double *  analogNum,
const double *  analogDen,
int  order,
double  sampleRate,
double  exactHz,
double *  digitalNum,
double *  digitalDen 
)
inlinestaticnoexcept

The bilinear transform of a whole analog network, polynomial to polynomial, with no factoring.

H(s) = (n_0 + n_1 s + ... + n_N s^N) / (d_0 + d_1 s + ... + d_N s^N) becomes b(z) / a(z), of the same order: with s = K (1 - z^-1) / (1 + z^-1), each power s^j times (1 + z^-1)^N is K^j (1 - z^-1)^j (1 + z^-1)^(N - j), a fixed binomial row, so the digital coefficients are sums of the analog ones over those rows. The result is scaled so a[0] = 1; it feeds NormalizedLadderN directly.

Parameters
analogNumN + 1 coefficients of the numerator, from s^0.
analogDenN + 1 coefficients of the denominator, from s^0.
orderN, 1 to 16.
sampleRateSample rate in Hz.
exactHzFrequency that keeps its place (from 0.49 of the rate up, or 0, none does and K = 2 fs).
digitalNumReceives the N + 1 coefficients of b, from z^0.
digitalDenReceives the N + 1 coefficients of a, from z^0.
Returns
False (outputs untouched) for an order out of range or a denominator that maps to a[0] = 0.

Definition at line 387 of file FilterDesign.h.

◆ digital() [1/4]

static int dspark::FilterDesign::digital ( const AnalogCascade &  cascade,
const Band &  band,
BiquadCoeffs *  out,
int  capacity 
)
inlinestaticnoexcept

Every section of a cascade through digital().

Parameters
cascadeAnalog cascade.
bandThe frequencies of the fit.
outReceives the sections.
capacityRoom in out.
Returns
Number of sections written.

Definition at line 523 of file FilterDesign.h.

◆ digital() [2/4]

static int dspark::FilterDesign::digital ( const AnalogCascade &  cascade,
double  sampleRate,
BiquadCoeffs *  out,
int  capacity 
)
inlinestaticnoexcept

Every section of a cascade through digital(), for one design.

Definition at line 532 of file FilterDesign.h.

◆ digital() [3/4]

static BiquadCoeffs dspark::FilterDesign::digital ( const AnalogSection &  section,
const Band &  band 
)
inlinestaticnoexcept

The biquad whose magnitude follows an analog section up to Nyquist.

The section returned is minimum phase and has positive gain, whatever the sign and the phase of the prototype; an all-pass prototype gives an all-pass section with the poles of the prototype.

Parameters
sectionAnalog section with its poles in the left half plane.
bandThe frequencies of the fit (it carries the sample rate).
Returns
A stable section (the bilinear transform of the section when the prototype is not one this design can follow).

Definition at line 434 of file FilterDesign.h.

◆ digital() [4/4]

static BiquadCoeffs dspark::FilterDesign::digital ( const AnalogSection &  section,
double  sampleRate 
)
inlinestaticnoexcept

digital() for one design: builds the Band of the rate on the spot.

Definition at line 509 of file FilterDesign.h.

◆ ellipticHighCut()

static AnalogCascade dspark::FilterDesign::ellipticHighCut ( double  hz,
int  order = 12,
double  rippleDb = 0.05,
double  stopDb = 100.0 
)
inlinestaticnoexcept

Elliptic high cut: flat to the corner within the ripple, then a wall down to the stop level.

With the defaults (order 12, 0.05 dB, 100 dB) the stop band starts a fifth of an octave above the corner.

Parameters
hzEnd of the pass band.
order1 to 16.
rippleDbRipple of the pass band, 0.001 to 3 dB.
stopDbAttenuation of the stop band, 20 to 140 dB.

Definition at line 797 of file FilterDesign.h.

◆ ellipticLowCut()

static AnalogCascade dspark::FilterDesign::ellipticLowCut ( double  hz,
int  order = 12,
double  rippleDb = 0.05,
double  stopDb = 100.0 
)
inlinestaticnoexcept

Elliptic low cut: ellipticHighCut() mirrored about the corner.

Definition at line 807 of file FilterDesign.h.

◆ ellipticStopRatio()

static double dspark::FilterDesign::ellipticStopRatio ( int  order = 12,
double  rippleDb = 0.05,
double  stopDb = 100.0 
)
inlinestaticnoexcept

Where the stop band of an elliptic cut starts, as a ratio to its corner (above 1; the same for the high cut and, inverted, for the low cut).

Definition at line 821 of file FilterDesign.h.

◆ highCut()

static AnalogCascade dspark::FilterDesign::highCut ( double  hz,
double  slopeDbPerOctave,
double  q = kButterworthQ 
)
inlinestaticnoexcept

High cut (low-pass) of any slope from 0 to 96 dB per octave.

The slope is a real number. Every 6 dB per octave is one unit of order, and whole orders are Butterworth filters. Between them the poles sit on the same semicircle at the angles of a real order, and one section with a finite zero (a pair, or one real pole) keeps the corner at -3 dB and the cascade on the curve 1 / (1 + w^(2 order)) down to -36 dB. Under 6 dB per octave the filter is the fraction of a pole, (1 + s)^(-order), built from six real poles and zeros two octaves apart: it leaves 3 dB times the order at the corner and nothing at a slope of zero.

Parameters
hzCorner frequency.
slopeDbPerOctaveSlope, 0 to 96 (0 gives an empty cascade).
qResonance of the sharpest pole pair, as the Q of a 12 dB per octave filter (0.7071 is Butterworth at every slope). It has no effect up to 6 dB per octave and comes in between 6 and 12.

Definition at line 573 of file FilterDesign.h.

◆ highShelf()

static AnalogCascade dspark::FilterDesign::highShelf ( double  hz,
double  gainDb,
double  order,
double  q = kButterworthQ 
)
inlinestaticnoexcept

High shelf of any order: lowShelf() mirrored about the corner.

Definition at line 614 of file FilterDesign.h.

◆ lowCut()

static AnalogCascade dspark::FilterDesign::lowCut ( double  hz,
double  slopeDbPerOctave,
double  q = kButterworthQ 
)
inlinestaticnoexcept

Low cut (high-pass) of any slope from 0 to 96 dB per octave: highCut() mirrored about the corner.

Definition at line 582 of file FilterDesign.h.

◆ lowShelf()

static AnalogCascade dspark::FilterDesign::lowShelf ( double  hz,
double  gainDb,
double  order,
double  q = kButterworthQ 
)
inlinestaticnoexcept

Low shelf of any order from 1, whole or not: gainDb below the corner, half of it (in decibels) at the corner, 6 dB per octave of transition per order.

A Butterworth shelf (S. J. Orfanidis, "High-Order Digital Parametric Equalizer Design", J. Audio Eng. Soc., 2005): it never overshoots, and a cut is the exact inverse of the boost of the same size.

From order 2 up, q is the Q of the sharpest pair of poles and of zeros (0.7071: the Butterworth shelf). Above it the response rises over the plateau before the corner and dips after it; the plateau, the half gain at the corner and the inverse cut stay as they are.

Definition at line 605 of file FilterDesign.h.

◆ magnitudeDb()

static double dspark::FilterDesign::magnitudeDb ( const BiquadCoeffs *  sections,
int  n,
double  hz,
double  sampleRate 
)
inlinestaticnoexcept

Magnitude in decibels of n digital sections in series at a frequency.

Definition at line 538 of file FilterDesign.h.

◆ tilt()

static AnalogCascade dspark::FilterDesign::tilt ( double  pivotHz,
double  slopeDbPerOctave 
)
inlinestaticnoexcept

Tilt: the same slope across the whole band, 0 dB at the pivot.

Eight real zeros and poles two octaves apart, from 2.5 Hz to 160 kHz, in four sections. The slope goes from -6 to +6 dB per octave (positive tilts towards the treble); at the two ends it is one zero and one pole.

Parameters
pivotHzFrequency that keeps its level.
slopeDbPerOctaveSlope, -6 to +6 (0 gives an empty cascade).

Definition at line 740 of file FilterDesign.h.

◆ tiltSlopeWithin()

static double dspark::FilterDesign::tiltSlopeWithin ( double  pivotHz,
double  slopeDbPerOctave,
double  ceilingDb 
)
inlinestaticnoexcept

The slope a tilt can have at a pivot without lifting the end that rises (20 kHz for a positive slope, 20 Hz for a negative one) by more than a ceiling: the slope asked for, or the one that lands that end on the ceiling. The end that falls is not limited.

Parameters
pivotHzFrequency that keeps its level.
slopeDbPerOctaveSlope asked for, in dB per octave.
ceilingDbMost the rising end may be lifted, in dB.

Definition at line 776 of file FilterDesign.h.

Member Data Documentation

◆ kButterworthQ

constexpr double dspark::FilterDesign::kButterworthQ = 0.7071067811865476
staticconstexpr

The Q that leaves a cut maximally flat.

Definition at line 550 of file FilterDesign.h.

◆ kFitSteps

constexpr int dspark::FilterDesign::kFitSteps = 2
staticconstexpr

Most joint steps a fit takes.

Definition at line 287 of file FilterDesign.h.

◆ kMaxCutSlope

constexpr double dspark::FilterDesign::kMaxCutSlope = 96.0
staticconstexpr

Steepest cut, in decibels per octave.

Definition at line 551 of file FilterDesign.h.

◆ kMaxOrder

constexpr int dspark::FilterDesign::kMaxOrder = AnalogCascade::kMaxSections
staticconstexpr

Highest order of bells, shelves and band filters.

Definition at line 552 of file FilterDesign.h.


The documentation for this class was generated from the following file: