我试图在库adaptfilt 2.0中使用python 3.4中的“echo cancel”示例,如下所示:
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的类型。
答案 0 :(得分:1)
我也能够得到适应性的2.0&#39;通过投射数据&#39;来处理音乐数据(具有44.1kHz采样率的单声道.wav文件)。到float64。
自适应滤波器需要更长时间才能收敛,但它运行正常。下面是均方重量误差图,显示滤波器收敛。
我还想补充一点,将其应用于音乐,您可能需要更长的滤镜(M)。 &#39; speech.npy&#39;原始脚本中使用的数组只是一个没有采样率信息的数组,但我们可以假设语音文件中的采样率低于44.1kHz。
我演奏了#speech; npy&#39;以不同的采样率回来,只是通过听听听起来自然的声音,我猜测它在10-12kHz范围内。这意味着&#34;脉冲响应&#34;存储在&#39; coeffs&#39;中的是~5.7 ms(假设采样率为10kHz)。在44.1kHz时,脉冲响应仅为~1.3ms。这非常短,不太可能模拟真实信号的脉冲响应。
答案 1 :(得分:0)
由于在模块内某处使用int16类型进行计算时出现数值问题,会出现此错误。使用您的示例并将输入数据转换为浮点类型,例如
{{1}}
为我解决了这个问题。