Lightweight and fast second-order IIR filter (biquad) in TDF-II form.
This is not a regular DSP module, as it exposes state and coefficients, and it's not appropriate for time-varying operation. If you need that, check out bw_ap2, bw_hs2, bw_ls2, bw_mm2, bw_notch, bw_peak, and bw_svf.
Version: 1.0.0
License:
Included in Brickworks, which is:
Here you can download one or more example VST3 plugins for Windows, macOS and Linux. Source code of the audio engine(s) is included in the archive(s).
Description | Link |
---|---|
Second-order recursive filter | Download |
Module type: Utility
static inline void bw_iir2_reset(
float x_0,
float * BW_RESTRICT y_0,
float * BW_RESTRICT s1_0,
float * BW_RESTRICT s2_0,
float b0,
float b1,
float b2,
float a1,
float a2);
Computes and puts the initial output in y_0
and the initial states in s1_0
and s2_0
, given the initial input x_0
and coefficients b0
, b1
, b2
, a1
, and a2
.
The given coefficients must describe a stable filter.
static inline void bw_iir2_reset_multi(
const float * x_0,
float * y_0,
float * BW_RESTRICT s1_0,
float * s2_0,
float b0,
float b1,
float b2,
float a1,
float a2,
size_t n_channels);
Computes and puts each of the n_channels
initial outputs in y_0
and initial states in s1_0
and s2_0
, given the corresponding initial inputs x_0
and coefficients b0
, b1
, b2
, a1
, and a2
.
y_0
and/or s_0
may be BW_NULL
, in which case the corresponding values are not written anywhere.
The given coefficients must describe a stable filter.
static inline void bw_iir2_process1(
float x,
float * BW_RESTRICT y,
float * BW_RESTRICT s1,
float * BW_RESTRICT s2,
float b0,
float b1,
float b2,
float a1,
float a2);
Processes one input sample x
using coefficients b0
, b1
, b2
, a1
, and a2
. The output sample and next states value are put in y
and s1
/s2
respectively.
The given coefficients must describe a stable filter.
static inline void bw_iir2_process(
const float * x,
float * y,
float * BW_RESTRICT s1,
float * BW_RESTRICT s2,
float b0,
float b1,
float b2,
float a1,
float a2,
size_t n_samples);
Processes the first n_samples
of the input buffer x
and fills the first n_samples
of the output buffer y
, while using coefficients b0
, b1
, b2
, a1
, and a2
. The next state values are put in s1
and s2
.
The given coefficients must describe a stable filter.
static inline void bw_iir2_process_multi(
const float * const * x,
float * const * y,
float * BW_RESTRICT s1,
float * BW_RESTRICT s2,
float b0,
float b1,
float b2,
float a1,
float a2,
size_t n_channels,
size_t n_samples);
Processes the first n_samples
of the n_channels
input buffers x
and fills the first n_samples
of the n_channels
output buffers y
, while using coefficients b0
, b1
, b2
, a1
, and a2
. The next n_channels
state values are put in s1
and s2
.
The given coefficients must describe a stable filter.
static inline void bw_iir2_coeffs_ap2(
float sample_rate,
float cutoff,
float Q,
char prewarp_at_cutoff,
float prewarp_freq,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, b2
, a1
, and a2
resulting in a second-order allpass filter (180° shift at cutoff, approaching 360° shift at high frequencies) with unitary gain, using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be positive), the cutoff
frequency (Hz, in [1e-6f
, 1e12f
]), and the quality factor Q
(in [1e-6f
, 1e6f
]). If prewarp_freq
is 0
, then the prewarping frequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
static inline void bw_iir2_coeffs_bp2(
float sample_rate,
float cutoff,
float Q,
char prewarp_at_cutoff,
float prewarp_freq,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, b2
, a1
, and a2
resulting in a second-order bandpass filter (6 dB/oct) with peak gain Q
(linear gain), using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be finite and positive), the cutoff
frequency (Hz, in [1e-6f
, 1e12f
]), and the quality factor Q
(in [1e-6f
, 1e6f
]). If prewarp_freq
is 0
, then the prewarping frequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
static inline void bw_iir2_coeffs_hp2(
float sample_rate,
float cutoff,
float Q,
char prewarp_at_cutoff,
float prewarp_freq,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, b2
, a1
, and a2
resulting in a second-order highpass filter (12 dB/oct) with gain asymptotically approaching unity as frequency increases, using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be finite and positive), the cutoff
frequency (Hz, in [1e-6f
, 1e12f
]), and the quality factor Q
(in [1e-6f
, 1e6f
]). If prewarp_freq
is 0
, then the prewarping frequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
static inline void bw_iir2_coeffs_hs2(
float sample_rate,
float cutoff,
float Q,
char prewarp_at_cutoff,
float prewarp_freq,
char high_gain_dB,
float high_gain,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, b2
, a1,
and a2
resulting in a second-order high shelf filter (12 dB/oct) with unitary DC gain, using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be finite and positive), the cutoff
frequency (Hz, must be finite and positive), the quality factor Q
(in [1e-6f
, 1e6f
]), and the high-frequency gain high_gain
, either as linear gain (in [1e-30f
, 1e30f
]) if high_gain_dB
is 0
, or otherwise in dB (in [-600.f
, 600.f
]). If prewarp_freq
is 0
, then the prewarpingfrequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
cutoff * bw_sqrtf(bw_sqrtf(high_gain))
must be in [1e-6f
, 1e12f
], where high_gain
is expressed as linear gain.
static inline void bw_iir2_coeffs_lp2(
float sample_rate,
float cutoff,
float Q,
char prewarp_at_cutoff,
float prewarp_freq,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, b2
, a1
, and a2
resulting in a second-order lowpass filter (12 dB/oct) with unitary DC gain, using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be finite and positive), the cutoff
frequency (Hz, in [1e-6f
, 1e12f
]), and the quality factor Q
(in [1e-6f
, 1e6f
]). If prewarp_freq
is 0
, then the prewarping frequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
static inline void bw_iir2_coeffs_ls2(
float sample_rate,
float cutoff,
float Q,
char prewarp_at_cutoff,
float prewarp_freq,
char dc_gain_dB,
float dc_gain,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, b2
, a1,
and a2
resulting in a second-order low shelf filter (12 dB/oct) with gain asymptotically approaching unity as frequency increases, using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be finite and positive), the cutoff
frequency (Hz, must be finite and positive), the quality factor Q
(in [1e-6f
, 1e6f
]), and the dc_gain
, either as linear gain (in [1e-30f
, 1e30f
]) if high_gain_dB
is 0
, or otherwise in dB (in [-600.f
, 600.f
]). If prewarp_freq
is 0
, then the prewarping frequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
cutoff * bw_rpcf(bw_sqrtf(bw_sqrtf(dc_gain)))
must be in [1e-6f
, 1e12f
], where dc_gain
is expressed as linear gain.
static inline void bw_iir2_coeffs_mm2(
float sample_rate,
float cutoff,
float Q,
char prewarp_at_cutoff,
float prewarp_freq,
float coeff_x,
float coeff_lp,
float coeff_bp,
float coeff_hp,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, b2
, a1
, and a2
resulting in a second-order filter implementing an approximation of the Laplace-domain transfer function
H(s) = coeff_x + (coeff_hp s^2 + 2 pi fc s coeff_bp + (2 pi fc)^2 coeff_lp) / (s^2 + 2 pi fc / Q s + (2 pi fc)^2)
where fc is the cutoff frequency and Q is the quality factor, using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be finite and positive), the cutoff
frequency (Hz, in [1e-6f
, 1e12f
]), the quality factor Q
(in [1e-6f
, 1e6f
]), and output coefficients coeff_x
, coeff_lp
, coeff_bp
, coeff_hp
(all must be finite). If prewarp_freq
is 0
, then the prewarping frequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
static inline void bw_iir2_coeffs_notch(
float sample_rate,
float cutoff,
float Q,
char prewarp_at_cutoff,
float prewarp_freq,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, b2
, a1
, and a2
resulting in a second-order notch filter with unitary gain at DC and asymptotically as frequency increases, and null gain at cutoff frequency, using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be finite and positive), the cutoff
frequency (Hz, in [1e-6f
, 1e12f
]), and the quality factor Q
(in [1e-6f
, 1e6f
]). If prewarp_freq
is 0
, then the prewarping frequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
static inline void bw_iir2_coeffs_peak(
float sample_rate,
float cutoff,
char use_bandwidth,
float Q_bandwidth,
char prewarp_at_cutoff,
float prewarp_freq,
char peak_gain_dB,
float peak_gain,
float * BW_RESTRICT b0,
float * BW_RESTRICT b1,
float * BW_RESTRICT b2,
float * BW_RESTRICT a1,
float * BW_RESTRICT a2);
Computes and puts coefficient values in b0
, b1
, and a1
resulting in a second-order peak filter with unitary gain at DC and asymptotically as frequency increases, using the bilinear transform with prewarping.
It takes the sample_rate
(Hz, must be finite and positive), the cutoff
frequency (Hz, in [1e-6f
, 1e12f
]), and the peak_gain
, either as linear gain (in [1e-30f
, 1e30f
]) if peak_gain_dB
is 0
, or otherwise in dB (in [-600.f
, 600.f
]). Q_bandwidth
indicates either the quality factor (in [1e-6f
, 1e6f
]) if use_bandwidth
is 0
, or otherwise the bandwidth (octaves, in [1e-6f
, 90.f
]), designating the distance between midpoint gain frequencies (i.e., frequencies with gain = peak gain / 2 in dB terms). If prewarp_freq
is 0
, then the prewarping frequency matches cutoff
, otherwise the value specified by prewarp_freq
(Hz, in [1e-6f
, 1e12f
], however interally limited to avoid instability) is used.
If use_bandwidth
is non-0
, then bw_sqrtf(bw_pow2f(Q_bandwidth) * peak_gain) * bw_rcpf(bw_pow2f(Q_bandwidth) - 1.f)
must be in [1e-6f
, 1e6f
], where peak_gain
is expressed as linear gain.
template<size_t N_CHANNELS>
void iir2Reset(
const float * x0,
float * y0,
float * BW_RESTRICT s10,
float * BW_RESTRICT s20,
float b0,
float b1,
float b2,
float a1,
float a2);
# ifndef BW_CXX_NO_ARRAY
template<size_t N_CHANNELS>
void iir2Reset(
std::array<float, N_CHANNELS> x0,
std::array<float, N_CHANNELS> * BW_RESTRICT y0,
std::array<float, N_CHANNELS> * BW_RESTRICT s10,
std::array<float, N_CHANNELS> * BW_RESTRICT s20,
float b0,
float b1,
float b2,
float a1,
float a2);
# endif
template<size_t N_CHANNELS>
void iir2Process(
const float * const * x,
float * const * y,
float * BW_RESTRICT s1,
float * BW_RESTRICT s2,
float b0,
float b1,
float b2,
float a1,
float a2,
size_t nSamples);
# ifndef BW_CXX_NO_ARRAY
template<size_t N_CHANNELS>
void iir2Process(
std::array<const float *, N_CHANNELS> x,
std::array<float *, N_CHANNELS> y,
std::array<float, N_CHANNELS> * BW_RESTRICT s1,
std::array<float, N_CHANNELS> * BW_RESTRICT s2,
float b0,
float b1,
float b2,
float a1,
float a2,
size_t nSamples);
# endif