python低通滤波器
时间: 2023-10-20 08:06:50 浏览: 103
Python中可以使用scipy库中的signal模块来实现低通滤波器。具体步骤如下:
1. 导入必要的库和模块:
```
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
```
2. 定义信号和采样频率:
```
t = np.linspace(0, 1, 1000, endpoint=False)
x = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t) + np.sin(2 * np.pi * 20 * t)
fs = 1000 # 采样频率
```
3. 设计低通滤波器:
```
cutoff_freq = 15 # 截止频率
nyquist_freq = 0.5 * fs # 奈奎斯特频率
normalized_cutoff_freq = cutoff_freq / nyquist_freq # 归一化截止频率
b, a = signal.butter(4, normalized_cutoff_freq, btype='low', analog=False) # 4阶Butterworth低通滤波器
```
4. 使用滤波器对信号进行滤波:
```
filtered_signal = signal.lfilter(b, a, x)
```
5. 绘制原始信号和滤波后的信号:
```
plt.figure()
plt.plot(t, x, label='Original Signal')
plt.plot(t, filtered_signal, label='Filtered Signal')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.legend()
plt.show()
```
阅读全文