I am using Apple's Accelerate Framework, vDSP biquad function https://developer.apple.com/documentation/accelerate/1450838-vdsp_biquad?language=objc#parameters in order to emulate pythons sosfilt function. I have done all the filtering but notice I have transient signals that I am trying to remove. In python, this is done with finding the initial conditions from sosfilt_zi https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.sosfilt_zi.html#scipy.signal.sosfilt_zi and multiplying by the first element in the data that is being filtered. I followed this amazing example Matlab filtfilt() function implementation in Java.
When I print out the initial conditions in python using sosfilt_zi, I am getting two values for each section (I'm using a second-order cascade) so I have four values in total.
However in the single channel biquad function for iOS, they are asking for (2*M)+2 for the Delay. I was assuming that the initial conditions are related to the Delay and that this is what I would need to change in order to adjust my signal and remove the transient.
The function definition for the Delay Parameter for the biquad function in iOS is " An array of single-precision values initialized with direct-form 1 “past” state data for each section of the biquad. The length of the array should be (2 * M) + 2, where M is the number of sections. For each section m, Delay[2m:2m+1] represent the two delayed input values for section m and Delay[2M:2M+1] represent the two delayed output values of the filter. After this function executes, this array contains the final state data of the filters."
If I have 2 sections, then my delay array should look like :
[0:1] - input delay value for section 0
[2:3] - input delay value for section 1
[4:5] - output delay value
I initially had the Delay set to an array of 6 zeros. If the output of sosfilt_zi would be the proper values for the two input sections of the Delay, then how could I find the delayed output values of the filter?
Is anyone familiar with vDSP and able to remove transient signals or can explain how to adjust the Delay values for a cascaded second-order sections? Is adjusting the Delay values the correct move and how could I get these values in python?
I was thinking that my delay array would look like (using values shown in second link below):
[0.72095219 -0.28358224] - section 0
[-0.80004591 0.80004591] - section 1
However, I don't know what that output would be if this is even correct. I'd appreciate any help! I've found really little documentation or examples on the iOS biquad function. Thank you!
Butterworth Bandpass filter - Initial Conditions is being applied
Output of sosfilt_zi - gives inital conditions
Result of Biquad Filter from iOS vDSP on top and result of sosfilt with initial conditions on bottom