This post was entirely written using the IPython notebook. Its content is BSD-licensed. You can see a static view or download this notebook with the help of nbviewer at 20160907_ImpulseResponses.ipynb.
In this notebook, we're going to explore the fascinating world of impulse responses. This blog post is inspired by many interesting projects of Allen Downey and in particular this student project of his.
In many fields, the output of a system can be represented as the transformation of an input by a function, which we will call G. Symbollically, we would write this:
$$ y = G * x $$It turns out that if you choose G wisely, you can solve many problems of physics. The name G is chosen after George Green, who invented the concept. For a good introduction about Green's functions, I recommend this Wolfram Blog entry.
Green functions come in many variants, depending on the problem to be solved, see Wikipedia. In this posting, I propose to do the followine:
First, let's introduce the theory background we'll need to perform our task.
Actually, we need to introduce two theory items in this notebook: the convolution theorem, which is a classic signal processing way of teaching and the wave equation solution using a Green's function.
The convolution theorem states that to apply a certain function G, I can use the Fourier frequency space to do it.
All sounds that move through space obey the wave equation. It turns out that the wave equation can be solved with the following Green's function:
$$ G(t, \rho) = {\displaystyle {\frac {1}{2\pi c{\sqrt {c^{2}t^{2}-\rho ^{2}}}}}\Theta (t-\rho /c)} $$Here $\rho$ is the distance to the receiving point, $c$ is the celerity of the medium and ${\displaystyle \Theta (t)}$ is the Heaviside step function.
As explained above, let's now apply an impulse response to a sound. We will use a sound from FreeSound to test our example. First, we need to download our sound as a WAV file to work with it. To do this, we write a function that downloads it from the internet, stores it in a temporary file and uses FFMpeg to convert to WAV:
import urllib
import tempfile
def download_mp3_as_np_array(url):
"Downloads a mp3 file and converts it to a numpy array."
pass
Let's now download the sound segment that we will use in this post:
snd = 0