Home > database >  Generate histogram plot for each MFCC coefficient in python
Generate histogram plot for each MFCC coefficient in python

Time:08-17

How to go about generating the histogram plot in python for each of the MFCC coefficients extracted from an audio file. As shown enter image description here

How can I go about achieving this same plot in python? I am using librosa as the MFCC feature extraction. Here is my code for the MFCC feature extraction:

import librosa
import librosa.display
import numpy as np
import wavio
# Parameters
rate = 44100    # samples per second
T = 1           # sample duration (seconds)
f = 440.0       # sound frequency (Hz)
# Compute waveform samples
t = np.linspace(0, T, T*rate, endpoint=False)
x = np.sin(2*np.pi * f * t)
# Write the samples to a file
wavio.write("sine.wav", x, rate, sampwidth=3)

y, sr = librosa.load('sine.wav')
mfcc_coeff = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)

CodePudding user response:

Seemed that you didn't require that the sound be a pure sine wave but had just created that to have something to work with, so the below code is just loading in one of librosa's example sounds.

Then the meat of resolving your question is using pyplot's hist method on whichever mfcc coefficient you want to look at.

import librosa
import matplotlib.pyplot as plt

COEFFICIENT_TO_ANALYZE = 15
N_BINS = 60

y, sr = librosa.load(librosa.ex('brahms'), sr=None)
mfcc_coeff = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)

fig, ax = plt.subplots()
ax.hist(mfcc_coeff[COEFFICIENT_TO_ANALYZE, :], N_BINS)
ax.set_title(f'MFCC Coefficient {COEFFICIENT_TO_ANALYZE}')
plt.show()
  • Related