pyrpl.hardware_modules.iir package¶
Submodules¶
pyrpl.hardware_modules.iir.bodefit module¶
pyrpl.hardware_modules.iir.iir module¶
-
class
pyrpl.hardware_modules.iir.iir.
IIR
(rp, name)[source]¶ Bases:
pyrpl.hardware_modules.filter.FilterModule
-
bypass
¶ IIR is bypassed
-
coefficients
¶
-
complex_poles
¶
-
complex_zeros
¶
-
data_curve
¶ NA curve id to use as a basis for the graphical filter design
-
end
= 'zeros'¶
-
gain
¶
-
iirfilter
= None¶
-
internal_overflow
¶ returns True if the IIR filter has experienced an internal overflow (leading to saturation) since the last reset
-
loops
¶ Decimation factor of IIR w.r.t. 125 MHz. Must be at least 5.
-
on
¶ IIR is on
-
output_saturation
¶ returns True if the output of the IIR filter has saturated since the last reset
-
overflow
¶ a string indicating the overflow status of the iir module
-
overflow_bitfield
¶ Bitmask for various overflow conditions
-
poles
¶
-
real_poles
¶
-
real_zeros
¶
-
sampling_time
¶
-
select_pole_or_zero
(value, logdist=True, search_in=['real_poles', 'real_zeros', 'complex_poles', 'complex_zeros'])[source]¶ selects the pole or zero closest to value
logdist=True computes the distance in logarithmic units search_in may be used to restrict the search to certain sublists
-
setup
(**kwds)¶ Setup an IIR filter
the transfer function of the filter will be (k ensures DC-gain = g):
(s-2*pi*z[0])*(s-2*pi*z[1])...- H(s) = k*——————-
- (s-2*pi*p[0])*(s-2*pi*p[1])...
- coefficients data to be passed to iir.bodeplot to plot the
- realized transfer function
-
input
¶ selects the input signal of the module
-
loops
Decimation factor of IIR w.r.t. 125 MHz. Must be at least 5.
-
zeros
¶
-
poles
-
output_direct
¶ selects to which analog output the module signal is sent directly
-
inputfilter
¶ Input filter bandwidths [Hz]. 0 = off, positive bandwidth <=> lowpass, negative bandwidth <=> highpass.
-
gain
-
on
IIR is on
-
bypass
IIR is bypassed
-
data_curve
NA curve id to use as a basis for the graphical filter design
-
start
= 'complex'¶
-
transfer_function
(frequencies, extradelay=0, kind='final')[source]¶ Returns a complex np.array containing the transfer function of the current IIR module setting for the given frequency array. The best-possible estimation of delays is automatically performed for all kinds of transfer function. The setting of ‘bypass’ is ignored for this computation, i.e. the theoretical and measured transfer functions can only agree if bypass is False.
Parameters: - frequencies (np.array or float) – Frequencies to compute the transfer function for
- extradelay (float) – External delay to add to the transfer function (in s). If zero, only the delay for internal propagation from input to output_signal is used. If the module is fed to analog inputs and outputs, an extra delay of the order of 150 ns must be passed as an argument for the correct delay modelisation.
- kind (str) –
The IIR filter design is composed of a number of steps. Each step slightly modifies the transfer function to adapt it to the implementation of the IIR. The various intermediate transfer functions can be helpful to debug the iir filter.
kind should be one of the following (default is ‘implemented’): - ‘all’: returns a list of data to be passed to iir.bodeplot
with all important kinds of transfer functions for debugging- ‘continuous’: the designed transfer function in continuous time
- ‘before_partialfraction_continuous’: continuous filter just before partial fraction expansion of the coefficients. The partial fraction expansion introduces a large numerical error for higher order filters, so this is a good place to check whether this is a problem for a particular filter design
- ‘before_partialfraction_discrete’: discretized filter just before partial fraction expansion of the coefficients. The partial fraction expansion introduces a large numerical error for higher order filters, so this is a good place to check whether this is a problem for a particular filter design
- ‘before_partialfraction_discrete_zoh’: same as previous, but zero order hold assumption is used to transform from continuous to discrete
- ‘discrete’: the transfer function after transformation to discrete time
- ‘discrete_samplehold’: same as discrete, but zero delay between subsequent biquads is assumed
- ‘highprecision’: hypothetical transfer function assuming that 64 bit fixed point numbers were used in the fpga (decimal point at bit 48)
- ‘implemented’: transfer function after rounding the coefficients to the precision of the fpga
Returns: - tf (np.array(..., dtype=np.complex)) – The complex open loop transfer function of the module.
- If kind==’all’, a list of plotdata tuples is returned that can be
- passed directly to iir.bodeplot().
-
zeros
-
-
class
pyrpl.hardware_modules.iir.iir.
IirComplexListProperty
(*args, **kwargs)[source]¶ Bases:
pyrpl.hardware_modules.iir.iir.IirFloatListProperty
,pyrpl.attributes.ComplexAttributeListProperty
slave property to store complex part of zeros and poles
-
class
pyrpl.hardware_modules.iir.iir.
IirFloatListProperty
(*args, **kwargs)[source]¶ Bases:
pyrpl.attributes.FloatAttributeListProperty
slave property to store real part of zeros and poles
-
list_changed
(module, operation, index, value=None)[source]¶ make sure that an element from one of the four lists is selected at once
-
-
class
pyrpl.hardware_modules.iir.iir.
IirListProperty
(min=<MagicMock name='mock.inf.__neg__()' id='140551782582288'>, max=<MagicMock name='mock.inf' id='140551782527568'>, increment=0, log_increment=False, **kwargs)[source]¶ Bases:
pyrpl.attributes.ComplexProperty
master property to store zeros and poles
-
default
= []¶
-
-
class
pyrpl.hardware_modules.iir.iir.
OverflowProperty
(default=None, doc='', ignore_errors=False, call_setup=False)[source]¶
-
class
pyrpl.hardware_modules.iir.iir.
SignalLauncherIir
(module)[source]¶ Bases:
pyrpl.modules.SignalLauncher
-
update_plot
= <MagicMock name='mock.QtCore.Signal()' id='140551782370384'>¶
-
pyrpl.hardware_modules.iir.iir_theory module¶
-
class
pyrpl.hardware_modules.iir.iir_theory.
IirFilter
(zeros, poles, gain, loops=None, dt=8e-09, minloops=4, maxloops=1023, iirstages=16, totalbits=32, shiftbits=29, tol=0.001, frequencies=None, inputfilter=0, moduledelay=5)[source]¶ Bases:
object
Computes coefficients and predicts transfer functions of an IIR filter.
Parameters: - sys ((zeros, poles, gain)) –
zeros: list of complex zeros poles: list of complex poles gain: DC-gain
zeros/poles with nonzero imaginary part should come in complex conjugate pairs, otherwise the conjugate zero/pole will automatically be added. After this, the number of poles should exceed the number of zeros at least by one, otherwise a real pole near the Nyquist frequency will automatically be added until there are more poles than zeros.
- loops (int or None) – the number of FPGA cycles per filter sample. None tries to automatically find the value leading to the highest possible sampling frequency. If the numerical precision of the filter coefficients in the FPGA is the limiting, manually setting a higher value of loops may improve the filter performance.
- dt (float) – the FPGA clock frequency. Should be very close to 8e-9
- minoops (int) – minimum number of loops (constant of the FPGA design)
- maxloops (int) – maximum number of loops (constant of the FPGA design)
- tol (float) – tolerancee for matching conjugate pole/zero pairs. 1e-3 is okay.
- intermediatereturn (str or None) – if set to a valid option, the algorithm will stop at the specified step and return an intermediate result for debugging. Valid options are
Returns: - coefficients
- coefficients is an array of float arrays of length six, which hold the
- filter coefficients to be passed directly to the iir module
IirFilter.loops
of theIirFilter
instance is- automatically corrected to a number of loops compatible with the
- implemented design.
-
coefficients
¶ Computes and returns the coefficients of the IIR filter set by
IirFilter.sys
.Parameters: - sys ((zeros, poles, gain)) –
zeros: list of complex zeros poles: list of complex poles gain: DC-gain
zeros/poles with nonzero imaginary part should come in complex conjugate pairs, otherwise the conjugate zero/pole will automatically be added. After this, the number of poles should exceed the number of zeros at least by one, otherwise a real pole near the nyquist frequency will automatically be added until there are more poles than zeros.
- loops (int or None) – the number of FPGA cycles per filter sample. None tries to automatically find the value leading to the highest possible sampling frequency. If the numerical precision of the filter coefficients in the FPGA is the limiting, manually setting a higher value of loops may improve the filter performance.
- dt (float) – the FPGA clock frequency. Should be very close to 8e-9
- minoops (int) – minimum number of loops (constant of the FPGA design)
- maxloops (int) – maximum number of loops (constant of the FPGA design)
- tol (float) – tolerancee for matching conjugate pole/zero pairs. 1e-3 is okay.
- intermediatereturn (str or None) – if set to a valid option, the algorithm will stop at the specified step and return an intermediate result for debugging. Valid options are
Returns: - coefficients
- coefficients is an array of float arrays of length six, which hold the
- filter coefficients to be passed directly to the iir module
IirFilter.loops
of theIirFilter
instance is- automatically corrected to a number of loops compatible with the
- implemented design.
- sys ((zeros, poles, gain)) –
-
coefficients_rounded
¶
-
designdata
¶
-
frequencies
¶
-
minimize_delay
(coefficients=None)[source]¶ Minimizes the delay of coefficients by rearranging the biquads in an optimal way (highest frequency poles get minimum delay.
Parameters: coefficients – Returns: Return type: new coefficients
-
prewarp
(z, p, dt=8e-09)[source]¶ prewarps frequencies in order to correct warping effect in discrete time conversion
-
proper_sys
¶ Makes sure that a system is strictly proper and that all complex poles/zeros have conjugate parters.
Parameters: - zeros (list of zeros) –
- poles (list of poles) –
- loops (number of loops to implement. Can be None for autodetection.) –
- minloops (minimum number of loops that is acceptable) –
- maxloops (minimum number of loops that is acceptable) –
- iirstages (number of biquads available for implementation) –
- tol (tolerance for matching complex conjugate partners) –
Returns: - (zeros, poles, minloops) - the corrected lists of poles/zeros and the
- number of loops that are minimally needed for implementation
-
rescaled_sys
¶ rescales poles and zeros with 2pi and returns the prefactor corresponding to dc-gain gain
-
rp2coefficients
(r, p, c, tol=0)[source]¶ Pairs residues and corresponding poles into second order sections.
Parameters: - r (array with numerator coefficient) –
- p (array with poles) –
- tol (tolerance for combining complex conjugate pairs.) –
Returns: coefficients
Return type: array((N, 6), dtype=np.float64) where N is number of biquads
-
sampling_rate
¶
-
sys
¶
-
tf_coefficients
(frequencies=None, coefficients=None, delay=False)[source]¶ computes implemented transfer function - assuming no delay and infinite precision (actually floating-point precision) Returns the discrete transfer function realized by coefficients at frequencies.
Parameters: - coefficients (np.array) – coefficients as returned from iir module
- frequencies (np.array) – frequencies to compute the transfer function for
- dt (float) – discrete sampling time (seconds)
- zoh (bool) – If true, zero-order hold implementation is assumed. Otherwise, the delay is expected to depend on the index of biquad.
Returns: Return type: np.array(.., dtype=np.complex)
-
tf_continuous
(frequencies=None)[source]¶ Returns the continuous transfer function of sys at frequencies.
Parameters: - sys (tuple) – (zeros, poles, gain) zeros: list of complex zeros poles: list of complex poles gain: float
- frequencies (np.array) – frequencies to compute the transfer function for
Returns: Return type: np.array(.., dtype=np.complex)
-
tf_discrete
(rp_discrete=None, frequencies=None)[source]¶ Returns the discrete transfer function realized by coefficients at frequencies.
Parameters: - rpz (np.array) – coefficients as returned from iir module (array of biquad coefficients)
- frequencies (np.array) – frequencies to compute the transfer function for
- dt (float) – discrete sampling time (seconds)
- delay_per_cycle (float) – the biquad at coefficients[i] experiences an extra delay of i*delay_per_cycle
- zoh (bool) – If true, zero-order hold implementation is assumed. Otherwise, the delay is expected to depend on the index of biquad.
Returns: Return type: np.array(.., dtype=np.complex)
-
tf_final
(frequencies=None)[source]¶ Returns the discrete transfer function realized by coefficients at frequencies.
Parameters: - coefficients (np.array) – coefficients as returned from iir module
- frequencies (np.array) – frequencies to compute the transfer function for
- dt (float) – discrete sampling time (seconds)
- zoh (bool) – If true, zero-order hold implementation is assumed. Otherwise, the delay is expected to depend on the index of biquad.
Returns: Return type: np.array(.., dtype=np.complex)
-
tf_partialfraction
(frequencies=None)[source]¶ Returns the transfer function just before the partial fraction expansion for frequencies.
Parameters: Returns: Return type: np.array(.., dtype=np.complex)
-
tf_rounded
(frequencies=None, delay=False)[source]¶ Returns the discrete transfer function realized by coefficients at frequencies.
Parameters: - coefficients (np.array) – coefficients as returned from iir module
- frequencies (np.array) – frequencies to compute the transfer function for
- dt (float) – discrete sampling time (seconds)
- zoh (bool) – If true, zero-order hold implementation is assumed. Otherwise, the delay is expected to depend on the index of biquad.
Returns: Return type: np.array(.., dtype=np.complex)
- sys ((zeros, poles, gain)) –
-
pyrpl.hardware_modules.iir.iir_theory.
bodeplot
(data, xlog=False)[source]¶ plots a bode plot of the data x, y
Parameters: - data (a list of tuples (f, tf[, label]) where f are frequencies and tf) – complex transfer data, and label the label for data
- xlog (sets xaxis to logscale) –
- figure –
-
pyrpl.hardware_modules.iir.iir_theory.
cont2discrete
(r, p, c, dt=8e-09)[source]¶ Transforms residue and pole from continuous to discrete time
Parameters: Returns: Return type: (r, p) with the transformation applied
-
pyrpl.hardware_modules.iir.iir_theory.
discrete2cont
(r, p, c, dt=8e-09)[source]¶ Transforms residues and poles from discrete time to continuous
Parameters: Returns: Return type: r, p with the transformation applied
-
pyrpl.hardware_modules.iir.iir_theory.
freqs
(sys, w)[source]¶ This function computes the frequency response of a zpk system at an array of frequencies.
It loosely mimicks ‘scipy.signal.freqs’.
Parameters: Returns: Return type: np.array(.., dtype=np.complex) with the response
-
pyrpl.hardware_modules.iir.iir_theory.
freqs_rp
(r, p, c, w)[source]¶ same as freqs, but takes a list of residues and poles
-
pyrpl.hardware_modules.iir.iir_theory.
freqz_
(sys, w, dt=8e-09)[source]¶ This function computes the frequency response of a zpk system at an array of frequencies.
It loosely mimicks ‘scipy.signal.frequresp’.
Parameters: Returns: Return type: np.array(.., dtype=np.complex) with the response
-
pyrpl.hardware_modules.iir.iir_theory.
residues
(z, p, k)[source]¶ this function uses the residue method (Heaviside Cover-up method) to perform the partial fraction expansion of a rational function defined by zeros, poles and a prefactor k. No intermediate conversion into a polynome is performed, which makes this function less prone to finite precision issues. In the current version, no pole value may occur twice and the number of poles must be strictly greater than the number of zeros.
Returns: - np.array(dtype=np.complex128) containing the numerator array a of the
- expansion – product_i( s - z[i] ) a[j]
- k ————————- = sum_j ( ———- ) – product_j( s - p[j] ) s - p[j]
-
pyrpl.hardware_modules.iir.iir_theory.
sos2zpk
(sos)[source]¶ Return zeros, poles, and gain of a series of second-order sections
Parameters: sos (array_like) – Array of second-order filter coefficients, must have shape (n_sections, 6)
. See sosfilt for the SOS filter format specification.Returns: - z (ndarray) – Zeros of the transfer function.
- p (ndarray) – Poles of the transfer function.
- k (float) – System gain.
Notes
New in version 0.16.0.