通过低通滤波器后音频文件听起来 bad/noisy
audio file sounds bad/noisy after passing through low pass filter
我试图让我的音频通过低通滤波器,以滤除其中的噪音。但是,wav 的输出非常嘈杂,我无法理解为什么。找到原始和过滤后的 wav 及其响应。 link 以下的频谱图。
enter link description here
我用过的代码是:
#
import numpy as np
from scipy.signal import butter, lfilter, freqz, filtfilt
from matplotlib import pyplot as plt
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
frq, data = wavfile.read('original.wav')
# Filter requirements.
order = 5
fs = frq # sample rate, Hz
cutoff = 4000 # desired cutoff frequency of the filter, Hz
# Get the filter coefficients so we can check its frequency response.
#b, a = butter_lowpass(cutoff, fs, order)
# Filter the data, and plot both the original and filtered signals.
y = butter_lowpass_filter(data, cutoff, fs, order)
wavfile.write('LPF_filttered.wav', frq, y)
# Get the filter coefficients so we can check its frequency response.
b, a = butter_lowpass(cutoff, fs, order)
# Plot the frequency response.
w, h = freqz(b, a, worN=8000)
plt.subplot(2, 1, 1)
plt.plot(0.5*fs*w/np.pi, np.abs(h), 'b')
plt.plot(cutoff, 0.5*np.sqrt(2), 'ko')
plt.axvline(cutoff, color='k')
plt.xlim(0, 0.5*fs)
plt.title("Lowpass Filter Frequency Response")
plt.xlabel('Frequency [Hz]')
plt.grid()
# First make some data to be filtered. # seconds
n = len(data) # total number of samples
t = np.linspace(0, 1.0 , n, endpoint=False)
# "Noisy" data. We want to recover the 1.2 Hz signal from this.
plt.subplot(2, 1, 2)
plt.plot(t, data, 'b-', label='data')
plt.plot(t, y, 'g-', linewidth=2, label='filtered data')
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
plt.subplots_adjust(hspace=0.35)
plt.show()
1) 这是实现过滤器的正确方法吗?还是我做错了什么?因为生成的音频太失真了。
2)正确的方法是什么,这样我才能得到无噪音的音频文件
提前致谢。
将保存输出音频的行替换为以下内容:
wavfile.write('LPF_filttered.wav', frq, np.int16(y/np.max(np.abs(y)) * 32767))
我试图让我的音频通过低通滤波器,以滤除其中的噪音。但是,wav 的输出非常嘈杂,我无法理解为什么。找到原始和过滤后的 wav 及其响应。 link 以下的频谱图。 enter link description here
我用过的代码是:
#
import numpy as np
from scipy.signal import butter, lfilter, freqz, filtfilt
from matplotlib import pyplot as plt
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
frq, data = wavfile.read('original.wav')
# Filter requirements.
order = 5
fs = frq # sample rate, Hz
cutoff = 4000 # desired cutoff frequency of the filter, Hz
# Get the filter coefficients so we can check its frequency response.
#b, a = butter_lowpass(cutoff, fs, order)
# Filter the data, and plot both the original and filtered signals.
y = butter_lowpass_filter(data, cutoff, fs, order)
wavfile.write('LPF_filttered.wav', frq, y)
# Get the filter coefficients so we can check its frequency response.
b, a = butter_lowpass(cutoff, fs, order)
# Plot the frequency response.
w, h = freqz(b, a, worN=8000)
plt.subplot(2, 1, 1)
plt.plot(0.5*fs*w/np.pi, np.abs(h), 'b')
plt.plot(cutoff, 0.5*np.sqrt(2), 'ko')
plt.axvline(cutoff, color='k')
plt.xlim(0, 0.5*fs)
plt.title("Lowpass Filter Frequency Response")
plt.xlabel('Frequency [Hz]')
plt.grid()
# First make some data to be filtered. # seconds
n = len(data) # total number of samples
t = np.linspace(0, 1.0 , n, endpoint=False)
# "Noisy" data. We want to recover the 1.2 Hz signal from this.
plt.subplot(2, 1, 2)
plt.plot(t, data, 'b-', label='data')
plt.plot(t, y, 'g-', linewidth=2, label='filtered data')
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
plt.subplots_adjust(hspace=0.35)
plt.show()
1) 这是实现过滤器的正确方法吗?还是我做错了什么?因为生成的音频太失真了。
2)正确的方法是什么,这样我才能得到无噪音的音频文件
提前致谢。
将保存输出音频的行替换为以下内容:
wavfile.write('LPF_filttered.wav', frq, np.int16(y/np.max(np.abs(y)) * 32767))