# Recreating matlab second order filter in python

I want to create some custom filters in Matlab filterDesigner tool and then apply those filters to a large set of audio files. I want to use python to do the application of the filters to the audios.

I created a Butterworth filter in Matlab and exported it as an ASCII file. In there I get two datas. SOS Matrix and Scale Values. Then I read the file in python and got the SOS matrix and filtered my signal using the result as follows (using scipy.sosfilt)

from scipy import signal

x_sos = signal.sosfilt(sos, x)  # x is my signal


The result I get has the desired shape except for the amplitude being different (I applied the same filter in Matlab and compared the output from the python code). I understand that scale values needs to come into this equation. But how do I incorporate that? I don't understand this answer. This question has answers explaining the scale values but still I have no idea how I should use that in the python code

EDIT: (After getting the answer) To anyone who is interested in the final code, here it is. And there's an explanation again in this thread

from scipy import signal
import numpy as np

x_sos = signal.sosfilt(sos, x) * np.prod(scale)  # x is my signal

• Hmm... by documentations, Matlab:sosfilt returns sos coefficients in matrix format ... signal:sosfilt waits array type input for sos parameter... . Nov 20, 2020 at 11:57
• I am using the filterDesigner in matlab and exporting the dilter as ASCII file. This question has the format of the file if you're interested to take a look at
– doca
Nov 20, 2020 at 12:09

## 1 Answer

The scale values should be all $$1$$ except for the first Unless that's $$1$$ too. Multiply your output signal with that first scale value OR multiply the first three numbers in the first three row of you SOS matrix with that number before filtering.

EDIT

Turns out the filter designer makes this more convoluted than it needs to be. Just multiply all the scale values and multiply output with the product.

• But the matlab saved file has these scale values 0.096,0.089,0.083,0.078,0.074,0.070,0.068,0.066,0.064,0.063,0.063. I have these comments in it % Filter Structure : Direct-Form II, Second-Order Sections % Number of Sections : 11 % Stable : Yes % Linear Phase : No
– doca
Nov 20, 2020 at 14:24