# 数字滤波器设计实践介绍

### FIR 滤波器设计

• 实际的 FIR 设计通常包括过渡带宽度和最大通带和阻带波纹不超过允许值的滤波器。除了这些设计设定之外，还必须选择滤波器阶数，它等效于选择截断的冲激响应的长度。

FIR 滤波器广受欢迎，因为它们非常稳定，并可以设计成具有线性相位的滤波器。尽管如此，这些滤波器仍可能有长瞬时响应，在某些应用中的计算成本可能很高。

```Fpass = 0.37; Fstop = 0.43; Ap = 1; Ast = 30; d = designfilt("lowpassfir", ... PassbandFrequency=Fpass, ... StopbandFrequency=Fstop, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast); fa = filterAnalyzer(d,FilterNames="Equiripple_Design");```

`N = filtord(d)`
```N = 39 ```

`info(d)`
```ans = 25x32 char array 'FIR Digital Filter (real) ' '------------------------- ' 'Filter Length : 40 ' 'Stable : Yes ' 'Linear Phase : Yes (Type 2) ' ' ' 'Design Method Information ' 'Design Algorithm : Equiripple ' ' ' 'Design Options ' 'Density Factor : 16 ' 'Maximum Phase : false ' 'Minimum Order : any ' 'Minimum Phase : false ' 'Stopband Decay : 0 ' 'Stopband Shape : flat ' 'Uniform Grid : true ' ' ' 'Design Specifications ' 'Sample Rate : 2 (normalized)' 'Response : Lowpass ' 'Passband Ripple : 1 dB ' 'Stopband Atten. : 30 dB ' 'Passband Edge : 0.37 ' 'Stopband Edge : 0.43 ' ```
• 默认情况下，`designfilt` 函数会选择一个等波纹设计算法。线性相位等波纹滤波器是令人满意的，因为对于给定阶数，这种滤波器与理想滤波器的最大可能偏差最小。

```dk = designfilt("lowpassfir", ... PassbandFrequency=Fpass, ... StopbandFrequency=Fstop, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast, ... DesignMethod="kaiserwin"); addFilters(fa,dk,FilterNames="Kaiser_Window_Design");```

`N = filtord(dk)`
```N = 52 ```

```Fpass = 370; Fstop = 430; Ap = 1; Ast = 30; Fs = 2000; d = designfilt("lowpassfir", ... PassbandFrequency=Fpass, ... StopbandFrequency=Fstop, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast, ... SampleRate=Fs); filterAnalyzer(d)```

```N = 30; Fpass = 370; Fstop = 430; Fs = 2000; % Design method defaults to "equiripple" when omitted deq = designfilt("lowpassfir", ... FilterOrder=N, ... PassbandFrequency=Fpass, ... StopbandFrequency=Fstop, ... SampleRate=Fs); dls = designfilt("lowpassfir", ... FilterOrder=N, ... PassbandFrequency=Fpass, ... StopbandFrequency=Fstop, ... DesignMethod="ls", ... SampleRate=Fs); filterAnalyzer(deq,dls,FilterNames=["Equiripple_Design_2" "Least_Squares_Design_2"])```

• 如果您要尽可能降低某个频带内信号的能量，请使用最小二乘设计。

```deqw = designfilt("lowpassfir", ... FilterOrder=N, ... PassbandFrequency=Fpass, ... StopbandFrequency=Fstop, ... PassbandWeight=1, ... StopbandWeight=10, ... SampleRate=Fs); filterAnalyzer(deq,deqw,FilterNames=["Equiripple_Design_3" "Equiripple_Design_With_Weighted_Stopband"])```

• 您可以使用不同窗口来控制阻带衰减，同时保持滤波器阶数不变。

```dhamming = designfilt("lowpassfir", ... FilterOrder=100, ... CutoffFrequency=60, ... SampleRate=1000, ... Window="hamming"); dchebwin = designfilt("lowpassfir", ... FilterOrder=100, ... CutoffFrequency=60, ... SampleRate=1000, ... Window={"chebwin",90}); filterAnalyzer(dhamming,dchebwin,FilterNames=["Hamming_Window" "Chebyshev_Window"])```

### IIR 滤波器设计

FIR 滤波器的缺点之一是它们需要很大的滤波器阶数才能满足某些设计设定。如果波纹保持不变，滤波器阶数与过渡带宽度成反比。通过使用反馈，使用小得多的滤波器阶数即可满足一组设计设定。这就是 IIR 滤波器设计背后的思想。“无限冲激响应” (IIR) 一词源于这样的事实：当冲激施加到滤波器时，输出永远不会衰减到零。

• 当计算资源非常宝贵时，IIR 滤波器非常有用。然而，稳定的因果 IIR 滤波器无法提供完美的线性相位。在要求相位线性的情况下，避免使用 IIR 设计。

```Fp = 100; Fst = 300; Ap = 1; Ast = 60; Fs = 2e3; dbutter = designfilt("lowpassiir", ... PassbandFrequency=Fp, ... StopbandFrequency=Fst, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast, ... SampleRate=Fs, ... DesignMethod="butter");```

• 巴特沃斯和切比雪夫 I 型滤波器都具有最平坦的阻带。对于给定的滤波器阶数，需要在通带波纹和过渡带宽度之间进行权衡。

```dcheby1 = designfilt("lowpassiir", ... PassbandFrequency=Fp, ... StopbandFrequency=Fst, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast, ... SampleRate=Fs, ... DesignMethod="cheby1");```

• 切比雪夫 II 型滤波器具有最平坦的通带和等波纹阻带。

```dcheby2 = designfilt("lowpassiir", ... PassbandFrequency=Fp, ... StopbandFrequency=Fst, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast, ... DesignMethod="cheby2", ... SampleRate=Fs);```

• 椭圆滤波器能够以最小的阶数获得给定的过渡带宽度。

```dellip = designfilt("lowpassiir", ... PassbandFrequency=Fp, ... StopbandFrequency=Fst, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast, ... DesignMethod="ellip", ... SampleRate=Fs);```

• 对于相同的设定约束，巴特沃斯方法产生最高阶，椭圆方法产生最小阶。

`FilterOrders = [filtord(dbutter) filtord(dcheby1) filtord(dcheby2) filtord(dellip)]`
```FilterOrders = 1×4 7 5 5 4 ```
```fa = filterAnalyzer(dbutter,dcheby1,dcheby2,dellip, ... FilterNames=["Butterworth","Chebyshev_Type_I" "Chebyshev_Type_II" "Elliptic"]); zoom(fa,"xy",[0 1e3 -80 2])```

`zoom(fa,"xy",[0 150 -3 2])`

• 使用 "`MatchExactly"` 参数来约束设计算法，使其与一个频带完全匹配。另一个频带超出其设定。

```dellip1 = designfilt("lowpassiir", ... PassbandFrequency=Fp, ... StopbandFrequency=Fst, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast, ... DesignMethod="ellip", ... SampleRate=Fs, ... MatchExactly="passband"); dellip2 = designfilt("lowpassiir", ... PassbandFrequency=Fp, ... StopbandFrequency=Fst, ... PassbandRipple=Ap, ... StopbandAttenuation=Ast, ... DesignMethod="ellip", ... SampleRate=Fs, ... MatchExactly="stopband"); fa = filterAnalyzer(dellip,dellip1,dellip2, ... FilterNames=["Matched_Passband_And_Stopband" ... "Matched_Passband" "Matched_Stopband"]);```

`zoom(fa,"xy",[0 1e3 -80 2])`

• 如果关注相位问题，请记住巴特沃斯和切比雪夫 II 型设计具有最平坦的群延迟，因此引入的失真最小。

```fa = filterAnalyzer(dbutter,dcheby1,dcheby2,dellip,Analysis="groupdelay", ... FilterNames=["Butterworth_1" "Chebyshev_Type_I_2" "Chebyshev_Type_II_2" "Elliptic_2"]);```