简体   繁体   中英

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. 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

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

Previously I was using python2.7 and it executed without any errors. Now I am using Python3.7.0

The problem is that c3 is a list of strings . lfilter expects a sequence of numerical values. It will not automatically convert strings to numbers, so you'll have to convert those strings to numbers in your code before calling lfilter .

Do something like

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

before passing c3 to 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). It would make sense to convert the strings to numbers at the point where c3 is created.

(The cryptic error message is a bug in lfilter ; you should have gotten a nicer error message. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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