在Python3.7.0中使用scipy.signal.lfilter()时出错:<内置函数=“” _linear_filter =“”>返回NULL而不设置错误

时间:2018-09-03 07:37:55

标签: python scipy

我有256个数据元素的列表。我想使用椭圆过滤器过滤数据。

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

运行此命令时,我看到滤波器设计和系数是正确的,但是使用lfilter会出错

  

lfilter中的文件“ C:\ Users \ gtec \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ scipy \ signal \ signaltools.py”,行1354       返回sigtools._linear_filter(b,a,x,轴)   SystemError:未设置错误就返回NULL

以前我使用的是python2.7,它执行时没有任何错误。现在我正在使用Python3.7.0

1 个答案:

答案 0 :(得分:1)

问题在于c3字符串的列表。 lfilter需要一系列数值。它不会自动将字符串转换为数字,因此在调用lfilter之前,必须在代码中将这些字符串转换为数字。

做类似的事情

c3 = [float(t) for t in c3]

在将c3传递到lfilter之前。

更好的方法是回顾一下在“真实”代码中实际创建c3的方式(假设问题中的代码是一个简化的示例)。在创建c3时将字符串转换为数字是很有意义的。

(神秘的错误消息是bug in lfilter;您应该得到了更好的错误消息。:)