0

我正在尝试使用库adaptfilt 2.0中的 python 3.4 中的“回声取消”示例,如下所示:

import numpy as np
import adaptfilt as adf

# Get u(n) - this is available on github or pypi in the examples folder
u = np.load('speech.npy')

# Generate received signal d(n) using randomly chosen coefficients
coeffs = np.concatenate(([0.8], np.zeros(8), [-0.7], np.zeros(9),
                         [0.5], np.zeros(11), [-0.3], np.zeros(3),
                         [0.1], np.zeros(20), [-0.05]))

d = np.convolve(u, coeffs)

# Add background noise
v = np.random.randn(len(d)) * np.sqrt(5000)
d += v

# Apply adaptive filter
M = 100  # Number of filter taps in adaptive filter
step = 0.1  # Step size
y, e, w = adf.nlms(u, d, M, step, returnCoeffs=True)

# Calculate mean square weight error
mswe = adf.mswe(w, coeffs)

它按预期工作。但是后来我想对音乐文件中的一些真实数据做同样的事情,我得到了一个错误:

Traceback (most recent call last):
  File "C:/Python34/Lib/site-packages/adaptfilt/echocancel.py", line 86, in <module>
    y, e, w = adf.nlms(u, d, M, step, returnCoeffs=True)
  File "C:\Python34\Lib\site-packages\adaptfilt\nlms.py", line 149, in nlms
    w = leakstep * w + step * normFactor * x * e[n]
FloatingPointError: invalid value encountered in multiply

我使用的代码是这样的:

import numpy as np
import adaptfilt as adf
import pyaudio
import wave

np.seterr(all='raise')
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(2),
                        channels = 1,
                        rate = 44100,
                        input = True,
                        output = True,
                        #stream_callback = self.callback
                        )
wf = wave.open("XXX.wav", 'rb')
while u != " ":
    data = wf.readframes(1024)
    u = np.fromstring(data, np.int16)

    # Generate received signal d(n) using randomly chosen coefficients
    coeffs = np.concatenate(([0.8], np.zeros(8), [-0.7], np.zeros(9),
                             [0.5], np.zeros(11), [-0.3], np.zeros(3),
                             [0.1], np.zeros(20), [-0.05]))

    coeffs.dtype = np.int16

    d = np.convolve(u, coeffs)

    # Add background noise
    v = np.random.randn(len(d)) * np.sqrt(5000)
    d += v

    # Apply adaptive filter
    M = 100  # Number of filter taps in adaptive filter
    step = 0.1  # Step size
    y, e, w = adf.nlms(u, d, M, step, returnCoeffs=True)

    # Calculate mean square weight error
    mswe = adf.mswe(w, coeffs)

    stream.write(y.astype(np.int16).tostring())

我看到的唯一区别是“speech.npy”中的数组是 float64 类型,而 wav 文件中的数组是 int16 类型。

4

2 回答 2

1

通过将“数据”转换为 float64,我还能够让“adaptfilt 2.0”处理音乐数据(采样率为 44.1kHz 的单声道 .wav 文件)。

自适应滤波器需要更长的时间才能收敛,但它工作正常。下面是显示滤波器收敛的均方权重误差图。

http://i.imgur.com/xcpFUn2.png

我还要补充一点,要将其应用于音乐,您可能需要更长的过滤器抽头 (M)。原始脚本中使用的“speech.npy”数组只是一个没有采样率信息的数组,但我们可以假设语音文件中的采样率低于 44.1kHz。

我以不同的采样率播放了“speech.npy”,仅通过听听起来自然的声音,我猜它在 10-12kHz 范围内。这意味着存储在“coeffs”中的“脉冲响应”约为 5.7 ms(假设采样率为 10kHz)。在 44.1kHz 时,脉冲响应仅为 ~1.3ms。这很短,不太可能模拟真实信号的脉冲响应。

于 2015-09-28T23:11:20.403 回答
0

使用 int16 类型在模块内部某处进行计算时,由于数值问题而出现此错误。使用您的示例并将输入数据转换为浮点类型,例如

u = np.fromstring(data, np.int16)
u = np.float64(u)

为我解决了这个问题。

于 2015-04-23T09:22:08.237 回答