如何在python中为wav文件实现高通音频过滤器

时间:2017-03-04 03:27:15

标签: python-3.x audio filtering audio-processing highpass-filter

我正在开发一个与音频处理相关的项目,我想为存储在内存和现场语音中的一些波形文件实现高通音频滤波器。我在这里发现了一些与我的问题有关的问题,但不完全像

1→ Python scipy.signal.remez high pass filter design yields strange transfer function

2→ Python: High Pass FIR Filter by Windowing

但我没有得到如何修改已保存音频文件和现场演讲的可用代码。

我找到了一段用于保存音频文件的低通滤波器的代码,该代码工作正常,但我无法找到一个高通滤波器。

    file_input = '3.wav'
            file_output = 'filter.wav'

            cutOffFrequency = 400.0

            def running_mean(x, windowSize):
            cumsum = np.cumsum(np.insert(x, 0, 0))
            return (cumsum[windowSize:] - cumsum[:-windowSize]) / windowSize

            def interpret_wav(raw_bytes, n_frames, n_channels, sample_width, interleaved = True):

                if sample_width == 1:
                    dtype = np.uint8
                elif sample_width == 2:
                    dtype = np.int16
                else:
                    raise ValueError("Only supports 8 and 16 bit audio formats.")

                channels = np.fromstring(raw_bytes, dtype=dtype)

                if interleaved:

                    channels.shape = (n_frames, n_channels)
                    channels = channels.T
                else:

                    channels.shape = (n_channels, n_frames)

                return channels

            with contextlib.closing(wave.open(file_input,'rb')) as spf:
                sampleRate = spf.getframerate()
                ampWidth = spf.getsampwidth()
                nChannels = spf.getnchannels()
                nFrames = spf.getnframes()


                signal = spf.readframes(nFrames*nChannels)
                spf.close()
                channels = interpret_wav(signal, nFrames, nChannels, ampWidth, True)



                freqRatio = (cutOffFrequency/sampleRate)
                N = int(math.sqrt(0.196196 + freqRatio**2)/freqRatio)


                filtered = running_mean(channels[0], N).astype(channels.dtype)

                wav_file = wave.open(file_output, "w")
                wav_file.setparams((1, ampWidth, sampleRate, nFrames, spf.getcomptype(), spf.getcompname()))
                wav_file.writeframes(filtered.tobytes('C'))
                wav_file.close()

这是我用于低通滤波器的代码。

0 个答案:

没有答案