[英]Error using scipy.signal.lfilter() in Python3.7.0 : <built-in function _linear_filter> returned NULL without setting an error
I have a list of 256 data elements. 我有256个数据元素的列表。 I want to filter this data using elliptical filter.
我想使用椭圆过滤器过滤此数据。
import matplotlib.pyplot as plt
from scipy.signal import *
import numpy as np
def elliptical_bandpass():
Fs=256
lowcut=5
highcut=30
order=5
Rp = 0.5; # Passband Ripple (dB)
Rs = 30; # Stopband Ripple (dB)
nyq = Fs/2 #Nyquist frequency
wp = lowcut / nyq
ws = highcut / nyq
c3=['221', '262', '333', '429', '522', '592', '630', '656', '668', '645', '581', '486', '395', '324', '265', '214', '172', '171', '214', '282', '353', '420', '498', '584', '650', '679', '661', '622', '571', '503', '415', '316', '240', '200', '185', '188', '204', '256', '344', '443', '527', '582', '627', '665', '676', '644', '567', '481', '404', '337', '271', '204', '168', '175', '218', '277', '340', '419', '513', '599', '653', '662', '649', '622', '578', '506', '407', '317', '252', '213', '188', '173', '194', '258', '352', '445', '517', '578', '632', '671', '672', '626', '561', '491', '422', '341', '254', '188', '165', '184', '224', '271', '337', '424', '522', '598', '638', '652', '653', '637', '585', '497', '397', '314', '258', '215', '180', '172', '202', '272', '352', '427', '502', '579', '649', '680', '664', '615', '555', '498', '424', '335', '251', '195', '180', '187', '212', '258', '338', '442', '533', '594', '628', '649', '661', '640', '579', '490', '402', '332', '266', '206', '164', '166', '216', '285', '357', '425', '501', '584', '644', '669', '655', '624', '580', '509', '414', '311', '236', '202', '190', '191', '207', '258', '345', '441', '521', '577', '626', '667', '676', '643', '567', '483', '407', '334', '261', '194', '162', '176', '222', '280', '342', '422', '517', '603', '654', '662', '650', '626', '579', '505', '404', '315', '252', '213', '187', '173', '196', '262', '352', '442', '513', '580', '642', '679', '674', '622', '553', '483', '413', '336', '254', '196', '177', '191', '221', '260', '328', '422', '524', '603', '640', '655', '656', '637', '583', '492', '397', '319', '263', '217', '176', '168', '204', '278', '361', '436', '509', '583', '645', '672', '656', '616', '565', '507', '425', '325', '238', '188', '179', '190', '213', '260', '338', '440']
n, Wn = ellipord(wp, ws, Rp,Rs)
print('Wn IS ----', Wn)
b,a=ellip(order,Rp,Rs,[wp, ws], btype='band') #get filter coefficients
print('b coeff from filter code -- ',b)
print('a coeff from filter code -- ',a)
c3_filtered=lfilter(b,a,c3)
print('filtered data-',c3_filtered)
print('len of filtered data', len(c3_filtered))
w, h = freqz(b, a, worN=2000) #used to plot the frequency response
plt.figure()
plt.plot((Fs * 0.5 / np.pi) * w, abs(h), label="order = %d" % order)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.grid(True)
plt.legend(loc='best')
plt.show()
elliptical_bandpass()
When I run this, I see filter design and coefficients to be correct, but I get an error using lfilter 运行此命令时,我看到滤波器设计和系数是正确的,但是使用lfilter时出现错误
File "C:\\Users\\gtec\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\scipy\\signal\\signaltools.py", line 1354, in lfilter return sigtools._linear_filter(b, a, x, axis) SystemError: returned NULL without setting an error
lfilter返回sigtools中的文件“ C:\\ Users \\ gtec \\ AppData \\ Local \\ Programs \\ Python \\ Python37-32 \\ lib \\ site-packages \\ scipy \\ signal \\ signaltools.py”,行sigtools._linear_filter(b, x,轴)SystemError:未设置错误即返回NULL
Previously I was using python2.7 and it executed without any errors. 以前我使用的是python2.7,它执行时没有任何错误。 Now I am using Python3.7.0
现在我正在使用Python3.7.0
The problem is that c3
is a list of strings . 问题是
c3
是一个字符串列表。 lfilter
expects a sequence of numerical values. lfilter
需要一系列数值。 It will not automatically convert strings to numbers, so you'll have to convert those strings to numbers in your code before calling lfilter
. 它不会自动将字符串转换为数字,因此在调用
lfilter
之前,必须在代码lfilter
这些字符串转换为数字。
Do something like 做类似的事情
c3 = [float(t) for t in c3]
before passing c3
to lfilter
. 在将
c3
传递给lfilter
之前。
Even better would be to look back at how you actually create c3
in your "real" code (assuming the code in the question is a simplified example). 更好的办法是回顾一下如何在“真实”代码中实际创建
c3
(假设问题中的代码是一个简化的示例)。 It would make sense to convert the strings to numbers at the point where c3
is created. 在创建
c3
的点将字符串转换为数字是有意义的。
(The cryptic error message is a bug in lfilter
; you should have gotten a nicer error message. :) (神秘的错误消息是
lfilter
的错误 ;您应该已经得到了更好的错误消息。:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.