次のようなライブラリ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 のタイプであることです。