1
$\begingroup$

I am loading in audio data collected in a rosbag using the audio_common_msgs package to collect the data.

I now want to plot the frequency vs time plot of the audio.

I have seen ways to convert the data to mp3, but I am not sure how to plot the data after converting to mp3?

Is there a way to directly store the audio data and plot it using some Python package? Or even after storing it as an mp3 or WAV file?

$\endgroup$
0

1 Answer 1

0
$\begingroup$
Extracting the data

From this answer to get the amplitude data from an mp3 audio files using python, use pydub:

from pydub import AudioSegment

sound = AudioSegment.from_mp3("test.mp3")

# get raw audio data as a bytestring
raw_data = sound.raw_data
# get the frame rate
sample_rate = sound.frame_rate
# get amount of bytes contained in one sample
sample_size = sound.sample_width
# get channels
channels = sound.channels

Plotting the data

Look at Matplotlib for plotting data in Python. It's very comprehensive (almost dauntingly so).

However, there are loads of tutorials and examples out there. I used this (amongst other things) to learn it: Python Plotting With Matplotlib (Guide).

You can probably skim and skip the super-funky examples and just get a basic X-Y plot displayed pretty quickly.

StackOverflow also has a number of simple examples provided in the answers.

Putting them together - an example

From this answer to Plot spectogram from mp3:

from pydub import AudioSegment
import matplotlib.pyplot as plt
from scipy.io import wavfile
from tempfile import mktemp

mp3_audio = AudioSegment.from_file('speech.mp3', format="mp3")  # read mp3
wname = mktemp('.wav')  # use temporary file
mp3_audio.export(wname, format="wav")  # convert to wav
FS, data = wavfile.read(wname)  # read wav file
plt.specgram(data, Fs=FS, NFFT=128, noverlap=0)  # plot
plt.show()

Spectogram

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.