In [1]:
%matplotlib inline
import seaborn
import numpy, scipy, matplotlib.pyplot as plt, IPython.display as ipd
import librosa, librosa.display
plt.rcParams['figure.figsize'] = (14, 5)

Zero Crossing Rate¶

The zero crossing rate indicates the number of times that a signal crosses the horizontal axis.

Let's load a signal:

In [2]:
x, sr = librosa.load('audio/simple_loop.wav')

Listen to the signal:

In [3]:
ipd.Audio(x, rate=sr)
Out[3]:

Plot the signal:

In [4]:
librosa.display.waveplot(x, sr=sr)
Out[4]:
<matplotlib.collections.PolyCollection at 0x118567e50>

Let's zoom in:

In [5]:
n0 = 6500
n1 = 7500
plt.plot(x[n0:n1])
Out[5]:
[<matplotlib.lines.Line2D at 0x10aa9b050>]

I count five zero crossings. Let's compute the zero crossings using librosa.

In [6]:
zero_crossings = librosa.zero_crossings(x[n0:n1], pad=False)
In [7]:
zero_crossings.shape
Out[7]:
(1000,)

That computed a binary mask where True indicates the presence of a zero crossing. To find the total number of zero crossings, use sum:

In [8]:
print sum(zero_crossings)
5

To find the zero-crossing rate over time, use zero_crossing_rate:

In [9]:
zcrs = librosa.feature.zero_crossing_rate(x)
print zcrs.shape
(1, 97)

Plot the zero-crossing rate:

In [10]:
plt.plot(zcrs[0])
Out[10]:
[<matplotlib.lines.Line2D at 0x11831e3d0>]

Note how the high zero-crossing rate corresponds to the presence of the snare drum.

The reason for the high rate near the beginning is because the silence oscillates quietly around zero:

In [11]:
plt.plot(x[:1000])
Out[11]:
[<matplotlib.lines.Line2D at 0x11af4b2d0>]

A simple hack around this is to add a small constant before computing the zero crossing rate:

In [12]:
zcrs = librosa.feature.zero_crossing_rate(x + 0.0001)
plt.plot(zcrs[0])
Out[12]:
[<matplotlib.lines.Line2D at 0x11b2ef4d0>]

Questions¶

Try for other audio files. Does the zero-crossing rate still return something useful in polyphonic mixtures?

In [13]:
ls audio
125_bounce.wav         conga_groove.wav       prelude_cmaj.wav
58bpm.wav              funk_groove.mp3        simple_loop.wav
c_strum.wav            jangle_pop.mp3         simple_piano.wav
clarinet_c6.wav        latin_groove.mp3       tone_440.wav
classic_rock_beat.wav  oboe_c6.wav