Skip to content

data.preprocs.ProcFilter1d

Class ยท Source

proc = mdnc.data.preprocs.ProcFilter1d(
    axis=-1, band_low=3.0, band_high=15.0, nyquist=500.0,
    filter_type='butter', out_type='sosfilt2', filter_args=None,
    inds=None, parent=None
)

This is a homogeneous processor. It is an implementation of the 1D IIR band-pass filters (also supports low-pass or high-pass filter).

The IIR filer would be only performed on the chosen axis. If users want to filter the data along multiple dimensions, using a stack of this instance may be needed, for example:

proc = ProcFilter1d(axis=1, parent=ProcFilter1d(axis=2, ...))
Warning

Plese pay attention to the results. This operation is not invertible, and the postprocess() would do nothing.

Arguments

Requries

Argument Type Description
axis int The axis where we apply the 1D filter.
band_low float The lower cut-off frequency. If only set this value, the filter would become high-pass.
band_high float The higher cut-off frequency. If only set this value, the filter become high-pass.
nyquist float The nyquist frequency of the data.
filter_type str The IIR filter type, could be: 'butter', 'cheby1', 'cheby2', 'ellip', or 'bessel'. See the filter type list to check the details.
out_type str The output filter paramter type, could be 'sosfilt2', 'filt2', 'sos', 'ba'. See the out type list to check the details.
filter_args str A dictionary including other filter arguments, not all arguments are required for each filter, could contain 'order', 'ripple', 'attenuation'. See the filter argument list to check the details.
inds int or
(int, )
Index or indicies of variables where the user implemented methods would be broadcasted. The variables not listed in this argument would be passed to the output without any processing. If set None, methods would be broadcasted to all variables.
parent ProcAbstract Another instance derived from mdnc.data.preprocs.ProcAbstract. The output of parent.preprocess() would be used as the input of self.preprocess(). The input of self.postprocess() would be used as the input of parent.preprocess().

Filter types

Argument Description
butter Butterworth IIR filter, see scipy.signal.butter.
cheby1 Chebyshev type I IIR filter, see scipy.signal.cheby1.
cheby2 Chebyshev type II IIR filter, see scipy.signal.cheby2.
ellip Elliptic (Cauer) IIR filter, see scipy.signal.ellip.
bessel Bessel/Thomson IIR filter, see scipy.signal.bessel.

Out types

Argument Description
sosfilt2 Forward-backward second-order filter coefficients, see scipy.signal.sosfiltfilt.
filt2 Forward-backward first-order filter coefficients, see scipy.signal.filtfilt.
sos Second-order filter coefficients, see scipy.signal.sosfilt.
ba First-order filter coefficients, see scipy.signal.lfilter.

Filter arguments

The arguments in the following table are the default values of the filter_args. If one value is marked as , it means the argument is not available for this filter.

Argument butter cheby1 cheby2 ellip bessel
order 10 4 10 4 10
ripple 5 5
attenuation 40 40

Methods

preprocess

y_1, y_2, ... = proc.preprocess(x_1, x_2, ...)

The preprocess function. Calculate the filterd results for each argument.

If parent exists, the input of this function comes from the output of parent.preprocess(). Otherwise, the input would comes from the input varibable directly.

Requries

Argument Type Description
(x, ) np.ndarray A sequence of variables. Each variable comes from the parent's outputs (if parent exists). The output of this method would be passed as the input of the next processor (if this processor is used as parent).

Returns

Argument Description
(y, ) A sequence of np.ndarray, the final preprocessed data.

postprocess

x_1, x_2, ... = proc.postprocess(y_1, y_2, ...)

The postprocess function. Nothing would be done during the post-processing stage of this processor, i.e. x = proc.postprocess(x).

If parent exists, the output of this function would be passed as the input of parent.postprocess(). Otherwise, the output would be returned to users directly.

Requries

Argument Type Description
(y, ) np.ndarray A sequence of variables. Each variable comes from the next processors's outputs (if parent exists). The output of this method would be passed as the input of the parent's method.

Returns

Argument Description
(x, ) A sequence of np.ndarray, the final postprocessed data.

Properties

parent

proc.parent

The parent processor of this instance. The processor is also a derived class of ProcAbstract. If the parent does not exist, would return None.


has_ind

proc.has_ind

A bool flag, showing whether this processor and its all parent processors have inds configured or initialized with _disable_inds. In this case, the arguments of preprocess() and postprocess() would not share the same operation. We call such kind of processors "Inhomogeneous processors".

Examples

The processor need to be derived. We have two ways to implement the derivation, see the following examples.

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import numpy as np
import matplotlib.pyplot as plt
import mdnc

proc = mdnc.data.preprocs.ProcFilter1d(axis=-1, filter_type='butter', band_low=3.0, band_high=15.0, nyquist=100)
random_rng = np.random.default_rng()
data = random_rng.uniform(low=1 - 0.01, high=1 + 0.01, size=[1, 1024])
t = proc.preprocess(data)
t_b = proc.postprocess(t)

with mdnc.utils.draw.setFigure(font_size=12):
    fig, axs = plt.subplots(nrows=3, ncols=1, sharex=True, figsize=(12, 5))
    axs[0].plot(t[0])
    axs[1].plot(t_b[0])
    axs[2].plot(data[0])
    axs[0].set_ylabel('Preprocessing')
    axs[1].set_ylabel('Inversed\npreprocessing')
    axs[2].set_ylabel('Raw\ndata')
    plt.tight_layout()
    plt.show()


Last update: March 14, 2021

Comments