Biquad Filters#
Single Biquad#
A second order biquadratic filter, which can be used to implement many common second order filters. The filter had been implemented in the direct form 1, and uses the xcore.ai vector unit to calculate the 5 filter taps in a single instruction.
Coefficients are stored in Q1.30 format to benefit from the vector unit, allowing for a filter
coefficient range of [-2, 1.999]
. For some high gain biquads (e.g. high shelf filters), the
numerator coefficients may exceed this range. If this is the case, the numerator coefficients only
should be right-shifted until they fit within the range (the denominator coefficients cannot become
larger than 2.0 without the poles exceeding the unit circle). The shift should be passed into the API,
and the output signal from the biquad will then have a left-shift applied. This is equivalent to
reducing the overall signal level in the biquad, then returning to unity gain afterwards.
The state
should be initialised to 0
. The state
and coeffs
must be word-aligned.
-
int32_t adsp_biquad(int32_t new_sample, q2_30 coeffs[5], int32_t state[8], left_shift_t lsh)#
Biquad filter. This function implements a biquad filter. The filter is implemented as a direct form 1.
- Parameters:
new_sample – New sample to be filtered
coeffs – Filter coefficients
state – Filter state
lsh – Left shift compensation value
- Returns:
int32_t Filtered sample
- class audio_dsp.dsp.biquad.biquad(coeffs: list[float], fs: int, n_chans: int = 1, b_shift: int = 0, Q_sig: int = 27)
A second order biquadratic filter instance.
This implements a direct form 1 biquad filter, using the coefficients provided at initialisation: a0*y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] - a2*y[n-2]
For efficiency the biquad coefficients are normalised by a0 and the output a coefficients multiplied by -1.
- Parameters:
- coeffslist[float]
List of normalised biquad coefficients in the form in the form [b0, b1, b2, -a1, -a2]/a0
- fsint
Sampling frequency in Hz.
- n_chansint
Number of channels the block runs on.
- b_shiftint
The number of right shift bits applied to the b coefficients. The default coefficient scaling allows for a maximum coefficient value of 2, but high gain shelf and peaking filters can have coefficients above this value. Shifting the b coefficients down allows coefficients greater than 2, with the cost of b_shift bits of precision.
- Q_sig: int, optional
Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.
- Attributes:
- fsint
Sampling frequency in Hz.
- n_chansint
Number of channels the block runs on.
- Q_sig: int
Q format of the signal, number of bits after the decimal point.
- coeffslist[float]
List of normalised float biquad coefficients in the form in the form [b0, b1, b2, -a1, -a2]/a0, rounded to int32 precision.
- int_coeffslist[int]
List of normalised int biquad coefficients in the form in the form [b0, b1, b2, -a1, -a2]/a0, scaled and rounded to int32.
- process(sample: float, channel: int = 0) float
Filter a single sample using direct form 1 biquad using floating point maths.
- Parameters:
- samplefloat
The input sample to be processed.
- channelint, optional
The channel index to process the sample on. Default is 0.
- Returns:
- float
The processed sample.
- update_coeffs(new_coeffs: list[float])
Update the saved coefficients to the input values.
- Parameters:
- new_coeffslist[float]
The new coefficients to be updated.
- reset_state()
Reset the biquad saved states to zero.
Cascaded Biquads#
The cascaded biquad module is equivalent to 8 individual biquad filters connected in series. It can be used to implement a simple parametric equaliser or high-order Butterworth filters, implemented as cascaded second order sections.
-
int32_t adsp_cascaded_biquads_8b(int32_t new_sample, q2_30 coeffs[40], int32_t state[64], left_shift_t lsh[8])#
8-band cascaded biquad filter This function implements an 8-band cascaded biquad filter. The filter is implemented as a direct form 1 filter.
Note
The filter coefficients must be in [5][8]
- Parameters:
new_sample – New sample to be filtered
coeffs – Filter coefficients
state – Filter state
lsh – Left shift compensation value
- Returns:
int32_t Filtered sample
- class audio_dsp.dsp.cascaded_biquads.cascaded_biquads_8(coeffs_list, fs, n_chans, Q_sig=27)
A class representing a cascaded biquad filter with up to 8 biquads.
This can be used to either implement a parametric equaliser or a higher order filter built out of second order sections.
8 biquad objects are always created, if there are less than 8 biquads in the cascade, the remaining biquads are set to bypass (b0 = 1).
For documentation on individual biquads, see
audio_dsp.dsp.biquad.biquad
.- Parameters:
- coeffs_listlist
List of coefficients for each biquad in the cascade.
- fsint
Sampling frequency in Hz.
- n_chansint
Number of channels the block runs on.
- Q_sig: int, optional
Q format of the signal, number of bits after the decimal point. Defaults to Q4.27.
- Attributes:
- fsint
Sampling frequency in Hz.
- n_chansint
Number of channels the block runs on.
- Q_sig: int
Q format of the signal, number of bits after the decimal point.
- biquadslist
List of biquad objects representing each biquad in the cascade.
- process(sample, channel=0)
Process the input sample through the cascaded biquads using floating point maths.
- Parameters:
- samplefloat
The input sample to be processed.
- channelint
The channel index to process the sample on.
- Returns:
- float
The processed output sample.
- reset_state()
Reset the biquad saved states to zero.