## IIR 滤波器设计

### IIR 滤波器方法概述

`yulewalk`

`maxflat`

### 使用模拟原型的经典 IIR 滤波器设计

#### 完成经典 IIR 滤波器设计

Bessel（仅模拟）

`[b,a] = besself(n,Wn,options)`

`[z,p,k] = besself(n,Wn,options)`

`[A,B,C,D] = besself(n,Wn,options)`

`[b,a] = butter(n,Wn,options)`

`[z,p,k] = butter(n,Wn,options)`

`[A,B,C,D] = butter(n,Wn,options)`

`[b,a] = cheby1(n,Rp,Wn,options)`

`[z,p,k] = cheby1(n,Rp,Wn,options)`

`[A,B,C,D] = cheby1(n,Rp,Wn,options)`

`[b,a] = cheby2(n,Rs,Wn,options)`

`[z,p,k] = cheby2(n,Rs,Wn,options)`

`[A,B,C,D] = cheby2(n,Rs,Wn,options)`

`[b,a] = ellip(n,Rp,Rs,Wn,options)`

`[z,p,k] = ellip(n,Rp,Rs,Wn,options)`

`[A,B,C,D] = ellip(n,Rp,Rs,Wn,options)`

```[b,a] = butter(5,0.4); % Lowpass Butterworth [b,a] = cheby1(4,1,[0.4 0.7]); % Bandpass Chebyshev Type I [b,a] = cheby2(6,60,0.8,'high'); % Highpass Chebyshev Type II [b,a] = ellip(3,1,60,[0.4 0.7],'stop'); % Bandstop elliptic ```

```[b,a] = butter(5,0.4,'s'); % Analog Butterworth filter ```

#### 按照频域设定设计 IIR 滤波器

`[n,Wn] = buttord(Wp,Ws,Rp,Rs)`

`[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs)`

`[n,Wn] = cheb2ord(Wp,Ws,Rp,Rs)`

`[n,Wn] = ellipord(Wp,Ws,Rp,Rs)`

```[n,Wn] = buttord([1000 2000]/5000,[500 2500]/5000,1,60) [b,a] = butter(n,Wn);```
```n = 12 Wn = 0.1951 0.4080```

```[n,Wn] = ellipord([1000 2000]/5000,[500 2500]/5000,1,60) [b,a] = ellip(n,1,60,Wn);```
```n = 5 Wn = 0.2000 0.4000```

### 经典 IIR 滤波器类型的比较

#### 贝塞尔滤波器

```[z,p,k] = ellipap(5,0.5,20); w = logspace(-1,1,1000); h = freqs(k*poly(z),poly(p),w); semilogx(w,abs(h)), grid xlabel('Frequency (rad/s)') ylabel('Magnitude')```

#### 直接 IIR 滤波器设计

`yulewalk` 函数通过拟合指定的频率响应来设计递归 IIR 数字滤波器。`yulewalk` 的名称反映其求滤波器分母系数的方法：它求理想的指定幅值平方响应的逆 FFT，并使用所得的自相关函数样本求解修正的尤尔-沃克方程。以下语句

```[b,a] = yulewalk(n,f,m) ```

```m = [0 0 1 1 0 0 1 1 0 0]; f = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 1]; [b,a] = yulewalk(10,f,m); [h,w] = freqz(b,a,128) plot(f,m,w/pi,abs(h)) ```

#### 广义巴特沃斯滤波器设计

`[b,a] = maxflat(3,3,0.25)`
```b = 0.0317 0.0951 0.0951 0.0317 a = 1.0000 -1.4590 0.9104 -0.1978```
`[b,a] = butter(3,0.25)`
```b = 0.0317 0.0951 0.0951 0.0317 a = 1.0000 -1.4590 0.9104 -0.1978```

`[b,a] = maxflat(3,1,0.25)`
```b = 0.0950 0.2849 0.2849 0.0950 a = 1.0000 -0.2402```

`maxflat` 的第三个输入是半功率频率，该频率介于 0 和 1 之间，幅值响应为 $1/\sqrt{2}$

`maxflat(4,'sym',0.3)`
```ans = 0.0331 0.2500 0.4337 0.2500 0.0331```