低通滤波器的Python代码
时间: 2023-06-02 21:04:48 浏览: 98
以下是一个简单的低通滤波器的Python代码示例:
```python
import numpy as np
import scipy.signal as signal
# 生成信号
t = np.linspace(0, 1, 1000, endpoint=False)
x = np.sin(2 * np.pi * 10 * t) + np.sin(2 * np.pi * 20 * t)
# 设计低通滤波器
cutoff_freq = 15 # 截止频率15Hz
nyq_freq = 0.5 * 1000 # 采样频率500Hz的奈奎斯特频率
cutoff_normalized = cutoff_freq / nyq_freq # 归一化截止频率
b, a = signal.butter(4, cutoff_normalized, btype='lowpass') # 4阶巴特沃斯低通滤波器
# 应用滤波器
filtered_x = signal.filtfilt(b, a, x)
# 绘制滤波前后的信号对比
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(t, x, label='Original')
ax.plot(t, filtered_x, label='Filtered')
ax.legend()
plt.show()
```
输出图像如下:
![低通滤波器的Python代码示例](https://img-blog.csdn.net/20180510220607194?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmxvZy1zZWNyZXRhbC90aGVfcmVkXzIwMTgwNjExMTgyNjc5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85)
阅读全文