在Python中通过Scipy创建一个带通滤波器?

时间:2018-01-04 18:38:00

标签: python filter scipy fft frequency

有没有办法在Python 3.6中通过scipylibrosa为16KHz wav文件创建快速带通滤波器,以滤除300-3400Hz人类语音频段之外的噪音?这是一个sample wav file,背景噪音很低。

更新: 是的,我已经看过/试过了How to implement band-pass Butterworth filter with Scipy.signal.butter。不幸的是,过滤后的声音非常变形。从本质上讲,整个代码都是这样做的:

lo,hi=300,3400
sr,y=wavfile.read(wav_file)
b,a=butter(N=6, Wn=[2*lo/sr, 2*hi/sr], btype='band')
x = lfilter(b,a,y)
sounddevice.play(x, sr)  # playback

我做错了什么或如何改进,以便正确滤除背景噪音。

以下是使用上述链接显示原始文件和已过滤文件的信息。可视化看起来很合理,但听起来很可怕:(如何解决这个问题?

enter image description here

2 个答案:

答案 0 :(得分:3)

显然,在写入非标准化的64位浮点数据时会出现问题。通过将x转换为16位或32位整数,或通过将x归一化到范围[-1,1]并转换为32浮点,我得到一个合理的输出文件。

我没有使用sounddevice;相反,我将过滤后的数据保存到新的WAV文件并播放。以下是适用于我的变体:

# Convert to 16 integers
wavfile.write('off_plus_noise_filtered.wav', sr, x.astype(np.int16))

...或

# Convert to 32 bit integers
wavfile.write('off_plus_noise_filtered.wav', sr, x.astype(np.int32))

...或

# Convert to normalized 32 bit floating point
normalized_x = x / np.abs(x).max()
wavfile.write('off_plus_noise_filtered.wav', sr, normalized_x.astype(np.float32))

输出整数时,可以放大这些值,以最大限度地减少因截断浮点值而导致的精度损失:

x16 = (normalized_x * (2**15-1)).astype(np.int16)
wavfile.write('off_plus_noise_filtered.wav', sr, x16)

答案 1 :(得分:1)

以下代码用于从此处生成带通滤波器:https://scipy.github.io/old-wiki/pages/Cookbook/ButterworthBandpass

     from scipy.signal import butter, lfilter


def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = butter(order, [low, high], btype='band')
    return b, a


def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y


if __name__ == "__main__":
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.signal import freqz

    # Sample rate and desired cutoff frequencies (in Hz).
    fs = 5000.0
    lowcut = 500.0
    highcut = 1250.0

    # Plot the frequency response for a few different orders.
    plt.figure(1)
    plt.clf()
    for order in [3, 6, 9]:
        b, a = butter_bandpass(lowcut, highcut, fs, order=order)
        w, h = freqz(b, a, worN=2000)
        plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="order = %d" % order)

    plt.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)],
             '--', label='sqrt(0.5)')
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Gain')
    plt.grid(True)
    plt.legend(loc='best')

    # Filter a noisy signal.
    T = 0.05
    nsamples = T * fs
    t = np.linspace(0, T, nsamples, endpoint=False)
    a = 0.02
    f0 = 600.0
    x = 0.1 * np.sin(2 * np.pi * 1.2 * np.sqrt(t))
    x += 0.01 * np.cos(2 * np.pi * 312 * t + 0.1)
    x += a * np.cos(2 * np.pi * f0 * t + .11)
    x += 0.03 * np.cos(2 * np.pi * 2000 * t)
    plt.figure(2)
    plt.clf()
    plt.plot(t, x, label='Noisy signal')

    y = butter_bandpass_filter(x, lowcut, highcut, fs, order=6)
    plt.plot(t, y, label='Filtered signal (%g Hz)' % f0)
    plt.xlabel('time (seconds)')
    plt.hlines([-a, a], 0, T, linestyles='--')
    plt.grid(True)
    plt.axis('tight')
    plt.legend(loc='upper left')

    plt.show() 

看看这是否有助于您的事业。 你可以在这里指定所需的频率:

# Sample rate and desired cutoff frequencies (in Hz).
        fs = 5000.0
        lowcut = 500.0
        highcut = 1250.0