基于带通滤波器here,我尝试使用以下代码制作一个多波段滤波器。然而,滤波后的信号接近零,这影响了频谱绘制的结果。每个波段的滤波器系数是否应该归一化?请有人建议我如何修复滤波器?
from scipy.signal import butter, sosfreqz, sosfilt
from scipy.signal import spectrogram
import matplotlib
import matplotlib.pyplot as plt
from scipy.fft import fft
import numpy as np
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
sos = butter(order, [low, high], analog=False, btype='band', output='sos')
return sos
def multiband_filter(data, bands, fs, order=10):
sos_list = []
for lowcut, highcut in bands:
sos = butter_bandpass(lowcut, highcut, fs, order=order)
scalar = max(abs(fft(sos, 2000)))
# sos = sos / scalar
sos_list += [sos]
# sos_list = [butter_bandpass(lowcut, highcut, fs, order=order) for lowcut, highcut in bands]
# Combine filters into a single filter
sos = np.vstack(sos_list)
# Apply the multiband filter to the data
y = sosfilt(sos, data)
return y, sos_list
def get_toy_signal():
t = np.arange(0, 0.3, 1 / fs)
fq = [-np.inf] + [x / 12 for x in range(-9, 3, 1)]
mel = [5, 3, 1, 3, 5, 5, 5, 0, 3, 3, 3, 0, 5, 8, 8, 0, 5, 3, 1, 3, 5, 5, 5, 5, 3, 3, 5, 3, 1]
acc = [5, 0, 8, 0, 5, 0, 5, 5, 3, 0, 3, 3, 5, 0, 8, 8, 5, 0, 8, 0, 5, 5, 5, 0, 3, 3, 5, 0, 1]
toy_signal = np.array([])
for kj in range(len(mel)):
note_signal = np.sum([np.sin(2 * np.pi * 440 * 2 ** ff * t)
for ff in [fq[acc[kj]] - 1, fq[acc[kj]], fq[mel[kj]] + 1]], axis=0)
zeros = np.zeros(int(0.01 * fs))
toy_signal = np.concatenate((toy_signal, note_signal, zeros))
toy_signal += np.random.normal(0, 1, len(toy_signal))
toy_signal = toy_signal / (np.max(np.abs(toy_signal)) + 0.1)
t_toy_signal = np.arange(len(toy_signal)) / fs
return t_toy_signal, toy_signal
if __name__ == "__main__":
fontsize = 12
# Sample rate and desired cut_off frequencies (in Hz).
fs = 3000
f1, f2 = 100, 200
f3, f4 = 470, 750
f5, f6 = 800, 850
f7, f8 = 1000, 1000.1
cut_off = [(f1, f2), (f3, f4), (f5, f6), (f7, f8)]
# cut_off = [(f1, f2), (f3, f4)]
# cut_off = [(f1, f2)]
# cut_off = [f1]
t_toy_signal, toy_signal = get_toy_signal()
# toy_signal -= np.mean(toy_signal)
# t_toy_signal = wiener(t_toy_signal)
fig, ax = plt.subplots(6, 1, figsize=(8, 12))
fig.tight_layout()
ax[0].plot(t_toy_signal, toy_signal)
ax[0].set_title('Original toy_signal', fontsize=fontsize)
ax[0].set_xlabel('Time (s)', fontsize=fontsize)
ax[0].set_ylabel('Magnitude', fontsize=fontsize)
ax[0].set_xlim(left=0, right=max(t_toy_signal))
sos_list = [butter_bandpass(lowcut, highcut, fs, order=10) for lowcut, highcut in cut_off]
# Combine filters into a single filter
sos = np.vstack(sos_list)
# w *= 0.5 * fs / np.pi # Convert w to Hz.
#####################################################################
# First plot the desired ideal response as a green(ish) rectangle.
#####################################################################
# Plot the frequency response
for i in range(len(cut_off)):
w, h = sosfreqz(sos_list[i], worN=2000)
ax[1].plot(0.5 * fs * w / np.pi, np.abs(h), label=f'Band {i + 1}: {cut_off[i]} Hz')
ax[1].set_title('Multiband Filter Frequency Response')
ax[1].set_xlabel('Frequency [Hz]')
ax[1].set_ylabel('Gain')
ax[1].legend()
# ax[1].set_xlim(0, max(*cut_off) + 100)
#####################################################################
# Spectrogram of original signal
#####################################################################
f, t, Sxx = spectrogram(toy_signal, fs,
nperseg=930, noverlap=0)
ax[2].pcolormesh(t, f, np.abs(Sxx),
norm=matplotlib.colors.LogNorm(vmin=np.min(Sxx), vmax=np.max(Sxx)),
)
ax[2].set_title('Spectrogram of original toy_signal', fontsize=fontsize)
ax[2].set_xlabel('Time (s)', fontsize=fontsize)
ax[2].set_ylabel('Frequency (Hz)', fontsize=fontsize)
#####################################################################
# Compute filtered signal
#####################################################################
# Apply the multiband filter to the data
# toy_signal_filtered = sosfilt(sos, toy_signal)
toy_signal_filtered = np.sum([sosfilt(sos, toy_signal) for sos in sos_list], axis=0)
#####################################################################
# Spectrogram of filtered signal
#####################################################################
f, t, Sxx = spectrogram(toy_signal_filtered, fs,
nperseg=930, noverlap=0)
ax[3].pcolormesh(t, f, np.abs(Sxx),
norm=matplotlib.colors.LogNorm(vmin=np.min(Sxx),
vmax=np.max(Sxx))
)
ax[3].set_title('Spectrogram of filtered toy_signal', fontsize=fontsize)
ax[3].set_xlabel('Time (s)', fontsize=fontsize)
ax[3].set_ylabel('Frequency (Hz)', fontsize=fontsize)
ax[4].plot(t_toy_signal, toy_signal_filtered)
ax[4].set_title('Filtered toy_signal', fontsize=fontsize)
ax[4].set_xlim(left=0, right=max(t_toy_signal))
ax[4].set_xlabel('Time (s)', fontsize=fontsize)
ax[4].set_ylabel('Magnitude', fontsize=fontsize)
N = 1512
X = fft(toy_signal, n=N)
Y = fft(toy_signal_filtered, n=N)
# fig.set_size_inches((10, 4))
ax[5].plot(np.arange(N) / N * fs, 20 * np.log10(abs(X)), 'r-', label='FFT original signal')
ax[5].plot(np.arange(N) / N * fs, 20 * np.log10(abs(Y)), 'g-', label='FFT filtered signal')
ax[5].set_xlim(xmax=fs / 2)
ax[5].set_ylim(ymin=-20)
ax[5].set_ylabel(r'Power Spectrum (dB)', fontsize=fontsize)
ax[5].set_xlabel("frequency (Hz)", fontsize=fontsize)
ax[5].grid()
ax[5].legend(loc='upper right')
plt.tight_layout()
plt.show()
plt.figure()
# fig.set_size_inches((10, 4))
plt.plot(np.arange(N) / N * fs, 20 * np.log10(abs(X)), 'r-', label='FFT original signal')
plt.plot(np.arange(N) / N * fs, 20 * np.log10(abs(Y)), 'g-', label='FFT filtered signal')
plt.xlim(xmax=fs / 2)
plt.ylim(ymin=-20)
plt.ylabel(r'Power Spectrum (dB)', fontsize=fontsize)
plt.xlabel("frequency (Hz)", fontsize=fontsize)
plt.grid()
plt.legend(loc='upper right')
plt.tight_layout()
plt.show()
字符串
的数据
以下是使用@Warren Weckesser
后的注解:
toy_signal_filtered = np.mean([sosfilt(sos, toy_signal) for sos in sos_list], axis=0)
型
的数据
以下是使用@Warren Weckesser
后的评论:
toy_signal_filtered = np.sum([sosfilt(sos, toy_signal) for sos in sos_list], axis=0)
型
的
下面是一个使用窄带的示例:
的
2条答案
按热度按时间9ceoxa921#
Warren在评论中写的是一种更好的推荐方法,只需计算单独带通滤波信号的总和。
话虽如此,对于想要创建和应用 * 单 * 多波段滤波器的人来说,他可以尝试通过combining filters来实现这一点:
但要使其稳定可能很困难(小心滤波器阶数),使其陡峭也更难。
我觉得这很有趣,我自己也试过:
字符串
SOS版本
型
100d 1xx 1c 1d 1x的字符串
正如你所看到的,滤波器的斜率不是很陡。
swvgeqrz2#
你的滤波信号得到“乘”由对方,因为你计算他们在系列.在我下面的代码中,我改变了你的代码,所以它计算你的filtered_signals一个接一个,然后把它们一个接一个地绘制在你的图.我希望这就是你试图实现:
的数据
字符串
对于频率响应,在你的情况下,你可能不应该使用对数标度图。但是
ax[1].semilogy
在y轴上使用对数标度图。的数据