# “Subtracting” one audio stream from another audio stream

I am a long time software engineer but have practically zero experience with signal/audio processing. I am interested in learning about signal processing via a use-case we have for one of our audio components. NOTE: this is just a learning exercise for me... it is not a priority that the end result be useful.

We have a component (A) which produces audio (via a speaker). We have another component (B) which records that audio (via a simple microphone).

What I would like to do is use A to record B's audio. Then I would like to, if even possible, compare the two streams. The goal would be to remove the audio that was present in stream A - leaving the ambient conditions which existed during the recording - I understand full removal is not possible.

I realize that there are phase and magnitude issues. I also realize that it isn't just a simple matter of "subtracting" B from A. That said, my assumption is you can subtract A from A.

I would like to understand how to approach the problem. Again, this is a learning experience for me (there are no deadlines); I am more than willing to start from the beginning.

Any advice/suggestions would be much appreciated.

This setup shares some similarities with system identification problems, where $A$ would be the input of the LTI system you want to estimate the transfer function of, $B$ being the output; and the "ambient sound" being the additive noise. The LTI assumption is reasonable provided your converters/amplifiers/transducers are of decent quality.

So the steps would be:

• Use a system identification technique to find the FIR filter $\hat{h}$ that minimizes the mean-square error between $\hat{h} \star A$ and $B$. A simple method, which might not be the most suitable here, is to divide the cross-correlation of $A$ and $B$ by the autocorrelation of $A$. Explanation here. The limitation is that it will not work well for long recordings (you might better compute your estimates on shorter segments and average them) - and that music is not the best "probe" signal to send into a system to estimate its response.
• You can now use $\hat{h} \star A$ as an estimate of the original signal A as "heard" by the microphone and subtract it from $B$ to retrieve the ambient sound.

I gave a shot at this using a music clip (A), applying a reverb and slight amp model to simulate a speaker in a room, then mixing in a cat audio sample to get (B), then estimating an impulse response from the (A, B) pair, then subtracting the filtered A from B. This shows some results but a better FIR estimation technique might help here! (note that I truncated the estimated IR to its first 5000 samples to speed up computations).

Note that there are algorithms for doing this adaptively (such as LMS). This might be more suitable for your problem if $A$ and $B$ are processed in realtime rather than offline. Such algorithms form the basis of echo cancellation systems used in telecommunications.

• Thank you very much @pinchenettes! Your c_cat_estimated.wav is exactly the type of result I am trying to achieve. Is there any way you could let me know how you did it (software used, etc.)? Please feel free to contact me directly: catxtwo@gmail.com – CaymanEss Apr 22 '13 at 11:46
• I used Audacity and Apple's standard audio-units to process the original audio and get the "room" effect. Cat sample was mixed in audacity too. The rest of the code is in python+numpy: gist.github.com/pichenettes/5434412 . This is the "simplest thing that could possibly work" - the core of the algorithm is only 6 lines of code. – pichenettes Apr 22 '13 at 12:01
• Again, thank you very much @pinchenettes. This will get me going. I'll eventually want to do this in realtime - do you believe that using LMS would still be feasible? – CaymanEss Apr 22 '13 at 12:14
• If you know that your impulse response is not going to change, you can compute it using a slow algorithm during the first seconds and then keep using it. Otherwise (speakers are moving, environment is changing...) you'll need an adaptive algorithm. Note that you'll probably have to deal with convolution with long impulse responses if you want to model rooms -> Check this for techniques dsp.stackexchange.com/questions/8771/… . I am not aware of adaptive filtering methods optimized for very long FIR responses though. – pichenettes Apr 22 '13 at 12:36
• The signals need to have the same length, number of channels, and sample rate. – pichenettes Apr 22 '13 at 13:48

The issue which will not allow exact ambient sound to be the output are:

(a) the loudness difference (b) phase difference due to time delay.

Thus overall the recorded signal by B will be (assuming the room behaves as an LTI system) $y_B[n]=\sum_{k=0}^{p}\alpha_k x_A[n-k]+x_{amb}[n])=(x*h)[n]+x_{amb}[n]$, where $h[n]=\alpha_n$ or so the room impulse response. Here $x_{amb}[n]$ is the ambient signal at B's location. Now, $z[n]=x_A[n]-y_B[n]$ you can see that you will have $z[n]\neq x_{amb}[n]$, unless you know the room impulse response or its estimate. To get the insight you may go forward by doing the subtraction and seeing the spectrogram of the resulting signal and comparing with the ambient signal's and only A's signal spectrogram.

The other way around is to place the mic close to A to minimize the effect of room impulse response, such a technique is used in noise cancelling headphones where the ambient noise is to be removed. http://en.wikipedia.org/wiki/Noise-cancelling_headphonese.

• Thank you very much @Neeks. Is there a name for the yB[n] equation (algorithm)? Again, I am totally new to dsp thus it would be beneficial to me to be able to read up on specific algorithms/concepts. – CaymanEss Apr 22 '13 at 11:35
• It is called speech and audio dereverberation. It is analogous to a de-convolution problem once you assume the reverberation operation to be due to an LTI system i.t $h(t)$ is an LTI system. – Neeks Jun 7 '13 at 6:01

That sounds like a standard case for "acoustic echo cancellation". A LOT of patents, research papers and academic theses exist on the topic (starting in 1967, I believe with M.M. Sondhi's paper "An adaptive acoustic Echo Cancellor").

This is a decent book on the topic http://www.amazon.com/Advances-Network-Acoustic-Cancellation-Processing/dp/3540417214

• Thank you @Hilmar. On your recommendation I have read the overview of echo cancellation and several papers on noise cancellation. Am I wrong in thinking that cancelation is the opposite of what I want though? Again, my goal is to obtain the ambient noise, not a cleaned up version of the original signal. – CaymanEss Apr 22 '13 at 11:39