How to Plot Mfcc in Python Using Matplotlib?
Last Updated :
29 Feb, 2024
Mel-frequency cepstral coefficients (MFCC) are widely used in audio signal processing and speech recognition tasks. They represent the spectral characteristics of an audio signal and are commonly used as features for various machine-learning applications. In this article, we will explore how to compute and visualize MFCC using Python and Matplotlib.
Plot Mfcc in Python Using Matplotlib
Below is the step-by-step approach to plot Mfcc in Python using Matplotlib:
Step 1: Installation
Before starting, install the following libraries with the help of the following commands:
pip install numpy librosa matplotlib
Step 2: Getting Started
Let’s start by importing the necessary libraries and loading an audio file. For this example, we’ll use the librosa library to load an audio file.
Python3
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
|
Step 3: Load an audio file
Now, we will load an audio file by using the following code.
Python3
audio_file = 'myaudio.wav'
y, sr = librosa.load(audio_file)
|
Step 4: Compute MFCC
Now, let’s compute the MFCC of the audio signal using librosa’s mfcc function:
Python3
mfccs = librosa.feature.mfcc(y, sr = sr, n_mfcc = 13 )
|
Step 5: Visualize MFCC
To visualize the MFCC, we can use Matplotlib to create a heatmap. Each row in the MFCC matrix represents a different coefficient, and each column represents a frame in the audio signal.
Python3
plt.figure(figsize = ( 10 , 5 ))
librosa.display.specshow(mfccs, x_axis = 'time' )
plt.colorbar()
plt.title( 'MFCC' )
plt.xlabel( 'Time' )
plt.ylabel( 'MFCC Coefficients' )
plt.show()
|
Step 6: Customization
You can customize the plot further by adding axis labels, title, and adjusting the color map. Additionally, you can choose the number of MFCC coefficients by modifying the n_mfcc parameter when computing the MFCC.
Python3
plt.figure(figsize = ( 10 , 5 ))
librosa.display.specshow(mfccs, x_axis = 'time' , cmap = 'viridis' , hop_length = 512 )
plt.colorbar( format = '%+2.0f dB' )
plt.title( 'MFCC' )
plt.xlabel( 'Time (s)' )
plt.ylabel( 'MFCC Coefficients' )
plt.show()
|
Complete Code
Python3
import librosa
import librosa.display
import matplotlib.pyplot as plt
def plot_mfcc(audio_path):
y, sr = librosa.load(audio_path)
mfccs = librosa.feature.mfcc(y = y, sr = sr, n_mfcc = 13 )
plt.figure(figsize = ( 10 , 4 ))
librosa.display.specshow(mfccs, x_axis = 'time' , cmap = 'viridis' )
plt.colorbar( format = '%+2.0f dB' )
plt.title( 'MFCC' )
plt.xlabel( 'Time' )
plt.ylabel( 'MFCC Coefficient' )
plt.show()
audio_file_path = "path/to/your/audio/file.wav"
plot_mfcc(audio_file_path)
|
Output:
Share your thoughts in the comments
Please Login to comment...