Converts zeros, poles and gain to second order section form.
ZP2SOS(z, p, k, "flag")
(sos, g) = ZP2SOS(z, p, k, "flag")
z 
 
A series, the zeros. 

p 
 
A series, the poles. 

k 
 
Optional. A scalar, the gain. Defaults to 1.0. 

"flag" 
 
Optional. A string, the stage ordering flag:

An Nx6 array, the system coefficients in second order section form.
(sos, g) = ZP2SOS(z, p, k, "flag") returns the SOS coefficients and gain in two separate variables.
z = {0.0, 2.0};
p = {0.5, 0.2};
k = 1.0;
sos = zp2cas(z, p, k);
sos == {{1, 2, 0, 1, 0.7, 0.1}}
The 2nd order cascade filter coefficients represent the following Z transform:
ZP2SOS converts the zeros, poles and gain of a discrete system to second order section form where the coefficients represent the following Z transform:
or:
where G is the system gain, b_{k} and a_{k} are the filter coefficients for the k^{th} stage.
The SOS form is an Nx6 array:
{{b_{10}, b_{11}, b_{12}, 1, a_{11}, a_{12}},
_{ }{b_{20}, b_{21}, b_{22}, 1, a_{21}, a_{22},}
...
{b_{N0}, b_{N1}, b_{N2}, 1, a_{N1}, a_{N2}}}
Each row of the SOS coefficients represents a second order stage.
If the gain is not returned as a separate variable, the gain is embedded into the first stage.
ZP2SOS also works for analog cascade coefficients. In this case, the SOS system function becomes:
or equivalently:
To yield real coefficients, the zeros and poles must occur in complex conjugate pairs or be real.
The stages are ordered such that the poles of each stage are closer to the unit circle than the previous stage. The zeros of each stage are chosen to be closest to the poles of the same stage.
If "flag" == "down", the stages are reversed.
See SOS2ZP to convert cascade coefficients to zeros, poles and gain terms.