简体   繁体   English

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

[英]band pass filter ValueError: Digital filter critical frequencies must be 0 < Wn < 1

I am trying to use bandpass filter on an ecg signals, this is the code:我正在尝试对 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

This error appears every time I try to run this function:每次我尝试运行此 function 时都会出现此错误:

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

the error text is:错误文本是:

Digital filter critical frequencies must be 0 < Wn < 1数字滤波器临界频率必须为 0 < Wn < 1

It may be because of the input of parameter fs .可能是因为输入了参数fs It has to be larger than any of 2 * Wn .它必须大于 2 * Wn任何一个。 As in scipy/signal/filter_design.py source code:如 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

And later:然后:

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

As Darkoob12 pointed out in the comments to the question, lowcut cannot equal zero.正如lowcut在对该问题的评论中指出的那样,低切不能等于零。 If lowcut is set to zero, it will produce the error message seen by OP.如果lowcut设置为零,它将产生 OP 看到的错误消息。

scipy.signal.butter(N, Wn, btype='low', analog=False, output='ba') scipy.signal.butter(N, Wn, btype='low',analog=False, output='ba')

Wn : array_like Wn : array_like

A scalar or length-2 sequence giving the critical frequencies.给出临界频率的标量或长度为 2 的序列。 For a Butterworth filter, this is the point at which the gain drops to 1/sqrt(2) that of the passband (the “-3 dB point”).对于巴特沃斯滤波器,这是增益下降到通带的 1/sqrt(2) 的点(“-3 dB 点”)。 For digital filters, Wn is normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample.对于数字滤波器,Wn 从 0 归一化为 1,其中 1 是奈奎斯特频率,pi 弧度/样本。 (Wn is thus in half-cycles / sample.) For analog filters, Wn is an angular frequency (eg rad/s). (因此,Wn 以半周期/样本为单位。)对于模拟滤波器,Wn 是角频率(例如弧度/秒)。

Your exception is here where low/high is not [0,1]你的例外是在这里低/高不是 [0,1]

b, a = butter(filter_order, [low, high], btype='band', analog=False)

for filter_type == 1 , Wn is float other than nparray.对于filter_type == 1Wn是 nparray 以外的浮点数。

https://github.com/scipy/scipy/blob/v0.19.1/scipy/signal/filter_design.py#L2226-L2297 https://github.com/scipy/scipy/blob/v0.19.1/scipy/signal/filter_design.py#L2226-L2297

>>> N, Wn = signal.buttord([20, 50], [14, 60], 3, 40, True)
>>> b, a = signal.butter(N, Wn, 'band', True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM