Skip to content

Filters

Filters

Source code in cmtj/utils/filters.py
 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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.
        """
        # Nyquist is half of the sampling freq
        nyq = 0.5 * fs
        if isinstance(pass_freq, float):
            if pass_freq == 0:
                pass_freq = 0.1
                try:
                    b, a = butter(
                        order,
                        [0.9 * pass_freq / nyq, pass_freq / nyq],
                        btype="bandpass",
                        analog=False,
                    )
                except ValueError as e:
                    print(fs, pass_freq, nyq, 0.9 * pass_freq / nyq,
                          pass_freq / nyq)
                    raise ValueError("Error in filtering") from e
        elif isinstance(pass_freq, tuple):
            b, a = butter(order, [pass_freq[0], pass_freq[1]],
                          btype="bandpass",
                          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 np.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
 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
37
38
39
@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.
    """
    # Nyquist is half of the sampling freq
    nyq = 0.5 * fs
    if isinstance(pass_freq, float):
        if pass_freq == 0:
            pass_freq = 0.1
            try:
                b, a = butter(
                    order,
                    [0.9 * pass_freq / nyq, pass_freq / nyq],
                    btype="bandpass",
                    analog=False,
                )
            except ValueError as e:
                print(fs, pass_freq, nyq, 0.9 * pass_freq / nyq,
                      pass_freq / nyq)
                raise ValueError("Error in filtering") from e
    elif isinstance(pass_freq, tuple):
        b, a = butter(order, [pass_freq[0], pass_freq[1]],
                      btype="bandpass",
                      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 np.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@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
57
58
59
60
61
62
63
64
@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