Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sidharrth2002/6979480c4281d84d36bc3c54d20d8c58 to your computer and use it in GitHub Desktop.
Save sidharrth2002/6979480c4281d84d36bc3c54d20d8c58 to your computer and use it in GitHub Desktop.
Visualise waveform and spectrogram using Librosa
# display one audio waveform in time domain
def plot_waveform(audio_file_path):
# # Your code goes here (do not change the function definition)
plt.figure(figsize=(12, 4))
waveform, sample_rate = librosa.load(audio_file_path, sr=None)
librosa.display.waveshow(waveform, sr=sample_rate, color="orange")
plt.title("Waveform of " + str(audio_file_path.split("/")[-1]))
plt.grid(True)
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.show()
# extract one spectrogram for one audio sample and display ####
def build_spectogram(file_path):
plt.interactive(False)
plt.figure(figsize=(12, 4))
# Your code goes here (do not change the function definition)
y, sr = librosa.load(file_path, sr=None)
stft = librosa.stft(y, hop_length=256)
stft_db = librosa.amplitude_to_db(np.abs(stft), ref=np.max)
librosa.display.specshow(
stft_db, sr=sr, x_axis="time", y_axis="log", cmap="inferno"
)
plt.colorbar()
plt.title("Spectrogram of " + str(file_path.split("/")[-1]))
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment