带通滤波器 ValueError:数字滤波器临界频率必须为 0 < Wn < 1

新手上路,请多包涵

我正在尝试对 ecg 信号使用带通滤波器,这是代码:

 from scipy.signal import butter

def bandpass_filter(self, data, lowcut, highcut, signal_freq, filter_order):
        nyquist_freq = 0.5 * signal_freq
        low = lowcut / nyquist_freq
        high = highcut / nyquist_freq
        b, a = butter(filter_order, [low, high], btype='band', analog=False)
        y = lfilter(b,a, data)
        return y

def detect_peaks(self):
    self.filtered_ecg_measurements = self.bandpass_filter(ecg_measurements,
                                         lowcut=self.filter_lowcut,
                                         highcut=self.filter_highcut,
                                         signal_freq=self.signal_frequency,
                                         filter_order=self.filter_order)
    self.signal_frequency = 250
    self.filter_lowcut = 0.0
    self.filter_highcut = 15.0
    self.filter_order = 1

每次我尝试运行此功能时都会出现此错误:

 Traceback (most recent call last):
  File "D:/Project/code/untitled/test.py", line 297, in <module>
    log_data=True, plot_data=True, show_plot=False)
  File "D:/Project/code/untitled/test.py", line 98, in __init__
    self.detect_peaks()
  File "D:/Project/code/untitled/test.py", line 135, in detect_peaks
    filter_order=self.filter_order)
  File "D:/Project/code/untitled/test.py", line 256, in bandpass_filter
    b, a = butter(filter_order, [low, high], btype='band', analog=False)
  File "C:\Users\AppData\Roaming\Python\Python36\site-packages\scipy\signal\filter_design.py", line 2394, in butter
    output=output, ftype='butter')
  File "C:\Users\AppData\Roaming\Python\Python36\site-packages\scipy\signal\filter_design.py", line 1959, in iirfilter
    raise ValueError("Digital filter critical frequencies "
ValueError: Digital filter critical frequencies must be 0 < Wn < 1

错误文本是:

数字滤波器临界频率必须为 0 < Wn < 1

原文由 f.zahraa 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.8k
1 个回答

可能是因为输入了参数 fs 。它必须大于 2 * Wn 中的任何一个。如 scipy/signal/filter_design.py 源代码:

 if fs is not None:
    if analog:
        raise ValueError("fs cannot be specified for an analog filter")
    Wn = 2*Wn/fs

然后:

 if not analog:
    if numpy.any(Wn <= 0) or numpy.any(Wn >= 1):
        raise ValueError("Digital filter critical frequencies must be 0 < Wn < 1")

原文由 Jiayin Shi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题