我已经构建了一个python类,它具有读取、记录、保存、回放和显示带有PyAudio和Matplotlib的音频文件的方法。我现在想实现一个简单的低通滤波器。我已经设法组合了一个方法,使用窗口化的运行平均值生成一个低通滤波器,但这会产生比原始信号稍小的信号。在
我的下一个尝试是使用巴特沃斯过滤器化学信号. 我已经搜索了stackoverflow以确保我正确地实现了它,而且我认为我是正确的。然而,当我使用低通滤波器时,我的信号变成白噪声。我有什么遗漏吗?在
我在下面附上了相关的代码;请记住,这些函数是一个更大类的一部分。audio
是原始音频信号,self.RATE
是音频录制的采样率,self.filename
是存储原始音频记录的文件名。在
def butter_lowpass(self,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(self,data, cutoff, fs, order=5):
b, a = self.butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
def lowpass(self):
# Filter requirements.
order = 6
fs = self.RATE # sample rate, Hz
cutoff = 1000 # desired cutoff frequency of the filter, Hz
# Get the filter coefficients.
b, a = self.butter_lowpass(cutoff, fs, order)
audio,duration,frames,bps,dt = self.read_audio(self.filename)
filtered = self.butter_lowpass_filter(audio, cutoff, fs, order)
# Rewrite to file.
wav_file = wave.open(self.filename, "w")
wav_file.setparams((1, bps, self.RATE, frames, 'NONE', 'not compressed'))
wav_file.writeframes(filtered.tobytes('C'))
wav_file.close()
目前没有回答
相关问题 更多 >
编程相关推荐