我正在尝试使用python创建一个简单的10频段均衡器。 为了实现这一点,我编写了两个函数,但是我对增益有疑问。 我想为每个频段设置一个增益,但是它不起作用。
这里有一个例子。需要一个单通道的wav“ audio.wav”文件才能工作。
import numpy as np
import matplotlib.pyplot as plt
import scipy.io.wavfile as wav
from scipy import signal
from scipy.signal import butter, lfilter
def bandpass_filter(data, lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='bandpass')
filtered = lfilter(b, a, data)
return filtered
def equalizer_10band (data, fs, gain1=0, gain2=0, gain3=0, gain4=0, gain5=0, gain6=0, gain7=0, gain8=0, gain9=0, gain10=0):
band1 = bandpass_filter(data, 20, 39, fs, order=2)* 10**(gain1/20)
band2 = bandpass_filter(data, 40, 79, fs, order=3)*10**(gain2/20)
band3 = bandpass_filter(data, 80, 159, fs, order=3)*10**(gain3/20)
band4 = bandpass_filter(data, 160, 299, fs, order=3)* 10**(gain4/20)
band5 = bandpass_filter(data, 300, 599, fs, order=3)* 10**(gain5/20)
band6 = bandpass_filter(data, 600, 1199, fs, order=3)* 10**(gain6/20)
band7 = bandpass_filter(data, 1200, 2399, fs, order=3)* 10**(gain7/20)
band8 = bandpass_filter(data, 2400, 4999, fs, order=3)* 10**(gain8/20)
band9 = bandpass_filter(data, 5000, 9999, fs, order=3)* 10**(gain9/20)
band10 = bandpass_filter(data, 10000, 20000, fs, order=3)* 10**(gain10/20)
signal = band1 + band2 + band3 + band4 + band5 + band6 + band7 + band8 + band9 + band10
return signal
freq_s, data = wav.read("audio.wav")
N = len(data)
t = 1/freq_s * np.arange(N)
f = freq_s/N * np.arange(N)
#computing fft of original signal
F_data = np.fft.fft(data)/N
#appying equalizer
equalized = equalizer_10band(data, freq_s, -100,-100,-100,0,0,0,0,0,0,0)
#computing fft of filtered signal
Y = np.fft.fft(equalized)/N
plt.figure(figsize=(10, 8))
plt.subplot(2,1,1)
plt.plot(t, equalized,'-b',label=r"$Filtered amplitude(t)$")
plt.xlabel('time[s]')
plt.subplot(2,1,1)
plt.plot(t, data,'-r',label=r"$Original amplitude(t)$")
plt.xlabel('time[s]')
plt.legend()
plt.grid()
plt.subplot(2,1,2)
plt.plot(f[:N//2],np.abs(F_data[:N//2]),'-r',label=r"$Original magnitude(f)$")
plt.xlabel('f [Hz]')
plt.xlim([0,5e3])
plt.plot(f[:N//2],np.abs(Y[:N//2]),'-b',label=r"$Filtered magnitude(f)$")
plt.xlabel('f [Hz]')
plt.xlim([0,5e3])
plt.legend()
plt.tight_layout()
plt.grid()
在第二幅图中,它显示了原始信号和滤波后信号的频谱重叠,我希望看到滤波后的信号的前三个频带的频率为零,但它们与原始信号大致相同。 我还附有光谱图的快照。
可以帮我吗?