代码拉取完成,页面将自动刷新
# -*- 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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。