Python adaptfilt-echo canceller example with two .wav files
<p>this code isa modification from the example of adaptfilt2.0 echo canceller</p>

<pre><code>  u(n) -------&gt;-&gt;------+-----------&gt;-&gt;-----------
                       |                        |
              +-----------------+      +------------------+
          +-&gt;-| Adaptive filter |      |    John's Room   |
          |   +-----------------+      +------------------+
          |            | -y(n)                  |
          |            |           d(n)         |
  e(n) ---+---&lt;-&lt;------+-----------&lt;-&lt;----------+----&lt;-&lt;---- v(n)
</code></pre>

<p>i need to read two wave audio files audio1.wav like emily's signal and audio2.wav like john's signal, the thing is that the code only use audio1.wav and dont read audio2.wav in the process, so when the code run only use the audio1.wav for u(n) and v(n) and not assigns audio2.wav to the variable v(n).</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt
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 =callback
                        )

#Read U
wf1 = wave.open("audio1.wav", 'r')

data= wf1.readframes(1024)
u = np.fromstring(data, np.int16)
u = np.float64(u)

#read V
wf2= wave.open("audio2.wav", 'r')
data2= wf2.readframes(1024)
v= np.fromstring(data2, np.int16)
v = np.float64(u)

#both signals
d = u+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)

plt.figure()
plt.title('Em u(n)')
plt.plot(u)
plt.grid()
plt.xlabel('Samples')

plt.figure()
plt.title('john v(n)')
plt.plot(v)
plt.grid()
plt.xlabel('Samples')


plt.show()
</code></pre>

<p>the images shows my problem </p>

<p><a href="http://i.stack.imgur.com/LcBqP.png" rel="nofollow">Emily</a></p>

<p><a href="http://i.stack.imgur.com/kF5Vs.png" rel="nofollow">John's signal</a></p>

<p>What is wrong on the code that doesnt make me use both signals?</p>

<p>It looks like you have a "copy and paste" error.  After reading <code>audio2.wav</code>, this line</p>

<pre><code>v = np.float64(u)
</code></pre>

<p>should be </p>

<pre><code>v = np.float64(v)
</code></pre>

<p>(Now that you see it is a trivial mistake, feel free to delete your question!)</p>

<hr>

<p>P.S. Using <code>np.float64</code> works, but the idiomatic way to do this with numpy is:</p>

<pre><code>v = v.astype(np.float64)
</code></pre>

python numpy audio wave pyaudio