IirFilter¶
IirFilter applies the same recursive filter to every channel of its input, for example a band-pass in front of a spike detector. It needs fewer operations per sample than an FIR filter of the same sharpness, but its delay differs from one frequency to the next.
The filter is a cascade of second-order sections, a form in which SciPy and MATLAB can give a filter design. Falcon doesn't design the filter: its coefficients come from such a tool (see Designing the coefficients).
Input and output¶
| Name | Carries | |
|---|---|---|
| Input | input |
Samples whose channel values are 32-bit floating-point numbers. |
| Output | output |
The filtered samples: the same channels and the same rate as the input, each sample with the header of the input sample it was computed from. |
Options¶
| Option | Meaning | Default |
|---|---|---|
design_rate |
The rate, in Hz, that the coefficients were designed for. It must equal the input's rate: at another rate every cutoff frequency would shift. | Required. |
gain |
One factor for the whole cascade. | 1 |
sections |
The second-order sections, in the order the input passes through them. Each is a BiquadSection with five coefficients: b0, b1, b2, a1 and a2. |
Required: at least one section. |
What it computes¶
A section has the transfer function
with already divided out, so that it is 1. The filter is the product of its sections and the gain :
For every sample and every channel, a section computes its output from its input and two values, and , that it keeps from one sample to the next:
This arrangement is known as Transposed Direct Form II. The output of one section is the input of the next. The gain adds no operation per sample: it is multiplied into the first section's coefficients before the filter runs.
Every channel uses the same coefficients and keeps its own and . The channel values arrive and leave as 32-bit numbers. The filter computes with 64-bit numbers, because a section feeds its own rounded results back into itself and 64-bit arithmetic keeps that rounding error small.
Starting without a transient¶
A channel often carries a constant offset. A filter that started with would see that offset as a step, so its output would swing before settling. A spike detector after the filter would report such a swing as spikes on every channel.
Falcon instead starts each section as if its first input value had always been its input. For a constant input , a section settles at
where the first section's coefficients include the gain. The two kept values that go with it are
Each section's output therefore starts at its settled value , which is also the first input value of the next section. The filter's output starts settled too: at 0 for a band-pass.
Lost samples¶
Every sample's header says how many samples were lost before it. When that count rises, the input has a gap whose two sides don't join. Filtering across the gap would give a swing of the same kind as a start from zero.
The filter therefore starts again, as described above, on the first sample after a gap. It also writes a warning to the log, such as "restarted after 12 lost samples, at sample 4500000".
Stability¶
Every section must be stable. A section is stable when both of its poles lie inside the unit circle, which holds if and only if
The output of an unstable filter grows without bound. A mistyped or usually breaks one of the two conditions. An or that is infinite or not a number breaks one as well. The coefficients are not checked.
Delay¶
The filter doesn't wait for later samples: it computes sample of its output from the input's samples up to .
The delay it adds is the group delay of the design, which grows with the number of sections and with the sharpness of the band's edges.
Tip
Where a detection must come early, design the filter no sharper than the experiment needs.
Designing the coefficients¶
In SciPy, ask for second-order sections with output="sos". Each row then holds b0, b1, b2, a0, a1 and a2, with a0 equal to 1. SciPy has already multiplied the gain into the first section, so gain stays 1.
from scipy import signal
sections = signal.butter(2, [300, 6000], btype="bandpass", fs=30000, output="sos")
for b0, b1, b2, a0, a1, a2 in sections:
print(b0, b1, b2, a1, a2)
This band-pass, from 300 Hz to 6000 Hz for an input at 30 kHz, has two sections. Rounded to ten digits:
| Section | b0 |
b1 |
b2 |
a1 |
a2 |
|---|---|---|---|---|---|
| 1 | 0.1906166001 |
0.3812332002 |
0.1906166001 |
-0.4235311646 |
0.2257098675 |
| 2 | 1 |
-2 |
1 |
-1.911551238 |
0.9156533005 |
With design_rate set to 30000, the filter passes 1500 Hz unchanged, lets 71% of the amplitude through at 300 Hz and at 6000 Hz and removes a constant offset.
In MATLAB, [sos, g] = zp2sos(z, p, k) gives rows in the same layout. Its output g is the value for gain.