3

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: 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

pavlos163
  • 2,730
  • 4
  • 38
  • 82

2 Answers2

4

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.

Look at the variable you're currently ignoring from librosa.load, that is the sampling rate, which will let you figure out the timescale.

  • Thanks! Just to be sure I got it, my sampling rate is approx. 22000. Does that mean that, because my audio file is 5 seconds, the x axis represents the 22000*5 = 110000 samples that were taken, and not time? – pavlos163 May 07 '17 at 19:18
  • 1
    @pk1914 "approx. 22000" is a strange thing to say about the sampling rate of a "normal" sound file. Does it happen to be (exactly) 22050? – Matthias May 08 '17 at 10:30
  • You are right! I guess it is a special number? Why so? Thanks! – pavlos163 May 08 '17 at 17:27
0

This is why you're using matplotlib.pyplot 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 you can just use the proper way to do this with librosa:

import librosa
import librosa.display

y, sr = librosa.load(<path_audio_file>, sr=<sample_rate>)
fig, ax = librosa.display.waveplot(y, sr=sr)

As it retains the sample rate as information, then it will normalize the time series at the right time length!

Note that for using librosa.display you need to explicitly import it.

If you're interested in more details check librosa.display.waveplot.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Luan Souza
  • 159
  • 1
  • 1
  • 11