Audio data processing and exploratory data analysis (EDA) using the librosa library in Python. Let's break it down step by step:
-
Importing Libraries:
import pandas as pd import numpy as np import librosa import matplotlib.pyplot as plt import seaborn as sns import glob
This section imports necessary libraries for data manipulation, audio processing, visualization, and file path handling.
-
Data Reading and EDA:
train_path = glob.glob("../DATASET/archive/audio_data/train/*/*")
This line uses the
glob
module to retrieve file paths matching a specific pattern. It stores the paths of audio files in the training dataset.an_audio_file = train_path[0] librosa_audio, librosa_sample_rate = librosa.load(an_audio_file)
These lines load the first audio file from the training dataset using librosa's
load()
function and store the audio data and sample rate in variables.pd.Series(librosa_audio).plot(title="Raw Data")
This line plots the raw audio data using Pandas Series plot.
an_audio_file_trimmed = librosa.effects.trim(librosa_audio, top_db=30)
This line trims the silent parts of the audio file using librosa's
effects.trim()
function.D = librosa.stft(an_audio_file_trimmed[0]) S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
These lines compute the Short-Time Fourier Transform (STFT) and convert it to decibels.
melspec = librosa.feature.melspectrogram(y=librosa_audio, sr=librosa_sample_rate, n_mels=40)
This line computes the Mel spectrogram of the audio data.
librosa.display.specshow(melspec)
This line displays the Mel spectrogram using librosa's
display.specshow()
function.librosa.display.specshow(S_db.T)
This line displays the STFT in decibels. The
T
attribute transposes the matrix to fit the plot.
This code snippet gives an overview of how to load, preprocess, and visualize audio data using the librosa library in Python. If you have any specific questions or need further explanation on any part, feel free to ask!