[英]how to obtain a filtered signal by using scipy.signal.butter & lfilter?
I have a program whose purpose is to filter a noisy signal using butterworth filter. 我有一个程序,其目的是使用Butterworth滤波器来过滤噪声信号。 the code is listed below.
代码在下面列出。 The program cannot be complied because I did something wrong at the last step "y = butter_bandpass_filter(v_numbers, lowcut, highcut, fs, order=6)".
无法执行该程序,因为我在最后一步“ y = butter_bandpass_filter(v_numbers,lowcut,highcut,fs,order = 6)”做错了什么。 What i want to get are three plots: 1.input signal in time domain, 2. butterworth filter in frquency domain.
我想要得到的是三个曲线图:1.时域中的输入信号,2.频率域中的巴特沃斯滤波器。 3. output filtered signal in time domain.
3.在时域中输出滤波后的信号。
from scipy.signal import butter, lfilter
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y
if __name__ == "__main__":
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import freqz
# Sample rate and desired cutoff frequencies (in Hz).
fs = 5000.0
lowcut = 0.0
highcut = 2000.0
# Plot the frequency response for a few different orders.
plt.figure(1)
plt.clf()
for order in [3, 6, 9]:
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
w, h = freqz(b, a, worN=2000)
plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="order = %d" % order)
plt.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)],
'--', label='sqrt(0.5)')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.grid(True)
plt.legend(loc='best')
# Filter a noisy signal.
T = 0.9004
nsamples = T * fs
t = np.linspace(0, T, nsamples, endpoint=False)
a = 0.02
f0 =600.0
# Plot the frequency response for a few different orders.
f = open('NIRS_data.txt','r')
number_string = f.readline()
v_numbers = []
while number_string != '':
numbers = number_string.split()
for number in numbers:
v_numbers.append( number )
number_string = f.readline()
plt.figure()
plt.clf()
plt.plot(t,v_numbers, label = 'Noisy signal')
y = butter_bandpass_filter(v_numbers, lowcut, highcut, fs, order=6)
plt.plot(t, y, label='Filtered signal (%g Hz)')
plt.xlabel('time (seconds)')
plt.show()
A part of txt file is shown below. txt文件的一部分如下所示。 The amount of datas is 4502.
数据量为4502。
8.2178200e-02 8.2173600e-02 8.2129400e-02 8.2209000e-02 8.2183000e-02 8.2098900e-02 8.2162500e-02 8.2157700e-02 8.2177900e-02 8.2177600e-02 8.2088400e-02 8.2142900e-02 8.2179600e-02 8.2159200e-02 8.2144800e-02 8.2139000e-02 8.2121200e-02 8.2157900e-02 8.2142600e-02 8.2190600e-02 8.2129500e-02 8.2125800e-02 8.2097500e-02 8.2087300e-02 8.2206800e-02 8.2175400e-02 8.2183300e-02 8.2197400e-02 8.2129500e-02 8.2101600e-02 8.2117800e-02 8.2125900e-02 8.2131300e-02 8.2107600e-02 8.2146900e-02 8.2122400e-02 8.2111800e-02 8.2156100e-02 8.2088500e-02 8.2135300e-02 8.2119700e-02 8.2100800e-02 8.2135700e-02 8.2126900e-02 8.2134000e-02 8.2111000e-02 8.2101600e-02 8.2108600e-02 8.2142900e-02 8.2091000e-02 8.2117700e-02 8.2061400e-02 8.2085200e-02 8.2080400e-02 8.2075400e-02 8.2064400e-02 8.2059700e-02 8.2098200e-02 8.2077200e-02 8.2138200e-02 8.2116300e-02 8.2092000e-02 8.2071900e-02 8.2092500e-02 8.2056900e-02 8.2108900e-02 8.2061300e-02 8.2064300e-02 8.2063900e-02 8.2120600e-02 8.2049500e-02 8.2087 8.2178200e-02 8.2173600e-02 8.2129400e-02 8.2209000e-02 8.2183000e-02 8.2098900e-02 8.2162500e-02 8.2157700e-02 8.2177900e-02 8.2177600e-02 8.2088400e-02 8.2142900e-02 8.2179600e -02 8.2159200e-02 8.2144800e-02 8.2139000e-02 8.2121200e-02 8.2157900e-02 8.2142600e-02 8.2190600e-02 8.2129500e-02 8.2125800e-02 8.2097500e-02 8.2087300e-02 8.2206800e-02 8.2175400e-02 8.2183300e-02 8.2197400e-02 8.2129500e-02 8.2101600e-02 8.2117800e-02 8.2125900e-02 8.2131300e-02 8.2107600e-02 8.2146900e-02 8.2122400e-02 8.2111800e-02 8.2156100e -02 8.2088500e-02 8.2135300e-02 8.2119700e-02 8.2100800e-02 8.2135700e-02 8.2126900e-02 8.2134000e-02 8.2111000e-02 8.2101600e-02 8.2108600e-02 8.2142900e-02 8.2091000e-02 8.2117700e-02 8.2061400e-02 8.2085200e-02 8.2080400e-02 8.2075400e-02 8.2064400e-02 8.2059700e-02 8.2098200e-02 8.2077200e-02 8.2138200e-02 8.2116300e-02 8.2092000e-02 8.2071900e -02 8.2092500e-02 8.2056900e-02 8.2108900e-02 8.2061300e-02 8.2064300e-02 8.2063900e-02 8.2120600e-02 8.2049500e-02 8.2087 300e-02 8.2066800e-02 8.2074900e-02 8.2052400e-02 8.2093200e-02 8.2061800e-02 8.2043700e-02 8.2070500e-02 8.2056900e-02 8.2084000e-02 8.2075900e-02 8.2065900e-02 8.2054200e-02 8.2037400e-02 8.2040600e-02 8.2085500e-02 8.2029000e-02 8.2057000e-02 8.2045700e-02 8.2112600e-02 8.2068000e-02 8.2034900e-02 8.2045200e-02 8.2046400e-02 8.2067300e-02 8.2080500e-02 8.2021400e-02 8.2047300e-02 8.2060200e-02 8.2042900e-02 8.2065200e-02 8.2056100e-02 8.1990900e-02 8.2055700e-02 8.2030300e-02 8.2103400e-02 8.2092600e-02 8.1995200e-02 8.2075300e-02 8.2001500e-02 8.2064000e-02 8.2033500e-02 8.2042800e-02 8.2037400e-02 8.2002000e-02 8.2057900e-02 8.2025100e-02 8.2038900e-02 8.2035200e-02 8.2005700e-02 8.2016700e-02 8.2012800e-02 8.1984900e-02 8.2066200e-02 8.2029600e-02 8.2027400e-02 8.2012200e-02 8.2009400e-02 8.2024900e-02 8.2038700e-02 8.2034700e-02 8.2016200e-02 8.1964500e-02 8.2019400e-02 8.2010500e-02 8.2004100e-02 8.2057500e-02 8.2052300e-02 8.2004500e-02 8.1998400e-02 8.2011600e-02 8.2038400e-0
300e-02 8.2066800e-02 8.2074900e-02 8.2052400e-02 8.2093200e-02 8.2061800e-02 8.2043700e-02 8.2070500e-02 8.2056900e-02 8.2084000e-02 8.2075900e-02 8.2065900e-02 8.2054200e- 02 8.2037400e-02 8.2040600e-02 8.2085500e-02 8.2029000e-02 8.2057000e-02 8.2045700e-02 8.2112600e-02 8.2068000e-02 8.2034900e-02 8.2045200e-02 8.2046400e-02 8.2067300e-02 8.2080500 e-02 8.2021400e-02 8.2047300e-02 8.2060200e-02 8.2042900e-02 8.2065200e-02 8.2056100e-02 8.1990900e-02 8.2055700e-02 8.2030300e-02 8.2103400e-02 8.2092600e-02 8.1995200e- 02 8.2075300e-02 8.2001500e-02 8.2064000e-02 8.2033500e-02 8.2042800e-02 8.2037400e-02 8.2002000e-02 8.2057900e-02 8.2025100e-02 8.2038900e-02 8.2035200e-02 8.2005700e-02 8.2016700 e-02 8.2012800e-02 8.1984900e-02 8.2066200e-02 8.2029600e-02 8.2027400e-02 8.2012200e-02 8.2009400e-02 8.2024900e-02 8.2038700e-02 8.2034700e-02 8.2016200e-02 8.1964500e- 02 8.2019400e-02 8.2010500e-02 8.2004100e-02 8.2057500e-02 8.2052300e-02 8.2004500e-02 8.1998400e-02 8.2011600e-02 8.2038400e-0 2 8.2002500e-02 8.2005700e-02 8.2065900e-02 8.1991200e-02 8.2039900e-02 8.2028200e-02 8.2027000e-02 8.2021300e-02 8.2019600e-02 8.2032900e-02 8.2011700e-02 8.2017400e-02 8.2069400e-02 8.1998400e-02 8.2059400e-02 8.1958300e-02 8.1995800e-02 8.2018500e-02 8.1973400e-02 8.2008800e-02 8.1995900e-02 8.1989400e-02 8.1991800e-02 8.2000600e-02 8.2040400e-02 8.2035700e-02 8.1987800e-02 8.2027400e-02 8.2010800e-02 8.1991300e-02 8.1999400e-02 8.1926800e-02 8.2021100e-02 8.1967800e-02 8.1992600e-02 8.2022200e-02 8.1933100e-02 8.1998900e-02 8.2004300e-02 8.1991300e-02 8.2039500e-02 8.1998900e-02 8.2005400e-02 8.1997600e-02 8.1954500e-02 8.2000000e-02 8.1978000e-02 8.1990800e-02 8.1966200e-02 8.1997400e-02 8.2028700e-02 8.1957700e-02 8.2013700e-02 8.2052000e-02 8.1961400e-02 8.2007200e-02 8.1984800e-02 8.1999600e-02 8.2041800e-02 8.1990100e-02 8.2014500e-02 8.2008300e-02 8.1980400e-02 8.2000800e-02 8.1988200e-02 8.1979900e-02 8.2003400e-02 8.1921000e-02 8.1985600e-02 8.1995500e-02 8.1951000e-02 8.20
2 8.2002500e-02 8.2005700e-02 8.2065900e-02 8.1991200e-02 8.2039900e-02 8.2028200e-02 8.2027000e-02 8.2021300e-02 8.2019600e-02 8.2032900e-02 8.2011700e-02 8.2017400e-02 8.2069400 e-02 8.1998400e-02 8.2059400e-02 8.1958300e-02 8.1995800e-02 8.2018500e-02 8.1973400e-02 8.2008800e-02 8.1995900e-02 8.1989400e-02 8.1991800e-02 8.2000600e-02 8.2040400e- 02 8.2035700e-02 8.1987800e-02 8.2027400e-02 8.2010800e-02 8.1991300e-02 8.1999400e-02 8.1926800e-02 8.2021100e-02 8.1967800e-02 8.1992600e-02 8.2022200e-02 8.1933100e-02 8.1998900 e-02 8.2004300e-02 8.1991300e-02 8.2039500e-02 8.1998900e-02 8.2005400e-02 8.1997600e-02 8.1954500e-02 8.2000000e-02 8.1978000e-02 8.1990800e-02 8.1966200e-02 8.1997400e- 02 8.2028700e-02 8.1957700e-02 8.2013700e-02 8.2052000e-02 8.1961400e-02 8.2007200e-02 8.1984800e-02 8.1999600e-02 8.2041800e-02 8.1990100e-02 8.2014500e-02 8.2008300e-02 8.1980400 e-02 8.2000800e-02 8.1988200e-02 8.1979900e-02 8.2003400e-02 8.1921000e-02 8.1985600e-02 8.1995500e-02 8.1951000e-02 8.20 06500e-02 8.1977500e-02 8.2005200e-02 8.2000100e-02 8.1938300e-02 8.1993000e-02 8.1983800e-02 8.1995600e-02 8.1992500e-02 8.1976700e-02 8.2020400e-02 8.1986800e-02 8.1990200e-02 8.2007100e-02 8.1957500e-02 8.2021900e-02 8.1954900e-02 8.1995800e-02 8.1993800e-02 8.1992400e-02 8.1970100e-02 8.1989200e-02 8.1998800e-02 8.1991700e-02 8.1970500e-02 8.2000800e-02 8.1938300e-02 8.1965400e-02 8.1985000e-02 8.1930300e-02 8.1970600e-02
06500e-02 8.1977500e-02 8.2005200e-02 8.2000100e-02 8.1938300e-02 8.1993000e-02 8.1983800e-02 8.1995600e-02 8.1992500e-02 8.1976700e-02 8.2020400e-02 8.1986800e-02 8.1990200e- 02 8.2007100e-02 8.1957500e-02 8.2021900e-02 8.1954900e-02 8.1995800e-02 8.1993800e-02 8.1992400e-02 8.1970100e-02 8.1989200e-02 8.1998800e-02 8.1991700e-02 8.1970500e-02 8.2000800 e-02 8.1938300e-02 8.1965400e-02 8.1985000e-02 8.1930300e-02 8.1970600e-02
the error is stated below. 错误如下所述。
Traceback (most recent call last): File "C:\\WinPython-32bit-2.7.5.3\\python learning files\\python 2.7 DSP\\read_the_input_signal_with_certain_frequency.py", line 65, in y = butter_bandpass_filter(v_numbers, lowcut, highcut, fs, order=6) File "C:\\WinPython-32bit-2.7.5.3\\python learning files\\python 2.7 DSP\\read_the_input_signal_with_certain_frequency.py", line 14, in butter_bandpass_filter y = lfilter(b, a, data) File "C:\\WinPython-32bit-2.7.5.3\\python-2.7.5\\lib\\site-packages\\scipy\\signal\\signaltools.py", line 565, in lfilter return sigtools._linear_filter(b, a, x, axis) 追溯(最近一次通话最近):文件“ C:\\ WinPython-32bit-2.7.5.3 \\ python学习文件\\ python 2.7 DSP \\ read_the_input_signal_with_certain_frequency.py”,第65行,y = butter_bandpass_filter(v_numbers,lowcut,highcut,fs, order = 6)文件“ C:\\ WinPython-32bit-2.7.5.3 \\ python学习文件\\ python 2.7 DSP \\ read_the_input_signal_with_certain_frequency.py”,第14行,在butter_bandpass_filter y = lfilter(b,a,data)中,文件“ C:\\ WinPython-32bit-2.7.5.3 \\ python-2.7.5 \\ lib \\ site-packages \\ scipy \\ signal \\ signaltools.py“,行565,在lfilter返回sigtools._linear_filter(b,a,x,axis)
Thanks 谢谢
In this part of the code: 在这部分代码中:
f = open('NIRS_data.txt','r')
number_string = f.readline()
v_numbers = []
while number_string != '':
numbers = number_string.split()
for number in numbers:
v_numbers.append( number )
number_string = f.readline()
you haven't converted the fields to floating point values, so v_numbers
is a list of strings. 您尚未将字段转换为浮点值,因此
v_numbers
是字符串列表。 The error occurs when you call lfilter
with this list. 当您使用此列表调用
lfilter
时,将发生错误。
You could change the call to append
to 您可以更改呼叫以
append
到
v_numbers.append(float(number))
If each row in the file has the same number of fields, a better solution is to completely replace the code that reads the file with a call to np.loadtxt
. 如果文件中的每一行具有相同数量的字段,则更好的解决方案是使用对
np.loadtxt
的调用来完全替换读取文件的代码。 That is, 那是,
v_numbers = np.loadtxt('NIRS_data.txt').ravel()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.