BenjaminFranklinGates
BenjaminFranklinGates

Reputation: 115

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?

Upvotes: 1

Views: 707

Answers (2)

Michał Huras
Michał Huras

Reputation: 11

The librosa.display.waveplot function uses the sampling rate (sr) to calculate the duration (X-axis is time by default). This parameter should be the same in the loading function and display.
In librosa.load function you set sampling rate to 32 000, but in librosa.dispaly.waveplot to 3 200. That is why the plotted signal is 10 times longer than expected.

Upvotes: 1

Jay Patel
Jay Patel

Reputation: 1418

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.

Upvotes: 1

Related Questions