我正在用Python录制5秒钟的现场音频片段,希望将低于某个频率(例如10kHz)的所有声音都剪掉。这是我目前的脚本:
import pyaudio, wave, time, sys, os
from array import array
from scipy import signal
FORMAT=pyaudio.paInt16
CHANNELS=1
CHUNK=1024
RATE=44100
RECORD_SECONDS=5
def butter_highpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
return b, a
def butter_highpass_filter(data, cutoff, fs, order=5):
b, a = butter_highpass(cutoff, fs, order=order)
y = signal.filtfilt(b, a, data)
return y
audio=pyaudio.PyAudio()
stream=audio.open(format=FORMAT,channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
while True:
# read data
data=stream.read(CHUNK)
data_chunk=array('h',data)
data_chunk = butter_highpass_filter(data_chunk,10000,RATE)
frames=[]
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
# write to file
words = ["RECORDING-", time.strftime("%Y%m%d-%H%M%S"), ".wav"]
FILE_NAME= "".join(words)
wavfile=wave.open(FILE_NAME,'wb')
wavfile.setnchannels(CHANNELS)
wavfile.setsampwidth(audio.get_sample_size(FORMAT))
wavfile.setframerate(RATE)
wavfile.writeframes(b''.join(frames))
wavfile.close()
但这似乎不起作用。我想把所有(或尽可能多的)低于规定频率的声音都剪掉。为什么我使用的过滤器似乎不能将10kHz以下的声音过滤掉?我怎样才能让它工作
简短的
目标是对音频应用砖墙式10 kHz高通滤波器,然后保存它。音频连续录制并保存在5秒钟的片段中,以分离
.wav
文件到目前为止我们有什么
目前,当前脚本:
butter_high-pass_filter
)butter_high-pass_filter
使用signal.filtfiltdata_chunk
从未被使用过,因此高传递的音频帧从未保存到文件中(错误2)wav
文件解决方案
这里的问题是,解决方案是多方面的,需要当前脚本中缺少的多个部分。 此外,为了避免进一步的复杂化,需要采取与最初计划略有不同的方法。在保存
wav
样本数据之前,只需将过滤器应用于样本数据,而不是实时应用过滤器。 这个此外,外部永久
while
循环已被删除。时间和内存开始成为一个问题。程序可以简单地反复运行,直到用例覆盖更加清晰wav
文件)后不能进行过滤在这些问题得到解决之前,有太多可能的路线,每一条路线都有限制,实际上可以在一个答案中涵盖
故障
该流程的完整细分将在
脚本
评论
潜在的附加功能可能包括用
matplotlib
绘制频谱、对过滤后的音频进行规范化、将过程封装到自定义函数中,但这些都是留给OP的练习下面是归一化后音频频谱的屏幕截图。正如@marco在评论中提到的,滤波器确实有一个斜率,这是预期的。这可以通过增加滤波器的阶数来改善
相关问题 更多 >
编程相关推荐