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

Stores normalised biquad coefficients (b0, b1, b2, a1, a2), always double. More...

#include <Biquad.h>

Public Member Functions

BiquadCoeffs bypassed () const noexcept
 The set with these poles whose numerator equals its denominator.
 
double getMagnitude (double frequency, double sampleRate) const noexcept
 Evaluates magnitude response |H(f)| at a single frequency.
 
template<typename U >
void getMagnitudeForFrequencyArray (std::span< const U > frequencies, std::span< U > magnitudes, double sampleRate) const noexcept
 Computes magnitude responses for a batch of frequencies.
 

Static Public Member Functions

static BiquadCoeffs makeLowPass (double sampleRate, double freq, double Q=0.7071067811865476) noexcept
 Low-pass filter.
 
static BiquadCoeffs makeHighPass (double sampleRate, double freq, double Q=0.7071067811865476) noexcept
 High-pass filter.
 
static BiquadCoeffs makeBandPass (double sampleRate, double freq, double Q=0.7071067811865476) noexcept
 Band-pass filter (constant 0 dB peak gain).
 
static BiquadCoeffs makePeak (double sampleRate, double freq, double Q, double gainDb) noexcept
 Peak (parametric EQ) filter.
 
static BiquadCoeffs makePeakMatched (double sampleRate, double freq, double Q, double gainDb) noexcept
 Analog-matched ("de-cramped") peaking filter (Vicanek design).
 
static BiquadCoeffs makeLowPassMatched (double sampleRate, double freq, double Q=0.7071067811865476) noexcept
 Analog-matched second-order low-pass (Vicanek, "Matched Second Order Digital Filters", 2016).
 
static BiquadCoeffs makeHighPassMatched (double sampleRate, double freq, double Q=0.7071067811865476) noexcept
 Analog-matched second-order high-pass (Vicanek 2016).
 
static BiquadCoeffs makeBandPassMatched (double sampleRate, double freq, double Q=0.7071067811865476) noexcept
 Analog-matched band-pass, 0 dB at the centre (Vicanek 2016).
 
static BiquadCoeffs makeLowShelfMatched (double sampleRate, double freq, double gainDb) noexcept
 Analog-matched Butterworth low shelf (Vicanek, "Matched Two-Pole Digital Shelving Filters", 2024).
 
static BiquadCoeffs makeHighShelfMatched (double sampleRate, double freq, double gainDb) noexcept
 Analog-matched Butterworth high shelf (see makeLowShelfMatched()).
 
static BiquadCoeffs makeLowShelf (double sampleRate, double freq, double gainDb, double slope=1.0) noexcept
 Low-shelf filter.
 
static BiquadCoeffs makeHighShelf (double sampleRate, double freq, double gainDb, double slope=1.0) noexcept
 High-shelf filter.
 
static BiquadCoeffs makeNotch (double sampleRate, double freq, double Q=0.7071067811865476) noexcept
 Notch (band-reject) filter.
 
static BiquadCoeffs makeAllPass (double sampleRate, double freq, double Q=0.7071067811865476) noexcept
 All-pass filter.
 
static BiquadCoeffs makeFirstOrderLowPass (double sampleRate, double frequency) noexcept
 First-order (6 dB/oct) low-pass filter.
 
static BiquadCoeffs makeFirstOrderHighPass (double sampleRate, double frequency) noexcept
 First-order (6 dB/oct) high-pass filter.
 
static BiquadCoeffs makeTilt (double sampleRate, double pivotFreq, double gainDb) noexcept
 Creates a first-order tilt filter.
 
static BiquadCoeffs makeKWeightingShelf (double sampleRate) noexcept
 ITU-R BS.1770 K-weighting, stage 1: the head-related high shelf.
 
static BiquadCoeffs makeKWeightingHighPass (double sampleRate) noexcept
 ITU-R BS.1770 K-weighting, stage 2: the RLB high-pass.
 

Public Attributes

double b0 = 1.0
 
double b1 = 0.0
 
double b2 = 0.0
 
double a1 = 0.0
 
double a2 = 0.0
 

Detailed Description

Stores normalised biquad coefficients (b0, b1, b2, a1, a2), always double.

Coefficients are pre-normalised by a0 in every factory method, so the filter processing loop never needs to divide by a0. Memory is aligned to 32 bytes to facilitate explicit SIMD vectorisation and CPU cache optimal loads.

All factory methods clamp frequency, Q and slope to safe ranges, so any finite parameter combination yields a stable filter. NaN parameters are not sanitised (they propagate into the coefficients), and the sample rate is trusted as-is: the framework contract is a valid AudioSpec upstream.

There is no float instantiation and no coefficient type parameter: a corner far below the sample rate is exact as a DESIGN but cannot be REALISED in float (see the file-level documentation for the measurements), and a coefficient set that cannot be realised is not a coefficient set. One precision, one rule, and the magnitude response drawn from these numbers is the response the audio path actually applies. For DC removal specifically, use DCBlocker, which is built for it.

Definition at line 106 of file Biquad.h.

Member Function Documentation

◆ bypassed()

BiquadCoeffs dspark::BiquadCoeffs::bypassed ( ) const
inlinenoexcept

The set with these poles whose numerator equals its denominator.

Its transfer function is 1 at every frequency, with the poles (and the memory of a filter running on them) still there. A filter whose state is signal, such as BiquadCascade's direct form, can be moved to this set to leave the path and back to return to it, and what it adds to its input fades at the rate of its own poles instead of stopping with a click. A bell at 0 dB already is this set.

Returns
{1, a1, a2} over {1, a1, a2}.

Definition at line 661 of file Biquad.h.

◆ getMagnitude()

double dspark::BiquadCoeffs::getMagnitude ( double  frequency,
double  sampleRate 
) const
inlinenoexcept

Evaluates magnitude response |H(f)| at a single frequency.

Evaluates the transfer function H(z) = B(z)/A(z) at z = e^(j*2*pi*f/fs). Essential for drawing EQ curves and filter response plots. These are the same numbers the recursion runs on, so the drawn curve is the realised response, not a re-derivation of it.

Parameters
frequencyFrequency to evaluate in Hz.
sampleRateSample rate in Hz.
Returns
Magnitude (linear scale, 1.0 = unity gain).

Definition at line 683 of file Biquad.h.

◆ getMagnitudeForFrequencyArray()

template<typename U >
void dspark::BiquadCoeffs::getMagnitudeForFrequencyArray ( std::span< const U >  frequencies,
std::span< U >  magnitudes,
double  sampleRate 
) const
inlinenoexcept

Computes magnitude responses for a batch of frequencies.

Uses C++20 std::span for bounds-safe array access. Efficient batch evaluation for drawing frequency response curves. Templated on the array element type so a UI can keep its curve buffers in float.

Parameters
frequenciesSpan of input frequencies in Hz.
magnitudesSpan where the evaluated magnitudes will be stored. Must be at least as large as the frequencies span.
sampleRateSample rate in Hz.

Definition at line 715 of file Biquad.h.

◆ makeAllPass()

static BiquadCoeffs dspark::BiquadCoeffs::makeAllPass ( double  sampleRate,
double  freq,
double  Q = 0.7071067811865476 
)
inlinestaticnoexcept

All-pass filter.

Parameters
sampleRateSample rate in Hz.
freqCentre frequency in Hz.
QQuality factor (default: Butterworth).

Definition at line 500 of file Biquad.h.

◆ makeBandPass()

static BiquadCoeffs dspark::BiquadCoeffs::makeBandPass ( double  sampleRate,
double  freq,
double  Q = 0.7071067811865476 
)
inlinestaticnoexcept

Band-pass filter (constant 0 dB peak gain).

Implements the Audio EQ Cookbook BPF variant with b0 = alpha, whose peak gain at the centre frequency is exactly 0 dB regardless of Q, the most useful variant for mixing and crossover work.

Parameters
sampleRateSample rate in Hz.
freqCentre frequency in Hz.
QQuality factor (default: Butterworth).

Definition at line 172 of file Biquad.h.

◆ makeBandPassMatched()

static BiquadCoeffs dspark::BiquadCoeffs::makeBandPassMatched ( double  sampleRate,
double  freq,
double  Q = 0.7071067811865476 
)
inlinestaticnoexcept

Analog-matched band-pass, 0 dB at the centre (Vicanek 2016).

Impulse-invariant poles of H(s) = (s/Q) / (s^2 + s/Q + 1); a zero at DC and a numerator matching unity gain and a response extremum at the centre. Worst deviation over 20 Hz - 20 kHz at 48 kHz, centre 5 kHz, Q 0.707: 0.92 dB (bilinear 8.6 dB). Opt-in.

Definition at line 370 of file Biquad.h.

◆ makeFirstOrderHighPass()

static BiquadCoeffs dspark::BiquadCoeffs::makeFirstOrderHighPass ( double  sampleRate,
double  frequency 
)
inlinestaticnoexcept

First-order (6 dB/oct) high-pass filter.

Uses bilinear-transformed CR filter. Coefficients are already normalised (a0 = 1 implicit), so no normalise() call is needed.

Parameters
sampleRateSample rate in Hz.
frequencyCut-off frequency in Hz.

Definition at line 552 of file Biquad.h.

◆ makeFirstOrderLowPass()

static BiquadCoeffs dspark::BiquadCoeffs::makeFirstOrderLowPass ( double  sampleRate,
double  frequency 
)
inlinestaticnoexcept

First-order (6 dB/oct) low-pass filter.

Uses bilinear-transformed RC filter. Coefficients are already normalised (a0 = 1 implicit), so no normalise() call is needed.

Parameters
sampleRateSample rate in Hz.
frequencyCut-off frequency in Hz.

Definition at line 529 of file Biquad.h.

◆ makeHighPass()

static BiquadCoeffs dspark::BiquadCoeffs::makeHighPass ( double  sampleRate,
double  freq,
double  Q = 0.7071067811865476 
)
inlinestaticnoexcept

High-pass filter.

Parameters
sampleRateSample rate in Hz.
freqCentre frequency in Hz.
QQuality factor (default: Butterworth).

Definition at line 143 of file Biquad.h.

◆ makeHighPassMatched()

static BiquadCoeffs dspark::BiquadCoeffs::makeHighPassMatched ( double  sampleRate,
double  freq,
double  Q = 0.7071067811865476 
)
inlinestaticnoexcept

Analog-matched second-order high-pass (Vicanek 2016).

Impulse-invariant poles of H(s) = s^2 / (s^2 + s/Q + 1); a double zero at DC with the gain set to the prototype's Q at the cutoff. Worst deviation over 20 Hz - 20 kHz at 48 kHz, cutoff 18 kHz, Q 0.707: 0.39 dB (bilinear 12.5 dB). Opt-in.

Definition at line 347 of file Biquad.h.

◆ makeHighShelf()

static BiquadCoeffs dspark::BiquadCoeffs::makeHighShelf ( double  sampleRate,
double  freq,
double  gainDb,
double  slope = 1.0 
)
inlinestaticnoexcept

High-shelf filter.

Parameters
sampleRateSample rate in Hz.
freqTransition frequency in Hz.
gainDbShelf gain in decibels.
slopeShelf slope (default: 1.0).

Definition at line 447 of file Biquad.h.

◆ makeHighShelfMatched()

static BiquadCoeffs dspark::BiquadCoeffs::makeHighShelfMatched ( double  sampleRate,
double  freq,
double  gainDb 
)
inlinestaticnoexcept

Analog-matched Butterworth high shelf (see makeLowShelfMatched()).

Definition at line 405 of file Biquad.h.

◆ makeKWeightingHighPass()

static BiquadCoeffs dspark::BiquadCoeffs::makeKWeightingHighPass ( double  sampleRate)
inlinestaticnoexcept

ITU-R BS.1770 K-weighting, stage 2: the RLB high-pass.

The official table 2 numerator is exactly [1, -2, 1], NOT normalized to unity passband gain (it passes about +0.04 dB); the -0.691 dB constant of the LUFS formula is tied to this exact cascade.

Definition at line 632 of file Biquad.h.

◆ makeKWeightingShelf()

static BiquadCoeffs dspark::BiquadCoeffs::makeKWeightingShelf ( double  sampleRate)
inlinestaticnoexcept

ITU-R BS.1770 K-weighting, stage 1: the head-related high shelf.

Pre-warped analog parameterization that reproduces the official table 1 coefficients at 48 kHz to machine precision (max error 8.9e-16) and stays within the standard's tolerance at every other rate. Cascade with makeKWeightingHighPass() for the full K filter (LoudnessMeter's).

Definition at line 607 of file Biquad.h.

◆ makeLowPass()

static BiquadCoeffs dspark::BiquadCoeffs::makeLowPass ( double  sampleRate,
double  freq,
double  Q = 0.7071067811865476 
)
inlinestaticnoexcept

Low-pass filter.

Parameters
sampleRateSample rate in Hz.
freqCentre frequency in Hz.
QQuality factor (default: 1/sqrt(2) = Butterworth).

Definition at line 119 of file Biquad.h.

◆ makeLowPassMatched()

static BiquadCoeffs dspark::BiquadCoeffs::makeLowPassMatched ( double  sampleRate,
double  freq,
double  Q = 0.7071067811865476 
)
inlinestaticnoexcept

Analog-matched second-order low-pass (Vicanek, "Matched Second Order Digital Filters", 2016).

Impulse-invariant poles of H(s) = 1 / (s^2 + s/Q + 1) and a numerator solved for unity gain at DC and the prototype's gain Q at the cutoff. Unlike makeLowPass() it does not pin a zero at Nyquist, so a high cutoff keeps its analog shape. Worst deviation from the prototype over 20 Hz - 20 kHz at 48 kHz, cutoff 15 kHz, Q 0.707: 0.58 dB (bilinear 9.4 dB). Opt-in: the response is not a bilinear design, so it differs from makeLowPass() near Nyquist by design.

Definition at line 321 of file Biquad.h.

◆ makeLowShelf()

static BiquadCoeffs dspark::BiquadCoeffs::makeLowShelf ( double  sampleRate,
double  freq,
double  gainDb,
double  slope = 1.0 
)
inlinestaticnoexcept

Low-shelf filter.

Parameters
sampleRateSample rate in Hz.
freqTransition frequency in Hz.
gainDbShelf gain in decibels.
slopeShelf slope (default: 1.0 for standard 6 dB/oct transition).

Definition at line 417 of file Biquad.h.

◆ makeLowShelfMatched()

static BiquadCoeffs dspark::BiquadCoeffs::makeLowShelfMatched ( double  sampleRate,
double  freq,
double  gainDb 
)
inlinestaticnoexcept

Analog-matched Butterworth low shelf (Vicanek, "Matched Two-Pole Digital Shelving Filters", 2024).

The prototype is the cookbook shelf with slope S = 1 (Q = 1/sqrt(2)), half the gain at freq. The design matches DC, Nyquist and two further points of the analog magnitude. Worst deviation over 20 Hz - 20 kHz at 44.1/48 kHz, centres up to 18 kHz, gains -12..+12 dB: 0.40 dB (bilinear 3.9 dB). Falls back to identity for |gain| < 0.01 dB. Opt-in.

Definition at line 399 of file Biquad.h.

◆ makeNotch()

static BiquadCoeffs dspark::BiquadCoeffs::makeNotch ( double  sampleRate,
double  freq,
double  Q = 0.7071067811865476 
)
inlinestaticnoexcept

Notch (band-reject) filter.

Parameters
sampleRateSample rate in Hz.
freqCentre frequency in Hz.
QQuality factor (default: Butterworth).

Definition at line 476 of file Biquad.h.

◆ makePeak()

static BiquadCoeffs dspark::BiquadCoeffs::makePeak ( double  sampleRate,
double  freq,
double  Q,
double  gainDb 
)
inlinestaticnoexcept

Peak (parametric EQ) filter.

Uses the correct Audio EQ Cookbook formula where A = 10^(dBgain/40), giving the expected gain in dB at the centre frequency.

Parameters
sampleRateSample rate in Hz.
freqCentre frequency in Hz.
QQuality factor.
gainDbGain in decibels (positive = boost, negative = cut).

Definition at line 201 of file Biquad.h.

◆ makePeakMatched()

static BiquadCoeffs dspark::BiquadCoeffs::makePeakMatched ( double  sampleRate,
double  freq,
double  Q,
double  gainDb 
)
inlinestaticnoexcept

Analog-matched ("de-cramped") peaking filter (Vicanek design).

The bilinear (cookbook) peaking filter cramps near Nyquist: high- frequency bells get narrower and their response is pinned to 0 dB at fs/2, deviating from the analog prototype above ~fs/6 (up to 11 dB in the audible band at 48 kHz). This design follows M. Vicanek, "Matched Second Order Digital Filters" (2016): the poles are the analog prototype's poles mapped by impulse invariance (z = e^(sT), so the bell keeps its analog width at any frequency), and the minimum-phase numerator is solved in closed form for unity gain at DC, the exact peak gain at the centre frequency, and a response extremum there.

The prototype is the cookbook bell, H(s) = (s^2 + s*w0*sqrt(G)/Q + w0^2) / (s^2 + s*w0/(sqrt(G)*Q) + w0^2), so Q means the same as in makePeak() and both designs converge at low frequencies. Measured against that prototype over 20 Hz - 20 kHz (gains +/-3..24 dB, Q 0.5..4, centres up to 20 kHz): worst deviation 2.9 dB and mean 0.14 dB at 48 kHz (cookbook: 11 dB worst). Every finite parameter set yields a stable, minimum-phase filter, including centres close to Nyquist.

Falls back to identity for |gain| < 0.01 dB.

Parameters
sampleRateSample rate in Hz.
freqCenter frequency in Hz.
QQuality factor (bandwidth = freq / Q at the half-dB gain).
gainDbPeak gain in decibels.

Definition at line 248 of file Biquad.h.

◆ makeTilt()

static BiquadCoeffs dspark::BiquadCoeffs::makeTilt ( double  sampleRate,
double  pivotFreq,
double  gainDb 
)
inlinestaticnoexcept

Creates a first-order tilt filter.

Tilts the spectrum around a pivot frequency: -gainDb/2 at DC, unity at the pivot, +gainDb/2 at Nyquist (total span = gainDb). A single-knob tonal balance control found in mastering EQs and channel strips (SSL, Neve, Tonelux Tilt).

Parameters
sampleRateSample rate in Hz.
pivotFreqPivot frequency in Hz (typically 600-3000 Hz).
gainDbTilt amount in dB (positive = bright, negative = dark).

Definition at line 578 of file Biquad.h.

Member Data Documentation

◆ a1

double dspark::BiquadCoeffs::a1 = 0.0

Definition at line 109 of file Biquad.h.

◆ a2

double dspark::BiquadCoeffs::a2 = 0.0

Definition at line 109 of file Biquad.h.

◆ b0

double dspark::BiquadCoeffs::b0 = 1.0

Definition at line 108 of file Biquad.h.

◆ b1

double dspark::BiquadCoeffs::b1 = 0.0

Definition at line 108 of file Biquad.h.

◆ b2

double dspark::BiquadCoeffs::b2 = 0.0

Definition at line 108 of file Biquad.h.


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