100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python带通滤波_python中的带通滤波器

python带通滤波_python中的带通滤波器

时间:2018-07-07 06:50:29

相关推荐

python带通滤波_python中的带通滤波器

可以使用函数^{}或^{}创建带通FIR滤波器。也可以使用^{}设计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()

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。