File size: 7,419 Bytes
fa90792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from scipy.signal import butter, lfilter
import torch
from scipy import signal
import librosa
import numpy as np

from scipy.signal import sosfiltfilt
from scipy.signal import butter, cheby1, cheby2, ellip, bessel
from scipy.signal import resample_poly


def align_length(x=None, y=None, Lx=None):
    """align the length of y to that of x

    Args:
        x (np.array): reference signal
        y (np.array): the signal needs to be length aligned

    Return:
        yy (np.array): signal with the same length as x
    """
    assert y is not None

    if Lx is None:
        Lx = len(x)
    Ly = len(y)

    if Lx == Ly:
        return y
    elif Lx > Ly:
        # pad y with zeros
        return np.pad(y, (0, Lx - Ly), mode="constant")
    else:
        # cut y
        return y[:Lx]


def bandpass_filter(x, lowcut, highcut, fs, order, ftype):
    """process input signal x using bandpass filter

    Args:
        x (np.array): input signal
        lowcut (float): low cutoff frequency
        highcut (float): high cutoff frequency
        order (int): the order of filter
        ftype (string): type of filter
            ['butter', 'cheby1', 'cheby2', 'ellip', 'bessel']

    Return:
        y (np.array): filtered signal
    """
    nyq = 0.5 * fs
    lo = lowcut / nyq
    hi = highcut / nyq

    if ftype == "butter":
        # b, a = butter(order, [lo, hi], btype='band')
        sos = butter(order, [lo, hi], btype="band", output="sos")
    elif ftype == "cheby1":
        sos = cheby1(order, 0.1, [lo, hi], btype="band", output="sos")
    elif ftype == "cheby2":
        sos = cheby2(order, 60, [lo, hi], btype="band", output="sos")
    elif ftype == "ellip":
        sos = ellip(order, 0.1, 60, [lo, hi], btype="band", output="sos")
    elif ftype == "bessel":
        sos = bessel(order, [lo, hi], btype="band", output="sos")
    else:
        raise Exception(f"The bandpass filter {ftype} is not supported!")

    # y = lfilter(b, a, x)
    y = sosfiltfilt(sos, x)

    if len(y) != len(x):
        y = align_length(x, y)
    return y


def lowpass_filter(x, highcut, fs, order, ftype):
    """process input signal x using lowpass filter

    Args:
        x (np.array): input signal
        highcut (float): high cutoff frequency
        order (int): the order of filter
        ftype (string): type of filter
            ['butter', 'cheby1', 'cheby2', 'ellip', 'bessel']

    Return:
        y (np.array): filtered signal
    """
    nyq = 0.5 * fs
    hi = highcut / nyq

    if ftype == "butter":
        sos = butter(order, hi, btype="low", output="sos")
    elif ftype == "cheby1":
        sos = cheby1(order, 0.1, hi, btype="low", output="sos")
    elif ftype == "cheby2":
        sos = cheby2(order, 60, hi, btype="low", output="sos")
    elif ftype == "ellip":
        sos = ellip(order, 0.1, 60, hi, btype="low", output="sos")
    elif ftype == "bessel":
        sos = bessel(order, hi, btype="low", output="sos")
    else:
        raise Exception(f"The lowpass filter {ftype} is not supported!")

    y = sosfiltfilt(sos, x)

    if len(y) != len(x):
        y = align_length(x, y)

    y_len = len(y)

    y = stft_hard_lowpass(y, hi, fs_ori=fs)

    y = sosfiltfilt(sos, y)

    if len(y) != y_len:
        y = align_length(y=y, Lx=y_len)

    return y


def stft_hard_lowpass(data, lowpass_ratio, fs_ori=44100):
    fs_down = int(lowpass_ratio * fs_ori)
    # downsample to the low sampling rate
    y = resample_poly(data, fs_down, fs_ori)

    # upsample to the original sampling rate
    y = resample_poly(y, fs_ori, fs_down)

    if len(y) != len(data):
        y = align_length(data, y)
    return y


def limit(integer, high, low):
    if integer > high:
        return high
    elif integer < low:
        return low
    else:
        return int(integer)


def lowpass(data, highcut, fs, order=5, _type="butter"):
    """
    :param data: np.float32 type 1d time numpy array, (samples,) , can not be (samples, 1) !!!!!!!!!!!!
    :param highcut: cutoff frequency
    :param fs: sample rate of the original data
    :param order: order of the filter
    :return: filtered data, (samples,)
    """

    if len(list(data.shape)) != 1:
        raise ValueError(
            "Error (chebyshev_lowpass_filter): Data "
            + str(data.shape)
            + " should be type 1d time array, (samples,) , can not be (samples, 1)"
        )

    if _type in "butter":
        order = limit(order, high=10, low=2)
        return lowpass_filter(
            x=data, highcut=int(highcut), fs=fs, order=order, ftype="butter"
        )
    elif _type in "cheby1":
        order = limit(order, high=10, low=2)
        return lowpass_filter(
            x=data, highcut=int(highcut), fs=fs, order=order, ftype="cheby1"
        )
    elif _type in "ellip":
        order = limit(order, high=10, low=2)
        return lowpass_filter(
            x=data, highcut=int(highcut), fs=fs, order=order, ftype="ellip"
        )
    elif _type in "bessel":
        order = limit(order, high=10, low=2)
        return lowpass_filter(
            x=data, highcut=int(highcut), fs=fs, order=order, ftype="bessel"
        )
    # elif(_type in "stft"):
    #     return stft_hard_lowpass(data, lowpass_ratio=highcut / int(fs / 2))
    # elif(_type in "stft_hard"):
    #     return stft_hard_lowpass_v0(data, lowpass_ratio=highcut / int(fs / 2))
    else:
        raise ValueError("Error: Unexpected filter type " + _type)


def bandpass(data, lowcut, highcut, fs, order=5, _type="butter"):
    """
    :param data: np.float32 type 1d time numpy array, (samples,) , can not be (samples, 1) !!!!!!!!!!!!
    :param lowcut: low cutoff frequency
    :param highcut: high cutoff frequency
    :param fs: sample rate of the original data
    :param order: order of the filter
    :param _type: type of filter
    :return: filtered data, (samples,)
    """
    if len(list(data.shape)) != 1:
        raise ValueError(
            "Error (chebyshev_lowpass_filter): Data "
            + str(data.shape)
            + " should be type 1d time array, (samples,) , can not be (samples, 1)"
        )
    if _type in "butter":
        order = limit(order, high=10, low=2)
        return bandpass_filter(
            x=data,
            lowcut=int(lowcut),
            highcut=int(highcut),
            fs=fs,
            order=order,
            ftype="butter",
        )
    elif _type in "cheby1":
        order = limit(order, high=10, low=2)
        return bandpass_filter(
            x=data,
            lowcut=int(lowcut),
            highcut=int(highcut),
            fs=fs,
            order=order,
            ftype="cheby1",
        )
    # elif(_type in "cheby2"):
    #     return bandpass_filter(x=data,lowcut=int(lowcut),highcut=int(highcut), fs=fs, order=order,ftype="cheby2")
    elif _type in "ellip":
        order = limit(order, high=10, low=2)
        return bandpass_filter(
            x=data,
            lowcut=int(lowcut),
            highcut=int(highcut),
            fs=fs,
            order=order,
            ftype="ellip",
        )
    elif _type in "bessel":
        order = limit(order, high=10, low=2)
        return bandpass_filter(
            x=data,
            lowcut=int(lowcut),
            highcut=int(highcut),
            fs=fs,
            order=order,
            ftype="bessel",
        )
    else:
        raise ValueError("Error: Unexpected filter type " + _type)