text_plain_output_1.png image_output_1.png
from scipy.stats import kurtosis

from scipy.stats import skew

import librosa

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

sounds_df = pd.read_csv('/kaggle/input/urbansound8k/UrbanSound8K.csv')

import matplotlib.pyplot as plt
for i in range(1, 11):
    fold_df = sounds_df[sounds_df['fold'] == i]
    fold_freq = fold_df['class'].value_counts()

def get_mfcc(filename, fold):
    wave, sr = librosa.load(f'../input/urbansound8k/fold{fold}/{filename}', mono=True, sr=22050)
    wave = librosa.util.normalize(wave)
    mfccs = librosa.feature.mfcc(y=wave, sr=sr, n_mfcc=40, hop_length=int(0.0232 * sr / 2.0), n_fft=int(0.0232 * sr))
    mfccs_min = mfccs.min(axis=1)
    mfccs_max = mfccs.max(axis=1)
    mfccs_median = np.median(mfccs, axis=1)
    mfccs_mean = np.mean(mfccs, axis=1)
    mfccs_var = np.var(mfccs, axis=1)
    mfccs_skewness = skew(mfccs, axis=1)
    mfccs_kurtosis = kurtosis(mfccs, axis=1)
    mfccs_first_derivative = np.diff(mfccs, n=1, axis=1)
    mfccs_first_derivative_mean = np.mean(mfccs_first_derivative, axis=1)
    mfccs_first_derivative_var = np.var(mfccs_first_derivative, axis=1)
    mfccs_second_derivative = np.diff(mfccs, n=2, axis=1)
    mfccs_second_derivative_mean = np.mean(mfccs_second_derivative, axis=1)
    mfccs_second_derivative_var = np.var(mfccs_second_derivative, axis=1)
    mfccs_stats = np.vstack((mfccs_min, mfccs_max, mfccs_median, mfccs_mean, mfccs_var, mfccs_skewness, mfccs_kurtosis, mfccs_first_derivative_mean, mfccs_first_derivative_var, mfccs_second_derivative_mean, mfccs_second_derivative_var))
    return pd.Series([mfccs, mfccs_stats.transpose()])

plt.tight_layout()

plt.figure(figsize=[15, 10])
for i in range(0, 9):
    ax = plt.subplot(3, 3, i + 1)
    img = librosa.display.specshow(sounds_df['mfccs'][i], x_axis='time')
    ax.set(title=sounds_df['class'][i])
    plt.colorbar()
plt.tight_layout()