作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使音频通过低通滤波器,以便从中滤除噪声。但是,wav的输出非常嘈杂,我无法理解为什么。找到原始的和过滤后的WAV及其对应的内容。链接下方的频谱图。
enter link description here
我使用的代码是:
#https://stackoverflow.com/questions/25191620/creating-lowpass-filter-in-scipy-understanding-methods-and-units
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()
最佳答案
将保存输出音频的行替换为以下内容:
wavfile.write('LPF_filttered.wav', frq, np.int16(y/np.max(np.abs(y)) * 32767))
关于python - 通过低通滤波器后,音频文件听起来不佳/嘈杂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60986056/
我是一名优秀的程序员,十分优秀!