# Biquad filter from Matlab to C using “tf2sos”

This is a biquad implementation I use:

float my_biquad(float in, float *coeffs, int scalefactor, float *delay)
{
float out = coeffs * in + delay;
out = out * (float)pow( 2, scalefactor );
delay = delay + coeffs*in - coeffs*out;
delay = coeffs*in - coeffs*out;

// etc...

return out;
}


It cascades several biquads:

for(int j=0;j<num_biquads;j++)
{
tmp = biquad(...);
}


Each biquad has its coefficients and a scalefactor. Scalefactor is integer.

On the other hand, I have a Matlab filter:

[B_coeffs, A_coeffs ] = butter(4, 100/(16000/2), 'high');


I need to implement the Matlab filter using the above biquad.

I tried using tf2sos which gave me two biquad sections:

[ss, gn] = tf2sos(B_coeffs, A_coeffs);


but feeding ss coefficients to biquad in C gave different results than Matlab version. Gain gn is left unused, and I don't even have scalefactors.

How do I go from Matlab to this particular version of biquad in C? How to create proper coeffs and scalefactors?

• See @RobertBristow-Johnson 's famous cookbook here which may help you: musicdsp.org/files/Audio-EQ-Cookbook.txt – Dan Boschen Feb 28 '17 at 12:23
• OK, I figured it out. First, gain is applied to nominator coeffs, and second, scalefactors just scale coeffs to fit into Q31 range [-1,1] (finding closest power of 2, to make descaling shift only in fixed-point domain). Therefore unscaling in the code: out = out * (float)pow( 2, scalefactor );. This produces the same result as Matlab version. – Danijel Mar 1 '17 at 12:21

## 1 Answer

What I can see from the code is the following mapping between the array coeffs[] and the filter coefficients $a_i$ and $b_i$ (note that $a_0=1$ and is not stored in coeffs[]):

$b_0=$coeffs, $b_1=$coeffs, $b_2=$coeffs

$a_1=$coeffs, $a_2=$coeffs

For the time being choose the scaling equal to $1$ (scalefactor=0) to check if everything else works. The final scaling per section will depend on the order of the sections, and on the goal of scaling (minimization of the possibility of overflow, or minimizing quantization noise).

• You're right about coeff mapping. But... how is the scaling calculated? And why? – Danijel Feb 28 '17 at 14:49
• @Danijel: In principle you can just use scaling=1. However, for a fixed-point implementation you may need to scale in order to avoid overflow. For a specific implementation you need to check at which points in your code overflow can occur and scale accordingly. – Matt L. Feb 28 '17 at 14:54
• @SleuthEye: Sure, that's a scaling of 1 (and a scalefactor=0). Thanks, just edited. – Matt L. Feb 28 '17 at 22:23
• Resolved it, see above comment. – Danijel Mar 1 '17 at 12:23