5 Best Ways to Plot MFCC in Python Using Matplotlib

πŸ’‘ Problem Formulation: In the field of audio processing, Mel Frequency Cepstral Coefficients (MFCCs) are crucial features used for speech and music analysis. Given a signal, we aim to compute the MFCC and visualize the sequence of MFCCs over time using Python and Matplotlib. The input is an audio file, while the desired output is a plot displaying the variation of MFCC coefficients throughout the audio duration.

Method 1: Using Librosa to Calculate MFCCs and Matplotlib for Plotting

The first method involves using the Librosa library to compute MFCCs from an audio file and Matplotlib’s imshow() function to display it. Librosa provides a user-friendly interface to process audio signals, while Matplotlib offers versatile options to visualize data in a comprehensible form.

Here’s an example:

import matplotlib.pyplot as plt
import librosa
import librosa.display

# Load the audio file
audio_path = 'path_to_audio.wav'
signal, sr = librosa.load(audio_path)

# Compute the MFCC
mfccs = librosa.feature.mfcc(signal, sr=sr)

# Plot the MFCCs
plt.figure(figsize=(10, 4))
librosa.display.specshow(mfccs, x_axis='time')
plt.colorbar()
plt.title('MFCC')
plt.tight_layout()
plt.show()

The output of this code is a visual representation of the MFCCs across the time domain of the audio file, providing insights into the cepstral characteristics of the signal.

This code snippet begins with loading an audio file using Librosa, then calculates its MFCCs, and finally plots the coefficients over time using Matplotlib. This method is straightforward and leverages the high-level functions provided by Librosa for both feature extraction and visualization.

Method 2: Custom Plotting MFCCs with Matplotlib’s Pyplot Interface

This method offers a custom approach to plot MFCCs using the Pyplot interface of Matplotlib. We’ll compute the MFCCs using an audio processing library of choice and then use Matplotlib to create a custom plot, allowing for flexible and fine-tuned visualizations.

Here’s an example:

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
from python_speech_features import mfcc

# Read audio file
sampling_rate, audio_signal = wavfile.read('path_to_audio.wav')

# Compute MFCC features
mfcc_features = mfcc(audio_signal, samplerate=sampling_rate)

# Plot
plt.figure(figsize=(10, 4))
plt.imshow(np.transpose(mfcc_features), cmap='hot', interpolation='nearest', aspect='auto')
plt.title('MFCC Features')
plt.ylabel('Coefficients')
plt.xlabel('Time')
plt.colorbar()
plt.show()

The output is a heatmap visualization of MFCCs with time on the x-axis and cepstral coefficients on the y-axis.

The code uses Python’s Speech Features library to compute the MFCCs from the audio signal, and the Pyplot interface from Matplotlib to customize the plot as a heatmap. This method allows for detailed adjustments to the plot’s appearance.

Method 3: Enhanced Visualization with Seaborn

This method incorporates Seaborn, a statistical data visualization library, to add aesthetic enhancements to the MFCC plot generated by Matplotlib. Seaborn works in harmony with Matplotlib and offers a higher level of interface for drawing informative and attractive statistical graphics.

Here’s an example:

import seaborn as sns
import librosa

# Compute MFCCs
audio_path = 'path_to_audio.wav'
signal, sr = librosa.load(audio_path)
mfccs = librosa.feature.mfcc(signal, sr=sr)

# Use Seaborn to enhance the Matplotlib plot
plt.figure(figsize=(10, 4))
sns.heatmap(np.transpose(mfccs), cmap='coolwarm', linewidths=.5, cbar_kws={"shrink": .5})
plt.title('Enhanced MFCC Heatmap')
plt.ylabel('Coefficients')
plt.xlabel('Time')
plt.show()

The resulting output is a visually-attractive heatmap with Seaborn’s enhancements applied to the MFCC coefficients.

In this snippet, after computing the MFCCs with Librosa, Seaborn’s heatmap function is used to plot the MFCC data. This method takes advantage of Seaborn’s advanced customization capabilities to create a pleasing and informative plot.

Method 4: 3D Surface Plot of MFCCs

For a three-dimensional perspective, this method utilizes Matplotlib’s 3D plotting capabilities to create a surface plot of the MFCCs. A 3D surface plot allows one to observe patterns from different angles by adding depth to the visualization.

Here’s an example:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import librosa
import numpy as np

# MFCC computation
audio_path = 'path_to_audio.wav'
signal, sr = librosa.load(audio_path)
mfccs = librosa.feature.mfcc(signal, sr=sr)

# 3D Surface plot
X = np.linspace(0, mfccs.shape[1], mfccs.shape[1])
Y = np.linspace(0, mfccs.shape[0], mfccs.shape[0])
X, Y = np.meshgrid(X, Y)
Z = mfccs

fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(111, projection='3d')
surface = ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')
fig.colorbar(surface)
ax.set_title('3D Surface Plot of MFCCs')
ax.set_ylabel('MFCC Coefficients')
ax.set_xlabel('Time')
ax.set_zlabel('Amplitude')
plt.show()

The output is a 3D plot providing a different perspective on the variation of MFCCs over time and coefficients.

Using Matplotlib’s 3D plotting tools, this code generates a 3D surface plot of the MFCCs where the amplitude of the MFCC coefficient is represented by the z-axis. This method is best for interactive analysis or when depth perception of features is necessary.

Bonus One-Liner Method 5: Quick MFCC Plot with Matplotlib’s Specgram

If you’re looking for a quick and dirty one-liner to get a sense of the MFCC, you can use Matplotlib’s specgram() function. It’s not a true MFCC plot, but it can be useful for a rough spectral analysis.

Here’s an example:

import matplotlib.pyplot as plt
import scipy.io.wavfile as wav

rate, data = wav.read('path_to_audio.wav')
plt.specgram(data, Fs=rate, cmap='inferno')
plt.colorbar()
plt.title('Quick Spectrogram')
plt.ylabel('Frequency (Hz)')
plt.xlabel('Time (sec)')
plt.show()

The output is a spectrogram visualizing the frequency content over time, which can serve as a proxy for MFCC visualization in some cases.

This quick one-liner uses the specgram() function to generate a spectrogram, which is similar to MFCCs as it represents the spectral content over time, but without the cepstral processing.

Summary/Discussion

  • Method 1: Using Librosa and Matplotlib for plotting MFCC. Strengths include simplicity and direct integration with Librosa. Weaknesses may be limited customization due to abstraction.
  • Method 2: Custom plotting with Pyplot. Strengths include high customization potential and independence from a specific audio processing library. Weaknesses are the increased complexity of the code.
  • Method 3: Enhanced visualization with Seaborn. Strengths are attractive visual enhancements and ease of use. Weaknesses include an additional dependency on Seaborn and potentially less control over the plot details.
  • Method 4: 3D Surface Plot of MFCCs. Strengths include a novel 3D perspective which can reveal patterns not visible in 2D. Weaknesses are complexity and the potential for overcomplication in some applications.
  • Method 5: Quick MFCC approximation with specgram(). Strengths are rapid visualization with a single line of code. Weaknesses include not being a true MFCC plot and potential loss of cepstral feature details.