View Raw SPL
/*****************************************************************************
*                                                                            *
*   SOS2CAS.SPL  Copyright (C) 2015 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Converts second order section form to cascade form          *
*                                                                            *
*   Revisions:   30 Jun 2015  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_SOS2CAS

    SOS2CAS  

    Purpose: 
             Converts second order section form to cascade coefficients.
                                                                        
    Format:  
             SOS2CAS(sos, g)

              sos - A Nx6 array where the first 3 columns are the numerator
                    terms and the last 3 columns are the denominator terms.
                    Each row represents a 2nd order stage.

                g - Optional. A real, the gain, Defaults to 1.0.

    Returns: 
             A series, the coefficients in 2nd order cascade form.

    Example:
             sos = {{1, 0, 0, 1, -0.5, 0.2}};
             c = sos2cas(sos, 1.0);

             c == {1, 1, 0, 0, -0.5, 0.2};

             The second order section filter coefficients represent the
             following Z transform:

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


             The resulting cascade form filter coefficients represent the
             same Z transform:

    Example:
             sos = {{1, 0, 0, 1, -0.754856, 0.292379},
                    {1, 0, 0, 1,  0.254856, 0}};

             g = 1.0;

             c = sos2cas(sos, g);

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

             The second order section filter coefficients represent the
             following 2 stage 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 resulting cascade form coefficients represent the same Z
             transform.

    Remarks:
             SOS2CAS converts second order section form filter coefficients
             to cascade form. Both forms represent the following
             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.

             The cascade coefficients are represented as a single
             column series with the coefficients in the following order:

             G, b10, b11, b12, a11, a12, b20, b21, b22, a21, a22, ...


             See CAS2SOS to convert cascade form coefficients to SOS form.

    See Also:
             Cas2dir
             Cas2sos
             Cascade
             Sos2zp
#endif


/* converts Nx6 arrays to cascade form */
sos2cas(sos, gain)
{
        local c;

        if (argc < 2)
        {
                if (argc < 1)
                {
                        sos = {};
                } 
        
                gain = 1.0;
        }

        if (numcols(sos) != 6) 
        {
                if (numcols(sos) == 1 && numrows(sos) == 6)
                {
                        sos = sos';
                }
                else
                {
                        error("sos2cas - Nx6 input array required");
                }
        }

        /* delete leading 1's denominator */
        sos = deletecol(sos, 4);

        /* column-wise */
        c = {gain, unravel(sos')};

        setdeltax(c, deltax(sos));
        setcomment(c, "Cascade");

        return(c);
}