我使用以下代码在python中实现了高通滤波器:
from scipy.signal import butter, filtfilt
import numpy as np
def butter_highpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='high', analog=False)
return b, a
def butter_highpass_filter(data, cutoff, fs, order=5):
b, a = butter_highpass(cutoff, fs, order=order)
y = filtfilt(b, a, data)
return y
rawdata = np.loadtxt('sampleSignal.txt', skiprows=0)
signal = rawdata
fs = 100000.0
cutoff = 100
order = 6
conditioned_signal = butter_highpass_filter(signal, cutoff, fs, order)
我将此滤波器应用于100 kHz电压信号,并且对于截止频率> = 60 Hz它可以正常工作。但它在下面不起作用。我想切断低于10赫兹的所有频率。我的错误是什么提示?我观察到的是滤波器的阶数越低,截止频率就越低。
答案 0 :(得分:12)
我希望这可以帮到你:
isset()
答案 1 :(得分:3)
由于我的声誉很低,我无法对你的问题发表评论 - “截止日期和过滤顺序之间有什么关系?”这不是您原来问题的答案。
对于FIR滤波器,对于给定的截止频率,对于更高阶滤波器,脉冲响应曲线的斜率(| H(f)| vs f)更陡峭。因此,为了在不需要的频率范围内实现更高的衰减,可以增加滤波器的阶数。但是当滤波器阶数如此之高以至于脉冲响应是理想的盒函数时会发生什么?您将看到符号间干扰(数字通信中的ISI)效果。当截止频率与采样频率的比率变小时,该效应的强度增加(考虑频域中的盒函数的宽度与sinc函数的主瓣的宽度之间的关系)。
当我尝试在TI DSP微控制器上实现一个非常窄的频带低通IIR滤波器时,我首先观察到了这一点。 TI库将滤波器实现为级联双四元结构,以处理众所周知的截断效应。这仍然没有解决问题,因为问题不仅仅是因为截断。我解决这个问题的方法是使用抗混叠滤波器,然后对输入信号进行下采样,然后使用我想要的低通IIR滤波器。
据我所知,您正在实施HPF,这是一种在频域中翻译的LPF。希望这能回答你的一些问题。如果下采样适合您,请告诉我。
答案 2 :(得分:0)
我添加了用于验证以上Konstantin Purtov代码的函数,因此您可以看到顺序和截止频率之间的关系。该代码主要来自Warren Weckesser。
import scipy
import sys
from scipy import signal
from scipy import pi
from scipy.io.wavfile import write
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import butter, lfilter, freqz
# ----- ----- ----- -----
def butter_highpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
return b, a
def butter_highpass_filter(data, cutoff, fs, order=5):
b, a = butter_highpass(cutoff, fs, order=order)
y = signal.filtfilt(b, a, data)
return y
# ----- -----
# (1)
def foo(sel):
if (sel == 1):
# Filter requirements.
order = 6
fs = 300.0 # sample rate, Hz
cutoff = 10 # desired cutoff frequency of the filter, Hz
# Get the filter coefficients so we can check its frequency response.
b, a = butter_highpass(cutoff, fs, order)
# Plot the frequency response.
w, h = freqz(b, a, worN=8000)
plt.subplot(2, 1, 1)
plt.plot(0.5 * fs * w / np.pi, np.abs(h), 'b')
plt.plot(cutoff, 0.5 * np.sqrt(2), 'ko')
plt.axvline(cutoff, color='k')
plt.xlim(0, 0.5 * fs)
plt.title("High Filter Frequency Response")
plt.xlabel('Frequency [Hz]')
plt.grid()
# Demonstrate the use of the filter.
# First make some data to be filtered.
T = 0.5 # seconds
n = int(T * fs) # total number of samples
t = np.linspace(0, T, n, endpoint=False)
# "Noisy" data. We want to recover the 20 Hz signal from this.
data = np.sin(1.2 * 2 * np.pi * t) + 1.5 * np.cos(5 * 2 * np.pi * t) + 0.5 * np.sin(20.0 * 2 * np.pi * t)
# Filter the data, and plot both the original and filtered signals.
y = butter_highpass_filter(data, cutoff, fs, order)
plt.subplot(2, 1, 2)
plt.plot(t, data, 'b-', label='data')
plt.plot(t, y, 'g-', linewidth=2, label='filtered data')
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
plt.subplots_adjust(hspace=0.35)
plt.show()
else:
print ('Please, choose among choices, thanks.')
# ----- -----
def main():
sel = int (sys.argv[1])
foo(sel)
# ----- ----- ----- ----- ----- -----
if __name__ == '__main__':
main()