IIR Filter Box Instrument

The IIR Filter Box implements infinite impulse response (IIR) filters using 4 cascaded Direct Form 1 second-order stages with a final output gain stage. The instrument has two independent filter chains, a control matrix to combine signals from both ADCs, input/output scaling and offsets and oscilloscope monitor probes to view signals at different points in the instrument.

Example Usage

The following example code and a wide range of other pymoku demo scripts can be found at the pymoku Github repository.

iirfilterbox_basic.py
# pymoku example: Basic IIR Filter Box
#
# This example demonstrates how you can configure the IIR Filter instrument,
# configure real-time monitoring of the input and output signals.
#
# (c) 2019 Liquid Instruments Pty. Ltd.
#
from pymoku import Moku
from pymoku.instruments import IIRFilterBox

# This script provides a basic example showing how to load coefficients from an array into the IIRFilterBox.

# The following example array produces an 8th order Direct-form 1 Chebyshev type 2 IIR filter with a
# normalized stopband frequency of 0.2 pi rad/sample and a stopband attenuation of 40 dB. Output gain
# is set to 1.0. See the IIRFilterBox documentation for array dimension specifics.
filt_coeff = 	[[1.0],
				[1.0000000000,0.6413900006,-1.0290561741,0.6413900006,-1.6378425857,0.8915664128],
				[1.0000000000,0.5106751138,-0.7507394931,0.5106751138,-1.4000444473,0.6706551819],
				[1.0000000000,0.3173108134,-0.3111365531,0.3173108134,-1.0873085012,0.4107935750],
				[1.0000000000,0.1301131088,0.1223154629,0.1301131088,-0.7955572476,0.1780989281]]


m = Moku.get_by_name('Moku')

try:
	i = m.deploy_or_connect(IIRFilterBox)

	i.set_frontend(1, fiftyr=True, atten=False, ac=False)
	i.set_frontend(2, fiftyr=True, atten=False, ac=False)

	# Both filters have the same coefficients, but the different sampling rates mean the resultant
	# transfer functions will be different by a factor of 128 (the ratio of sampling rates)
	i.set_filter(1, sample_rate='high', filter_coefficients=filt_coeff)
	i.set_filter(2, sample_rate='low',  filter_coefficients=filt_coeff)

	# Offset filter channel 1 input by 0.1V
	i.set_gains_offsets(1, input_offset = 0.1)
	# Filter channel 2 acts on sum of input 1 and 2
	i.set_control_matrix(2, scale_in1 = 0.5, scale_in2 = 0.5)

	# Set the monitor timebase to +-1msec
	i.set_timebase(-1e-3, 1e-3)

	# Set up monitoring of the input and output of the second filter channel.
	i.set_monitor('a', 'in2')
	i.set_monitor('b', 'out2')

	# Capture and print one set of time-domain input and output points
	d = i.get_realtime_data()
	print(d.ch1, d.ch2)
finally:
	m.close()

The IIRFilterBox Class

class pymoku.instruments.IIRFilterBox

The IIR Filter Box implements infinite impulse response (IIR) filters using 4 cascaded Direct Form 1 second-order stages with a final output gain stage. The total transfer function can be written:

\[H(z) = G * \prod_{k=1}^4 s_k * \frac{b_0k + b_1k * z^-1 + b_2k * z^-2}{1 + a_1k * z^-1 + a_2k * z^-2}\]

To specify a filter, you must supply an array containing the filter coefficients. The array should contain five rows and six columns. The first row has one column entry, corresponding to the overall gain factor G. The following four rows have six entries each, corresponding to the s, b0, b1, b2, a1 and a2 coefficients of the four cascaded SOS filters.

Example array dimensions:

G          
s1 b0.1 b1.1 b2.1 a1.1 a2.1
s2 b0.2 b1.2 b2.2 a1.2 a2.2
s3 b0.3 b1.3 b2.3 a1.3 a2.3
s4 b0.4 b1.4 b2.4 a1.4 a2.4

Each ‘a’ coefficient must be a float in the range [-4.0, +4.0). ‘s’ coefficients are multiplied into each ‘b’ coefficient before being sent to the device. These products (sN x b0.N, sN x b1.N, sN x b2.N) must also fall in the range [-4.0, +4.0). Internally, the ‘a’ and ‘b’ coefficients are represented as signed 48-bit fixed-point numbers, with 45 fractional bits. The gain coefficient G must be a float in the range [-8e6, 8e6 - 2^-24]. It is represented internally as a signed 48-bit fixed-point with 24 fractional bits.

Filter coefficients can be computed using signal processing toolboxes in e.g. MATLAB or SciPy.

Note

The overall output gain of the instrument is the product of the gain of the filter, set in the coefficient matrix, and the output stage gain set using set_gains_offsets.

Warning

Some coefficients may result in overflow or underflow, which degrade filter performance. Filter responses should be checked prior to use.

commit()

Apply all modified settings.

Note

If the autocommit feature has been turned off, this function can be used to manually apply any instrument settings to the Moku device. These instrument settings are those configured by calling all set_ and gen_ type functions. Manually calling this function allows you to atomically apply many instrument settings at once.

disable_output(*args, **kwargs)

Disables the output of the specified IIR filter channel.

Parameters:ch (int; {1,2}) – target channel
get_data(timeout=None, wait=True)

Get full-resolution data from the instrument.

This will pause the instrument and download the entire contents of the instrument’s internal memory. This may include slightly more data than the instrument is set up to record due to rounding of some parameters in the instrument.

All settings must be committed before you call this function. If pymoku.autocommit=True (the default) then this will always be true, otherwise you will need to have called commit first.

The download process may take a second or so to complete. If you require high rate data, e.g. for rendering a plot, see get_realtime_data.

If the wait parameter is true (the default), this function will wait for any new settings to be applied before returning. That is, if you have set a new timebase (for example), calling this with wait=True will guarantee that the data returned has this new timebase.

Note that if instrument configuration is changed, a trigger event must occur before data captured with that configuration set can become available. This can take an arbitrary amount of time. For this reason the timeout should be set appropriately.

Parameters:
  • timeout (float) – Maximum time to wait for new data, or None for indefinite.
  • wait (bool) – If true (default), waits for a new waveform to be captured with the most recently-applied settings, otherwise just return the most recently captured valid data.
Returns:

InstrumentData subclass, specific to the instrument.

get_frontend(channel)

Get the analog frontend configuration.

Parameters:channel (int; {1,2}) – Channel for which the relay settings are being retrieved
Returns:Array of bool with the front end configuration of channels - [0] 50 Ohm - [1] 10xAttenuation - [2] AC Coupling
get_realtime_data(timeout=None, wait=True)

Get downsampled data from the instrument with low latency.

Returns a new InstrumentData subclass (instrument-specific), containing a version of the data that may have been downsampled from the original in order to be transferred quickly.

This function always returns a new object at framerate (10Hz by default), whether or not there is new data in that object. This can be verified by checking the return object’s waveformid parameter, which increments each time a new waveform is captured internally.

The downsampled, low-latency nature of this data makes it particularly suitable for plotting in real time. If you require high-accuracy, high-resolution data for analysis, see get_data.

If the wait parameter is true (the default), this function will wait for any new settings to be applied before returning. That is, if you have set a new timebase (for example), calling this with wait=True will guarantee that the data returned has this new timebase.

Note that if instrument configuration is changed, a trigger event must occur before data captured with that configuration set can become available. This can take an arbitrary amount of time. For this reason the timeout should be set appropriately.

Parameters:
  • timeout (float) – Maximum time to wait for new data, or None for indefinite.
  • wait (bool) – If true (default), waits for a new waveform to be captured with the most recently-applied settings, otherwise just return the most recently captured valid data.
Returns:

InstrumentData subclass, specific to the instrument.

get_samplerate()
Returns:The current instrument sample rate (Hz)
set_control_matrix(*args, **kwargs)

Configure the input control matrix specifying the input signal mixing for the specified filter channel.

Input mixing allows a filter channel to act on a linear combination of the two input signals.

Parameters:
  • ch (int, {1, 2}) – target filter channel
  • scale_in1 (float, [-20, 20]) – linear scale factor of input 1 signal added to target filter channel input. To avoid quantization, use at most one decimal place.
  • scale_in2 (float, [-20, 20]) – linear scale factor of input 2 signal added to target filter channel input. To avoid quantization, use at most one decimal place.
set_defaults(*args, **kwargs)

Reset the IIR to sane defaults.

set_filter(ch, sample_rate, filter_coefficients)

Set SOS filter sample rate and filter coefficients. This also enables the input and outputs of the specified Moku:Lab channel.

Parameters:
  • ch (int; {1,2}) – target channel
  • sample_rate (string; {'high','low'}) – filter sample rate where ‘high’ ~ 15.625 MHz and ‘low’ ~ 122.070 kHz.
  • filter_coefficients (array;) – array containing SOS filter coefficients. Format is described in class documentation above.
set_framerate(*args, **kwargs)

Set framerate

set_frontend(channel, fiftyr=True, atten=False, ac=False)

Configures gain, coupling and termination for each channel.

Parameters:
  • channel (int; {1,2}) – Channel to which the settings should be applied
  • fiftyr (bool) – 50Ohm termination; default is 1MOhm.
  • atten (bool) – Turn on 10x attenuation. Changes the dynamic range between 1Vpp and 10Vpp.
  • ac (bool) – AC-couple; default DC.
set_gains_offsets(*args, **kwargs)

Configure pre- and post-filter scales and offsets for a given filter channel.

Note

The overall output gain of the instrument is the product of the gain of the filter, set by the filter coefficients, and the input/output stage gain set here.

Parameters:
  • ch (int, {1,2}) – target filter channel
  • output_gain (input_gain,) – channel scalars before and after the FIR filter
  • input_offset (float, [-1.0,1.0] Volts) – channel offset before the FIR filter
  • output_offset (float, [-2.0,2.0] Volts) – channel offset after the FIR filter
set_monitor(*args, **kwargs)

Configures the specified monitor channel to view the desired IIR Filter Box signal.

There are two 12-bit monitoring channels available, ‘a’ and ‘b’; each of these can be assigned to source signals from any of the internal filterbox monitoring points. Signals larger than 12-bits must be either truncated or clipped to the allowed size.

The source is one of:
  • adc1 : Channel 1 ADC input
  • in1 : Filter Channel 1 input (after mixing, offset and scaling)
  • out1 : Filter Channel 1 output
  • adc2 : Channel 2 ADC Input
  • in2 : Filter Channel 2 input (after mixing, offset and scaling)
  • out2 : Filter Channel 2 output
Parameters:
  • monitor_ch (str; {'a','b'}) – Monitor channel
  • source (str; {'adc1', 'in1', 'out1', 'adc2', 'in2', 'out2'}) – Signal to connect to the monitor channel
set_precision_mode(*args, **kwargs)

Change aquisition mode between downsampling and decimation. Precision mode, a.k.a Decimation, samples at full rate and applies a low-pass filter to the data. This improves precision. Normal mode works by direct downsampling, throwing away points it doesn’t need.

Parameters:state (bool) – Select Precision Mode
set_samplerate(*args, **kwargs)

Manually set the sample rate of the instrument.

The sample rate is automatically calculated and set in set_timebase.

This interface allows you to specify the rate at which data is sampled, and set a trigger offset in number of samples. This interface is useful for datalogging and capturing of data frames.

Parameters:
  • samplerate (float; 0 < samplerate <= MAX_SAMPLERATE smp/s) – Target samples per second. Will get rounded to the nearest allowable unit.
  • trigger_offset (int; -2^16 < trigger_offset < 2^31) – Number of samples before (-) or after (+) the trigger point to start capturing.
Raises:

ValueOutOfRangeException – if either parameter is out of range.

set_timebase(*args, **kwargs)

Set the left- and right-hand span for the time axis. Units are seconds relative to the trigger point.

Parameters:
  • t1 (float) – Time, in seconds, from the trigger point to the left of screen. This may be negative (trigger on-screen) or positive (trigger off the left of screen).
  • t2 (float) – As t1 but to the right of screen.
Raises:

InvalidConfigurationException – if the timebase is backwards or zero.

set_trigger(*args, **kwargs)

Set the trigger source for the monitor channel signals. This can be either of the input or monitor signals, or the external input.

Parameters:
  • source (string, {'in1','in2','A','B','ext'}) – Trigger Source. May be either an input or monitor channel (as set by set_monitor()), or external. External refers to the back-panel connector of the same name, allowing triggering from an externally-generated digital [LV]TTL or CMOS signal.
  • edge (string, {'rising','falling','both'}) – Which edge to trigger on. In Pulse Width modes this specifies whether the pulse is positive (rising) or negative (falling), with the ‘both’ option being invalid.
  • level (float, [-10.0, 10.0] volts) – Trigger level
  • minwidth (float, seconds) – Minimum Pulse Width. 0 <= minwidth < (2^32/samplerate). Can’t be used with maxwidth.
  • maxwidth (float, seconds) – Maximum Pulse Width. 0 <= maxwidth < (2^32/samplerate). Can’t be used with minwidth.
  • hysteresis (float, [100e-6, 1.0] volts) – Hysteresis around trigger point.
  • hf_reject (bool) – Enable high-frequency noise rejection
  • mode (string, {'auto', 'normal'}) – Trigger mode.
set_xmode(*args, **kwargs)

Set rendering mode for the horizontal axis.

Parameters:xmode (string, {'roll','sweep','fullframe'}) – Respectively; Roll Mode (scrolling), Sweep Mode (normal oscilloscope trace sweeping across the screen) or Full Frame (like sweep, but waits for the frame to be completed).