使用python过滤wav文件

时间:2014-07-23 20:29:09

标签: python audio filtering wav

所以我最近成功构建了一个系统,它将完全用python录制,绘制和播放音频wav文件。现在,我试图在录制时和开始绘制并将文件输出到扬声器之间进行一些滤波和音频混合。但是,我不知道从哪里开始。现在我要读取初始wav文件,应用低通滤波器,然后将新过滤的数据重新打包到新的wav文件中。这是我用来绘制初始数据的代码。

import matplotlib.pyplot as plt
import numpy as np
import wave
import sys

spf = wave.open('wavfile.wav','r')

#Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int16')

plt.figure(1)
plt.title('Signal Wave...')
plt.plot(signal)

以下是我用来生成单音测试音频文件的一些代码:

import numpy as np
import wave
import struct

freq = 440.0
data_size = 40000
fname = "High_A.wav"
frate = 11025.0  
amp = 64000.0    

sine_list_x = []
for x in range(data_size):
    sine_list_x.append(np.sin(2*np.pi*freq*(x/frate)))

wav_file = wave.open(fname, "w")

nchannels = 1
sampwidth = 2
framerate = int(frate)
nframes = data_size
comptype = "NONE"
compname = "not compressed"

wav_file.setparams((nchannels, sampwidth, framerate, nframes,
comptype, compname))

for s in sine_list_x:
    wav_file.writeframes(struct.pack('h', int(s*amp/2)))

wav_file.close()

我不确定如何应用所述音频过滤器并重新打包它。我们非常感谢您提供的任何帮助和/或建议。

2 个答案:

答案 0 :(得分:29)

第一步:您需要什么样的音频过滤器?

选择过滤后的带

对于以下步骤,我假设您需要低通滤波器

选择截止频率

Cutoff frequency是信号衰减-3dB的频率。

您的示例信号为440Hz,因此我们选择Cutoff frequency 400Hz 。然后通过低通400Hz滤波器衰减440Hz信号(大于-3dB)。

选择过滤器类型

根据this other stackoverflow answer

  

滤波器设计超出了Stack Overflow的范围 - 这是一个DSP   问题,而不是编程问题。过滤器设计由任何人涵盖   DSP教科书 - 去你的图书馆。我喜欢Proakis和Manolakis'   数字信号处理。 (Ifeachor和Jervis'数字信号   处理也不错。)

进入一个简单的例子,我建议使用移动平均值过滤器(对于简单的低通过滤器)。

请参阅Moving average

  

数学上,移动平均线是一种卷积,因此它可以被视为信号处理中使用的低通滤波器的一个例子

此移动平均低通滤波器是一个基本滤波器,它易于使用和理解。

移动平均线的参数是窗口长度

moving average窗口长度与Cutoff frequency之间的关系需要一点点数学,并解释为here

代码将是

import math

sampleRate = 11025.0 
cutOffFrequency = 400.0
freqRatio = (cutOffFrequency/sampleRate)

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

因此,在该示例中,窗口长度将 11

第二步:对过滤器进行编码

手工移动平均值

请参阅specific discussion on how to create a moving average in python

来自Alleo的解决方案

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

filtered = running_mean(signal, N)

使用lfilter

或者,正如dpwilson所建议的那样,我们也可以使用lfilter

win = numpy.ones(N)
win *= 1.0/N
filtered = scipy.signal.lfilter(win, [1], signal).astype(channels.dtype)

第三步:让我们把它放在一起

import matplotlib.pyplot as plt
import numpy as np
import wave
import sys
import math
import contextlib

fname = 'test.wav'
outname = 'filtered.wav'

cutOffFrequency = 400.0

# from http://stackoverflow.com/questions/13728392/moving-average-or-running-mean
def running_mean(x, windowSize):
  cumsum = np.cumsum(np.insert(x, 0, 0)) 
  return (cumsum[windowSize:] - cumsum[:-windowSize]) / windowSize

# from http://stackoverflow.com/questions/2226853/interpreting-wav-data/2227174#2227174
def interpret_wav(raw_bytes, n_frames, n_channels, sample_width, interleaved = True):

    if sample_width == 1:
        dtype = np.uint8 # unsigned char
    elif sample_width == 2:
        dtype = np.int16 # signed 2-byte short
    else:
        raise ValueError("Only supports 8 and 16 bit audio formats.")

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

    if interleaved:
        # channels are interleaved, i.e. sample N of channel M follows sample N of channel M-1 in raw data
        channels.shape = (n_frames, n_channels)
        channels = channels.T
    else:
        # channels are not interleaved. All samples from channel M occur before all samples from channel M-1
        channels.shape = (n_channels, n_frames)

    return channels

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

    # Extract Raw Audio from multi-channel Wav File
    signal = spf.readframes(nFrames*nChannels)
    spf.close()
    channels = interpret_wav(signal, nFrames, nChannels, ampWidth, True)

    # get window size
    # from http://dsp.stackexchange.com/questions/9966/what-is-the-cut-off-frequency-of-a-moving-average-filter
    freqRatio = (cutOffFrequency/sampleRate)
    N = int(math.sqrt(0.196196 + freqRatio**2)/freqRatio)

    # Use moviung average (only on first channel)
    filtered = running_mean(channels[0], N).astype(channels.dtype)

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

答案 1 :(得分:0)

sox library可用于静电噪声消除。

我发现this gist有一些有用的命令作为例子