# Regarding BIQUAD IIR implementation in MATLAB

I want to implement a high pass filter (order 4) in MATLAB using cascade form.

[b,a]=butter(4,0.0083,'high');% I am using butterworth filter
[sos,gain]=tf2sos(b,a);


Should I use [sos,gain] for implementing the same in cascade form or just [sos]. I read a post related to same and it says just [sos] should be used, however, I am not sure if that is the correct way. P.S. I want to create a MATLAB function for IIR filter(don't want to use the standard filter function in MATLAB) which would get the coefficients b,a & input.

• Could you link to that post? – Matt L. Jan 7 '16 at 8:09
• The highpass gain will be unity (for the normalized transfer function), so, in this case, it's safe to ignore the gain. – a concerned citizen Jan 2 '17 at 8:25

Both will work if used correctly

b,a]=butter(4,0.0083,'high');% I am using butterworth filter
% version 1
[sos,gain]=tf2sos(b,a);
y1 = gain*sosfilt(sos,x);
% version 2
[sos]=tf2sos(b,a);
y2 = sosfilt(sos,x);
% y1 will be equal to y2


EDIT: the answer below assumes that you called tf2sos with [sos,gain] as output arguments, and that your question is about the necessity of making use of the output variable gain. Thanks to Hilmar for pointing out this potential misunderstanding. His answer is about the equivalence of two different calls to tf2sos, one with two output arguments, and the other with just one. Note that when called with just one output argument, the resulting sos is different from the one obtained from a call with two output arguments.

If you want the correct scaling, in the sense that high frequency signals are passed through the filter with a gain of $1$, then you need to use the variable gain. If you don't use it you'll get the same high pass filter, but with some scaling. For fixed-point implementations, appropriate scaling is very important to minimize overflow and other quantization artefacts. For a Matlab implementation I wouldn't worry too much about scaling and just implement the filter with the correct gain.

• Sorry, I think this answer is wrong. If you omit the second output argument the overall gain is integrated into the first second order section. If you use two arguments all section are scaled so that $b_1=1$. In either case you get the same result – Hilmar Jan 7 '16 at 13:43
• @Hilmar: I understood the question differently. I thought the OP wanted to know if it's OK to just use sos (given that you have both, sos and gain). Re-reading the question, your interpretation probably makes more sense. However, in that case reading the Matlab documentation would have solved the problem for the OP ... – Matt L. Jan 7 '16 at 14:09