In [1]:
import numpy, scipy, matplotlib.pyplot as plt, pandas, librosa

Audio Representation

Time Domain

The basic representation of an audio signal is in the time domain.

Sound is air vibrating. An audio signal represents the fluctuation in air pressure caused by the vibration as a function of time.

Let's download and listen to a file:

In [2]:
import urllib
urllib.urlretrieve('http://audio.musicinformationretrieval.com/c_strum.wav')

x, fs = librosa.load('c_strum.wav', sr=44100)

from IPython.display import Audio
Audio(x, rate=fs)
Out[2]:

To plot a signal in the time domain, use librosa.display.waveplot:

In [3]:
librosa.display.waveplot(x, fs, alpha=0.5)
Out[3]:
<matplotlib.collections.PolyCollection at 0x112d35890>

Let's zoom in:

In [4]:
plt.plot(x[8000:9000])
Out[4]:
[<matplotlib.lines.Line2D at 0x11321eed0>]

Digital computers can only capture this data at discrete moments in time. The rate at which a computer captures audio data is called the sampling frequency (abbreviated fs) or sample rate (abbreviated sr). For this workshop, we will mostly work with a sampling frequency of 44100 Hz.

Autocorrelation

The autocorrelation of a signal describes the similarity of a signal against a delayed version of itself.

In [10]:
# Because the autocorrelation produces a symmetric signal, we only care about the "right half".
r = numpy.correlate(x, x, mode='full')[len(x)-1:]
print x.shape, r.shape
plt.plot(r[:10000])
plt.xlabel('Lag (samples)')
(204800,) (204800,)
Out[10]:
<matplotlib.text.Text at 0x113002950>
In [11]:
r = librosa.autocorrelate(x, max_size=10000)
plt.plot(r)
plt.xlabel('Lag (samples)')
Out[11]:
<matplotlib.text.Text at 0x113039ad0>
In [12]:
from essentia.standard import AutoCorrelation
autocorr = AutoCorrelation()
r = autocorr(x)
plt.plot(r[:10000])
plt.xlabel('Lag (samples)')
Out[12]:
<matplotlib.text.Text at 0x114c65d90>