Matlab's bandpass filter equivalent in python

Deadly 提交于 2020-04-18 03:49:26

问题


In matlab there is a function called bandpass that I often use. The doc of the function can be found here: https://ch.mathworks.com/help/signal/ref/bandpass.html

I am looking for a way to apply a bandpass filter in Python and get the same or almost the same output filtered signal.

My signal can be downloaded from here: https://gofile.io/?c=JBGVsH

Matlab code:

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

Python code:

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()

I need to find a way in python to apply similar filtering as in the Matlab example code block.

来源:https://stackoverflow.com/questions/60866193/matlabs-bandpass-filter-equivalent-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!