{ } Raw JSON

bundles / scipy latest / scipy / signal / _filter_design / zpk2sos

function

scipy.signal._filter_design:zpk2sos

source: /scipy/signal/_filter_design.py :1484

Signature

def   zpk2sos ( z p k pairing = None * analog = False )

Summary

Return second-order sections from zeros, poles, and gain of a system

Parameters

z : array_like

Zeros of the transfer function.

p : array_like

Poles of the transfer function.

k : float

System gain.

pairing : {None, 'nearest', 'keep_odd', 'minimal'}, optional

The method to use to combine pairs of poles and zeros into sections. If analog is False and pairing is None, pairing is set to 'nearest'; if analog is True, pairing must be 'minimal', and is set to that if it is None.

analog : bool, optional

If True, system is analog, otherwise discrete.

Returns

sos : ndarray

Array of second-order filter coefficients, with shape (n_sections, 6). See sosfilt for the SOS filter format specification.

Notes

The algorithm used to convert ZPK to SOS format is designed to minimize errors due to numerical precision issues. The pairing algorithm attempts to minimize the peak gain of each biquadratic section. This is done by pairing poles with the nearest zeros, starting with the poles closest to the unit circle for discrete-time systems, and poles closest to the imaginary axis for continuous-time systems.

pairing='minimal' outputs may not be suitable for sosfilt, and analog=True outputs will never be suitable for sosfilt.

Algorithms

The steps in the pairing='nearest', pairing='keep_odd', and pairing='minimal' algorithms are mostly shared. The 'nearest' algorithm attempts to minimize the peak gain, while 'keep_odd' minimizes peak gain under the constraint that odd-order systems should retain one section as first order. 'minimal' is similar to 'keep_odd', but no additional poles or zeros are introduced

The algorithm steps are as follows:

As a pre-processing step for pairing='nearest', pairing='keep_odd', add poles or zeros to the origin as necessary to obtain the same number of poles and zeros for pairing. If pairing == 'nearest' and there are an odd number of poles, add an additional pole and a zero at the origin.

The following steps are then iterated over until no more poles or zeros remain:

  • Take the (next remaining) pole (complex or real) closest to the unit circle (or imaginary axis, for analog=True) to begin a new filter section.

  • If the pole is real and there are no other remaining real poles [1], add the closest real zero to the section and leave it as a first order section. Note that after this step we are guaranteed to be left with an even number of real poles, complex poles, real zeros, and complex zeros for subsequent pairing iterations.

  • Else:

    • If the pole is complex and the zero is the only remaining real zero*, then pair the pole with the next closest zero (guaranteed to be complex). This is necessary to ensure that there will be a real zero remaining to eventually create a first-order section (thus keeping the odd order).

    • Else pair the pole with the closest remaining zero (complex or real).

    • Proceed to complete the second-order section by adding another pole and zero to the current pole and zero in the section:

      • If the current pole and zero are both complex, add their conjugates.

      • Else if the pole is complex and the zero is real, add the conjugate pole and the next closest real zero.

      • Else if the pole is real and the zero is complex, add the conjugate zero and the real pole closest to those zeros.

      • Else (we must have a real pole and real zero) add the next real pole closest to the unit circle, and then add the real zero closest to that pole.

[1]

This conditional can only be met for specific odd-order inputs with the pairing = 'keep_odd' or 'minimal' methods.

Array API Standard Support

zpk2sos has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ⛔                   
JAX                   ⚠️ no JIT
Dask                  ⚠️ computes graph     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

Design a 6th order low-pass elliptic digital filter for a system with a sampling rate of 8000 Hz that has a pass-band corner frequency of 1000 Hz. The ripple in the pass-band should not exceed 0.087 dB, and the attenuation in the stop-band should be at least 90 dB. In the following call to `ellip`, we could use ``output='sos'``, but for this example, we'll use ``output='zpk'``, and then convert to SOS format with `zpk2sos`:
from scipy import signal
import numpy as np
z, p, k = signal.ellip(6, 0.087, 90, 1000/(0.5*8000), output='zpk')
Now convert to SOS format.
sos = signal.zpk2sos(z, p, k)
The coefficients of the numerators of the sections:
sos[:, :3]
The symmetry in the coefficients occurs because all the zeros are on the unit circle. The coefficients of the denominators of the sections:
sos[:, 3:]
The next example shows the effect of the `pairing` option. We have a system with three poles and three zeros, so the SOS array will have shape (2, 6). The means there is, in effect, an extra pole and an extra zero at the origin in the SOS representation.
z1 = np.array([-1, -0.5-0.5j, -0.5+0.5j])
p1 = np.array([0.75, 0.8+0.1j, 0.8-0.1j])
With ``pairing='nearest'`` (the default), we obtain
signal.zpk2sos(z1, p1, 1)
The first section has the zeros {-0.5-0.05j, -0.5+0.5j} and the poles {0, 0.75}, and the second section has the zeros {-1, 0} and poles {0.8+0.1j, 0.8-0.1j}. Note that the extra pole and zero at the origin have been assigned to different sections. With ``pairing='keep_odd'``, we obtain:
signal.zpk2sos(z1, p1, 1, pairing='keep_odd')
The extra pole and zero at the origin are in the same section. The first section is, in effect, a first-order section. With ``pairing='minimal'``, the first-order section doesn't have the extra pole and zero at the origin:
signal.zpk2sos(z1, p1, 1, pairing='minimal')

See also

sosfilt

Aliases

  • scipy.signal.zpk2sos

Referenced by

This package