Search code examples
pythonmatplotliblibrosaaudacity

Why does librosa plot differ from matplotlib and audacity


I am reading pcm data from a file and then plotting it. Ive noticed that the plot varies between librosa.display.waveplot, plot and audacity.

Here is the code and images

%matplotlib inline
import matplotlib.pyplot as plt
import librosa.display
import numpy as np
import IPython.display as ipd
import matplotlib.pyplot as plt
import numpy, pylab

# the pcm file is 32le integer with a sampling rate of 16KHz
pcm_data = np.fromfile('someaudio.pcm', dtype=np.int32)

# the sample has the same sound as audacity
ipd.Audio(data=pcm_data, rate=16000) 

# all of these give the same resulting plot
plt.figure()
plt.subplot(3, 1, 1)
#librosa.display.waveplot(pcm_data, sr=16000)
#librosa.display.waveplot(pcm_data.astype('double'), sr=16000)
librosa.display.waveplot(pcm_data.astype('float'), max_points=None, sr=16000, max_sr=16000)

This result looks like enter image description here

# alternatively plot via matplotlib
pylab.plot(pcm_data)
pylab.show()

This result looks like enter image description here

The result from matplotlib looks like audacity enter image description here


Solution

  • matplotlib and Audacity show the actual signal samples, which apparently are all negative in the second half of the recording.

    librosa on the other hand shows an envelope of the absolute signal as explained in its documentation:

    Plot the amplitude envelope of a waveform.

    If y is monophonic, a filled curve is drawn between [-abs(y), abs(y)].

    y is the signal in this case.

    This effectively leads to a mirroring effect along the x-axis, which is why the librosa plot is symmetrical. matplotlib and Audacity apparently do no such thing.

    One might argue, that librosa's behavior effectively hides asymmetric waveforms (i.e., the amplitude of positive and negative samples is not similar), which are possible in the wild. From soundonsound.com:

    This asymmetry is due mainly to two things, the first being the relative phase relationships between the fundamental and different harmonic components in a harmonically complex signal. In combining different frequency signals with differing phase relationships, the result is often a distinctly asymmetrical waveform, and that waveform asymmetry often changes and evolves over time, too. That's just what happens when complex related signals are superimposed.

    One may also argue, that there isn't a lot of useful information in the asymmetry, as humans can usually not perceive it.

    If you believe librosa's behavior is unexpected or wrong, I recommend filling a bug report, asking for an explanation.