The cBiQuad class is a biquad digital filter implementation used for real-time audio processing. It implements the coefficients formulas by Robert Bristow-Johnson (Cookbook formulae), allowing precise control over frequency responses. The class supports:
10 filter types : Low Pass Filter (LPF), High Pass Filter (HPF), Band Pass Filter (BPF), Notch Filter, Parametric Equalizer (PEQ), and low/high Shelf filters (LSH/HSH) with 24dB variants
Stereo Processing : Native management of left/right channels with separate filter states
24dB Option : Cascade of two biquad stages for steep slope filters
Normalized Calculations : Precalculated coefficients for optimized processing
Tip To help configure and visualize filters, you can use the Python utility:
cd "....../DAD_FORGE/@Python Utilities/BiQuad Visu/"
uv run biquad.py
This tool displays the filter frequency response according to the selected parameters. —
Initializes the filter with configuration parameters: sampling frequency, cutoff frequency, gain in decibels, bandwidth, and filter type. Automatically calculates filter coefficients.
Parameter(s)
Â
sampleRate
Sampling rate in Hz (e.g., 44100.0f for CD audio)
cutoffFreq
Cutoff frequency in Hz where the response reaches -3dB
gainDb
Gain applied to the filter in decibels (positive = boost, negative = cut)
bandwidth
Filter bandwidth (wider = faster transition)
type
Type of filter to use (LPF, HPF, BPF, Notch, PEQ, LSH, HSH, AFP, LPF24, HPF24)
CalculateParameters
Element
Details
Method
void CalculateParameters()
Description
Recalculates the filter coefficients based on current parameters (cutoff frequency, gain, bandwidth, and type). Uses Robert Bristow-Johnson’s Cookbook formulas. Coefficients are normalized for optimized processing. Executes a memory barrier (DMB) to ensure data consistency in multi-threaded environments.
GainDb
Element
Details
Method
float GainDb(float freq)
Description
Calculates the filter gain in decibels at a given frequency. Allows for analyzing the frequency response without sample processing.
Parameter(s)
Â
freq
Analysis frequency in Hz (0 to sampleRate/2)
Return
Filter gain at the specified frequency, expressed in decibels (dB)
Fast stereo processing for 12dB/octave filters. Applies the filter to the left and right channels separately without additional stage cascading. Optimized for real-time processing with minimal CPU overhead.
Parameter(s)
Â
pIn
Pointer to the input audio buffer containing unfiltered samples
pOut
Pointer to the output audio buffer where filtered samples will be stored
Processes a single audio sample through the filter. For 24dB filters (LPF24, HPF24), it cascades two biquad stages: the first stage for the steep transition, and the second stage as the main stage. Supports mono or stereo processing via the Channel parameter.
Parameter(s)
Â
sample
Input audio sample (normalized amplitude)
Channel
Processing channel: Left for left channel, Right for right channel (default: Left)
Return
Filtered output audio sample
setSampleRate
Element
Details
Method
inline void setSampleRate(float sampleRate)
Description
Sets the sampling frequency of the audio system. Updates the inverse sampling rate for frequency calculations.
Parameter(s)
Â
sampleRate
Sampling frequency in Hz
setCutoffFreq
Element
Details
Method
inline void setCutoffFreq(float cutoffFreq)
Description
Sets the filter’s cutoff frequency. The frequency at which the response reaches -3dB relative to the nominal gain.
Parameter(s)
Â
cutoffFreq
Cutoff frequency in Hz
setGainDb
Element
Details
Method
inline void setGainDb(float gainDb)
Description
Sets the filter’s gain in decibels. Positive values increase amplitude (boost), negative values reduce amplitude (cut).
Parameter(s)
Â
gainDb
Gain in decibels (e.g., +6.0f for 6dB boost, -12.0f for 12dB cut)
setBandwidth
Element
Details
Method
inline void setBandwidth(float bandwidth)
Description
Sets the filter’s bandwidth. Controls the transition slope around the cutoff frequency. Wider = sharper transition.
Parameter(s)
Â
bandwidth
Normalized bandwidth (typically 0.01 to 1.0)
setType
Element
Details
Method
inline void setType(FilterType type)
Description
Sets the filter type to be used. Determines the coefficient formulas applied during parameter calculation.
Direct mono sample processing using the biquad difference equation: y[n] = b0x[n] + b1x[n-1] + b2x[n-2] - a1y[n-1] - a2*y[n-2]. Automatically updates the delay state for the next iteration.
Parameter(s)
Â
sample
Input audio sample (normalized amplitude)
FilterState
Reference to the filter state containing lag values x1, x2, y1, y2
Return
Filtered output audio sample
🔒 Protected / Private Methods
No protected or private methods.
📦 Data Members (Variables)
Public Variables
No public variables.
Protected / Private Variables
Member
Type
Description
m_InvSampleRate
float
Inverse sampling rate (1/sampleRate) in seconds
m_cutoffFreq
float
Cutoff frequency in Hz
m_gainDb
float
Filter gain in decibels
m_bandwidth
float
Normalized bandwidth parameter
m_type
FilterType
Configured filter type
m_a0
volatile float
Normalized numerator coefficient b0
m_a1
volatile float
Normalized numerator coefficient b1
m_a2
volatile float
Normalized numerator coefficient b2
m_a3
volatile float
Normalized denominator coefficient a1
m_a4
volatile float
Normalized denominator coefficient a2
m_FilterState[0]
sFilterState
Filter state: Stage 1 left channel
m_FilterState[1]
sFilterState
Filter state: Stage 2 left channel (main)
m_FilterState[2]
sFilterState
Filter state: Stage 1 right channel
m_FilterState[3]
sFilterState
Filter state: Stage 2 right channel (main)
💡 Usage Example
// Insert code example here#include"DadDSP.h"usingnamespaceDadDSP;intmain(){// Configuration audioconstfloatSAMPLE_RATE=44100.0f;// Creation and initialization of a Parametric EQ bandpass filtercBiQuadfilter;filter.Initialize(SAMPLE_RATE,1000.0f,3.0f,1.0f,FilterType::PEQ);// Dynamic configuration - real-time adjustmentfilter.setCutoffFreq(500.0f);// Change cutoff frequencyfilter.setGainDb(-6.0f);// 6dB cutfilter.setBandwidth(0.5f);// Moderate bandwidthfilter.CalculateParameters();// Recalculates the filter coefficients based on current parameters// Frequency response analysisfloatgainAt100Hz=filter.GainDb(100.0f);floatgainAt1000Hz=filter.GainDb(1000.0f);// Mono sample processingsFilterStatestate;state.Reset();for(inti=0;i<numSamples;i++){floatinputSample=audioInput[i];floatoutputSample=filter.Process(inputSample,state);audioOutput[i]=outputSample;}// Fast stereo processing (12dB/octave)cBiQuadstereoFilter;stereoFilter.Initialize(SAMPLE_RATE,2000.0f,0.0f,0.1f,FilterType::LPF);AudioBuffer*pIn=&inputBuffer;AudioBuffer*pOut=&outputBuffer;stereoFilter.ProcessFlast12dbStereo(pIn,pOut);// Full stereo processing (24dB/octave)cBiQuadhighPassFilter;highPassFilter.Initialize(SAMPLE_RATE,30.0f,0.0f,1.0f,FilterType::HPF24);for(inti=0;i<numSamples;i++){floatleftInput=pIn->Left[i];floatrightInput=pIn->Right[i];floatleftOutput=highPassFilter.Process(leftInput,eChannel::Left);floatrightOutput=highPassFilter.Process(rightInput,eChannel::Right);pOut->Left[i]=leftOutput;pOut->Right[i]=rightOutput;}return0;}
DAD_FORGE/DSP - Dad Design DSP Library Copyright (c) 2024-2026 Dad Design.