View Raw SPL
/*****************************************************************************
*                                                                            *
*   TF2SOS.SPL  Copyright (C) 2015 DSP Development Corporation               *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Converts direct form filter coefficients to SOS form        *
*                                                                            *
*   Revisions:   14 Jul 2015  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_TF2SOS

    TF2SOS  

    Purpose: 
             Converts direct form filter coefficients to second order section
             form.
                                                                        
    Format:  
             TF2SOS(b, a)

             (sos, gain) = TF2SOS(b, a)

                b - a series, numerator (i.e. zero) coefficients

                a - a series, denominator (i.e. pole) coefficients

    Returns: 
             An Nx6 column series, where the first 3 columns contain
             the numerator coefficients and the last three columns
             contain the denominator coefficients. Each row represents
             a 2nd order stage.

             (sos, gain) = TF2SOS(b, a) returns the Nx6 second order
             stages and gain in two separate variables.

    Example:
             b = {1};
             a = {1, -0.5, 0.2};
             sos = tf2sos(b, a);

             sos == {1, 0, 0, 1, -0.5, 0.2}

             The direct form filter coefficients represent the following
             Z transform:


                             1
             H(z) = -------------------
                            -1       -2
                    1 - 0.5z   + 0.2z

             
             The resulting 2nd order section coefficients represent the
             equivalent Z transform:

                                -1       -2
                        1 + 0.0z   + 0.0z 
             H(z) = 1 * -------------------
                                -1       -2
                        1 - 0.5z   + 0.2z


             Since the original coefficients represent a single 2nd order
             system, the SOS denominator coefficients are identical.

    Example:
             b = {1};
             a = {1, -0.5, 0.2, 0.1};
             (sos, gain) = tf2sos(b, a);

             sos == {{1, 0, 0, 1, -0.754856, 0.392379},
                     {1, 0, 0, 1,  0.254856, 0}}


             The filter coefficients represent the following Z transform:


                                 1
             H(z) = -----------------------------
                            -1       -2        -3
                    1 - 0.5z   + 0.2z    + 0.1z

             
             The resulting SOS coefficients represent the equivalent Z
             transform:

                                     -1       -2
                             1 + 0.0z   + 0.0z 
             H(z) = 1 * ----------------------------
                                     -1            -2
                        1 - 0.754856z   + 0.392379z


                                     -1       -2
                             1 + 0.0z   + 0.0z 
                      * ------------------------
                                     -1       -2
                        1 - 0.254856z   + 0.0z


             The direct form coefficients are represented by two stages
             of SOS coefficients.

    Remarks:
             TF2SOS converts direct form filter coefficients to second order
             section form. The direct form coefficients represent
             the following Z transform: 
  
                                        -1                 -Q
                    Y(z)   b[1] + b[2]*z   + ... + b[Q+1]*z
             H(z) = ---- = -------------------------------------
                                        -1                 -P
                    X(z)    1   + a[2]*z   + ... + a[P+1]*z


                        jw
             where z = e    unit circle (frequency response)
                   Q        order of zeros (numerator)
                   P        order of poles (denominator)


             The second order section form of the coefficients
             represent the equivalent Z transform:

                               -1       -2                -1       -2
                    b10 + b11 z  + b12 z       b20 + b21 z  + b22 z
         H(z) = G * ________________________ *  ________________________ ...
                               -1       -2                -1       -2
                      1 + a11 z  + a12 z         1 + a21 z  + a22 z



             where the SOS form is an Nx6 array:

             {{b10, b11, b12, 1, a11, a12},
              {b20, b21, b22, 1, a21, a22}, 

                            ...

              {bN0, bN1, bN2, 1, aN1, aN2}} 

             Each row of the SOS coefficients represents a second order stage.

             See SOS2TF to convert second order section form
             coefficients to direct form.

    See Also:
             Cas2sos
             Cas2tf
             Cas2zp
             Cascade
             Sos2cas
             Sos2tf
             Tf2cas
             Zp2cas
#endif


/* convert direct form B(z)/(A(z) to second order section form */
tf2sos(b, a)
{
        local c, sos, g;

        /* cascade form */
        c = tf2cas(b, a);

        if (outargc > 1)
        {
                (sos, g) = cas2sos(c);
                return(sos, g);
        }
        else
        {
                return(cas2sos(c));
        }
}