我有一个与this post直接相关的新问题-在Python内建的,我有一个具有给定特性的二阶IIR带通滤波器[以下代码是有意使用的]:
fs = 40e6 # 40 MHz f sample frequency
fc = 1e6/fs # 1 MHz center cutoff
BW = 20e3/fs # 20 kHz bandwidth
fl = (fc - BW/2)/fs # 0.99 MHz f lower cutoff
fh = (fc + BW/2)/fs # 1.01 MHz f higher cutoff
给出系数:
R = 1 - (3*BW)
K = (1 - 2*R*np.cos(2*np.pi*fc) + (R*R)) / (2 - 2*np.cos(2*np.pi*fc))
a0 = 1 - K # a0 = 0.00140
a1 = 2*(K-R)*np.cos(2*np.pi*fc) # a1 = 0.00018
a2 = (R*R) - K # a2 = -0.00158
b1 = 2*R*np.cos(2*np.pi*fc) # b1 = 1.97241
b2 = -(R*R) # b2 = -0.99700
正如ukrutt in the previous post所建议的那样,我使用了scipy.signal.freqz,但可悲的是没有得到我一直在寻找的响应-表示我相信过滤器正在按预期方式工作(下面的代码)。这是freqz的结果:
我的问题是:如何生成更像预期响应的图?
代码:
a = [0.0014086232031758072, 0.00018050359364826498, -0.001589126796824103]
b = [1.9724136161684902, -0.9970022500000001]
w,h = signal.freqz(a, b)
h_dB = 20 * np.log10(np.abs(h))
plt.plot(w/np.max(w),h_dB)
plt.grid()
最佳答案
使用线性x缩放比例,您将看不到任何漂亮的东西。我不知道numpy,但我熟悉matlab,并且有一些函数可以在日志中进行绘图。尝试对以下情况使用x对数刻度:
import matplotlib.pyplot as pyplot
fig = pyplot.figure()
ax = fig.add_subplot(2,1,1)
line, = ax.plot(w/np.max(w), h_dB, color='blue', lw=2)
ax.set_xscale('log')
show()
我还没有测试过它,我没有安装python :(
编辑:
我试图在Matlab中为IIR滤波器4阶和一个IIR滤波器20阶建模巴特沃斯滤波器。
%!/usr/local/bin/matlab
%% Inputs
fs = 40e6;
fc = 1e6;
BW = 20e3;
fl = (fc - BW/2);
fh = (fc + BW/2);
%% Build bandpass filter IIR Butterworth order 4
N = 4; % Filter Order
h = fdesign.bandpass('N,F3dB1,F3dB2', N, fl, fh, fs);
Hd1 = design(h, 'butter');
%% Build bandpass filter IIR Butterworth order 50
N = 20; % Filter Order
h = fdesign.bandpass('N,F3dB1,F3dB2', N, fl, fh, fs);
Hd2 = design(h, 'butter');
%% Compare
fvtool(Hd1,Hd2);
这是第一个滤波器的系数A和B:
FilterStructure: 'Direct-Form II Transposed'
A: [2.46193004641106e-06 0 -4.92386009282212e-06 0 2.46193004641106e-06]
B: [1 -3.94637005453608 5.88902106889851 -3.93761314372475 0.995566972065978]
如果有时间,我将尝试使用numpy进行相同的操作!