|
miniDSP
A small C library for audio DSP
|
Biquad (second-order IIR) filter implementation. More...
#include "biquad.h"
Go to the source code of this file.
Functions | |
| smp_type | BiQuad (smp_type sample, biquad *b) |
| Process one input sample through the biquad filter. | |
| biquad * | BiQuad_new (int type, smp_type dbGain, smp_type freq, smp_type srate, smp_type bandwidth) |
| Create a new biquad filter with the specified characteristics. | |
Biquad (second-order IIR) filter implementation.
Original implementation by Tom St Denis, based on: "Cookbook formulae for audio EQ biquad filter coefficients" by Robert Bristow-Johnson (pbjrb.nosp@m.j@vi.nosp@m.conet.nosp@m..com)
Reference: https://www.w3.org/2011/audio/audio-eq-cookbook.html
This code is in the public domain. See the original header: http://www.musicdsp.org/files/biquad.c
How a biquad filter works (for students):
A biquad filter is defined by the "difference equation":
y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2]
where x[n] is the current input sample, y[n] is the current output, and the subscripted values are previous samples. The six coefficients (a0, a1, a2, b0, b1, b2) completely determine the filter's behaviour.
Different choices of coefficients produce different filter types (low-pass, high-pass, etc.). The BiQuad_new() function computes the right coefficients from user-friendly parameters like frequency, bandwidth, and gain.
Definition in file biquad.c.
| smp_type BiQuad | ( | smp_type | sample, |
| biquad * | b | ||
| ) |
Process one input sample through the biquad filter.
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 a new biquad filter with the specified characteristics.
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.