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[2] * in + delay[0];    
    out = out * (float)pow( 2, scalefactor );    
    delay[0] = delay[1] + coeffs[1]*in - coeffs[4]*out;
    delay[1] = coeffs[0]*in - coeffs[3]*out;

    // etc...

    return out;
}


It cascades several biquads:

for(int j=0;j&lt;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?
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[2], $b_1=$coeffs[1], $b_2=$coeffs[0]

$a_1=$coeffs[4], $a_2=$coeffs[3]

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