1 Star 3 Fork 2

凌逆战/PyEqualizer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
scipy_filter_family.ipynb 77.13 KB
一键复制 编辑 原始数据 按行查看 历史
凌逆战 提交于 2年前 . first commit
# -*- coding:utf-8 -*-
# Author:凌逆战 | Never
# Date: 2023/8/12
"""
画出scipy.signal.butter函数的滤波器响应曲线
"""
import numpy as np
from scipy.signal import butter, freqz
import matplotlib.pyplot as plt
def plot_filter_response(b, a, title, subplot, cutoff=None, lowcut=None, highcut=None):
    w, h = freqz(b, a, worN=8000)
    subplot.plot(0.5*fs*w/np.pi, np.abs(h), 'b')
    if cutoff:
        subplot.axvline(cutoff, color='r', ls='--')
    if lowcut:
        subplot.axvline(lowcut, color='r', ls='--')
    if highcut:
        subplot.axvline(highcut, color='r', ls='--')
    subplot.set_title(title)
    subplot.set_xlabel('Frequency (Hz)')
    subplot.set_ylabel('Gain')
    subplot.grid(True)

fs = 6000.0  # Sample rate (Hz)
cutoff = 1000.0  # Desired cutoff frequency (Hz)

fig, axs = plt.subplots(1, 4, figsize=(20, 5))

# Lowpass filter
b, a = butter(5, cutoff / (0.5 * fs), btype='low')
plot_filter_response(b, a, 'Lowpass filter', axs[0], cutoff=cutoff)

# Highpass filter
b, a = butter(5, cutoff / (0.5 * fs), btype='high')
plot_filter_response(b, a, 'Highpass filter', axs[1], cutoff=cutoff)

# Bandpass filter
lowcut = 500.0
highcut = 1500.0
b, a = butter(5, [lowcut / (0.5 * fs), highcut / (0.5 * fs)], btype='band')
plot_filter_response(b, a, 'Bandpass filter', axs[2], lowcut=lowcut, highcut=highcut)

# Bandstop filter
b, a = butter(5, [lowcut / (0.5 * fs), highcut / (0.5 * fs)], btype='bandstop')
plot_filter_response(b, a, 'Bandstop filter', axs[3], lowcut=lowcut, highcut=highcut)

plt.tight_layout()
plt.show()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LXP-Never/py-equalizer.git
git@gitee.com:LXP-Never/py-equalizer.git
LXP-Never
py-equalizer
PyEqualizer
master

搜索帮助