Skip to main content
 首页 » 编程设计

python之python 中的带通滤波器

2025年05月04日1052php

我试图在 python 中获得一个带 128 点汉明窗的带通滤波器,截止频率为 0.7-4Hz。我从图像中获取信号样本。 (1 个样本 = 1 张图像)。 fps 经常变化。

这在 python 中如何完成?我读到这个:http://mpastell.com/2010/01/18/fir-with-scipy/但我发现 firwin 相当困惑。如何使用此变量 fps 完成此操作?

请您参考如下方法:

您可以使用函数 scipy.signal.firwin或 scipy.signal.firwin2创建带通 FIR 滤波器。您还可以使用 scipy.signal.remez 设计 FIR 滤波器

以下代码提供了一些用于创建带通 FIR 滤波器的便捷包装器。它使用这些来创建与问题中请求的数字相对应的带通滤波器。这假设采样是均匀完成的。如果采样不均匀,则 FIR 滤波器不合适。

from scipy.signal import firwin, remez, kaiser_atten, kaiser_beta 
 
# Several flavors of bandpass FIR filters. 
 
def bandpass_firwin(ntaps, lowcut, highcut, fs, window='hamming'): 
    nyq = 0.5 * fs 
    taps = firwin(ntaps, [lowcut, highcut], nyq=nyq, pass_zero=False, 
                  window=window, scale=False) 
    return taps 
 
def bandpass_kaiser(ntaps, lowcut, highcut, fs, width): 
    nyq = 0.5 * fs 
    atten = kaiser_atten(ntaps, width / nyq) 
    beta = kaiser_beta(atten) 
    taps = firwin(ntaps, [lowcut, highcut], nyq=nyq, pass_zero=False, 
                  window=('kaiser', beta), scale=False) 
    return taps 
 
def bandpass_remez(ntaps, lowcut, highcut, fs, width): 
    delta = 0.5 * width 
    edges = [0, lowcut - delta, lowcut + delta, 
             highcut - delta, highcut + delta, 0.5*fs] 
    taps = remez(ntaps, edges, [0, 1, 0], Hz=fs) 
    return taps 
 
 
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 = 63.0 
    lowcut = 0.7 
    highcut = 4.0 
 
    ntaps = 128 
    taps_hamming = bandpass_firwin(ntaps, lowcut, highcut, fs=fs) 
    taps_kaiser16 = bandpass_kaiser(ntaps, lowcut, highcut, fs=fs, width=1.6) 
    taps_kaiser10 = bandpass_kaiser(ntaps, lowcut, highcut, fs=fs, width=1.0) 
    remez_width = 1.0 
    taps_remez = bandpass_remez(ntaps, lowcut, highcut, fs=fs, 
                                width=remez_width) 
 
    # Plot the frequency responses of the filters. 
    plt.figure(1, figsize=(12, 9)) 
    plt.clf() 
 
    # First plot the desired ideal response as a green(ish) rectangle. 
    rect = plt.Rectangle((lowcut, 0), highcut - lowcut, 1.0, 
                         facecolor="#60ff60", alpha=0.2) 
    plt.gca().add_patch(rect) 
 
    # Plot the frequency response of each filter. 
    w, h = freqz(taps_hamming, 1, worN=2000) 
    plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="Hamming window") 
 
    w, h = freqz(taps_kaiser16, 1, worN=2000) 
    plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="Kaiser window, width=1.6") 
 
    w, h = freqz(taps_kaiser10, 1, worN=2000) 
    plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="Kaiser window, width=1.0") 
 
    w, h = freqz(taps_remez, 1, worN=2000) 
    plt.plot((fs * 0.5 / np.pi) * w, abs(h), 
             label="Remez algorithm, width=%.1f" % remez_width) 
 
    plt.xlim(0, 8.0) 
    plt.ylim(0, 1.1) 
    plt.grid(True) 
    plt.legend() 
    plt.xlabel('Frequency (Hz)') 
    plt.ylabel('Gain') 
    plt.title('Frequency response of several FIR filters, %d taps' % ntaps) 
 
    plt.show() 

这是脚本生成的情节。当然,在本地运行脚本更有用,因此您可以放大细节。