miniDSP
A small C library for audio DSP
Loading...
Searching...
No Matches
biquad.h File Reference

Biquad (second-order IIR) filter interface. More...

#include <math.h>
#include <stdlib.h>
Include dependency graph for biquad.h:
This graph shows which files directly or indirectly include this file:

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.
 
biquadBiQuad_new (int type, smp_type dbGain, smp_type freq, smp_type srate, smp_type bandwidth)
 Create and initialise a new biquad filter.
 

Detailed Description

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.

Macro Definition Documentation

◆ M_LN2

#define M_LN2   0.69314718055994530942

Definition at line 27 of file biquad.h.

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 31 of file biquad.h.

Typedef Documentation

◆ smp_type

typedef double smp_type

Definition at line 36 of file biquad.h.

Enumeration Type Documentation

◆ FILT_TYPE

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).

Definition at line 87 of file biquad.h.

Function Documentation

◆ BiQuad()

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.

Definition at line 49 of file biquad.c.

◆ BiQuad_new()

biquad * BiQuad_new ( int  type,
smp_type  dbGain,
smp_type  freq,
smp_type  srate,
smp_type  bandwidth 
)

Create and initialise a new biquad filter.

Parameters
typeOne of the FILT_TYPE values (LPF, HPF, etc.).
dbGainGain in dB (only used for PEQ, LSH, HSH).
freqCentre / corner frequency in Hz.
srateSampling rate in Hz (e.g. 44100, 48000).
bandwidthBandwidth in octaves (controls the filter's "width").
Returns
A heap-allocated biquad, or nullptr on failure. The caller must free() it when done.

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.

Parameters
typeFilter type (LPF, HPF, BPF, NOTCH, PEQ, LSH, HSH).
dbGainGain in dB. Only matters for PEQ, LSH, and HSH.
freqCentre/corner frequency in Hz.
srateSampling rate in Hz.
bandwidthBandwidth in octaves.
Returns
Pointer to a new biquad struct, or nullptr on error.

Note: the caller is responsible for calling free() on the returned pointer when the filter is no longer needed.

Definition at line 88 of file biquad.c.