简体   繁体   中英

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 在此处输入图片说明

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

This result looks like 在此处输入图片说明

The result from matplotlib looks like audacity 在此处输入图片说明

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 (ie, 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.

I received some answers via librosa forums. Here is one answer from Brian McFee:

Following onto what Vincent posted, librosa's wave plot does not show samples directly, for two reasons:

  • it would blow up the memory usage by keeping points at a higher resolution than is necessary for visualization

  • it can be obscured by high frequency noise.

Instead, librosa's plotter works more like a typical DAW, where the audio signal is down-sampled for viz purposes, and the envelope is visualized rather than the signal itself. These steps are accomplished by showing the max(abs(y[i:i+k])) rather than the samples y[i], y[i+1], ... y[i+k]. The length of the downsampling window is controlled by the parameters to waveplot.

Since the above- and below-axis information is discarded by taking the abs, we use the axis to separate left and right channels (left above, right below) in stereo signals. In mono signals, the envelope is reflected across the y axis, which produces the symmetric figure you reported.

Different DAWs will do these steps slightly differently, and a fancy implementation would revert down to sample plotting once you've zoomed in to a range such that doing so becomes feasible. Matplotlib doesn't make this entirely easy to pull off, so we opted for this compromise here. If you want sample-accurate plotting, we suggest to use pyplot.plt() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM