0
$\begingroup$

I found dfilt.df2sos in Matlab and trying to figure it out how do I do that in Python? Is there any function that works similarly with the Matlab function? I also tried to browse but I could not find a similar function in Scipy.

More dfilt.df2sos https://www.mathworks.com/help/signal/ref/dfilt.df2sos.html

Best,

$\endgroup$
  • 1
    $\begingroup$ dunno Python, but it's not hard to write an SOS if precision and roundoff ain't a problem. $\endgroup$ – robert bristow-johnson Mar 26 '19 at 6:49
0
$\begingroup$

This is Direct Form 1 but it's still cascaded SOS. it's in C, but you should be able to translate it to Python easily. the register keyword is not important.

typedef struct
{
float filterCoefficients[5];    /* b0, b1, b2, a1, a2   (a0 is normalized to 1) */
} sectionData;

void filterBuffer(int32 Nsamples, int32 Nsections, float* y, float* x, float* filterStates, sectionData** sections)
{
register int32 n;
register int32 i;
register sectionData** section_ptr;
register float sampleValue, state1, state2, *state_ptr, *coef_ptr;

for (n=0; n<Nsamples; n++)
    {
    section_ptr = sections;                     // reset section pointer
    state_ptr = filterStates;                   // reset state pointer

    sampleValue = x[n];                         // get input sample

    state2 = *state_ptr++;                      //   x[n-2]           
    state1 = *state_ptr--;                      //   x[n-1]           
    *state_ptr++ = state1;                      //   x[n-1] -> x[n-2] 
    *state_ptr++ = sampleValue;                 //   x[n]   -> x[n-1] 

    i = Nsections;
    while (--i >= 0)
        {
        coef_ptr = &((*section_ptr++)->filterCoefficients[0]);  // point to section filter coefficients

        sampleValue =  *coef_ptr++ * sampleValue;   //   b0*x[n]          
        sampleValue += *coef_ptr++ * state1;        //   b1*x[n-1]        
        sampleValue += *coef_ptr++ * state2;        //   b2*x[n-2]        

        state2 = *state_ptr++;                      //   y[n-2]           
        state1 = *state_ptr--;                      //   y[n-1]           
        *state_ptr++ = state1;                      //   y[n-1] -> y[n-2] 

        sampleValue += *coef_ptr++ * state1;        //  -a1*y[n-1]        
        sampleValue += *coef_ptr++ * state2;        //  -a2*y[n-2]        

        *state_ptr++ = sampleValue;                 //   y[n]   -> y[n-1] 

        //     at this point sampleValue is the output of the section just completed
        //     and will be the input to the section about to be computed unless this
        //     is the last section, in which case sampleValue is the output.
        //     state1 and state2 are the output states of the section just completed
        //     and will be the input states of of the section about to be computed.
        }

    y[n] = sampleValue;                             // store output sample
    }
}
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.