How to plot MFCC in Python?

2019-03-29 10:55发布

问题:

I'm just a beginner here in signal processing. Here is my code so far on extracting MFCC feature from an audio file (.WAV):

from python_speech_features import mfcc
import scipy.io.wavfile as wav

(rate,sig) = wav.read("AudioFile.wav")
mfcc_feat = mfcc(sig,rate)

print(mfcc_feat)

I just wanted to plot the mfcc features to know what it looks like.

回答1:

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

(rate,sig) = wav.read("AudioFile.wav")
mfcc_feat = mfcc(sig,rate)

print(mfcc_feat)
plt.plot(mfcc_feat)
plt.show()


回答2:

This will plot the MFCC as colors, which is a more popular way

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
fig, ax = plt.subplots()
mfcc_data= np.swapaxes(mfcc_data, 0 ,1)
cax = ax.imshow(mfcc_data, interpolation='nearest', cmap=cm.coolwarm, origin='lower')
ax.set_title('MFCC')

plt.show()


回答3:

The previous answer did no defined mfcc_data.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

(rate,sig) = wav.read("file.wav")
mfcc_feat = mfcc(sig,rate)

ig, ax = plt.subplots()
mfcc_data= np.swapaxes(mfcc_feat, 0 ,1)
cax = ax.imshow(mfcc_data, interpolation='nearest', cmap=cm.coolwarm, origin='lower', aspect='auto')
ax.set_title('MFCC')
#Showing mfcc_data
plt.show()
#Showing mfcc_feat
plt.plot(mfcc_feat)
plt.show()

MFCC_data MFCC_feat