gpt4 book ai didi

python - 通过低通滤波器后,音频文件听起来不佳/嘈杂

转载 作者:行者123 更新时间:2023-12-03 01:22:00 24 4
gpt4 key购买 nike

我正在尝试使音频通过低通滤波器,以便从中滤除噪声。但是,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()

1)这是实现过滤器的正确方法吗?还是我做错了什么?因为产生的音频太失真了。

2)什么是正确的方法,这样我将获得无噪音的音频文件

提前致谢。

最佳答案

将保存输出音频的行替换为以下内容:

wavfile.write('LPF_filttered.wav', frq, np.int16(y/np.max(np.abs(y)) * 32767))

关于python - 通过低通滤波器后,音频文件听起来不佳/嘈杂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60986056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com