我想实现一个名为passfilter的函数,它(获取输入信号、截止频率、滤波器类型和采样率)。对于这类参数,我希望有“低通”(移除频率>;截止,让频率<;=截止通)和“高通”(移除频率<;截止,让频率>;=截止通)选项。我想出了这段代码,但我没有真正的工作
import os
import numpy as np
from matplotlib import pyplot as plt
from scipy import fft
def passfilter(arr, cutoff, kind='lowpass', srate=256):
'''Returns a complex array with a filtered version of the input signal'''
freqs= fft.fftfreq(arr.size, d = srate )
arr = fft.fft(arr)
if kind == 'lowpass': arr [freqs > cutoff] = 0
elif kind == 'highpass': arr [freqs < cutoff] = 0
arr = fft.ifft(arr)
return arr
我用这个测试了它:
xs = np.linspace(0, 10, 1000)
arr = np.sin(xs) + 0.5 * np.sin(xs * 30) + 0.3 * np.sin(xs * 50)
plt.plot(arr, label='original')
plt.plot(passfilter(arr, 5, kind='lowpass').real, label='lowpass')
plt.plot(passfilter(arr, 5, kind='highpass').real, label='highpass')
plt.legend()
plt.show()
但是对于高通,我只得到一条0的静态线,对于低通,得到原始函数。 提前谢谢
目前没有回答
相关问题 更多 >
编程相关推荐