I am trying to plot the waveform of an audio file in Python.
This is my code (I am using the Librosa library):
import plot as plt
def save_plot(filename):
y, sr = librosa.load(filename)
plt.plot(y, 'audio', 'time', 'amplitude')
Where the plot.py file is:
import matplotlib.pylab as plt
def plot(vector, name, xlabel=None, ylabel=None):
plt.figure()
plt.plot(vector)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.plot()
plt.savefig('static/plots/' + name)
The weird thing is that, even though I get a plot that seems like a valid waveform:
The audio file is only 5 seconds long. Therefore, I don't understand what the x axis is talking about; it seems to go up to 90000?
Thanks
The waveform will have a data point at every time your audio file is sampled, they can be sampled from 8000 Hz to 48 kHz. 90,000\/5 = 18000 Hz.
This is why you're using matplotlib.pyplot<\/code> to plot your vector, which contains many terms as it (probably) samples 22050 data points per second.
If you got an audio file with 5 seconds then you get 5 * 22050 = 110250 data points, which will be plotted in the figure. Instead of using
matplotlib.pyplot<\/code> you can just use the proper way to do this with
librosa<\/code> :
import librosa
import librosa.display
y, sr = librosa.load(<path_audio_file>, sr=<sample_rate>)
fig, ax = librosa.display.waveplot(y, sr=sr)
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.