Skip to content

Filters

Filters

Source code in cmtj/utils/filters.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Filters:
    @staticmethod
    def butter_bandpass_filter(data: np.ndarray, pass_freq: tuple[float, float], fs: float, order: int = 5):
        """Basic bandpass (notch) butterworth filter.
        :param data: input data.
        :param pass_freq: the tuple of (low, high) band frequencies.
        :param fs: sampling frequency.
        """
        b, a = butter(order, pass_freq, btype="bandpass", fs=fs, analog=False)
        return lfilter(b, a, data, zi=None)

    @staticmethod
    def butter_lowpass_filter(data: np.ndarray, cutoff: float, fs: float, order: int = 5):
        """Low pass digital filter.
        :param data: data to be filtered.
        :param cutoff: cutoff frequency of the filter.
        :param fs: sampling frequency.
        :param order: order of the filter.
        """
        nyq = 0.5 * fs
        normal_cutoff = cutoff / nyq
        b, a = butter(order, normal_cutoff, btype="low", analog=False)
        return lfilter(b, a, data, zi=None)

    @staticmethod
    def detrend_axis(arr, axis):
        """Detrend axis for better spectrum visibility.
        :param arr: input array (spectrum)
        :param axis: axis along which to detrend
        """
        medians = np.median(arr, axis=axis)
        return (arr.T - medians).T if axis else arr - medians

butter_bandpass_filter(data, pass_freq, fs, order=5) staticmethod

Basic bandpass (notch) butterworth filter.

Parameters:

Name Type Description Default
data ndarray

input data.

required
pass_freq tuple[float, float]

the tuple of (low, high) band frequencies.

required
fs float

sampling frequency.

required
Source code in cmtj/utils/filters.py
 6
 7
 8
 9
10
11
12
13
14
@staticmethod
def butter_bandpass_filter(data: np.ndarray, pass_freq: tuple[float, float], fs: float, order: int = 5):
    """Basic bandpass (notch) butterworth filter.
    :param data: input data.
    :param pass_freq: the tuple of (low, high) band frequencies.
    :param fs: sampling frequency.
    """
    b, a = butter(order, pass_freq, btype="bandpass", fs=fs, analog=False)
    return lfilter(b, a, data, zi=None)

butter_lowpass_filter(data, cutoff, fs, order=5) staticmethod

Low pass digital filter.

Parameters:

Name Type Description Default
data ndarray

data to be filtered.

required
cutoff float

cutoff frequency of the filter.

required
fs float

sampling frequency.

required
order int

order of the filter.

5
Source code in cmtj/utils/filters.py
16
17
18
19
20
21
22
23
24
25
26
27
@staticmethod
def butter_lowpass_filter(data: np.ndarray, cutoff: float, fs: float, order: int = 5):
    """Low pass digital filter.
    :param data: data to be filtered.
    :param cutoff: cutoff frequency of the filter.
    :param fs: sampling frequency.
    :param order: order of the filter.
    """
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype="low", analog=False)
    return lfilter(b, a, data, zi=None)

detrend_axis(arr, axis) staticmethod

Detrend axis for better spectrum visibility.

Parameters:

Name Type Description Default
arr

input array (spectrum)

required
axis

axis along which to detrend

required
Source code in cmtj/utils/filters.py
29
30
31
32
33
34
35
36
@staticmethod
def detrend_axis(arr, axis):
    """Detrend axis for better spectrum visibility.
    :param arr: input array (spectrum)
    :param axis: axis along which to detrend
    """
    medians = np.median(arr, axis=axis)
    return (arr.T - medians).T if axis else arr - medians