|
miniDSP
A small C library for audio DSP
|
Biquad (second-order IIR) filter interface. More...
#include <math.h>#include <stdlib.h>

Go to the source code of this file.
Data Structures | |
| struct | biquad |
| State and coefficients for a single biquad filter section. More... | |
Macros | |
| #define | M_LN2 0.69314718055994530942 |
| #define | M_PI 3.14159265358979323846 |
Typedefs | |
| typedef double | smp_type |
Enumerations | |
| enum | FILT_TYPE { LPF , HPF , BPF , NOTCH , PEQ , LSH , HSH } |
| Filter types. More... | |
Functions | |
| smp_type | BiQuad (smp_type sample, biquad *b) |
| Process a single sample through the filter and return the result. | |
| biquad * | BiQuad_new (int type, smp_type dbGain, smp_type freq, smp_type srate, smp_type bandwidth) |
| Create and initialise a new biquad filter. | |
Biquad (second-order IIR) filter interface.
A biquad filter is the most common building block in audio equalizers. "Biquad" is short for "biquadratic" – it describes the ratio of two quadratic (degree-2) polynomials that defines the filter's transfer function in the z-domain:
H(z) = (b0 + b1*z^-1 + b2*z^-2) / (a0 + a1*z^-1 + a2*z^-2)
Every sample passes through five multiplications and four additions, making it very efficient. By choosing different coefficient values, the same structure can produce low-pass, high-pass, band-pass, notch, peaking EQ, and shelving filters.
Based on Robert Bristow-Johnson's "Audio EQ Cookbook": https://www.w3.org/2011/audio/audio-eq-cookbook.html
Definition in file biquad.h.
| enum FILT_TYPE |
Filter types.
Each type shapes the frequency response differently:
LPF – passes frequencies below the cutoff, attenuates above. HPF – passes frequencies above the cutoff, attenuates below. BPF – passes a band of frequencies, attenuates both sides. NOTCH – removes a narrow band of frequencies (inverse of BPF). PEQ – boosts or cuts a band (parametric EQ). LSH – boosts or cuts everything below a frequency (low shelf). HSH – boosts or cuts everything above a frequency (high shelf).
| smp_type BiQuad | ( | smp_type | sample, |
| biquad * | b | ||
| ) |
Process a single sample through the filter and return the result.
Call this once per sample in your audio loop.
Process a single sample through the filter and return the result.
This is the "hot loop" function – it gets called once for every single audio sample (e.g. 44,100 times per second at CD quality). That is why it must be very simple and fast.
The five multiply-accumulate operations implement: result = a0*x[n] + a1*x[n-1] + a2*x[n-2] - a3*y[n-1] - a4*y[n-2]
After computing the result, we shift the delay line: the current sample becomes x[n-1] for the next call, and x[n-1] becomes x[n-2]. Same for the output side.
| biquad * BiQuad_new | ( | int | type, |
| smp_type | dbGain, | ||
| smp_type | freq, | ||
| smp_type | srate, | ||
| smp_type | bandwidth | ||
| ) |
Create and initialise a new biquad filter.
| type | One of the FILT_TYPE values (LPF, HPF, etc.). |
| dbGain | Gain in dB (only used for PEQ, LSH, HSH). |
| freq | Centre / corner frequency in Hz. |
| srate | Sampling rate in Hz (e.g. 44100, 48000). |
| bandwidth | Bandwidth in octaves (controls the filter's "width"). |
Create and initialise a new biquad filter.
The function computes the six transfer-function coefficients (b0, b1, b2, a0, a1, a2) from the desired filter type and parameters, then normalises them by dividing through by a0.
| type | Filter type (LPF, HPF, BPF, NOTCH, PEQ, LSH, HSH). |
| dbGain | Gain in dB. Only matters for PEQ, LSH, and HSH. |
| freq | Centre/corner frequency in Hz. |
| srate | Sampling rate in Hz. |
| bandwidth | Bandwidth in octaves. |
Note: the caller is responsible for calling free() on the returned pointer when the filter is no longer needed.