Matlab在python中的带通滤波器等效

2024-04-24 09:24:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在matlab中有一个名为bandpass的函数,我经常使用它。 函数的文档可以在这里找到:https://ch.mathworks.com/help/signal/ref/bandpass.html

我正在寻找一种在Python中应用带通滤波器并获得相同或几乎相同的输出滤波信号的方法

我的信号可以从这里下载:https://gofile.io/?c=JBGVsH

Matlab代码:

load('mysignal.mat')
y = bandpass(x, [0.015,0.15], 1/0.7);
plot(x);hold on; plot(y)

enter image description here

Python代码:

import matplotlib.pyplot as plt
import scipy.io
from scipy.signal import butter, lfilter

x = scipy.io.loadmat("mysignal.mat")['x']

def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = butter(order, [low, high], btype='band')
    return b, a


def butter_bandpass_filter(data, lowcut, highcut, fs, order=6):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y

y = butter_bandpass_filter(x, 0.015, 0.15, 1/0.7, order=6)

plt.plot(x);plt.plot(y);plt.show()

enter image description here

我需要在python中找到一种方法来应用类似于Matlab示例代码块中的过滤


Tags: 函数代码httpsioimportplotorderplt