FIR低通滤波器的设计代码
时间: 2023-07-27 19:07:09 浏览: 247
下面是一种基于窗函数法的FIR低通滤波器设计代码示例:
```python
import numpy as np
from scipy.signal import firwin, lfilter
def fir_lowpass(cutoff, fs, numtaps=101, window='hamming'):
nyq = 0.5 * fs
cutoff_norm = cutoff / nyq
taps = firwin(numtaps, cutoff_norm, window=window, pass_zero=True)
return taps
def fir_lowpass_filter(data, cutoff, fs, numtaps=101, window='hamming'):
taps = fir_lowpass(cutoff, fs, numtaps=numtaps, window=window)
y = lfilter(taps, 1.0, data)
return y
```
其中,`fir_lowpass`函数用于计算FIR低通滤波器的系数,`fir_lowpass_filter`函数用于对数据进行滤波。
使用示例:
```python
# 采样频率
fs = 1000.0
# 截止频率
cutoff = 100.0
# 生成一组随机信号
t = np.linspace(0, 1, 1000, False) # 1秒钟,采样频率1000Hz
data = np.sin(2*np.pi*5*t) + np.sin(2*np.pi*50*t) + np.random.randn(len(t)) * 0.2
# 滤波
filtered_data = fir_lowpass_filter(data, cutoff, fs, numtaps=101, window='hamming')
```
可以根据具体需求修改截止频率、采样频率、滤波器阶数(即系数个数)和窗函数类型等参数。
阅读全文