bundles / scipy latest / scipy / signal / _filter_design / zpk2sos
function
scipy.signal._filter_design:zpk2sos
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_likeZeros of the transfer function.
p: array_likePoles of the transfer function.
k: floatSystem gain.
pairing: {None, 'nearest', 'keep_odd', 'minimal'}, optionalThe 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, optionalIf True, system is analog, otherwise discrete.
Returns
sos: ndarrayArray 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.
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-arrayapifor 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')✓
sos = signal.zpk2sos(z, p, k)
✓sos[:, :3]
✗sos[:, 3:]
✗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])✓
signal.zpk2sos(z1, p1, 1)
✓signal.zpk2sos(z1, p1, 1, pairing='keep_odd')
✓signal.zpk2sos(z1, p1, 1, pairing='minimal')
✓See also
Aliases
-
scipy.signal.zpk2sos