## 频率响应

### 数字域

freqz 使用基于 FFT 的算法来计算数字滤波器的 Z 变换频率响应。具体来说，语句

[h,w] = freqz(b,a,p) 

$H\left({e}^{j\omega }\right)=\frac{b\left(1\right)+b\left(2\right){e}^{-j\omega }+...+b\left(n+1\right){e}^{-j\omega n}}{a\left(1\right)+a\left(2\right){e}^{-j\omega }+...+a\left(m+1\right){e}^{-j\omega m}}$

freqz 可以接受其他参数，例如采样频率或由任意数量的频率点构成的向量。以下示例求 12 阶 Chebyshev I 类滤波器的 256 点频率响应。调用 freqz 将采样频率 fs 指定为 1000 Hz：

[b,a] = cheby1(12,0.5,200/500); [h,f] = freqz(b,a,256,1000); 

[b,a] = butter(9,400/1000); 

freqz(b,a,256,2000) 

freqz 也可以接受由任意数量的频率点构成的向量，以用于频率响应计算。例如，

w = linspace(0,pi); h = freqz(b,a,w); 

#### 传递函数的频率响应

$H\left(z\right)=\frac{0.05634\left(1+{z}^{-1}\right)\left(1-1.0166{z}^{-1}+{z}^{-2}\right)}{\left(1-0.683{z}^{-1}\right)\left(1-1.4461{z}^{-1}+0.7957{z}^{-2}\right)}.$

b0 = 0.05634; b1 = [1 1]; b2 = [1 -1.0166 1]; a1 = [1 -0.683]; a2 = [1 -1.4461 0.7957]; b = b0*conv(b1,b2); a = conv(a1,a2); [h,w] = freqz(b,a,'whole',2001);

plot(w/pi,20*log10(abs(h))) ax = gca; ax.YLim = [-100 20]; ax.XTick = 0:.5:2; xlabel('Normalized Frequency (\times\pi rad/sample)') ylabel('Magnitude (dB)')

#### FIR 带通滤波器的频率响应

sf1 = 0.1; pf1 = 0.35; pf2 = 0.8; sf2 = 0.9; pb = linspace(pf1,pf2,1e3)*pi; bp = designfilt('bandpassfir', ... 'StopbandAttenuation1',40, 'StopbandFrequency1',sf1,... 'PassbandFrequency1',pf1,'PassbandRipple',3,'PassbandFrequency2',pf2, ... 'StopbandFrequency2',sf2,'StopbandAttenuation2',30); [h,w] = freqz(bp,1024); hpb = freqz(bp,pb); subplot(2,1,1) plot(w/pi,abs(h),pb/pi,abs(hpb),'.-') axis([0 1 -1 2]) legend('Response','Passband','Location','South') ylabel('Magnitude') subplot(2,1,2) plot(w/pi,db(h),pb/pi,db(hpb),'.-') axis([0 1 -60 10]) xlabel('Normalized Frequency (\times\pi rad/sample)') ylabel('Magnitude (dB)')

#### 高通滤波器的幅值响应

[b,a] = butter(3,0.5,'high'); [h,w] = freqz(b,a); dB = mag2db(abs(h)); plot(w/pi,dB) xlabel('\omega / \pi') ylabel('Magnitude (dB)') ylim([-82 5])

fvtool(b,a)

### 模拟域

freqs 计算由两个输入系数向量 ba 定义的模拟滤波器的频率响应。其运算类似于 freqz 的运算；您可以指定要使用的频率点数量，提供由任意数量的频率点构成的向量，并绘制滤波器的幅值和相位响应。此示例说明如何计算和显示模拟频率响应。

#### 模拟 IIR 低通滤波器的比较

n = 5; f = 2e9; [zb,pb,kb] = butter(n,2*pi*f,'s'); [bb,ab] = zp2tf(zb,pb,kb); [hb,wb] = freqs(bb,ab,4096);

[z1,p1,k1] = cheby1(n,3,2*pi*f,'s'); [b1,a1] = zp2tf(z1,p1,k1); [h1,w1] = freqs(b1,a1,4096);

[z2,p2,k2] = cheby2(n,30,2*pi*f,'s'); [b2,a2] = zp2tf(z2,p2,k2); [h2,w2] = freqs(b2,a2,4096);

[ze,pe,ke] = ellip(n,3,30,2*pi*f,'s'); [be,ae] = zp2tf(ze,pe,ke); [he,we] = freqs(be,ae,4096);

plot(wb/(2e9*pi),mag2db(abs(hb))) hold on plot(w1/(2e9*pi),mag2db(abs(h1))) plot(w2/(2e9*pi),mag2db(abs(h2))) plot(we/(2e9*pi),mag2db(abs(he))) axis([0 4 -40 5]) grid xlabel('Frequency (GHz)') ylabel('Attenuation (dB)') legend('butter','cheby1','cheby2','ellip')

Butterworth 和 Chebyshev II 类滤波器具有平坦的通带和宽过渡带。Chebyshev I 类和椭圆滤波器转降更快，但有通带波纹。Chebyshev II 类设计函数的频率输入设置阻带的起点，而不是通带的终点。