Second Order Section Filtering¶
Module name: sosfiltering
This module contains second order section filtering routines implemented in c, cffi and numpy.
A bilinear transform converting sos analog weights to
sos digital weights is provided by bilinear_sos()
.
- There are different implementations of sos filtering routines:
A scipy.lfilter()-based
sosfilter_py()
cffi implementations with float- and double-precision and a mimo-implementation:
sosfilter_c()
(float)sosfilter_double_c()
(double)sosfilter_double_mimo_c()
(multi channel input and 3-dim output).
prototypes for the c-implementations (slowest, only for debugging)
The c-implementations are for real valued signals only.
With the function freqz()
you can check the
frequency response of your second order section filters.
For the cffi
you need pycparser
being installed.
Compiling the c source¶
Firstly i implemented a prototype-function in python for easy debugging “sosfilter_cprototype_py()”. After that i translated this prototype into a c-function. By compiling a shared library from it with the listed steps below, one can use the python cffi to access this shared library in python.
$ gcc -c -std=c99 -O3 sosfilter.c
$ gcc -shared -o sosfilter.so sosfilter.o
$ or the last line for windows users:
$ gcc -shared -o sosfilter.dll sosfilter.o
Functions¶
-
sosfiltering.
bilinear_sos
(d, c)[source]¶ Bilinear transformation of analog weights to digital weights. >>>>>>> 8d01abb1e1f252834c0666d50c645dd3d35a1f52
Bilinear transformation of analog weights to digital weights. Weights of IIR digital filter in cascade form with 2-pole sections; H(z)=H(z,1)H(z,2)...H(z,L/2) where L is the number of poles and each section is a ratio of quadratics.
Parameters: d : ndarray
Numerator weights of analog filter in 1-pole sections. d is dimensioned (L/2 x 2).
c : ndarray
Denominator weights, dimensioned same as d.
Returns: b : ndarray
Digital numerator weights, dimensioned (L/2 x 3).
a : ndarray
Digital denominator weights, dimensioned the same.
-
sosfiltering.
freqz
(sosmat, nsamples=44100, sample_rate=44100, plot=True)[source]¶ Plots Frequency response of sosmat.
-
sosfiltering.
sosfilter_c
(signal, sos, states=None)[source]¶ Second order section filter function using cffi
signal_out, states = sosfilter_c(signal_in, sos, states=None)
Parameters: signal : ndarray
Input array of shape (N x 0).
sos : ndarray
Second order section coefficients array of shape (K*6 x 0). One biquad -> 6 coefficients:
[b00, b01, b02, a00, a01, a02, ..., bK1, ..., aK2]
states : ndarray
Array with filter states. Initial value can be None.
Returns: signal : ndarray
Filtered signal of shape (N x 0).
states : ndarray
Array with filter states.
-
sosfiltering.
sosfilter_cprototype_py
(signal, sos, states)[source]¶ Prototype for second order section filtering c function. Implements a IIR DF-II biquad filter strucure.
-
sosfiltering.
sosfilter_double_c
(signal, sos, states=None)[source]¶ Second order section filter function using cffi, double precision.
signal_out, states = sosfilter_c(signal_in, sos, states=None)
Parameters: signal : ndarray
Signal array of shape (N x 0).
sos : ndarray
Second order section coefficients array of shape (K*6 x 0). One biquad -> 6 coefficients:
[b00, b01, b02, a00, a01, a02, ..., b10, bK1 ... , aK2]
states : ndarray
Filter states, initial value can be None.
Returns: signal :
Filtered signal array of shape (N x 0).
states : ndarray
Filter states, initial value can be None.
-
sosfiltering.
sosfilter_double_mimo_c
(signal, sos, states=None)[source]¶ Second order section filter function for multi channel input using cffi, double precision
signal_out, states = sosfilter_c(signal_in, sos, states=None)
Parameters: signal : ndarray
Signal array of shape (N x C).
sos : ndarray
Second order section filter coefficients (K*6 x B x C) np-array.
states : ndarray
Filter states, initial can be None. Otherwise shape is (K*2 x B x C)
Returns: signal : ndarray
Filtered signal of shape (N x B x C). Where N is the number of samples, B is th number of filter bands and C is the number of signal channels.
states : ndarray
Filter states of shape (K*2 x B x C).
-
sosfiltering.
sosfilter_mimo_cprototype_py
(signal_in, sos_in, states_in=None)[source]¶ Prototype for the mimo c-filter function. Implements a IIR DF-II biquad filter strucure. But with multiple input und multiple bands.
-
sosfiltering.
sosfilter_py
(x, sos, states=None)[source]¶ Second order section filter routing with scipy lfilter.
Parameters: x : ndarray
Input signal array.
sos : ndarray
Second order section coefficients array.
states : ndarray or None
Filter states, initial value can be None.
Returns: signal : ndarray
Filtered signal.
states : ndarray
Array with filter states.
butterworth
¶
The butterworth
module provides functions to design butterwort filters.
-
butterworth.
butter_analog_sos
(band, L, w1, w2=0)[source]¶ Returns analog filter coeffitients for Butterworth filters. compute analog weights of a butterworth filter
Parameters: band : {‘lowpass’, ‘highpass, ‘bandpass’, ‘bandstop’}
L : int
Order of lowpass / highpass filter.
w1 : scalar
Critical frequency one.
w2 : scalar
Critical frequency two. (for ‘bandpass’ or ‘bandstop’).
Returns: d, c : Analog weights of the filter
Notes
implements SOS H(s) butterwort if you need H(z) apply a bilinear transform
-
butterworth.
butter_sos
(band, L, v1, v2=0.0)[source]¶ Compute weights of a digital Butterworth filter in cascade form.
Parameters: band : {‘lowpass’, ‘highpass’, ‘bandpass’, ‘bandstop’}
L : int
Number of lowpass poles. L is doubled for ‘bandpass’ and ‘bandstop’. L must be even.
v1 : scalar
First critical frequency (Hz-s); 0.0 <v1 < 0.5.
v2 : scalar
Second critical frequency; v1 < v2 < 0.5. v2 is used only if ‘bandpass’ or ‘bandstop’.
Returns: sosmat : ndarray
Contains the numerator and denominator coeffs for each cascade in one row.
Notes
Adapted from: Samuel D. Stearns, “Digital Signal Processing with Examples in MATLAB”