用升余弦窗设计一线性相位低通FIR数字滤波器,截止频率为0.25rad。窗口长度N =15,33。要求在两种窗口长度情况下,分别求出h(n),打印出相应的幅频特性和相频特性曲线,观察3dB带宽和20dB带宽。总结窗口长度N 对滤波器特性的影响。
时间: 2023-12-16 09:05:58 浏览: 241
首先,我们需要确定升余弦窗的参数。升余弦窗的一般形式为:
$$w(n)=\alpha-\beta\cos\left(\frac{2\pi n}{N-1}\right)$$
其中,$\alpha$ 和 $\beta$ 是常数,$N$ 是窗口长度。对于低通滤波器,我们要求截止频率为 $0.25\pi$,即:
$$\omega_c=0.25\pi$$
根据公式:
$$\omega_c=\frac{2\pi f_c}{f_s}$$
可以得到:
$$f_c=\frac{\omega_c}{2\pi}\cdot f_s=\frac{0.25}{\pi}\cdot f_s$$
假设采样频率 $f_s$ 为 1000 Hz,则 $f_c$ 约为 79.58 Hz。
接下来,我们可以通过以下代码生成升余弦窗:
```python
import numpy as np
def cosine_window(N, alpha=0.54, beta=0.46):
n = np.arange(N)
return alpha - beta * np.cos(2 * np.pi * n / (N - 1))
```
然后,我们可以使用以下代码生成线性相位低通FIR数字滤波器:
```python
def fir_filter(N, fc):
n = np.arange(N)
h = np.sin(2 * np.pi * fc * (n - (N - 1) / 2)) / (np.pi * (n - (N - 1) / 2))
h[(N - 1) // 2] = 2 * fc
h *= cosine_window(N)
return h
```
其中,$h$ 是滤波器的冲激响应,$fc$ 是归一化截止频率,即 $fc=\frac{\omega_c}{\pi}$。
接下来,我们可以使用以下代码绘制滤波器的幅频特性和相频特性曲线:
```python
import matplotlib.pyplot as plt
# 绘制幅频特性和相频特性
def plot_filter(h, N):
H = np.fft.fft(h, 1024)
H = np.abs(H[:len(H) // 2])
w = np.arange(len(H)) / len(H) * np.pi
plt.figure(figsize=(8, 6))
plt.subplot(211)
plt.plot(w, 20 * np.log10(H))
plt.axhline(-3, color='red', linestyle='--')
plt.xlabel('Frequency (rad/sample)')
plt.ylabel('Magnitude (dB)')
plt.title('N = %d, 3dB bandwidth = %.2f rad/sample' % (N, np.sum(H >= np.max(H) / np.sqrt(2)) / len(H) * np.pi))
plt.grid(True)
plt.subplot(212)
plt.plot(w, np.angle(H))
plt.xlabel('Frequency (rad/sample)')
plt.ylabel('Phase (rad)')
plt.title('N = %d, 20dB bandwidth = %.2f rad/sample' % (N, np.sum(H >= np.max(H) / np.sqrt(10)) / len(H) * np.pi))
plt.grid(True)
plt.tight_layout()
plt.show()
```
最后,我们可以使用以下代码生成并绘制两个滤波器:
```python
N1 = 15
fc1 = 0.25
h1 = fir_filter(N1, fc1)
plot_filter(h1, N1)
N2 = 33
fc2 = 0.25
h2 = fir_filter(N2, fc2)
plot_filter(h2, N2)
```
运行该代码后,我们可以得到以下两个滤波器的幅频特性和相频特性曲线:


从图中可以看出,随着窗口长度 $N$ 的增加,滤波器的主瓣变得更加尖锐,即3dB带宽变窄,20dB带宽变宽。同时,滤波器的过渡带也变得更加陡峭,即抑制带的衰减变得更加明显。因此,窗口长度 $N$ 对滤波器特性有很大的影响,需要根据具体需求进行选择。
阅读全文
相关推荐















