pythonmatplotliblibrosa

Librosa Waveplot Displaying Wrong Time on X-Axis


I'm trying to visualize a waveplot as follows:

sig, rate = librosa.load(test_audio, sr=32000, offset=None)
plt.figure(figsize=(15, 5))
librosa.display.waveplot(sig, sr=3200)

which provides this result:

waveplot

If you look at this x-axis it makes this appear as an 8 minute audio file but it is actually only 47 seconds long. What am I missing here?


Solution

  • I think x-axis is in samples. By default the method downsamples the signal that means that to align another time series patron needs to compute its x positions in samples and then adjust these by the downsampling factor (by default 1000 / sr).

    Let me show you example, may be that will clear your doubts.

    import librosa
    import librosa.display
    import matplotlib.pyplot as plt
    import numpy as np
    
    y, sr = librosa.load(librosa.util.example_audio_file())
    
    tempo, beats = librosa.beat.beat_track(y=y, sr=sr, units='time', trim=False)
    
    fig = plt.figure(figsize=(15, 8))
    librosa.display.waveplot(y=y, sr=sr)
    plt.vlines(beats, -1, 1, color='r',linestyles="dashed")
    plt.grid()
    

    enter image description here

    You can see that now, all displays are in natural coordinates.